blob: f98852e8a023c6e25085bf0ba2656cffcd30453b [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"
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +000042#include "llvm/Support/CommandLine.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000043#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000044#include "llvm/Support/MathExtras.h"
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000045#include <sstream>
Evan Chenga8e29892007-01-19 07:51:42 +000046using namespace llvm;
47
Owen Andersone50ed302009-08-10 22:56:29 +000048static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000049 CCValAssign::LocInfo &LocInfo,
50 ISD::ArgFlagsTy &ArgFlags,
51 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000052static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000053 CCValAssign::LocInfo &LocInfo,
54 ISD::ArgFlagsTy &ArgFlags,
55 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000056static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000057 CCValAssign::LocInfo &LocInfo,
58 ISD::ArgFlagsTy &ArgFlags,
59 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000060static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000061 CCValAssign::LocInfo &LocInfo,
62 ISD::ArgFlagsTy &ArgFlags,
63 CCState &State);
64
Owen Andersone50ed302009-08-10 22:56:29 +000065void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
66 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000067 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000068 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000069 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
70 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000071
Owen Anderson70671842009-08-10 20:18:46 +000072 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +000073 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +000074 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000075 }
76
Owen Andersone50ed302009-08-10 22:56:29 +000077 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +000078 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Owen Anderson70671842009-08-10 20:18:46 +000079 setOperationAction(ISD::VSETCC, VT.getSimpleVT(), Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +000080 if (ElemTy == MVT::i8 || ElemTy == MVT::i16)
Owen Anderson70671842009-08-10 20:18:46 +000081 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson0696fdf2009-09-16 20:20:44 +000082 if (ElemTy != MVT::i32) {
83 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
84 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
85 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
86 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
87 }
Owen Anderson70671842009-08-10 20:18:46 +000088 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
89 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Owen Anderson70671842009-08-10 20:18:46 +000090 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Custom);
Anton Korobeynikov8e6c2b92009-08-21 12:40:35 +000091 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +000092 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +000093 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
94 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
95 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +000096 }
97
98 // Promote all bit-wise operations.
99 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000100 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000101 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
102 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000103 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000104 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000105 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000106 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000107 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000108 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000109 }
Bob Wilson16330762009-09-16 00:17:28 +0000110
111 // Neon does not support vector divide/remainder operations.
112 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
113 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
114 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
115 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
116 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
117 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000118}
119
Owen Andersone50ed302009-08-10 22:56:29 +0000120void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000121 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000122 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000123}
124
Owen Andersone50ed302009-08-10 22:56:29 +0000125void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000126 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000127 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000128}
129
Chris Lattnerf0144122009-07-28 03:13:23 +0000130static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
131 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000132 return new TargetLoweringObjectFileMachO();
Chris Lattner80ec2792009-08-02 00:34:36 +0000133 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000134}
135
Evan Chenga8e29892007-01-19 07:51:42 +0000136ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000137 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000138 Subtarget = &TM.getSubtarget<ARMSubtarget>();
139
Evan Chengb1df8f22007-04-27 08:15:43 +0000140 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000141 // Uses VFP for Thumb libfuncs if available.
142 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
143 // Single-precision floating-point arithmetic.
144 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
145 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
146 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
147 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000148
Evan Chengb1df8f22007-04-27 08:15:43 +0000149 // Double-precision floating-point arithmetic.
150 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
151 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
152 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
153 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000154
Evan Chengb1df8f22007-04-27 08:15:43 +0000155 // Single-precision comparisons.
156 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
157 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
158 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
159 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
160 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
161 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
162 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
163 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000164
Evan Chengb1df8f22007-04-27 08:15:43 +0000165 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
166 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
167 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
168 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
169 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
170 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
171 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
172 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000173
Evan Chengb1df8f22007-04-27 08:15:43 +0000174 // Double-precision comparisons.
175 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
176 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
177 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
178 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
179 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
180 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
181 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
182 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000183
Evan Chengb1df8f22007-04-27 08:15:43 +0000184 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
185 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
186 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
187 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
188 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
189 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
190 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
191 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000192
Evan Chengb1df8f22007-04-27 08:15:43 +0000193 // Floating-point to integer conversions.
194 // i64 conversions are done via library routines even when generating VFP
195 // instructions, so use the same ones.
196 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
197 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
198 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
199 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000200
Evan Chengb1df8f22007-04-27 08:15:43 +0000201 // Conversions between floating types.
202 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
203 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
204
205 // Integer to floating-point conversions.
206 // i64 conversions are done via library routines even when generating VFP
207 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000208 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
209 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000210 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
211 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
212 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
213 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
214 }
Evan Chenga8e29892007-01-19 07:51:42 +0000215 }
216
Bob Wilson2f954612009-05-22 17:38:41 +0000217 // These libcalls are not available in 32-bit.
218 setLibcallName(RTLIB::SHL_I128, 0);
219 setLibcallName(RTLIB::SRL_I128, 0);
220 setLibcallName(RTLIB::SRA_I128, 0);
221
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000222 // Libcalls should use the AAPCS base standard ABI, even if hard float
223 // is in effect, as per the ARM RTABI specification, section 4.1.2.
224 if (Subtarget->isAAPCS_ABI()) {
225 for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i) {
226 setLibcallCallingConv(static_cast<RTLIB::Libcall>(i),
227 CallingConv::ARM_AAPCS);
228 }
229 }
230
David Goodwinf1daf7d2009-07-08 23:10:31 +0000231 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000232 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000233 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000234 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000235 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000236 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
237 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000238
Owen Anderson825b72b2009-08-11 20:47:22 +0000239 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000240 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000241
242 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000243 addDRTypeForNEON(MVT::v2f32);
244 addDRTypeForNEON(MVT::v8i8);
245 addDRTypeForNEON(MVT::v4i16);
246 addDRTypeForNEON(MVT::v2i32);
247 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000248
Owen Anderson825b72b2009-08-11 20:47:22 +0000249 addQRTypeForNEON(MVT::v4f32);
250 addQRTypeForNEON(MVT::v2f64);
251 addQRTypeForNEON(MVT::v16i8);
252 addQRTypeForNEON(MVT::v8i16);
253 addQRTypeForNEON(MVT::v4i32);
254 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000255
Bob Wilson74dc72e2009-09-15 23:55:57 +0000256 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
257 // neither Neon nor VFP support any arithmetic operations on it.
258 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
259 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
260 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
261 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
262 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
263 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
264 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
265 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
266 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
267 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
268 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
269 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
270 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
271 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
272 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
273 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
274 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
275 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
276 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
277 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
278 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
279 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
280 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
281 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
282
Bob Wilson642b3292009-09-16 00:32:15 +0000283 // Neon does not support some operations on v1i64 and v2i64 types.
284 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
285 setOperationAction(ISD::MUL, MVT::v2i64, Expand);
286 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
287 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
288
Bob Wilson5bafff32009-06-22 23:27:02 +0000289 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
290 setTargetDAGCombine(ISD::SHL);
291 setTargetDAGCombine(ISD::SRL);
292 setTargetDAGCombine(ISD::SRA);
293 setTargetDAGCombine(ISD::SIGN_EXTEND);
294 setTargetDAGCombine(ISD::ZERO_EXTEND);
295 setTargetDAGCombine(ISD::ANY_EXTEND);
296 }
297
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000298 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000299
300 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000301 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000302
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000303 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000304 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000305
Evan Chenga8e29892007-01-19 07:51:42 +0000306 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000307 if (!Subtarget->isThumb1Only()) {
308 for (unsigned im = (unsigned)ISD::PRE_INC;
309 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000310 setIndexedLoadAction(im, MVT::i1, Legal);
311 setIndexedLoadAction(im, MVT::i8, Legal);
312 setIndexedLoadAction(im, MVT::i16, Legal);
313 setIndexedLoadAction(im, MVT::i32, Legal);
314 setIndexedStoreAction(im, MVT::i1, Legal);
315 setIndexedStoreAction(im, MVT::i8, Legal);
316 setIndexedStoreAction(im, MVT::i16, Legal);
317 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000318 }
Evan Chenga8e29892007-01-19 07:51:42 +0000319 }
320
321 // i64 operation support.
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000322 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000323 setOperationAction(ISD::MUL, MVT::i64, Expand);
324 setOperationAction(ISD::MULHU, MVT::i32, Expand);
325 setOperationAction(ISD::MULHS, MVT::i32, Expand);
326 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
327 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000328 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000329 setOperationAction(ISD::MUL, MVT::i64, Expand);
330 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb6207242009-08-01 00:16:10 +0000331 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000332 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000333 }
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000334 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000335 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000336 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000337 setOperationAction(ISD::SRL, MVT::i64, Custom);
338 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000339
340 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000341 setOperationAction(ISD::ROTL, MVT::i32, Expand);
342 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
343 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000344 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000345 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000346
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000347 // Only ARMv6 has BSWAP.
348 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000349 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000350
Evan Chenga8e29892007-01-19 07:51:42 +0000351 // These are expanded into libcalls.
Owen Anderson825b72b2009-08-11 20:47:22 +0000352 setOperationAction(ISD::SDIV, MVT::i32, Expand);
353 setOperationAction(ISD::UDIV, MVT::i32, Expand);
354 setOperationAction(ISD::SREM, MVT::i32, Expand);
355 setOperationAction(ISD::UREM, MVT::i32, Expand);
356 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
357 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000358
Owen Anderson825b72b2009-08-11 20:47:22 +0000359 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
360 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
361 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
362 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000363 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000364
Evan Chenga8e29892007-01-19 07:51:42 +0000365 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000366 setOperationAction(ISD::VASTART, MVT::Other, Custom);
367 setOperationAction(ISD::VAARG, MVT::Other, Expand);
368 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
369 setOperationAction(ISD::VAEND, MVT::Other, Expand);
370 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
371 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000372 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
373 // FIXME: Shouldn't need this, since no register is used, but the legalizer
374 // doesn't yet know how to not do that for SjLj.
375 setExceptionSelectorRegister(ARM::R0);
Evan Cheng86198642009-08-07 00:34:42 +0000376 if (Subtarget->isThumb())
Owen Anderson825b72b2009-08-11 20:47:22 +0000377 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Evan Cheng86198642009-08-07 00:34:42 +0000378 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000379 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
380 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000381
Evan Chengd27c9fc2009-07-03 01:43:10 +0000382 if (!Subtarget->hasV6Ops() && !Subtarget->isThumb2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000383 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
384 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000385 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000386 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000387
David Goodwinf1daf7d2009-07-08 23:10:31 +0000388 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only())
Jim Grosbache5165492009-11-09 00:11:35 +0000389 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR iff target supports vfp2.
Owen Anderson825b72b2009-08-11 20:47:22 +0000390 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000391
392 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000393 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000394
Owen Anderson825b72b2009-08-11 20:47:22 +0000395 setOperationAction(ISD::SETCC, MVT::i32, Expand);
396 setOperationAction(ISD::SETCC, MVT::f32, Expand);
397 setOperationAction(ISD::SETCC, MVT::f64, Expand);
398 setOperationAction(ISD::SELECT, MVT::i32, Expand);
399 setOperationAction(ISD::SELECT, MVT::f32, Expand);
400 setOperationAction(ISD::SELECT, MVT::f64, Expand);
401 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
402 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
403 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000404
Owen Anderson825b72b2009-08-11 20:47:22 +0000405 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
406 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
407 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
408 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
409 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000410
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000411 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000412 setOperationAction(ISD::FSIN, MVT::f64, Expand);
413 setOperationAction(ISD::FSIN, MVT::f32, Expand);
414 setOperationAction(ISD::FCOS, MVT::f32, Expand);
415 setOperationAction(ISD::FCOS, MVT::f64, Expand);
416 setOperationAction(ISD::FREM, MVT::f64, Expand);
417 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000418 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000419 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
420 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000421 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000422 setOperationAction(ISD::FPOW, MVT::f64, Expand);
423 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000424
Evan Chenga8e29892007-01-19 07:51:42 +0000425 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
David Goodwinf1daf7d2009-07-08 23:10:31 +0000426 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000427 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
428 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
429 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
430 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000431 }
Evan Chenga8e29892007-01-19 07:51:42 +0000432
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000433 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000434 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000435 setTargetDAGCombine(ISD::ADD);
436 setTargetDAGCombine(ISD::SUB);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000437
Evan Chenga8e29892007-01-19 07:51:42 +0000438 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Chenga8e29892007-01-19 07:51:42 +0000439 setSchedulingPreference(SchedulingForRegPressure);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000440
Evan Chengbc9b7542009-08-15 07:59:10 +0000441 // FIXME: If-converter should use instruction latency to determine
442 // profitability rather than relying on fixed limits.
443 if (Subtarget->getCPUString() == "generic") {
444 // Generic (and overly aggressive) if-conversion limits.
445 setIfCvtBlockSizeLimit(10);
446 setIfCvtDupBlockSizeLimit(2);
447 } else if (Subtarget->hasV6Ops()) {
448 setIfCvtBlockSizeLimit(2);
449 setIfCvtDupBlockSizeLimit(1);
450 } else {
451 setIfCvtBlockSizeLimit(3);
452 setIfCvtDupBlockSizeLimit(2);
Evan Cheng8557c2b2009-06-19 01:51:50 +0000453 }
454
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000455 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Bob Wilsone6abdff2009-05-18 20:55:32 +0000456 // Do not enable CodePlacementOpt for now: it currently runs after the
457 // ARMConstantIslandPass and messes up branch relaxation and placement
458 // of constant islands.
459 // benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000460}
461
Evan Chenga8e29892007-01-19 07:51:42 +0000462const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
463 switch (Opcode) {
464 default: return 0;
465 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000466 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
467 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000468 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000469 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
470 case ARMISD::tCALL: return "ARMISD::tCALL";
471 case ARMISD::BRCOND: return "ARMISD::BRCOND";
472 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000473 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000474 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
475 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
476 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000477 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000478 case ARMISD::CMPFP: return "ARMISD::CMPFP";
479 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
480 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
481 case ARMISD::CMOV: return "ARMISD::CMOV";
482 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000483
Evan Chenga8e29892007-01-19 07:51:42 +0000484 case ARMISD::FTOSI: return "ARMISD::FTOSI";
485 case ARMISD::FTOUI: return "ARMISD::FTOUI";
486 case ARMISD::SITOF: return "ARMISD::SITOF";
487 case ARMISD::UITOF: return "ARMISD::UITOF";
Evan Chenga8e29892007-01-19 07:51:42 +0000488
489 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
490 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
491 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000492
Jim Grosbache5165492009-11-09 00:11:35 +0000493 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
494 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000495
Evan Chengc5942082009-10-28 06:55:03 +0000496 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
497 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
498
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000499 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000500
Evan Cheng86198642009-08-07 00:34:42 +0000501 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
502
Bob Wilson5bafff32009-06-22 23:27:02 +0000503 case ARMISD::VCEQ: return "ARMISD::VCEQ";
504 case ARMISD::VCGE: return "ARMISD::VCGE";
505 case ARMISD::VCGEU: return "ARMISD::VCGEU";
506 case ARMISD::VCGT: return "ARMISD::VCGT";
507 case ARMISD::VCGTU: return "ARMISD::VCGTU";
508 case ARMISD::VTST: return "ARMISD::VTST";
509
510 case ARMISD::VSHL: return "ARMISD::VSHL";
511 case ARMISD::VSHRs: return "ARMISD::VSHRs";
512 case ARMISD::VSHRu: return "ARMISD::VSHRu";
513 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
514 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
515 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
516 case ARMISD::VSHRN: return "ARMISD::VSHRN";
517 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
518 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
519 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
520 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
521 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
522 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
523 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
524 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
525 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
526 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
527 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
528 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
529 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
530 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000531 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000532 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000533 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000534 case ARMISD::VREV64: return "ARMISD::VREV64";
535 case ARMISD::VREV32: return "ARMISD::VREV32";
536 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000537 case ARMISD::VZIP: return "ARMISD::VZIP";
538 case ARMISD::VUZP: return "ARMISD::VUZP";
539 case ARMISD::VTRN: return "ARMISD::VTRN";
Evan Chenga8e29892007-01-19 07:51:42 +0000540 }
541}
542
Bill Wendlingb4202b82009-07-01 18:50:55 +0000543/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000544unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Evan Cheng048e36f2009-10-02 06:57:25 +0000545 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 0 : 1;
Bill Wendling20c568f2009-06-30 22:38:32 +0000546}
547
Evan Chenga8e29892007-01-19 07:51:42 +0000548//===----------------------------------------------------------------------===//
549// Lowering Code
550//===----------------------------------------------------------------------===//
551
Evan Chenga8e29892007-01-19 07:51:42 +0000552/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
553static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
554 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000555 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000556 case ISD::SETNE: return ARMCC::NE;
557 case ISD::SETEQ: return ARMCC::EQ;
558 case ISD::SETGT: return ARMCC::GT;
559 case ISD::SETGE: return ARMCC::GE;
560 case ISD::SETLT: return ARMCC::LT;
561 case ISD::SETLE: return ARMCC::LE;
562 case ISD::SETUGT: return ARMCC::HI;
563 case ISD::SETUGE: return ARMCC::HS;
564 case ISD::SETULT: return ARMCC::LO;
565 case ISD::SETULE: return ARMCC::LS;
566 }
567}
568
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000569/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
570static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000571 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000572 CondCode2 = ARMCC::AL;
573 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000574 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000575 case ISD::SETEQ:
576 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
577 case ISD::SETGT:
578 case ISD::SETOGT: CondCode = ARMCC::GT; break;
579 case ISD::SETGE:
580 case ISD::SETOGE: CondCode = ARMCC::GE; break;
581 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000582 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000583 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
584 case ISD::SETO: CondCode = ARMCC::VC; break;
585 case ISD::SETUO: CondCode = ARMCC::VS; break;
586 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
587 case ISD::SETUGT: CondCode = ARMCC::HI; break;
588 case ISD::SETUGE: CondCode = ARMCC::PL; break;
589 case ISD::SETLT:
590 case ISD::SETULT: CondCode = ARMCC::LT; break;
591 case ISD::SETLE:
592 case ISD::SETULE: CondCode = ARMCC::LE; break;
593 case ISD::SETNE:
594 case ISD::SETUNE: CondCode = ARMCC::NE; break;
595 }
Evan Chenga8e29892007-01-19 07:51:42 +0000596}
597
Bob Wilson1f595bb2009-04-17 19:07:39 +0000598//===----------------------------------------------------------------------===//
599// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +0000600//===----------------------------------------------------------------------===//
601
602#include "ARMGenCallingConv.inc"
603
604// APCS f64 is in register pairs, possibly split to stack
Owen Andersone50ed302009-08-10 22:56:29 +0000605static bool f64AssignAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000606 CCValAssign::LocInfo &LocInfo,
607 CCState &State, bool CanFail) {
608 static const unsigned RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
609
610 // Try to get the first register.
611 if (unsigned Reg = State.AllocateReg(RegList, 4))
612 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
613 else {
614 // For the 2nd half of a v2f64, do not fail.
615 if (CanFail)
616 return false;
617
618 // Put the whole thing on the stack.
619 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
620 State.AllocateStack(8, 4),
621 LocVT, LocInfo));
622 return true;
623 }
624
625 // Try to get the second register.
626 if (unsigned Reg = State.AllocateReg(RegList, 4))
627 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
628 else
629 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
630 State.AllocateStack(4, 4),
631 LocVT, LocInfo));
632 return true;
633}
634
Owen Andersone50ed302009-08-10 22:56:29 +0000635static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000636 CCValAssign::LocInfo &LocInfo,
637 ISD::ArgFlagsTy &ArgFlags,
638 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000639 if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
640 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000641 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000642 !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
643 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000644 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000645}
646
647// AAPCS f64 is in aligned register pairs
Owen Andersone50ed302009-08-10 22:56:29 +0000648static bool f64AssignAAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000649 CCValAssign::LocInfo &LocInfo,
650 CCState &State, bool CanFail) {
651 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
652 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
653
654 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
655 if (Reg == 0) {
656 // For the 2nd half of a v2f64, do not just fail.
657 if (CanFail)
658 return false;
659
660 // Put the whole thing on the stack.
661 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
662 State.AllocateStack(8, 8),
663 LocVT, LocInfo));
664 return true;
665 }
666
667 unsigned i;
668 for (i = 0; i < 2; ++i)
669 if (HiRegList[i] == Reg)
670 break;
671
672 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
673 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
674 LocVT, LocInfo));
675 return true;
676}
677
Owen Andersone50ed302009-08-10 22:56:29 +0000678static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000679 CCValAssign::LocInfo &LocInfo,
680 ISD::ArgFlagsTy &ArgFlags,
681 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000682 if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
683 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000684 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000685 !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
686 return false;
687 return true; // we handled it
688}
689
Owen Andersone50ed302009-08-10 22:56:29 +0000690static bool f64RetAssign(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000691 CCValAssign::LocInfo &LocInfo, CCState &State) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000692 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
693 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
694
Bob Wilsone65586b2009-04-17 20:40:45 +0000695 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
696 if (Reg == 0)
697 return false; // we didn't handle it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000698
Bob Wilsone65586b2009-04-17 20:40:45 +0000699 unsigned i;
700 for (i = 0; i < 2; ++i)
701 if (HiRegList[i] == Reg)
702 break;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000703
Bob Wilson5bafff32009-06-22 23:27:02 +0000704 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bob Wilsone65586b2009-04-17 20:40:45 +0000705 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000706 LocVT, LocInfo));
707 return true;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000708}
709
Owen Andersone50ed302009-08-10 22:56:29 +0000710static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000711 CCValAssign::LocInfo &LocInfo,
712 ISD::ArgFlagsTy &ArgFlags,
713 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000714 if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
715 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000716 if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
Bob Wilson5bafff32009-06-22 23:27:02 +0000717 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000718 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000719}
720
Owen Andersone50ed302009-08-10 22:56:29 +0000721static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000722 CCValAssign::LocInfo &LocInfo,
723 ISD::ArgFlagsTy &ArgFlags,
724 CCState &State) {
725 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
726 State);
727}
728
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000729/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
730/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000731CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000732 bool Return,
733 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000734 switch (CC) {
735 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000736 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000737 case CallingConv::C:
738 case CallingConv::Fast:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000739 // Use target triple & subtarget features to do actual dispatch.
740 if (Subtarget->isAAPCS_ABI()) {
741 if (Subtarget->hasVFP2() &&
742 FloatABIType == FloatABI::Hard && !isVarArg)
743 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
744 else
745 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
746 } else
747 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000748 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000749 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000750 case CallingConv::ARM_AAPCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000751 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000752 case CallingConv::ARM_APCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000753 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000754 }
755}
756
Dan Gohman98ca4f22009-08-05 01:29:28 +0000757/// LowerCallResult - Lower the result values of a call into the
758/// appropriate copies out of appropriate physical registers.
759SDValue
760ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000761 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000762 const SmallVectorImpl<ISD::InputArg> &Ins,
763 DebugLoc dl, SelectionDAG &DAG,
764 SmallVectorImpl<SDValue> &InVals) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000765
Bob Wilson1f595bb2009-04-17 19:07:39 +0000766 // Assign locations to each value returned by this call.
767 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000768 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000769 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000770 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000771 CCAssignFnForNode(CallConv, /* Return*/ true,
772 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000773
774 // Copy all of the result registers out of their specified physreg.
775 for (unsigned i = 0; i != RVLocs.size(); ++i) {
776 CCValAssign VA = RVLocs[i];
777
Bob Wilson80915242009-04-25 00:33:20 +0000778 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000779 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000780 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000781 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000782 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000783 Chain = Lo.getValue(1);
784 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000785 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000786 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000787 InFlag);
788 Chain = Hi.getValue(1);
789 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +0000790 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +0000791
Owen Anderson825b72b2009-08-11 20:47:22 +0000792 if (VA.getLocVT() == MVT::v2f64) {
793 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
794 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
795 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000796
797 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000798 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000799 Chain = Lo.getValue(1);
800 InFlag = Lo.getValue(2);
801 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000802 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000803 Chain = Hi.getValue(1);
804 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +0000805 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +0000806 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
807 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000808 }
Bob Wilson1f595bb2009-04-17 19:07:39 +0000809 } else {
Bob Wilson80915242009-04-25 00:33:20 +0000810 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
811 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000812 Chain = Val.getValue(1);
813 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000814 }
Bob Wilson80915242009-04-25 00:33:20 +0000815
816 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000817 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +0000818 case CCValAssign::Full: break;
819 case CCValAssign::BCvt:
820 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
821 break;
822 }
823
Dan Gohman98ca4f22009-08-05 01:29:28 +0000824 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000825 }
826
Dan Gohman98ca4f22009-08-05 01:29:28 +0000827 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000828}
829
830/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
831/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +0000832/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +0000833/// a byval function parameter.
834/// Sometimes what we are copying is the end of a larger object, the part that
835/// does not fit in registers.
836static SDValue
837CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
838 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
839 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000840 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000841 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
842 /*AlwaysInline=*/false, NULL, 0, NULL, 0);
843}
844
Bob Wilsondee46d72009-04-17 20:35:10 +0000845/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000846SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +0000847ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
848 SDValue StackPtr, SDValue Arg,
849 DebugLoc dl, SelectionDAG &DAG,
850 const CCValAssign &VA,
851 ISD::ArgFlagsTy Flags) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000852 unsigned LocMemOffset = VA.getLocMemOffset();
853 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
854 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
855 if (Flags.isByVal()) {
856 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
857 }
858 return DAG.getStore(Chain, dl, Arg, PtrOff,
859 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chenga8e29892007-01-19 07:51:42 +0000860}
861
Dan Gohman98ca4f22009-08-05 01:29:28 +0000862void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +0000863 SDValue Chain, SDValue &Arg,
864 RegsToPassVector &RegsToPass,
865 CCValAssign &VA, CCValAssign &NextVA,
866 SDValue &StackPtr,
867 SmallVector<SDValue, 8> &MemOpChains,
868 ISD::ArgFlagsTy Flags) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000869
Jim Grosbache5165492009-11-09 00:11:35 +0000870 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +0000871 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +0000872 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
873
874 if (NextVA.isRegLoc())
875 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
876 else {
877 assert(NextVA.isMemLoc());
878 if (StackPtr.getNode() == 0)
879 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
880
Dan Gohman98ca4f22009-08-05 01:29:28 +0000881 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
882 dl, DAG, NextVA,
883 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000884 }
885}
886
Dan Gohman98ca4f22009-08-05 01:29:28 +0000887/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +0000888/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
889/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000890SDValue
891ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000892 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000893 bool isTailCall,
894 const SmallVectorImpl<ISD::OutputArg> &Outs,
895 const SmallVectorImpl<ISD::InputArg> &Ins,
896 DebugLoc dl, SelectionDAG &DAG,
897 SmallVectorImpl<SDValue> &InVals) {
Evan Chenga8e29892007-01-19 07:51:42 +0000898
Bob Wilson1f595bb2009-04-17 19:07:39 +0000899 // Analyze operands of the call, assigning locations to each operand.
900 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000901 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
902 *DAG.getContext());
903 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000904 CCAssignFnForNode(CallConv, /* Return*/ false,
905 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +0000906
Bob Wilson1f595bb2009-04-17 19:07:39 +0000907 // Get a count of how many bytes are to be pushed on the stack.
908 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +0000909
910 // Adjust the stack pointer for the new arguments...
911 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattnere563bbc2008-10-11 22:08:30 +0000912 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +0000913
Owen Anderson825b72b2009-08-11 20:47:22 +0000914 SDValue StackPtr = DAG.getRegister(ARM::SP, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000915
Bob Wilson5bafff32009-06-22 23:27:02 +0000916 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000917 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +0000918
Bob Wilson1f595bb2009-04-17 19:07:39 +0000919 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +0000920 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000921 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
922 i != e;
923 ++i, ++realArgIdx) {
924 CCValAssign &VA = ArgLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +0000925 SDValue Arg = Outs[realArgIdx].Val;
926 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +0000927
Bob Wilson1f595bb2009-04-17 19:07:39 +0000928 // Promote the value if needed.
929 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000930 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +0000931 case CCValAssign::Full: break;
932 case CCValAssign::SExt:
933 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
934 break;
935 case CCValAssign::ZExt:
936 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
937 break;
938 case CCValAssign::AExt:
939 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
940 break;
941 case CCValAssign::BCvt:
942 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
943 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000944 }
945
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000946 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +0000947 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000948 if (VA.getLocVT() == MVT::v2f64) {
949 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
950 DAG.getConstant(0, MVT::i32));
951 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
952 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000953
Dan Gohman98ca4f22009-08-05 01:29:28 +0000954 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +0000955 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
956
957 VA = ArgLocs[++i]; // skip ahead to next loc
958 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000959 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +0000960 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
961 } else {
962 assert(VA.isMemLoc());
963 if (StackPtr.getNode() == 0)
964 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
965
Dan Gohman98ca4f22009-08-05 01:29:28 +0000966 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
967 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000968 }
969 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000970 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000971 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000972 }
973 } else if (VA.isRegLoc()) {
974 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
975 } else {
976 assert(VA.isMemLoc());
977 if (StackPtr.getNode() == 0)
978 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
979
Dan Gohman98ca4f22009-08-05 01:29:28 +0000980 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
981 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000982 }
Evan Chenga8e29892007-01-19 07:51:42 +0000983 }
984
985 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000986 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +0000987 &MemOpChains[0], MemOpChains.size());
988
989 // Build a sequence of copy-to-reg nodes chained together with token chain
990 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +0000991 SDValue InFlag;
Evan Chenga8e29892007-01-19 07:51:42 +0000992 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Bob Wilson2dc4f542009-03-20 22:42:55 +0000993 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000994 RegsToPass[i].second, InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +0000995 InFlag = Chain.getValue(1);
996 }
997
Bill Wendling056292f2008-09-16 21:48:12 +0000998 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
999 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1000 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001001 bool isDirect = false;
1002 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001003 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001004 MachineFunction &MF = DAG.getMachineFunction();
1005 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chenga8e29892007-01-19 07:51:42 +00001006 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1007 GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001008 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001009 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001010 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001011 getTargetMachine().getRelocationModel() != Reloc::Static;
1012 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001013 // ARM call to a local ARM function is predicable.
1014 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
Evan Chengc60e76d2007-01-30 20:37:08 +00001015 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001016 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001017 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001018 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001019 ARMPCLabelIndex,
1020 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001021 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001022 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001023 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001024 DAG.getEntryNode(), CPAddr,
1025 PseudoSourceValue::getConstantPool(), 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001026 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001027 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001028 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001029 } else
1030 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +00001031 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001032 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001033 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001034 getTargetMachine().getRelocationModel() != Reloc::Static;
1035 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001036 // tBX takes a register source operand.
1037 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001038 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001039 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001040 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001041 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001042 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001043 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001044 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001045 DAG.getEntryNode(), CPAddr,
1046 PseudoSourceValue::getConstantPool(), 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001047 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001048 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001049 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001050 } else
Bill Wendling056292f2008-09-16 21:48:12 +00001051 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001052 }
1053
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001054 // FIXME: handle tail calls differently.
1055 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001056 if (Subtarget->isThumb()) {
1057 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001058 CallOpc = ARMISD::CALL_NOLINK;
1059 else
1060 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1061 } else {
1062 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001063 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1064 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001065 }
David Goodwinf1daf7d2009-07-08 23:10:31 +00001066 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb1Only()) {
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +00001067 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Owen Anderson825b72b2009-08-11 20:47:22 +00001068 Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001069 InFlag = Chain.getValue(1);
1070 }
1071
Dan Gohman475871a2008-07-27 21:46:04 +00001072 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001073 Ops.push_back(Chain);
1074 Ops.push_back(Callee);
1075
1076 // Add argument registers to the end of the list so that they are known live
1077 // into the call.
1078 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1079 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1080 RegsToPass[i].second.getValueType()));
1081
Gabor Greifba36cb52008-08-28 21:40:38 +00001082 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001083 Ops.push_back(InFlag);
Duncan Sands4bdcb612008-07-02 17:40:58 +00001084 // Returns a chain and a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00001085 Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Flag),
Duncan Sands4bdcb612008-07-02 17:40:58 +00001086 &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001087 InFlag = Chain.getValue(1);
1088
Chris Lattnere563bbc2008-10-11 22:08:30 +00001089 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1090 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001091 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001092 InFlag = Chain.getValue(1);
1093
Bob Wilson1f595bb2009-04-17 19:07:39 +00001094 // Handle result values, copying them out of physregs into vregs that we
1095 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001096 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1097 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001098}
1099
Dan Gohman98ca4f22009-08-05 01:29:28 +00001100SDValue
1101ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001102 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001103 const SmallVectorImpl<ISD::OutputArg> &Outs,
1104 DebugLoc dl, SelectionDAG &DAG) {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001105
Bob Wilsondee46d72009-04-17 20:35:10 +00001106 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001107 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001108
Bob Wilsondee46d72009-04-17 20:35:10 +00001109 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001110 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1111 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001112
Dan Gohman98ca4f22009-08-05 01:29:28 +00001113 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001114 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1115 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001116
1117 // If this is the first return lowered for this function, add
1118 // the regs to the liveout set for the function.
1119 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1120 for (unsigned i = 0; i != RVLocs.size(); ++i)
1121 if (RVLocs[i].isRegLoc())
1122 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001123 }
1124
Bob Wilson1f595bb2009-04-17 19:07:39 +00001125 SDValue Flag;
1126
1127 // Copy the result values into the output registers.
1128 for (unsigned i = 0, realRVLocIdx = 0;
1129 i != RVLocs.size();
1130 ++i, ++realRVLocIdx) {
1131 CCValAssign &VA = RVLocs[i];
1132 assert(VA.isRegLoc() && "Can only return in registers!");
1133
Dan Gohman98ca4f22009-08-05 01:29:28 +00001134 SDValue Arg = Outs[realRVLocIdx].Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001135
1136 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001137 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001138 case CCValAssign::Full: break;
1139 case CCValAssign::BCvt:
1140 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1141 break;
1142 }
1143
Bob Wilson1f595bb2009-04-17 19:07:39 +00001144 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001145 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001146 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001147 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1148 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001149 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001150 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001151
1152 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1153 Flag = Chain.getValue(1);
1154 VA = RVLocs[++i]; // skip ahead to next loc
1155 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1156 HalfGPRs.getValue(1), Flag);
1157 Flag = Chain.getValue(1);
1158 VA = RVLocs[++i]; // skip ahead to next loc
1159
1160 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001161 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1162 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001163 }
1164 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1165 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001166 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001167 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001168 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001169 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001170 VA = RVLocs[++i]; // skip ahead to next loc
1171 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1172 Flag);
1173 } else
1174 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1175
Bob Wilsondee46d72009-04-17 20:35:10 +00001176 // Guarantee that all emitted copies are
1177 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001178 Flag = Chain.getValue(1);
1179 }
1180
1181 SDValue result;
1182 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001183 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001184 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001185 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001186
1187 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001188}
1189
Bob Wilsonb62d2572009-11-03 00:02:05 +00001190// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1191// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1192// one of the above mentioned nodes. It has to be wrapped because otherwise
1193// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1194// be used to form addressing mode. These wrapped nodes will be selected
1195// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001196static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001197 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001198 // FIXME there is no actual debug info here
1199 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001200 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001201 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001202 if (CP->isMachineConstantPoolEntry())
1203 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1204 CP->getAlignment());
1205 else
1206 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1207 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001208 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001209}
1210
Bob Wilsonddb16df2009-10-30 05:45:42 +00001211SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001212 MachineFunction &MF = DAG.getMachineFunction();
1213 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1214 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001215 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001216 EVT PtrVT = getPointerTy();
Bob Wilsonddb16df2009-10-30 05:45:42 +00001217 BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001218 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1219 SDValue CPAddr;
1220 if (RelocM == Reloc::Static) {
1221 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1222 } else {
1223 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001224 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001225 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1226 ARMCP::CPBlockAddress,
1227 PCAdj);
1228 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1229 }
1230 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1231 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
1232 PseudoSourceValue::getConstantPool(), 0);
1233 if (RelocM == Reloc::Static)
1234 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001235 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001236 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001237}
1238
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001239// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001240SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001241ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
1242 SelectionDAG &DAG) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001243 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001244 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001245 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001246 MachineFunction &MF = DAG.getMachineFunction();
1247 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1248 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001249 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001250 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001251 ARMCP::CPValue, PCAdj, "tlsgd", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001252 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001253 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001254 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
1255 PseudoSourceValue::getConstantPool(), 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001256 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001257
Evan Chenge7e0d622009-11-06 22:24:13 +00001258 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001259 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001260
1261 // call __tls_get_addr.
1262 ArgListTy Args;
1263 ArgListEntry Entry;
1264 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001265 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001266 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001267 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001268 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001269 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1270 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001271 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001272 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001273 return CallResult.first;
1274}
1275
1276// Lower ISD::GlobalTLSAddress using the "initial exec" or
1277// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001278SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001279ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001280 SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001281 GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001282 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001283 SDValue Offset;
1284 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001285 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001286 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001287 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001288
Chris Lattner4fb63d02009-07-15 04:12:33 +00001289 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001290 MachineFunction &MF = DAG.getMachineFunction();
1291 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1292 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1293 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001294 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1295 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001296 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001297 ARMCP::CPValue, PCAdj, "gottpoff", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001298 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001299 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001300 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
1301 PseudoSourceValue::getConstantPool(), 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001302 Chain = Offset.getValue(1);
1303
Evan Chenge7e0d622009-11-06 22:24:13 +00001304 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001305 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001306
Evan Cheng9eda6892009-10-31 03:39:36 +00001307 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
1308 PseudoSourceValue::getConstantPool(), 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001309 } else {
1310 // local exec model
Evan Chenge4e4ed32009-08-28 23:18:09 +00001311 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, "tpoff");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001312 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001313 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001314 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
1315 PseudoSourceValue::getConstantPool(), 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001316 }
1317
1318 // The address of the thread local variable is the add of the thread
1319 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001320 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001321}
1322
Dan Gohman475871a2008-07-27 21:46:04 +00001323SDValue
1324ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001325 // TODO: implement the "local dynamic" model
1326 assert(Subtarget->isTargetELF() &&
1327 "TLS not implemented for non-ELF targets");
1328 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1329 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1330 // otherwise use the "Local Exec" TLS Model
1331 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1332 return LowerToTLSGeneralDynamicModel(GA, DAG);
1333 else
1334 return LowerToTLSExecModels(GA, DAG);
1335}
1336
Dan Gohman475871a2008-07-27 21:46:04 +00001337SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001338 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001339 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001340 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001341 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1342 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1343 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001344 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001345 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001346 new ARMConstantPoolValue(GV, UseGOTOFF ? "GOTOFF" : "GOT");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001347 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001348 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001349 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001350 CPAddr,
1351 PseudoSourceValue::getConstantPool(), 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001352 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001353 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001354 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001355 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001356 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
1357 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001358 return Result;
1359 } else {
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001360 // If we have T2 ops, we can materialize the address directly via movt/movw
1361 // pair. This is always cheaper.
1362 if (Subtarget->useMovt()) {
1363 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
1364 DAG.getTargetGlobalAddress(GV, PtrVT));
1365 } else {
1366 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
1367 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1368 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1369 PseudoSourceValue::getConstantPool(), 0);
1370 }
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001371 }
1372}
1373
Dan Gohman475871a2008-07-27 21:46:04 +00001374SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001375 SelectionDAG &DAG) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001376 MachineFunction &MF = DAG.getMachineFunction();
1377 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1378 unsigned ARMPCLabelIndex = 0;
Owen Andersone50ed302009-08-10 22:56:29 +00001379 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001380 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001381 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1382 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Dan Gohman475871a2008-07-27 21:46:04 +00001383 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +00001384 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001385 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001386 else {
Evan Chenge7e0d622009-11-06 22:24:13 +00001387 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001388 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
1389 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001390 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001391 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001392 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001393 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001394
Evan Cheng9eda6892009-10-31 03:39:36 +00001395 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1396 PseudoSourceValue::getConstantPool(), 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001397 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001398
1399 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001400 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001401 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001402 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00001403
Evan Cheng63476a82009-09-03 07:04:02 +00001404 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Evan Cheng9eda6892009-10-31 03:39:36 +00001405 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
1406 PseudoSourceValue::getGOT(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001407
1408 return Result;
1409}
1410
Dan Gohman475871a2008-07-27 21:46:04 +00001411SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001412 SelectionDAG &DAG){
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001413 assert(Subtarget->isTargetELF() &&
1414 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00001415 MachineFunction &MF = DAG.getMachineFunction();
1416 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1417 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Andersone50ed302009-08-10 22:56:29 +00001418 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001419 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001420 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00001421 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1422 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00001423 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001424 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001425 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001426 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1427 PseudoSourceValue::getConstantPool(), 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001428 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001429 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001430}
1431
Jim Grosbach0e0da732009-05-12 23:59:14 +00001432SDValue
1433ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001434 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001435 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001436 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001437 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00001438 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00001439 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00001440 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1441 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001442 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001443 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00001444 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1445 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001446 EVT PtrVT = getPointerTy();
1447 DebugLoc dl = Op.getDebugLoc();
1448 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1449 SDValue CPAddr;
1450 unsigned PCAdj = (RelocM != Reloc::PIC_)
1451 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001452 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001453 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
1454 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001455 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001456 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001457 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00001458 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1459 PseudoSourceValue::getConstantPool(), 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001460 SDValue Chain = Result.getValue(1);
1461
1462 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001463 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001464 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1465 }
1466 return Result;
1467 }
Jim Grosbachf9570122009-05-14 00:46:35 +00001468 case Intrinsic::eh_sjlj_setjmp:
Owen Anderson825b72b2009-08-11 20:47:22 +00001469 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(1));
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001470 }
1471}
1472
Dan Gohman475871a2008-07-27 21:46:04 +00001473static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001474 unsigned VarArgsFrameIndex) {
Evan Chenga8e29892007-01-19 07:51:42 +00001475 // vastart just stores the address of the VarArgsFrameIndex slot into the
1476 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001477 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001478 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001479 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001480 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001481 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001482}
1483
Dan Gohman475871a2008-07-27 21:46:04 +00001484SDValue
Evan Cheng86198642009-08-07 00:34:42 +00001485ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) {
1486 SDNode *Node = Op.getNode();
1487 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001488 EVT VT = Node->getValueType(0);
Evan Cheng86198642009-08-07 00:34:42 +00001489 SDValue Chain = Op.getOperand(0);
1490 SDValue Size = Op.getOperand(1);
1491 SDValue Align = Op.getOperand(2);
1492
1493 // Chain the dynamic stack allocation so that it doesn't modify the stack
1494 // pointer when other instructions are using the stack.
1495 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1496
1497 unsigned AlignVal = cast<ConstantSDNode>(Align)->getZExtValue();
1498 unsigned StackAlign = getTargetMachine().getFrameInfo()->getStackAlignment();
1499 if (AlignVal > StackAlign)
1500 // Do this now since selection pass cannot introduce new target
1501 // independent node.
1502 Align = DAG.getConstant(-(uint64_t)AlignVal, VT);
1503
1504 // In Thumb1 mode, there isn't a "sub r, sp, r" instruction, we will end up
1505 // using a "add r, sp, r" instead. Negate the size now so we don't have to
1506 // do even more horrible hack later.
1507 MachineFunction &MF = DAG.getMachineFunction();
1508 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1509 if (AFI->isThumb1OnlyFunction()) {
1510 bool Negate = true;
1511 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Size);
1512 if (C) {
1513 uint32_t Val = C->getZExtValue();
1514 if (Val <= 508 && ((Val & 3) == 0))
1515 Negate = false;
1516 }
1517 if (Negate)
1518 Size = DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(0, VT), Size);
1519 }
1520
Owen Anderson825b72b2009-08-11 20:47:22 +00001521 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
Evan Cheng86198642009-08-07 00:34:42 +00001522 SDValue Ops1[] = { Chain, Size, Align };
1523 SDValue Res = DAG.getNode(ARMISD::DYN_ALLOC, dl, VTList, Ops1, 3);
1524 Chain = Res.getValue(1);
1525 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1526 DAG.getIntPtrConstant(0, true), SDValue());
1527 SDValue Ops2[] = { Res, Chain };
1528 return DAG.getMergeValues(Ops2, 2, dl);
1529}
1530
1531SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00001532ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
1533 SDValue &Root, SelectionDAG &DAG,
1534 DebugLoc dl) {
1535 MachineFunction &MF = DAG.getMachineFunction();
1536 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1537
1538 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001539 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00001540 RC = ARM::tGPRRegisterClass;
1541 else
1542 RC = ARM::GPRRegisterClass;
1543
1544 // Transform the arguments stored in physical registers into virtual ones.
1545 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001546 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001547
1548 SDValue ArgValue2;
1549 if (NextVA.isMemLoc()) {
1550 unsigned ArgSize = NextVA.getLocVT().getSizeInBits()/8;
1551 MachineFrameInfo *MFI = MF.getFrameInfo();
David Greene3f2bf852009-11-12 20:49:22 +00001552 int FI = MFI->CreateFixedObject(ArgSize, NextVA.getLocMemOffset(),
1553 true, false);
Bob Wilson5bafff32009-06-22 23:27:02 +00001554
1555 // Create load node to retrieve arguments from the stack.
1556 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00001557 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
1558 PseudoSourceValue::getFixedStack(FI), 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00001559 } else {
1560 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001561 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001562 }
1563
Jim Grosbache5165492009-11-09 00:11:35 +00001564 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00001565}
1566
1567SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001568ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001569 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001570 const SmallVectorImpl<ISD::InputArg>
1571 &Ins,
1572 DebugLoc dl, SelectionDAG &DAG,
1573 SmallVectorImpl<SDValue> &InVals) {
1574
Bob Wilson1f595bb2009-04-17 19:07:39 +00001575 MachineFunction &MF = DAG.getMachineFunction();
1576 MachineFrameInfo *MFI = MF.getFrameInfo();
1577
Bob Wilson1f595bb2009-04-17 19:07:39 +00001578 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1579
1580 // Assign locations to all of the incoming arguments.
1581 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001582 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1583 *DAG.getContext());
1584 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001585 CCAssignFnForNode(CallConv, /* Return*/ false,
1586 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001587
1588 SmallVector<SDValue, 16> ArgValues;
1589
1590 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1591 CCValAssign &VA = ArgLocs[i];
1592
Bob Wilsondee46d72009-04-17 20:35:10 +00001593 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001594 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001595 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00001596
Bob Wilson5bafff32009-06-22 23:27:02 +00001597 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001598 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001599 // f64 and vector types are split up into multiple registers or
1600 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00001601 RegVT = MVT::i32;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001602
Owen Anderson825b72b2009-08-11 20:47:22 +00001603 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001604 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001605 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00001606 VA = ArgLocs[++i]; // skip ahead to next loc
1607 SDValue ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001608 Chain, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001609 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1610 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001611 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00001612 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001613 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
1614 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001615 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001616
Bob Wilson5bafff32009-06-22 23:27:02 +00001617 } else {
1618 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001619
Owen Anderson825b72b2009-08-11 20:47:22 +00001620 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00001621 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001622 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00001623 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001624 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001625 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001626 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001627 RC = (AFI->isThumb1OnlyFunction() ?
1628 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00001629 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001630 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00001631
1632 // Transform the arguments in physical registers into virtual ones.
1633 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001634 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001635 }
1636
1637 // If this is an 8 or 16-bit value, it is really passed promoted
1638 // to 32 bits. Insert an assert[sz]ext to capture this, then
1639 // truncate to the right size.
1640 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001641 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001642 case CCValAssign::Full: break;
1643 case CCValAssign::BCvt:
1644 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1645 break;
1646 case CCValAssign::SExt:
1647 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
1648 DAG.getValueType(VA.getValVT()));
1649 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1650 break;
1651 case CCValAssign::ZExt:
1652 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
1653 DAG.getValueType(VA.getValVT()));
1654 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1655 break;
1656 }
1657
Dan Gohman98ca4f22009-08-05 01:29:28 +00001658 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001659
1660 } else { // VA.isRegLoc()
1661
1662 // sanity check
1663 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00001664 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001665
1666 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001667 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
1668 true, false);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001669
Bob Wilsondee46d72009-04-17 20:35:10 +00001670 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001671 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00001672 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
1673 PseudoSourceValue::getFixedStack(FI), 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001674 }
1675 }
1676
1677 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00001678 if (isVarArg) {
1679 static const unsigned GPRArgRegs[] = {
1680 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1681 };
1682
Bob Wilsondee46d72009-04-17 20:35:10 +00001683 unsigned NumGPRs = CCInfo.getFirstUnallocated
1684 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001685
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001686 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1687 unsigned VARegSize = (4 - NumGPRs) * 4;
1688 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Rafael Espindolac1382b72009-10-30 14:33:14 +00001689 unsigned ArgOffset = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001690 if (VARegSaveSize) {
1691 // If this function is vararg, store any remaining integer argument regs
1692 // to their spots on the stack so that they may be loaded by deferencing
1693 // the result of va_next.
1694 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001695 VarArgsFrameIndex = MFI->CreateFixedObject(VARegSaveSize, ArgOffset +
David Greene3f2bf852009-11-12 20:49:22 +00001696 VARegSaveSize - VARegSize,
1697 true, false);
Dan Gohman475871a2008-07-27 21:46:04 +00001698 SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001699
Dan Gohman475871a2008-07-27 21:46:04 +00001700 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00001701 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001702 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001703 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00001704 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001705 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00001706 RC = ARM::GPRRegisterClass;
1707
Bob Wilson998e1252009-04-20 18:36:57 +00001708 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001709 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Evan Cheng9eda6892009-10-31 03:39:36 +00001710 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
1711 PseudoSourceValue::getFixedStack(VarArgsFrameIndex), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001712 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001713 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00001714 DAG.getConstant(4, getPointerTy()));
1715 }
1716 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001717 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001718 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001719 } else
1720 // This will point to the next argument passed via stack.
David Greene3f2bf852009-11-12 20:49:22 +00001721 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset, true, false);
Evan Chenga8e29892007-01-19 07:51:42 +00001722 }
1723
Dan Gohman98ca4f22009-08-05 01:29:28 +00001724 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00001725}
1726
1727/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00001728static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00001729 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001730 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00001731 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00001732 // Maybe this has already been legalized into the constant pool?
1733 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00001734 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001735 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
1736 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001737 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001738 }
1739 }
1740 return false;
1741}
1742
Evan Chenga8e29892007-01-19 07:51:42 +00001743/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1744/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00001745SDValue
1746ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
1747 SDValue &ARMCC, SelectionDAG &DAG, DebugLoc dl) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001748 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001749 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00001750 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001751 // Constant does not fit, try adjusting it by one?
1752 switch (CC) {
1753 default: break;
1754 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00001755 case ISD::SETGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00001756 if (isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001757 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001758 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001759 }
1760 break;
1761 case ISD::SETULT:
1762 case ISD::SETUGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00001763 if (C > 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001764 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001765 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001766 }
1767 break;
1768 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00001769 case ISD::SETGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00001770 if (isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001771 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001772 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001773 }
1774 break;
1775 case ISD::SETULE:
1776 case ISD::SETUGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00001777 if (C < 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001778 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001779 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001780 }
1781 break;
1782 }
1783 }
1784 }
1785
1786 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001787 ARMISD::NodeType CompareType;
1788 switch (CondCode) {
1789 default:
1790 CompareType = ARMISD::CMP;
1791 break;
1792 case ARMCC::EQ:
1793 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00001794 // Uses only Z Flag
1795 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001796 break;
1797 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001798 ARMCC = DAG.getConstant(CondCode, MVT::i32);
1799 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001800}
1801
1802/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001803static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Dale Johannesende064702009-02-06 21:50:26 +00001804 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001805 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00001806 if (!isFloatingPointZero(RHS))
Owen Anderson825b72b2009-08-11 20:47:22 +00001807 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001808 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001809 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
1810 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001811}
1812
Evan Cheng06b53c02009-11-12 07:13:11 +00001813SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001814 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001815 SDValue LHS = Op.getOperand(0);
1816 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001817 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001818 SDValue TrueVal = Op.getOperand(2);
1819 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00001820 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001821
Owen Anderson825b72b2009-08-11 20:47:22 +00001822 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001823 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001824 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng06b53c02009-11-12 07:13:11 +00001825 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
Dale Johannesende064702009-02-06 21:50:26 +00001826 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001827 }
1828
1829 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001830 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00001831
Owen Anderson825b72b2009-08-11 20:47:22 +00001832 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1833 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001834 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
1835 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00001836 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001837 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001838 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001839 // FIXME: Needs another CMP because flag can have but one use.
Dale Johannesende064702009-02-06 21:50:26 +00001840 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001841 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Dale Johannesende064702009-02-06 21:50:26 +00001842 Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00001843 }
1844 return Result;
1845}
1846
Evan Cheng06b53c02009-11-12 07:13:11 +00001847SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00001848 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001849 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001850 SDValue LHS = Op.getOperand(2);
1851 SDValue RHS = Op.getOperand(3);
1852 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00001853 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001854
Owen Anderson825b72b2009-08-11 20:47:22 +00001855 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001856 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001857 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng06b53c02009-11-12 07:13:11 +00001858 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001859 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Dale Johannesende064702009-02-06 21:50:26 +00001860 Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001861 }
1862
Owen Anderson825b72b2009-08-11 20:47:22 +00001863 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Chenga8e29892007-01-19 07:51:42 +00001864 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001865 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001866
Dale Johannesende064702009-02-06 21:50:26 +00001867 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001868 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1869 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1870 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00001871 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00001872 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001873 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001874 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00001875 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00001876 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001877 }
1878 return Res;
1879}
1880
Dan Gohman475871a2008-07-27 21:46:04 +00001881SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) {
1882 SDValue Chain = Op.getOperand(0);
1883 SDValue Table = Op.getOperand(1);
1884 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001885 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001886
Owen Andersone50ed302009-08-10 22:56:29 +00001887 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00001888 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
1889 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00001890 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00001891 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00001892 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00001893 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
1894 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00001895 if (Subtarget->isThumb2()) {
1896 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
1897 // which does another jump to the destination. This also makes it easier
1898 // to translate it to TBB / TBH later.
1899 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00001900 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00001901 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001902 }
Evan Cheng66ac5312009-07-25 00:33:29 +00001903 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00001904 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
1905 PseudoSourceValue::getJumpTable(), 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00001906 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001907 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00001908 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001909 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00001910 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
1911 PseudoSourceValue::getJumpTable(), 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00001912 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00001913 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001914 }
Evan Chenga8e29892007-01-19 07:51:42 +00001915}
1916
Dan Gohman475871a2008-07-27 21:46:04 +00001917static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Dale Johannesende064702009-02-06 21:50:26 +00001918 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001919 unsigned Opc =
1920 Op.getOpcode() == ISD::FP_TO_SINT ? ARMISD::FTOSI : ARMISD::FTOUI;
Owen Anderson825b72b2009-08-11 20:47:22 +00001921 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
1922 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001923}
1924
Dan Gohman475871a2008-07-27 21:46:04 +00001925static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001926 EVT VT = Op.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001927 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001928 unsigned Opc =
1929 Op.getOpcode() == ISD::SINT_TO_FP ? ARMISD::SITOF : ARMISD::UITOF;
1930
Owen Anderson825b72b2009-08-11 20:47:22 +00001931 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
Dale Johannesende064702009-02-06 21:50:26 +00001932 return DAG.getNode(Opc, dl, VT, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001933}
1934
Dan Gohman475871a2008-07-27 21:46:04 +00001935static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00001936 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00001937 SDValue Tmp0 = Op.getOperand(0);
1938 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00001939 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001940 EVT VT = Op.getValueType();
1941 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001942 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
1943 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001944 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
1945 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001946 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001947}
1948
Jim Grosbach0e0da732009-05-12 23:59:14 +00001949SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
1950 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1951 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00001952 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001953 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
1954 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00001955 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00001956 ? ARM::R7 : ARM::R11;
1957 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
1958 while (Depth--)
1959 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
1960 return FrameAddr;
1961}
1962
Dan Gohman475871a2008-07-27 21:46:04 +00001963SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00001964ARMTargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman475871a2008-07-27 21:46:04 +00001965 SDValue Chain,
1966 SDValue Dst, SDValue Src,
1967 SDValue Size, unsigned Align,
Dan Gohman707e0182008-04-12 04:36:06 +00001968 bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00001969 const Value *DstSV, uint64_t DstSVOff,
1970 const Value *SrcSV, uint64_t SrcSVOff){
Evan Cheng4102eb52007-10-22 22:11:27 +00001971 // Do repeated 4-byte loads and stores. To be improved.
Dan Gohman707e0182008-04-12 04:36:06 +00001972 // This requires 4-byte alignment.
1973 if ((Align & 3) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00001974 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001975 // This requires the copy size to be a constant, preferrably
1976 // within a subtarget-specific limit.
1977 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
1978 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00001979 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001980 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001981 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00001982 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001983
1984 unsigned BytesLeft = SizeVal & 3;
1985 unsigned NumMemOps = SizeVal >> 2;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001986 unsigned EmittedNumMemOps = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001987 EVT VT = MVT::i32;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001988 unsigned VTSize = 4;
Evan Cheng4102eb52007-10-22 22:11:27 +00001989 unsigned i = 0;
Evan Chenge5e7ce42007-05-18 01:19:57 +00001990 const unsigned MAX_LOADS_IN_LDM = 6;
Dan Gohman475871a2008-07-27 21:46:04 +00001991 SDValue TFOps[MAX_LOADS_IN_LDM];
1992 SDValue Loads[MAX_LOADS_IN_LDM];
Dan Gohman1f13c682008-04-28 17:15:20 +00001993 uint64_t SrcOff = 0, DstOff = 0;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001994
Evan Cheng4102eb52007-10-22 22:11:27 +00001995 // Emit up to MAX_LOADS_IN_LDM loads, then a TokenFactor barrier, then the
1996 // same number of stores. The loads and stores will get combined into
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001997 // ldm/stm later on.
Evan Cheng4102eb52007-10-22 22:11:27 +00001998 while (EmittedNumMemOps < NumMemOps) {
1999 for (i = 0;
2000 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00002001 Loads[i] = DAG.getLoad(VT, dl, Chain,
Owen Anderson825b72b2009-08-11 20:47:22 +00002002 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
2003 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00002004 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00002005 TFOps[i] = Loads[i].getValue(1);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002006 SrcOff += VTSize;
2007 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002008 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002009
Evan Cheng4102eb52007-10-22 22:11:27 +00002010 for (i = 0;
2011 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00002012 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Owen Anderson825b72b2009-08-11 20:47:22 +00002013 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
2014 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00002015 DstSV, DstSVOff + DstOff);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002016 DstOff += VTSize;
2017 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002018 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00002019
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002020 EmittedNumMemOps += i;
2021 }
2022
Bob Wilson2dc4f542009-03-20 22:42:55 +00002023 if (BytesLeft == 0)
Evan Cheng4102eb52007-10-22 22:11:27 +00002024 return Chain;
2025
2026 // Issue loads / stores for the trailing (1 - 3) bytes.
2027 unsigned BytesLeftSave = BytesLeft;
2028 i = 0;
2029 while (BytesLeft) {
2030 if (BytesLeft >= 2) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002031 VT = MVT::i16;
Evan Cheng4102eb52007-10-22 22:11:27 +00002032 VTSize = 2;
2033 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00002034 VT = MVT::i8;
Evan Cheng4102eb52007-10-22 22:11:27 +00002035 VTSize = 1;
2036 }
2037
Dale Johannesen0f502f62009-02-03 22:26:09 +00002038 Loads[i] = DAG.getLoad(VT, dl, Chain,
Owen Anderson825b72b2009-08-11 20:47:22 +00002039 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
2040 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00002041 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00002042 TFOps[i] = Loads[i].getValue(1);
2043 ++i;
2044 SrcOff += VTSize;
2045 BytesLeft -= VTSize;
2046 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002047 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00002048
2049 i = 0;
2050 BytesLeft = BytesLeftSave;
2051 while (BytesLeft) {
2052 if (BytesLeft >= 2) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002053 VT = MVT::i16;
Evan Cheng4102eb52007-10-22 22:11:27 +00002054 VTSize = 2;
2055 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00002056 VT = MVT::i8;
Evan Cheng4102eb52007-10-22 22:11:27 +00002057 VTSize = 1;
2058 }
2059
Dale Johannesen0f502f62009-02-03 22:26:09 +00002060 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Owen Anderson825b72b2009-08-11 20:47:22 +00002061 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
2062 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00002063 DstSV, DstSVOff + DstOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00002064 ++i;
2065 DstOff += VTSize;
2066 BytesLeft -= VTSize;
2067 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002068 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002069}
2070
Duncan Sands1607f052008-12-01 11:39:25 +00002071static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00002072 SDValue Op = N->getOperand(0);
Dale Johannesende064702009-02-06 21:50:26 +00002073 DebugLoc dl = N->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00002074 if (N->getValueType(0) == MVT::f64) {
Jim Grosbache5165492009-11-09 00:11:35 +00002075 // Turn i64->f64 into VMOVDRR.
Owen Anderson825b72b2009-08-11 20:47:22 +00002076 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2077 DAG.getConstant(0, MVT::i32));
2078 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2079 DAG.getConstant(1, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00002080 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chengc7c77292008-11-04 19:57:48 +00002081 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002082
Jim Grosbache5165492009-11-09 00:11:35 +00002083 // Turn f64->i64 into VMOVRRD.
2084 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00002085 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002086
Chris Lattner27a6c732007-11-24 07:07:01 +00002087 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002088 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
Chris Lattner27a6c732007-11-24 07:07:01 +00002089}
2090
Bob Wilson5bafff32009-06-22 23:27:02 +00002091/// getZeroVector - Returns a vector of specified type with all zero elements.
2092///
Owen Andersone50ed302009-08-10 22:56:29 +00002093static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002094 assert(VT.isVector() && "Expected a vector type");
2095
2096 // Zero vectors are used to represent vector negation and in those cases
2097 // will be implemented with the NEON VNEG instruction. However, VNEG does
2098 // not support i64 elements, so sometimes the zero vectors will need to be
2099 // explicitly constructed. For those cases, and potentially other uses in
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002100 // the future, always build zero vectors as <16 x i8> or <8 x i8> bitcasted
Bob Wilson5bafff32009-06-22 23:27:02 +00002101 // to their dest type. This ensures they get CSE'd.
2102 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002103 SDValue Cst = DAG.getTargetConstant(0, MVT::i8);
2104 SmallVector<SDValue, 8> Ops;
2105 MVT TVT;
2106
2107 if (VT.getSizeInBits() == 64) {
2108 Ops.assign(8, Cst); TVT = MVT::v8i8;
2109 } else {
2110 Ops.assign(16, Cst); TVT = MVT::v16i8;
2111 }
2112 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002113
2114 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2115}
2116
2117/// getOnesVector - Returns a vector of specified type with all bits set.
2118///
Owen Andersone50ed302009-08-10 22:56:29 +00002119static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002120 assert(VT.isVector() && "Expected a vector type");
2121
Bob Wilson929ffa22009-10-30 20:13:25 +00002122 // Always build ones vectors as <16 x i8> or <8 x i8> bitcasted to their
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002123 // dest type. This ensures they get CSE'd.
Bob Wilson5bafff32009-06-22 23:27:02 +00002124 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002125 SDValue Cst = DAG.getTargetConstant(0xFF, MVT::i8);
2126 SmallVector<SDValue, 8> Ops;
2127 MVT TVT;
2128
2129 if (VT.getSizeInBits() == 64) {
2130 Ops.assign(8, Cst); TVT = MVT::v8i8;
2131 } else {
2132 Ops.assign(16, Cst); TVT = MVT::v16i8;
2133 }
2134 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002135
2136 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2137}
2138
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002139/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
2140/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Evan Cheng06b53c02009-11-12 07:13:11 +00002141SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op, SelectionDAG &DAG) {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002142 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2143 EVT VT = Op.getValueType();
2144 unsigned VTBits = VT.getSizeInBits();
2145 DebugLoc dl = Op.getDebugLoc();
2146 SDValue ShOpLo = Op.getOperand(0);
2147 SDValue ShOpHi = Op.getOperand(1);
2148 SDValue ShAmt = Op.getOperand(2);
2149 SDValue ARMCC;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002150 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002151
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002152 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
2153
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002154 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2155 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2156 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
2157 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2158 DAG.getConstant(VTBits, MVT::i32));
2159 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
2160 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002161 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002162
2163 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2164 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng06b53c02009-11-12 07:13:11 +00002165 ARMCC, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002166 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002167 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC,
2168 CCR, Cmp);
2169
2170 SDValue Ops[2] = { Lo, Hi };
2171 return DAG.getMergeValues(Ops, 2, dl);
2172}
2173
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002174/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
2175/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Evan Cheng06b53c02009-11-12 07:13:11 +00002176SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op, SelectionDAG &DAG) {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002177 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2178 EVT VT = Op.getValueType();
2179 unsigned VTBits = VT.getSizeInBits();
2180 DebugLoc dl = Op.getDebugLoc();
2181 SDValue ShOpLo = Op.getOperand(0);
2182 SDValue ShOpHi = Op.getOperand(1);
2183 SDValue ShAmt = Op.getOperand(2);
2184 SDValue ARMCC;
2185
2186 assert(Op.getOpcode() == ISD::SHL_PARTS);
2187 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2188 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2189 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
2190 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2191 DAG.getConstant(VTBits, MVT::i32));
2192 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
2193 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
2194
2195 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2196 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2197 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng06b53c02009-11-12 07:13:11 +00002198 ARMCC, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002199 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
2200 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMCC,
2201 CCR, Cmp);
2202
2203 SDValue Ops[2] = { Lo, Hi };
2204 return DAG.getMergeValues(Ops, 2, dl);
2205}
2206
Bob Wilson5bafff32009-06-22 23:27:02 +00002207static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2208 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00002209 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002210 DebugLoc dl = N->getDebugLoc();
2211
2212 // Lower vector shifts on NEON to use VSHL.
2213 if (VT.isVector()) {
2214 assert(ST->hasNEON() && "unexpected vector shift");
2215
2216 // Left shifts translate directly to the vshiftu intrinsic.
2217 if (N->getOpcode() == ISD::SHL)
2218 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002219 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002220 N->getOperand(0), N->getOperand(1));
2221
2222 assert((N->getOpcode() == ISD::SRA ||
2223 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2224
2225 // NEON uses the same intrinsics for both left and right shifts. For
2226 // right shifts, the shift amounts are negative, so negate the vector of
2227 // shift amounts.
Owen Andersone50ed302009-08-10 22:56:29 +00002228 EVT ShiftVT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002229 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2230 getZeroVector(ShiftVT, DAG, dl),
2231 N->getOperand(1));
2232 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2233 Intrinsic::arm_neon_vshifts :
2234 Intrinsic::arm_neon_vshiftu);
2235 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002236 DAG.getConstant(vshiftInt, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002237 N->getOperand(0), NegatedCount);
2238 }
2239
Eli Friedmance392eb2009-08-22 03:13:10 +00002240 // We can get here for a node like i32 = ISD::SHL i32, i64
2241 if (VT != MVT::i64)
2242 return SDValue();
2243
2244 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00002245 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00002246
Chris Lattner27a6c732007-11-24 07:07:01 +00002247 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2248 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002249 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00002250 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002251
Chris Lattner27a6c732007-11-24 07:07:01 +00002252 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00002253 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002254
Chris Lattner27a6c732007-11-24 07:07:01 +00002255 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00002256 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2257 DAG.getConstant(0, MVT::i32));
2258 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2259 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002260
Chris Lattner27a6c732007-11-24 07:07:01 +00002261 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2262 // captures the result into a carry flag.
2263 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Owen Anderson825b72b2009-08-11 20:47:22 +00002264 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002265
Chris Lattner27a6c732007-11-24 07:07:01 +00002266 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00002267 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002268
Chris Lattner27a6c732007-11-24 07:07:01 +00002269 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002270 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00002271}
2272
Bob Wilson5bafff32009-06-22 23:27:02 +00002273static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2274 SDValue TmpOp0, TmpOp1;
2275 bool Invert = false;
2276 bool Swap = false;
2277 unsigned Opc = 0;
2278
2279 SDValue Op0 = Op.getOperand(0);
2280 SDValue Op1 = Op.getOperand(1);
2281 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00002282 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002283 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2284 DebugLoc dl = Op.getDebugLoc();
2285
2286 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2287 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002288 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002289 case ISD::SETUNE:
2290 case ISD::SETNE: Invert = true; // Fallthrough
2291 case ISD::SETOEQ:
2292 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2293 case ISD::SETOLT:
2294 case ISD::SETLT: Swap = true; // Fallthrough
2295 case ISD::SETOGT:
2296 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2297 case ISD::SETOLE:
2298 case ISD::SETLE: Swap = true; // Fallthrough
2299 case ISD::SETOGE:
2300 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2301 case ISD::SETUGE: Swap = true; // Fallthrough
2302 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2303 case ISD::SETUGT: Swap = true; // Fallthrough
2304 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2305 case ISD::SETUEQ: Invert = true; // Fallthrough
2306 case ISD::SETONE:
2307 // Expand this to (OLT | OGT).
2308 TmpOp0 = Op0;
2309 TmpOp1 = Op1;
2310 Opc = ISD::OR;
2311 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2312 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2313 break;
2314 case ISD::SETUO: Invert = true; // Fallthrough
2315 case ISD::SETO:
2316 // Expand this to (OLT | OGE).
2317 TmpOp0 = Op0;
2318 TmpOp1 = Op1;
2319 Opc = ISD::OR;
2320 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2321 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2322 break;
2323 }
2324 } else {
2325 // Integer comparisons.
2326 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002327 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002328 case ISD::SETNE: Invert = true;
2329 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2330 case ISD::SETLT: Swap = true;
2331 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2332 case ISD::SETLE: Swap = true;
2333 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2334 case ISD::SETULT: Swap = true;
2335 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2336 case ISD::SETULE: Swap = true;
2337 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2338 }
2339
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002340 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00002341 if (Opc == ARMISD::VCEQ) {
2342
2343 SDValue AndOp;
2344 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2345 AndOp = Op0;
2346 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2347 AndOp = Op1;
2348
2349 // Ignore bitconvert.
2350 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2351 AndOp = AndOp.getOperand(0);
2352
2353 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2354 Opc = ARMISD::VTST;
2355 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2356 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2357 Invert = !Invert;
2358 }
2359 }
2360 }
2361
2362 if (Swap)
2363 std::swap(Op0, Op1);
2364
2365 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
2366
2367 if (Invert)
2368 Result = DAG.getNOT(dl, Result, VT);
2369
2370 return Result;
2371}
2372
2373/// isVMOVSplat - Check if the specified splat value corresponds to an immediate
2374/// VMOV instruction, and if so, return the constant being splatted.
2375static SDValue isVMOVSplat(uint64_t SplatBits, uint64_t SplatUndef,
2376 unsigned SplatBitSize, SelectionDAG &DAG) {
2377 switch (SplatBitSize) {
2378 case 8:
2379 // Any 1-byte value is OK.
2380 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Owen Anderson825b72b2009-08-11 20:47:22 +00002381 return DAG.getTargetConstant(SplatBits, MVT::i8);
Bob Wilson5bafff32009-06-22 23:27:02 +00002382
2383 case 16:
2384 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
2385 if ((SplatBits & ~0xff) == 0 ||
2386 (SplatBits & ~0xff00) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002387 return DAG.getTargetConstant(SplatBits, MVT::i16);
Bob Wilson5bafff32009-06-22 23:27:02 +00002388 break;
2389
2390 case 32:
2391 // NEON's 32-bit VMOV supports splat values where:
2392 // * only one byte is nonzero, or
2393 // * the least significant byte is 0xff and the second byte is nonzero, or
2394 // * the least significant 2 bytes are 0xff and the third is nonzero.
2395 if ((SplatBits & ~0xff) == 0 ||
2396 (SplatBits & ~0xff00) == 0 ||
2397 (SplatBits & ~0xff0000) == 0 ||
2398 (SplatBits & ~0xff000000) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002399 return DAG.getTargetConstant(SplatBits, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002400
2401 if ((SplatBits & ~0xffff) == 0 &&
2402 ((SplatBits | SplatUndef) & 0xff) == 0xff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002403 return DAG.getTargetConstant(SplatBits | 0xff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002404
2405 if ((SplatBits & ~0xffffff) == 0 &&
2406 ((SplatBits | SplatUndef) & 0xffff) == 0xffff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002407 return DAG.getTargetConstant(SplatBits | 0xffff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002408
2409 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
2410 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
2411 // VMOV.I32. A (very) minor optimization would be to replicate the value
2412 // and fall through here to test for a valid 64-bit splat. But, then the
2413 // caller would also need to check and handle the change in size.
2414 break;
2415
2416 case 64: {
2417 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
2418 uint64_t BitMask = 0xff;
2419 uint64_t Val = 0;
2420 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
2421 if (((SplatBits | SplatUndef) & BitMask) == BitMask)
2422 Val |= BitMask;
2423 else if ((SplatBits & BitMask) != 0)
2424 return SDValue();
2425 BitMask <<= 8;
2426 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002427 return DAG.getTargetConstant(Val, MVT::i64);
Bob Wilson5bafff32009-06-22 23:27:02 +00002428 }
2429
2430 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002431 llvm_unreachable("unexpected size for isVMOVSplat");
Bob Wilson5bafff32009-06-22 23:27:02 +00002432 break;
2433 }
2434
2435 return SDValue();
2436}
2437
2438/// getVMOVImm - If this is a build_vector of constants which can be
2439/// formed by using a VMOV instruction of the specified element size,
2440/// return the constant being splatted. The ByteSize field indicates the
2441/// number of bytes of each element [1248].
2442SDValue ARM::getVMOVImm(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
2443 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
2444 APInt SplatBits, SplatUndef;
2445 unsigned SplatBitSize;
2446 bool HasAnyUndefs;
2447 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
2448 HasAnyUndefs, ByteSize * 8))
2449 return SDValue();
2450
2451 if (SplatBitSize > ByteSize * 8)
2452 return SDValue();
2453
2454 return isVMOVSplat(SplatBits.getZExtValue(), SplatUndef.getZExtValue(),
2455 SplatBitSize, DAG);
2456}
2457
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002458static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
2459 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002460 unsigned NumElts = VT.getVectorNumElements();
2461 ReverseVEXT = false;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002462 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002463
2464 // If this is a VEXT shuffle, the immediate value is the index of the first
2465 // element. The other shuffle indices must be the successive elements after
2466 // the first one.
2467 unsigned ExpectedElt = Imm;
2468 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002469 // Increment the expected index. If it wraps around, it may still be
2470 // a VEXT but the source vectors must be swapped.
2471 ExpectedElt += 1;
2472 if (ExpectedElt == NumElts * 2) {
2473 ExpectedElt = 0;
2474 ReverseVEXT = true;
2475 }
2476
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002477 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002478 return false;
2479 }
2480
2481 // Adjust the index value if the source operands will be swapped.
2482 if (ReverseVEXT)
2483 Imm -= NumElts;
2484
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002485 return true;
2486}
2487
Bob Wilson8bb9e482009-07-26 00:39:34 +00002488/// isVREVMask - Check if a vector shuffle corresponds to a VREV
2489/// instruction with the specified blocksize. (The order of the elements
2490/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002491static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
2492 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00002493 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
2494 "Only possible block sizes for VREV are: 16, 32, 64");
2495
Bob Wilson8bb9e482009-07-26 00:39:34 +00002496 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00002497 if (EltSz == 64)
2498 return false;
2499
2500 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002501 unsigned BlockElts = M[0] + 1;
Bob Wilson8bb9e482009-07-26 00:39:34 +00002502
2503 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
2504 return false;
2505
2506 for (unsigned i = 0; i < NumElts; ++i) {
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002507 if ((unsigned) M[i] !=
Bob Wilson8bb9e482009-07-26 00:39:34 +00002508 (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
2509 return false;
2510 }
2511
2512 return true;
2513}
2514
Bob Wilsonc692cb72009-08-21 20:54:19 +00002515static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
2516 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002517 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2518 if (EltSz == 64)
2519 return false;
2520
Bob Wilsonc692cb72009-08-21 20:54:19 +00002521 unsigned NumElts = VT.getVectorNumElements();
2522 WhichResult = (M[0] == 0 ? 0 : 1);
2523 for (unsigned i = 0; i < NumElts; i += 2) {
2524 if ((unsigned) M[i] != i + WhichResult ||
2525 (unsigned) M[i+1] != i + NumElts + WhichResult)
2526 return false;
2527 }
2528 return true;
2529}
2530
Bob Wilson324f4f12009-12-03 06:40:55 +00002531/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
2532/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2533/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
2534static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2535 unsigned &WhichResult) {
2536 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2537 if (EltSz == 64)
2538 return false;
2539
2540 unsigned NumElts = VT.getVectorNumElements();
2541 WhichResult = (M[0] == 0 ? 0 : 1);
2542 for (unsigned i = 0; i < NumElts; i += 2) {
2543 if ((unsigned) M[i] != i + WhichResult ||
2544 (unsigned) M[i+1] != i + WhichResult)
2545 return false;
2546 }
2547 return true;
2548}
2549
Bob Wilsonc692cb72009-08-21 20:54:19 +00002550static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
2551 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002552 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2553 if (EltSz == 64)
2554 return false;
2555
Bob Wilsonc692cb72009-08-21 20:54:19 +00002556 unsigned NumElts = VT.getVectorNumElements();
2557 WhichResult = (M[0] == 0 ? 0 : 1);
2558 for (unsigned i = 0; i != NumElts; ++i) {
2559 if ((unsigned) M[i] != 2 * i + WhichResult)
2560 return false;
2561 }
2562
2563 // VUZP.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
Bob Wilson324f4f12009-12-03 06:40:55 +00002570/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
2571/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2572/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
2573static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2574 unsigned &WhichResult) {
2575 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2576 if (EltSz == 64)
2577 return false;
2578
2579 unsigned Half = VT.getVectorNumElements() / 2;
2580 WhichResult = (M[0] == 0 ? 0 : 1);
2581 for (unsigned j = 0; j != 2; ++j) {
2582 unsigned Idx = WhichResult;
2583 for (unsigned i = 0; i != Half; ++i) {
2584 if ((unsigned) M[i + j * Half] != Idx)
2585 return false;
2586 Idx += 2;
2587 }
2588 }
2589
2590 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2591 if (VT.is64BitVector() && EltSz == 32)
2592 return false;
2593
2594 return true;
2595}
2596
Bob Wilsonc692cb72009-08-21 20:54:19 +00002597static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
2598 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002599 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2600 if (EltSz == 64)
2601 return false;
2602
Bob Wilsonc692cb72009-08-21 20:54:19 +00002603 unsigned NumElts = VT.getVectorNumElements();
2604 WhichResult = (M[0] == 0 ? 0 : 1);
2605 unsigned Idx = WhichResult * NumElts / 2;
2606 for (unsigned i = 0; i != NumElts; i += 2) {
2607 if ((unsigned) M[i] != Idx ||
2608 (unsigned) M[i+1] != Idx + NumElts)
2609 return false;
2610 Idx += 1;
2611 }
2612
2613 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00002614 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002615 return false;
2616
2617 return true;
2618}
2619
Bob Wilson324f4f12009-12-03 06:40:55 +00002620/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
2621/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2622/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
2623static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2624 unsigned &WhichResult) {
2625 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2626 if (EltSz == 64)
2627 return false;
2628
2629 unsigned NumElts = VT.getVectorNumElements();
2630 WhichResult = (M[0] == 0 ? 0 : 1);
2631 unsigned Idx = WhichResult * NumElts / 2;
2632 for (unsigned i = 0; i != NumElts; i += 2) {
2633 if ((unsigned) M[i] != Idx ||
2634 (unsigned) M[i+1] != Idx)
2635 return false;
2636 Idx += 1;
2637 }
2638
2639 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2640 if (VT.is64BitVector() && EltSz == 32)
2641 return false;
2642
2643 return true;
2644}
2645
2646
Owen Andersone50ed302009-08-10 22:56:29 +00002647static SDValue BuildSplat(SDValue Val, EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002648 // Canonicalize all-zeros and all-ones vectors.
Bob Wilsond06791f2009-08-13 01:57:47 +00002649 ConstantSDNode *ConstVal = cast<ConstantSDNode>(Val.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002650 if (ConstVal->isNullValue())
2651 return getZeroVector(VT, DAG, dl);
2652 if (ConstVal->isAllOnesValue())
2653 return getOnesVector(VT, DAG, dl);
2654
Owen Andersone50ed302009-08-10 22:56:29 +00002655 EVT CanonicalVT;
Bob Wilson5bafff32009-06-22 23:27:02 +00002656 if (VT.is64BitVector()) {
2657 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002658 case 8: CanonicalVT = MVT::v8i8; break;
2659 case 16: CanonicalVT = MVT::v4i16; break;
2660 case 32: CanonicalVT = MVT::v2i32; break;
2661 case 64: CanonicalVT = MVT::v1i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002662 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002663 }
2664 } else {
2665 assert(VT.is128BitVector() && "unknown splat vector size");
2666 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002667 case 8: CanonicalVT = MVT::v16i8; break;
2668 case 16: CanonicalVT = MVT::v8i16; break;
2669 case 32: CanonicalVT = MVT::v4i32; break;
2670 case 64: CanonicalVT = MVT::v2i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002671 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002672 }
2673 }
2674
2675 // Build a canonical splat for this value.
2676 SmallVector<SDValue, 8> Ops;
2677 Ops.assign(CanonicalVT.getVectorNumElements(), Val);
2678 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, &Ops[0],
2679 Ops.size());
2680 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Res);
2681}
2682
2683// If this is a case we can't handle, return null and let the default
2684// expansion code take care of it.
2685static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bob Wilsond06791f2009-08-13 01:57:47 +00002686 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002687 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002688 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002689
2690 APInt SplatBits, SplatUndef;
2691 unsigned SplatBitSize;
2692 bool HasAnyUndefs;
2693 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00002694 if (SplatBitSize <= 64) {
2695 SDValue Val = isVMOVSplat(SplatBits.getZExtValue(),
2696 SplatUndef.getZExtValue(), SplatBitSize, DAG);
2697 if (Val.getNode())
2698 return BuildSplat(Val, VT, DAG, dl);
2699 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00002700 }
2701
2702 // If there are only 2 elements in a 128-bit vector, insert them into an
2703 // undef vector. This handles the common case for 128-bit vector argument
2704 // passing, where the insertions should be translated to subreg accesses
2705 // with no real instructions.
2706 if (VT.is128BitVector() && Op.getNumOperands() == 2) {
2707 SDValue Val = DAG.getUNDEF(VT);
2708 SDValue Op0 = Op.getOperand(0);
2709 SDValue Op1 = Op.getOperand(1);
2710 if (Op0.getOpcode() != ISD::UNDEF)
2711 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op0,
2712 DAG.getIntPtrConstant(0));
2713 if (Op1.getOpcode() != ISD::UNDEF)
2714 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op1,
2715 DAG.getIntPtrConstant(1));
2716 return Val;
Bob Wilson5bafff32009-06-22 23:27:02 +00002717 }
2718
2719 return SDValue();
2720}
2721
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002722/// isShuffleMaskLegal - Targets can use this to indicate that they only
2723/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
2724/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
2725/// are assumed to be legal.
2726bool
2727ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
2728 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002729 if (VT.getVectorNumElements() == 4 &&
2730 (VT.is128BitVector() || VT.is64BitVector())) {
2731 unsigned PFIndexes[4];
2732 for (unsigned i = 0; i != 4; ++i) {
2733 if (M[i] < 0)
2734 PFIndexes[i] = 8;
2735 else
2736 PFIndexes[i] = M[i];
2737 }
2738
2739 // Compute the index in the perfect shuffle table.
2740 unsigned PFTableIndex =
2741 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2742 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2743 unsigned Cost = (PFEntry >> 30);
2744
2745 if (Cost <= 4)
2746 return true;
2747 }
2748
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002749 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00002750 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002751
2752 return (ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
2753 isVREVMask(M, VT, 64) ||
2754 isVREVMask(M, VT, 32) ||
2755 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00002756 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
2757 isVTRNMask(M, VT, WhichResult) ||
2758 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00002759 isVZIPMask(M, VT, WhichResult) ||
2760 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
2761 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
2762 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002763}
2764
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002765/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
2766/// the specified operations to build the shuffle.
2767static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
2768 SDValue RHS, SelectionDAG &DAG,
2769 DebugLoc dl) {
2770 unsigned OpNum = (PFEntry >> 26) & 0x0F;
2771 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
2772 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
2773
2774 enum {
2775 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
2776 OP_VREV,
2777 OP_VDUP0,
2778 OP_VDUP1,
2779 OP_VDUP2,
2780 OP_VDUP3,
2781 OP_VEXT1,
2782 OP_VEXT2,
2783 OP_VEXT3,
2784 OP_VUZPL, // VUZP, left result
2785 OP_VUZPR, // VUZP, right result
2786 OP_VZIPL, // VZIP, left result
2787 OP_VZIPR, // VZIP, right result
2788 OP_VTRNL, // VTRN, left result
2789 OP_VTRNR // VTRN, right result
2790 };
2791
2792 if (OpNum == OP_COPY) {
2793 if (LHSID == (1*9+2)*9+3) return LHS;
2794 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
2795 return RHS;
2796 }
2797
2798 SDValue OpLHS, OpRHS;
2799 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
2800 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
2801 EVT VT = OpLHS.getValueType();
2802
2803 switch (OpNum) {
2804 default: llvm_unreachable("Unknown shuffle opcode!");
2805 case OP_VREV:
2806 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
2807 case OP_VDUP0:
2808 case OP_VDUP1:
2809 case OP_VDUP2:
2810 case OP_VDUP3:
2811 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002812 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002813 case OP_VEXT1:
2814 case OP_VEXT2:
2815 case OP_VEXT3:
2816 return DAG.getNode(ARMISD::VEXT, dl, VT,
2817 OpLHS, OpRHS,
2818 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
2819 case OP_VUZPL:
2820 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002821 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002822 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
2823 case OP_VZIPL:
2824 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002825 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002826 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
2827 case OP_VTRNL:
2828 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002829 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2830 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002831 }
2832}
2833
Bob Wilson5bafff32009-06-22 23:27:02 +00002834static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002835 SDValue V1 = Op.getOperand(0);
2836 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00002837 DebugLoc dl = Op.getDebugLoc();
2838 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002839 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002840 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00002841
Bob Wilson28865062009-08-13 02:13:04 +00002842 // Convert shuffles that are directly supported on NEON to target-specific
2843 // DAG nodes, instead of keeping them as shuffles and matching them again
2844 // during code selection. This is more efficient and avoids the possibility
2845 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00002846 // FIXME: floating-point vectors should be canonicalized to integer vectors
2847 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002848 SVN->getMask(ShuffleMask);
2849
2850 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
Bob Wilson0ce37102009-08-14 05:08:32 +00002851 int Lane = SVN->getSplatIndex();
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00002852 // If this is undef splat, generate it via "just" vdup, if possible.
2853 if (Lane == -1) Lane = 0;
2854
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002855 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
2856 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00002857 }
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002858 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002859 DAG.getConstant(Lane, MVT::i32));
Bob Wilson0ce37102009-08-14 05:08:32 +00002860 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002861
2862 bool ReverseVEXT;
2863 unsigned Imm;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002864 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002865 if (ReverseVEXT)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002866 std::swap(V1, V2);
2867 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002868 DAG.getConstant(Imm, MVT::i32));
2869 }
2870
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002871 if (isVREVMask(ShuffleMask, VT, 64))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002872 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002873 if (isVREVMask(ShuffleMask, VT, 32))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002874 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002875 if (isVREVMask(ShuffleMask, VT, 16))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002876 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
2877
Bob Wilsonc692cb72009-08-21 20:54:19 +00002878 // Check for Neon shuffles that modify both input vectors in place.
2879 // If both results are used, i.e., if there are two shuffles with the same
2880 // source operands and with masks corresponding to both results of one of
2881 // these operations, DAG memoization will ensure that a single node is
2882 // used for both shuffles.
2883 unsigned WhichResult;
2884 if (isVTRNMask(ShuffleMask, VT, WhichResult))
2885 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2886 V1, V2).getValue(WhichResult);
2887 if (isVUZPMask(ShuffleMask, VT, WhichResult))
2888 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
2889 V1, V2).getValue(WhichResult);
2890 if (isVZIPMask(ShuffleMask, VT, WhichResult))
2891 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
2892 V1, V2).getValue(WhichResult);
2893
Bob Wilson324f4f12009-12-03 06:40:55 +00002894 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
2895 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2896 V1, V1).getValue(WhichResult);
2897 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
2898 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
2899 V1, V1).getValue(WhichResult);
2900 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
2901 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
2902 V1, V1).getValue(WhichResult);
2903
Bob Wilsonc692cb72009-08-21 20:54:19 +00002904 // If the shuffle is not directly supported and it has 4 elements, use
2905 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002906 if (VT.getVectorNumElements() == 4 &&
2907 (VT.is128BitVector() || VT.is64BitVector())) {
2908 unsigned PFIndexes[4];
2909 for (unsigned i = 0; i != 4; ++i) {
2910 if (ShuffleMask[i] < 0)
2911 PFIndexes[i] = 8;
2912 else
2913 PFIndexes[i] = ShuffleMask[i];
2914 }
2915
2916 // Compute the index in the perfect shuffle table.
2917 unsigned PFTableIndex =
2918 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2919
2920 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2921 unsigned Cost = (PFEntry >> 30);
2922
2923 if (Cost <= 4)
2924 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
2925 }
Bob Wilsond8e17572009-08-12 22:31:50 +00002926
Bob Wilson22cac0d2009-08-14 05:16:33 +00002927 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00002928}
2929
Bob Wilson5bafff32009-06-22 23:27:02 +00002930static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002931 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002932 DebugLoc dl = Op.getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00002933 SDValue Vec = Op.getOperand(0);
2934 SDValue Lane = Op.getOperand(1);
Bob Wilson934f98b2009-10-15 23:12:05 +00002935 assert(VT == MVT::i32 &&
2936 Vec.getValueType().getVectorElementType().getSizeInBits() < 32 &&
2937 "unexpected type for custom-lowering vector extract");
2938 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
Bob Wilson5bafff32009-06-22 23:27:02 +00002939}
2940
Bob Wilsona6d65862009-08-03 20:36:38 +00002941static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
2942 // The only time a CONCAT_VECTORS operation can have legal types is when
2943 // two 64-bit vectors are concatenated to a 128-bit vector.
2944 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
2945 "unexpected CONCAT_VECTORS");
2946 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00002947 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00002948 SDValue Op0 = Op.getOperand(0);
2949 SDValue Op1 = Op.getOperand(1);
2950 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00002951 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
2952 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00002953 DAG.getIntPtrConstant(0));
2954 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00002955 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
2956 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00002957 DAG.getIntPtrConstant(1));
2958 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00002959}
2960
Dan Gohman475871a2008-07-27 21:46:04 +00002961SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00002962 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002963 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00002964 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00002965 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002966 case ISD::GlobalAddress:
2967 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
2968 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002969 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00002970 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
2971 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002972 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Evan Cheng86198642009-08-07 00:34:42 +00002973 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002974 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
2975 case ISD::SINT_TO_FP:
2976 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
2977 case ISD::FP_TO_SINT:
2978 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
2979 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00002980 case ISD::RETURNADDR: break;
Jim Grosbach0e0da732009-05-12 23:59:14 +00002981 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002982 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002983 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00002984 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00002985 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00002986 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00002987 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00002988 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002989 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00002990 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00002991 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
2992 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2993 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00002994 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00002995 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002996 }
Dan Gohman475871a2008-07-27 21:46:04 +00002997 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00002998}
2999
Duncan Sands1607f052008-12-01 11:39:25 +00003000/// ReplaceNodeResults - Replace the results of node with an illegal result
3001/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00003002void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
3003 SmallVectorImpl<SDValue>&Results,
3004 SelectionDAG &DAG) {
Chris Lattner27a6c732007-11-24 07:07:01 +00003005 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00003006 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003007 llvm_unreachable("Don't know how to custom expand this!");
Duncan Sands1607f052008-12-01 11:39:25 +00003008 return;
3009 case ISD::BIT_CONVERT:
3010 Results.push_back(ExpandBIT_CONVERT(N, DAG));
3011 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00003012 case ISD::SRL:
Duncan Sands1607f052008-12-01 11:39:25 +00003013 case ISD::SRA: {
Bob Wilson5bafff32009-06-22 23:27:02 +00003014 SDValue Res = LowerShift(N, DAG, Subtarget);
Duncan Sands1607f052008-12-01 11:39:25 +00003015 if (Res.getNode())
3016 Results.push_back(Res);
3017 return;
3018 }
Chris Lattner27a6c732007-11-24 07:07:01 +00003019 }
3020}
Chris Lattner27a6c732007-11-24 07:07:01 +00003021
Evan Chenga8e29892007-01-19 07:51:42 +00003022//===----------------------------------------------------------------------===//
3023// ARM Scheduler Hooks
3024//===----------------------------------------------------------------------===//
3025
3026MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00003027ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +00003028 MachineBasicBlock *BB,
3029 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003030 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00003031 DebugLoc dl = MI->getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003032 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00003033 default:
3034 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng007ea272009-08-12 05:17:19 +00003035 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00003036 // To "insert" a SELECT_CC instruction, we actually have to insert the
3037 // diamond control-flow pattern. The incoming instruction knows the
3038 // destination vreg to set, the condition code register to branch on, the
3039 // true/false values to select between, and a branch opcode to use.
3040 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003041 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00003042 ++It;
3043
3044 // thisMBB:
3045 // ...
3046 // TrueVal = ...
3047 // cmpTY ccX, r1, r2
3048 // bCC copy1MBB
3049 // fallthrough --> copy0MBB
3050 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003051 MachineFunction *F = BB->getParent();
3052 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3053 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenb6728402009-02-13 02:25:56 +00003054 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00003055 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003056 F->insert(It, copy0MBB);
3057 F->insert(It, sinkMBB);
Evan Chenga8e29892007-01-19 07:51:42 +00003058 // Update machine-CFG edges by first adding all successors of the current
3059 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00003060 // Also inform sdisel of the edge changes.
3061 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
3062 E = BB->succ_end(); I != E; ++I) {
3063 EM->insert(std::make_pair(*I, sinkMBB));
3064 sinkMBB->addSuccessor(*I);
3065 }
Evan Chenga8e29892007-01-19 07:51:42 +00003066 // Next, remove all successors of the current block, and add the true
3067 // and fallthrough blocks as its successors.
Evan Chengce319102009-09-19 09:51:03 +00003068 while (!BB->succ_empty())
Evan Chenga8e29892007-01-19 07:51:42 +00003069 BB->removeSuccessor(BB->succ_begin());
3070 BB->addSuccessor(copy0MBB);
3071 BB->addSuccessor(sinkMBB);
3072
3073 // copy0MBB:
3074 // %FalseValue = ...
3075 // # fallthrough to sinkMBB
3076 BB = copy0MBB;
3077
3078 // Update machine-CFG edges
3079 BB->addSuccessor(sinkMBB);
3080
3081 // sinkMBB:
3082 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
3083 // ...
3084 BB = sinkMBB;
Dale Johannesenb6728402009-02-13 02:25:56 +00003085 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00003086 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
3087 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
3088
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003089 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00003090 return BB;
3091 }
Evan Cheng86198642009-08-07 00:34:42 +00003092
3093 case ARM::tANDsp:
3094 case ARM::tADDspr_:
3095 case ARM::tSUBspi_:
3096 case ARM::t2SUBrSPi_:
3097 case ARM::t2SUBrSPi12_:
3098 case ARM::t2SUBrSPs_: {
3099 MachineFunction *MF = BB->getParent();
3100 unsigned DstReg = MI->getOperand(0).getReg();
3101 unsigned SrcReg = MI->getOperand(1).getReg();
3102 bool DstIsDead = MI->getOperand(0).isDead();
3103 bool SrcIsKill = MI->getOperand(1).isKill();
3104
3105 if (SrcReg != ARM::SP) {
3106 // Copy the source to SP from virtual register.
3107 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(SrcReg);
3108 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3109 ? ARM::tMOVtgpr2gpr : ARM::tMOVgpr2gpr;
3110 BuildMI(BB, dl, TII->get(CopyOpc), ARM::SP)
3111 .addReg(SrcReg, getKillRegState(SrcIsKill));
3112 }
3113
3114 unsigned OpOpc = 0;
3115 bool NeedPred = false, NeedCC = false, NeedOp3 = false;
3116 switch (MI->getOpcode()) {
3117 default:
3118 llvm_unreachable("Unexpected pseudo instruction!");
3119 case ARM::tANDsp:
3120 OpOpc = ARM::tAND;
3121 NeedPred = true;
3122 break;
3123 case ARM::tADDspr_:
3124 OpOpc = ARM::tADDspr;
3125 break;
3126 case ARM::tSUBspi_:
3127 OpOpc = ARM::tSUBspi;
3128 break;
3129 case ARM::t2SUBrSPi_:
3130 OpOpc = ARM::t2SUBrSPi;
3131 NeedPred = true; NeedCC = true;
3132 break;
3133 case ARM::t2SUBrSPi12_:
3134 OpOpc = ARM::t2SUBrSPi12;
3135 NeedPred = true;
3136 break;
3137 case ARM::t2SUBrSPs_:
3138 OpOpc = ARM::t2SUBrSPs;
3139 NeedPred = true; NeedCC = true; NeedOp3 = true;
3140 break;
3141 }
3142 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(OpOpc), ARM::SP);
3143 if (OpOpc == ARM::tAND)
3144 AddDefaultT1CC(MIB);
3145 MIB.addReg(ARM::SP);
3146 MIB.addOperand(MI->getOperand(2));
3147 if (NeedOp3)
3148 MIB.addOperand(MI->getOperand(3));
3149 if (NeedPred)
3150 AddDefaultPred(MIB);
3151 if (NeedCC)
3152 AddDefaultCC(MIB);
3153
3154 // Copy the result from SP to virtual register.
3155 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(DstReg);
3156 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3157 ? ARM::tMOVgpr2tgpr : ARM::tMOVgpr2gpr;
3158 BuildMI(BB, dl, TII->get(CopyOpc))
3159 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
3160 .addReg(ARM::SP);
3161 MF->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
3162 return BB;
3163 }
Evan Chenga8e29892007-01-19 07:51:42 +00003164 }
3165}
3166
3167//===----------------------------------------------------------------------===//
3168// ARM Optimization Hooks
3169//===----------------------------------------------------------------------===//
3170
Chris Lattnerd1980a52009-03-12 06:52:53 +00003171static
3172SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
3173 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00003174 SelectionDAG &DAG = DCI.DAG;
3175 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003176 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00003177 unsigned Opc = N->getOpcode();
3178 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
3179 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
3180 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
3181 ISD::CondCode CC = ISD::SETCC_INVALID;
3182
3183 if (isSlctCC) {
3184 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
3185 } else {
3186 SDValue CCOp = Slct.getOperand(0);
3187 if (CCOp.getOpcode() == ISD::SETCC)
3188 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
3189 }
3190
3191 bool DoXform = false;
3192 bool InvCC = false;
3193 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
3194 "Bad input!");
3195
3196 if (LHS.getOpcode() == ISD::Constant &&
3197 cast<ConstantSDNode>(LHS)->isNullValue()) {
3198 DoXform = true;
3199 } else if (CC != ISD::SETCC_INVALID &&
3200 RHS.getOpcode() == ISD::Constant &&
3201 cast<ConstantSDNode>(RHS)->isNullValue()) {
3202 std::swap(LHS, RHS);
3203 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003204 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00003205 Op0.getOperand(0).getValueType();
3206 bool isInt = OpVT.isInteger();
3207 CC = ISD::getSetCCInverse(CC, isInt);
3208
3209 if (!TLI.isCondCodeLegal(CC, OpVT))
3210 return SDValue(); // Inverse operator isn't legal.
3211
3212 DoXform = true;
3213 InvCC = true;
3214 }
3215
3216 if (DoXform) {
3217 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
3218 if (isSlctCC)
3219 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
3220 Slct.getOperand(0), Slct.getOperand(1), CC);
3221 SDValue CCOp = Slct.getOperand(0);
3222 if (InvCC)
3223 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
3224 CCOp.getOperand(0), CCOp.getOperand(1), CC);
3225 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
3226 CCOp, OtherOp, Result);
3227 }
3228 return SDValue();
3229}
3230
3231/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
3232static SDValue PerformADDCombine(SDNode *N,
3233 TargetLowering::DAGCombinerInfo &DCI) {
3234 // added by evan in r37685 with no testcase.
3235 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003236
Chris Lattnerd1980a52009-03-12 06:52:53 +00003237 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
3238 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
3239 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
3240 if (Result.getNode()) return Result;
3241 }
3242 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3243 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3244 if (Result.getNode()) return Result;
3245 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003246
Chris Lattnerd1980a52009-03-12 06:52:53 +00003247 return SDValue();
3248}
3249
3250/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
3251static SDValue PerformSUBCombine(SDNode *N,
3252 TargetLowering::DAGCombinerInfo &DCI) {
3253 // added by evan in r37685 with no testcase.
3254 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003255
Chris Lattnerd1980a52009-03-12 06:52:53 +00003256 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
3257 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3258 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3259 if (Result.getNode()) return Result;
3260 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003261
Chris Lattnerd1980a52009-03-12 06:52:53 +00003262 return SDValue();
3263}
3264
Jim Grosbache5165492009-11-09 00:11:35 +00003265/// PerformVMOVRRDCombine - Target-specific dag combine xforms for ARMISD::VMOVRRD.
3266static SDValue PerformVMOVRRDCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003267 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003268 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00003269 SDValue InDouble = N->getOperand(0);
Jim Grosbache5165492009-11-09 00:11:35 +00003270 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003271 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003272 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003273}
3274
Bob Wilson5bafff32009-06-22 23:27:02 +00003275/// getVShiftImm - Check if this is a valid build_vector for the immediate
3276/// operand of a vector shift operation, where all the elements of the
3277/// build_vector must have the same constant integer value.
3278static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
3279 // Ignore bit_converts.
3280 while (Op.getOpcode() == ISD::BIT_CONVERT)
3281 Op = Op.getOperand(0);
3282 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
3283 APInt SplatBits, SplatUndef;
3284 unsigned SplatBitSize;
3285 bool HasAnyUndefs;
3286 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
3287 HasAnyUndefs, ElementBits) ||
3288 SplatBitSize > ElementBits)
3289 return false;
3290 Cnt = SplatBits.getSExtValue();
3291 return true;
3292}
3293
3294/// isVShiftLImm - Check if this is a valid build_vector for the immediate
3295/// operand of a vector shift left operation. That value must be in the range:
3296/// 0 <= Value < ElementBits for a left shift; or
3297/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003298static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003299 assert(VT.isVector() && "vector shift count is not a vector type");
3300 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3301 if (! getVShiftImm(Op, ElementBits, Cnt))
3302 return false;
3303 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
3304}
3305
3306/// isVShiftRImm - Check if this is a valid build_vector for the immediate
3307/// operand of a vector shift right operation. For a shift opcode, the value
3308/// is positive, but for an intrinsic the value count must be negative. The
3309/// absolute value must be in the range:
3310/// 1 <= |Value| <= ElementBits for a right shift; or
3311/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003312static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00003313 int64_t &Cnt) {
3314 assert(VT.isVector() && "vector shift count is not a vector type");
3315 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3316 if (! getVShiftImm(Op, ElementBits, Cnt))
3317 return false;
3318 if (isIntrinsic)
3319 Cnt = -Cnt;
3320 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
3321}
3322
3323/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
3324static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
3325 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
3326 switch (IntNo) {
3327 default:
3328 // Don't do anything for most intrinsics.
3329 break;
3330
3331 // Vector shifts: check for immediate versions and lower them.
3332 // Note: This is done during DAG combining instead of DAG legalizing because
3333 // the build_vectors for 64-bit vector element shift counts are generally
3334 // not legal, and it is hard to see their values after they get legalized to
3335 // loads from a constant pool.
3336 case Intrinsic::arm_neon_vshifts:
3337 case Intrinsic::arm_neon_vshiftu:
3338 case Intrinsic::arm_neon_vshiftls:
3339 case Intrinsic::arm_neon_vshiftlu:
3340 case Intrinsic::arm_neon_vshiftn:
3341 case Intrinsic::arm_neon_vrshifts:
3342 case Intrinsic::arm_neon_vrshiftu:
3343 case Intrinsic::arm_neon_vrshiftn:
3344 case Intrinsic::arm_neon_vqshifts:
3345 case Intrinsic::arm_neon_vqshiftu:
3346 case Intrinsic::arm_neon_vqshiftsu:
3347 case Intrinsic::arm_neon_vqshiftns:
3348 case Intrinsic::arm_neon_vqshiftnu:
3349 case Intrinsic::arm_neon_vqshiftnsu:
3350 case Intrinsic::arm_neon_vqrshiftns:
3351 case Intrinsic::arm_neon_vqrshiftnu:
3352 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00003353 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003354 int64_t Cnt;
3355 unsigned VShiftOpc = 0;
3356
3357 switch (IntNo) {
3358 case Intrinsic::arm_neon_vshifts:
3359 case Intrinsic::arm_neon_vshiftu:
3360 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
3361 VShiftOpc = ARMISD::VSHL;
3362 break;
3363 }
3364 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
3365 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
3366 ARMISD::VSHRs : ARMISD::VSHRu);
3367 break;
3368 }
3369 return SDValue();
3370
3371 case Intrinsic::arm_neon_vshiftls:
3372 case Intrinsic::arm_neon_vshiftlu:
3373 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
3374 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003375 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003376
3377 case Intrinsic::arm_neon_vrshifts:
3378 case Intrinsic::arm_neon_vrshiftu:
3379 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
3380 break;
3381 return SDValue();
3382
3383 case Intrinsic::arm_neon_vqshifts:
3384 case Intrinsic::arm_neon_vqshiftu:
3385 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3386 break;
3387 return SDValue();
3388
3389 case Intrinsic::arm_neon_vqshiftsu:
3390 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3391 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003392 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003393
3394 case Intrinsic::arm_neon_vshiftn:
3395 case Intrinsic::arm_neon_vrshiftn:
3396 case Intrinsic::arm_neon_vqshiftns:
3397 case Intrinsic::arm_neon_vqshiftnu:
3398 case Intrinsic::arm_neon_vqshiftnsu:
3399 case Intrinsic::arm_neon_vqrshiftns:
3400 case Intrinsic::arm_neon_vqrshiftnu:
3401 case Intrinsic::arm_neon_vqrshiftnsu:
3402 // Narrowing shifts require an immediate right shift.
3403 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
3404 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003405 llvm_unreachable("invalid shift count for narrowing vector shift intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003406
3407 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003408 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003409 }
3410
3411 switch (IntNo) {
3412 case Intrinsic::arm_neon_vshifts:
3413 case Intrinsic::arm_neon_vshiftu:
3414 // Opcode already set above.
3415 break;
3416 case Intrinsic::arm_neon_vshiftls:
3417 case Intrinsic::arm_neon_vshiftlu:
3418 if (Cnt == VT.getVectorElementType().getSizeInBits())
3419 VShiftOpc = ARMISD::VSHLLi;
3420 else
3421 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
3422 ARMISD::VSHLLs : ARMISD::VSHLLu);
3423 break;
3424 case Intrinsic::arm_neon_vshiftn:
3425 VShiftOpc = ARMISD::VSHRN; break;
3426 case Intrinsic::arm_neon_vrshifts:
3427 VShiftOpc = ARMISD::VRSHRs; break;
3428 case Intrinsic::arm_neon_vrshiftu:
3429 VShiftOpc = ARMISD::VRSHRu; break;
3430 case Intrinsic::arm_neon_vrshiftn:
3431 VShiftOpc = ARMISD::VRSHRN; break;
3432 case Intrinsic::arm_neon_vqshifts:
3433 VShiftOpc = ARMISD::VQSHLs; break;
3434 case Intrinsic::arm_neon_vqshiftu:
3435 VShiftOpc = ARMISD::VQSHLu; break;
3436 case Intrinsic::arm_neon_vqshiftsu:
3437 VShiftOpc = ARMISD::VQSHLsu; break;
3438 case Intrinsic::arm_neon_vqshiftns:
3439 VShiftOpc = ARMISD::VQSHRNs; break;
3440 case Intrinsic::arm_neon_vqshiftnu:
3441 VShiftOpc = ARMISD::VQSHRNu; break;
3442 case Intrinsic::arm_neon_vqshiftnsu:
3443 VShiftOpc = ARMISD::VQSHRNsu; break;
3444 case Intrinsic::arm_neon_vqrshiftns:
3445 VShiftOpc = ARMISD::VQRSHRNs; break;
3446 case Intrinsic::arm_neon_vqrshiftnu:
3447 VShiftOpc = ARMISD::VQRSHRNu; break;
3448 case Intrinsic::arm_neon_vqrshiftnsu:
3449 VShiftOpc = ARMISD::VQRSHRNsu; break;
3450 }
3451
3452 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003453 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003454 }
3455
3456 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00003457 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003458 int64_t Cnt;
3459 unsigned VShiftOpc = 0;
3460
3461 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
3462 VShiftOpc = ARMISD::VSLI;
3463 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
3464 VShiftOpc = ARMISD::VSRI;
3465 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00003466 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003467 }
3468
3469 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
3470 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00003471 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003472 }
3473
3474 case Intrinsic::arm_neon_vqrshifts:
3475 case Intrinsic::arm_neon_vqrshiftu:
3476 // No immediate versions of these to check for.
3477 break;
3478 }
3479
3480 return SDValue();
3481}
3482
3483/// PerformShiftCombine - Checks for immediate versions of vector shifts and
3484/// lowers them. As with the vector shift intrinsics, this is done during DAG
3485/// combining instead of DAG legalizing because the build_vectors for 64-bit
3486/// vector element shift counts are generally not legal, and it is hard to see
3487/// their values after they get legalized to loads from a constant pool.
3488static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
3489 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003490 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003491
3492 // Nothing to be done for scalar shifts.
3493 if (! VT.isVector())
3494 return SDValue();
3495
3496 assert(ST->hasNEON() && "unexpected vector shift");
3497 int64_t Cnt;
3498
3499 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003500 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003501
3502 case ISD::SHL:
3503 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
3504 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003505 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003506 break;
3507
3508 case ISD::SRA:
3509 case ISD::SRL:
3510 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
3511 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
3512 ARMISD::VSHRs : ARMISD::VSHRu);
3513 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003514 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003515 }
3516 }
3517 return SDValue();
3518}
3519
3520/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
3521/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
3522static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
3523 const ARMSubtarget *ST) {
3524 SDValue N0 = N->getOperand(0);
3525
3526 // Check for sign- and zero-extensions of vector extract operations of 8-
3527 // and 16-bit vector elements. NEON supports these directly. They are
3528 // handled during DAG combining because type legalization will promote them
3529 // to 32-bit types and it is messy to recognize the operations after that.
3530 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
3531 SDValue Vec = N0.getOperand(0);
3532 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00003533 EVT VT = N->getValueType(0);
3534 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003535 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3536
Owen Anderson825b72b2009-08-11 20:47:22 +00003537 if (VT == MVT::i32 &&
3538 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00003539 TLI.isTypeLegal(Vec.getValueType())) {
3540
3541 unsigned Opc = 0;
3542 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003543 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003544 case ISD::SIGN_EXTEND:
3545 Opc = ARMISD::VGETLANEs;
3546 break;
3547 case ISD::ZERO_EXTEND:
3548 case ISD::ANY_EXTEND:
3549 Opc = ARMISD::VGETLANEu;
3550 break;
3551 }
3552 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
3553 }
3554 }
3555
3556 return SDValue();
3557}
3558
Dan Gohman475871a2008-07-27 21:46:04 +00003559SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003560 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003561 switch (N->getOpcode()) {
3562 default: break;
Chris Lattnerd1980a52009-03-12 06:52:53 +00003563 case ISD::ADD: return PerformADDCombine(N, DCI);
3564 case ISD::SUB: return PerformSUBCombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00003565 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson5bafff32009-06-22 23:27:02 +00003566 case ISD::INTRINSIC_WO_CHAIN:
3567 return PerformIntrinsicCombine(N, DCI.DAG);
3568 case ISD::SHL:
3569 case ISD::SRA:
3570 case ISD::SRL:
3571 return PerformShiftCombine(N, DCI.DAG, Subtarget);
3572 case ISD::SIGN_EXTEND:
3573 case ISD::ZERO_EXTEND:
3574 case ISD::ANY_EXTEND:
3575 return PerformExtendCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003576 }
Dan Gohman475871a2008-07-27 21:46:04 +00003577 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003578}
3579
Bill Wendlingaf566342009-08-15 21:21:19 +00003580bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
3581 if (!Subtarget->hasV6Ops())
3582 // Pre-v6 does not support unaligned mem access.
3583 return false;
3584 else if (!Subtarget->hasV6Ops()) {
3585 // v6 may or may not support unaligned mem access.
3586 if (!Subtarget->isTargetDarwin())
3587 return false;
3588 }
3589
3590 switch (VT.getSimpleVT().SimpleTy) {
3591 default:
3592 return false;
3593 case MVT::i8:
3594 case MVT::i16:
3595 case MVT::i32:
3596 return true;
3597 // FIXME: VLD1 etc with standard alignment is legal.
3598 }
3599}
3600
Evan Chenge6c835f2009-08-14 20:09:37 +00003601static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
3602 if (V < 0)
3603 return false;
3604
3605 unsigned Scale = 1;
3606 switch (VT.getSimpleVT().SimpleTy) {
3607 default: return false;
3608 case MVT::i1:
3609 case MVT::i8:
3610 // Scale == 1;
3611 break;
3612 case MVT::i16:
3613 // Scale == 2;
3614 Scale = 2;
3615 break;
3616 case MVT::i32:
3617 // Scale == 4;
3618 Scale = 4;
3619 break;
3620 }
3621
3622 if ((V & (Scale - 1)) != 0)
3623 return false;
3624 V /= Scale;
3625 return V == (V & ((1LL << 5) - 1));
3626}
3627
3628static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
3629 const ARMSubtarget *Subtarget) {
3630 bool isNeg = false;
3631 if (V < 0) {
3632 isNeg = true;
3633 V = - V;
3634 }
3635
3636 switch (VT.getSimpleVT().SimpleTy) {
3637 default: return false;
3638 case MVT::i1:
3639 case MVT::i8:
3640 case MVT::i16:
3641 case MVT::i32:
3642 // + imm12 or - imm8
3643 if (isNeg)
3644 return V == (V & ((1LL << 8) - 1));
3645 return V == (V & ((1LL << 12) - 1));
3646 case MVT::f32:
3647 case MVT::f64:
3648 // Same as ARM mode. FIXME: NEON?
3649 if (!Subtarget->hasVFP2())
3650 return false;
3651 if ((V & 3) != 0)
3652 return false;
3653 V >>= 2;
3654 return V == (V & ((1LL << 8) - 1));
3655 }
3656}
3657
Evan Chengb01fad62007-03-12 23:30:29 +00003658/// isLegalAddressImmediate - Return true if the integer value can be used
3659/// as the offset of the target addressing mode for load / store of the
3660/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00003661static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00003662 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00003663 if (V == 0)
3664 return true;
3665
Evan Cheng65011532009-03-09 19:15:00 +00003666 if (!VT.isSimple())
3667 return false;
3668
Evan Chenge6c835f2009-08-14 20:09:37 +00003669 if (Subtarget->isThumb1Only())
3670 return isLegalT1AddressImmediate(V, VT);
3671 else if (Subtarget->isThumb2())
3672 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00003673
Evan Chenge6c835f2009-08-14 20:09:37 +00003674 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00003675 if (V < 0)
3676 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00003677 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00003678 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00003679 case MVT::i1:
3680 case MVT::i8:
3681 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00003682 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003683 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003684 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00003685 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003686 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003687 case MVT::f32:
3688 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00003689 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00003690 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00003691 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00003692 return false;
3693 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003694 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00003695 }
Evan Chenga8e29892007-01-19 07:51:42 +00003696}
3697
Evan Chenge6c835f2009-08-14 20:09:37 +00003698bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
3699 EVT VT) const {
3700 int Scale = AM.Scale;
3701 if (Scale < 0)
3702 return false;
3703
3704 switch (VT.getSimpleVT().SimpleTy) {
3705 default: return false;
3706 case MVT::i1:
3707 case MVT::i8:
3708 case MVT::i16:
3709 case MVT::i32:
3710 if (Scale == 1)
3711 return true;
3712 // r + r << imm
3713 Scale = Scale & ~1;
3714 return Scale == 2 || Scale == 4 || Scale == 8;
3715 case MVT::i64:
3716 // r + r
3717 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
3718 return true;
3719 return false;
3720 case MVT::isVoid:
3721 // Note, we allow "void" uses (basically, uses that aren't loads or
3722 // stores), because arm allows folding a scale into many arithmetic
3723 // operations. This should be made more precise and revisited later.
3724
3725 // Allow r << imm, but the imm has to be a multiple of two.
3726 if (Scale & 1) return false;
3727 return isPowerOf2_32(Scale);
3728 }
3729}
3730
Chris Lattner37caf8c2007-04-09 23:33:39 +00003731/// isLegalAddressingMode - Return true if the addressing mode represented
3732/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003733bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00003734 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00003735 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00003736 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00003737 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003738
Chris Lattner37caf8c2007-04-09 23:33:39 +00003739 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003740 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003741 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003742
Chris Lattner37caf8c2007-04-09 23:33:39 +00003743 switch (AM.Scale) {
3744 case 0: // no scale reg, must be "r+i" or "r", or "i".
3745 break;
3746 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00003747 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00003748 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00003749 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00003750 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00003751 // ARM doesn't support any R+R*scale+imm addr modes.
3752 if (AM.BaseOffs)
3753 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003754
Bob Wilson2c7dab12009-04-08 17:55:28 +00003755 if (!VT.isSimple())
3756 return false;
3757
Evan Chenge6c835f2009-08-14 20:09:37 +00003758 if (Subtarget->isThumb2())
3759 return isLegalT2ScaledAddressingMode(AM, VT);
3760
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003761 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00003762 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00003763 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00003764 case MVT::i1:
3765 case MVT::i8:
3766 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003767 if (Scale < 0) Scale = -Scale;
3768 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003769 return true;
3770 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00003771 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003772 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00003773 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00003774 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003775 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003776 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00003777 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003778
Owen Anderson825b72b2009-08-11 20:47:22 +00003779 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00003780 // Note, we allow "void" uses (basically, uses that aren't loads or
3781 // stores), because arm allows folding a scale into many arithmetic
3782 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003783
Chris Lattner37caf8c2007-04-09 23:33:39 +00003784 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00003785 if (Scale & 1) return false;
3786 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00003787 }
3788 break;
Evan Chengb01fad62007-03-12 23:30:29 +00003789 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00003790 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00003791}
3792
Evan Cheng77e47512009-11-11 19:05:52 +00003793/// isLegalICmpImmediate - Return true if the specified immediate is legal
3794/// icmp immediate, that is the target has icmp instructions which can compare
3795/// a register against the immediate without having to materialize the
3796/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00003797bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00003798 if (!Subtarget->isThumb())
3799 return ARM_AM::getSOImmVal(Imm) != -1;
3800 if (Subtarget->isThumb2())
3801 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00003802 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00003803}
3804
Owen Andersone50ed302009-08-10 22:56:29 +00003805static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00003806 bool isSEXTLoad, SDValue &Base,
3807 SDValue &Offset, bool &isInc,
3808 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00003809 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
3810 return false;
3811
Owen Anderson825b72b2009-08-11 20:47:22 +00003812 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00003813 // AddressingMode 3
3814 Base = Ptr->getOperand(0);
3815 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003816 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003817 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003818 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00003819 isInc = false;
3820 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3821 return true;
3822 }
3823 }
3824 isInc = (Ptr->getOpcode() == ISD::ADD);
3825 Offset = Ptr->getOperand(1);
3826 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00003827 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00003828 // AddressingMode 2
3829 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003830 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003831 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003832 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00003833 isInc = false;
3834 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3835 Base = Ptr->getOperand(0);
3836 return true;
3837 }
3838 }
3839
3840 if (Ptr->getOpcode() == ISD::ADD) {
3841 isInc = true;
3842 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
3843 if (ShOpcVal != ARM_AM::no_shift) {
3844 Base = Ptr->getOperand(1);
3845 Offset = Ptr->getOperand(0);
3846 } else {
3847 Base = Ptr->getOperand(0);
3848 Offset = Ptr->getOperand(1);
3849 }
3850 return true;
3851 }
3852
3853 isInc = (Ptr->getOpcode() == ISD::ADD);
3854 Base = Ptr->getOperand(0);
3855 Offset = Ptr->getOperand(1);
3856 return true;
3857 }
3858
Jim Grosbache5165492009-11-09 00:11:35 +00003859 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00003860 return false;
3861}
3862
Owen Andersone50ed302009-08-10 22:56:29 +00003863static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00003864 bool isSEXTLoad, SDValue &Base,
3865 SDValue &Offset, bool &isInc,
3866 SelectionDAG &DAG) {
3867 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
3868 return false;
3869
3870 Base = Ptr->getOperand(0);
3871 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
3872 int RHSC = (int)RHS->getZExtValue();
3873 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
3874 assert(Ptr->getOpcode() == ISD::ADD);
3875 isInc = false;
3876 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3877 return true;
3878 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
3879 isInc = Ptr->getOpcode() == ISD::ADD;
3880 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
3881 return true;
3882 }
3883 }
3884
3885 return false;
3886}
3887
Evan Chenga8e29892007-01-19 07:51:42 +00003888/// getPreIndexedAddressParts - returns true by value, base pointer and
3889/// offset pointer and addressing mode by reference if the node's address
3890/// can be legally represented as pre-indexed load / store address.
3891bool
Dan Gohman475871a2008-07-27 21:46:04 +00003892ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
3893 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003894 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00003895 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003896 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00003897 return false;
3898
Owen Andersone50ed302009-08-10 22:56:29 +00003899 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00003900 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00003901 bool isSEXTLoad = false;
3902 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
3903 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003904 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003905 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
3906 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
3907 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003908 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003909 } else
3910 return false;
3911
3912 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00003913 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00003914 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00003915 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
3916 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00003917 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00003918 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00003919 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00003920 if (!isLegal)
3921 return false;
3922
3923 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
3924 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00003925}
3926
3927/// getPostIndexedAddressParts - returns true by value, base pointer and
3928/// offset pointer and addressing mode by reference if this node can be
3929/// combined with a load / store to form a post-indexed load / store.
3930bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00003931 SDValue &Base,
3932 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003933 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00003934 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003935 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00003936 return false;
3937
Owen Andersone50ed302009-08-10 22:56:29 +00003938 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00003939 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00003940 bool isSEXTLoad = false;
3941 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003942 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003943 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
3944 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003945 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003946 } else
3947 return false;
3948
3949 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00003950 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00003951 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00003952 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003953 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00003954 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00003955 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
3956 isInc, DAG);
3957 if (!isLegal)
3958 return false;
3959
3960 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
3961 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00003962}
3963
Dan Gohman475871a2008-07-27 21:46:04 +00003964void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00003965 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003966 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003967 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00003968 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00003969 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003970 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00003971 switch (Op.getOpcode()) {
3972 default: break;
3973 case ARMISD::CMOV: {
3974 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00003975 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00003976 if (KnownZero == 0 && KnownOne == 0) return;
3977
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003978 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00003979 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
3980 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00003981 KnownZero &= KnownZeroRHS;
3982 KnownOne &= KnownOneRHS;
3983 return;
3984 }
3985 }
3986}
3987
3988//===----------------------------------------------------------------------===//
3989// ARM Inline Assembly Support
3990//===----------------------------------------------------------------------===//
3991
3992/// getConstraintType - Given a constraint letter, return the type of
3993/// constraint it is for this target.
3994ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00003995ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
3996 if (Constraint.size() == 1) {
3997 switch (Constraint[0]) {
3998 default: break;
3999 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004000 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00004001 }
Evan Chenga8e29892007-01-19 07:51:42 +00004002 }
Chris Lattner4234f572007-03-25 02:14:49 +00004003 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00004004}
4005
Bob Wilson2dc4f542009-03-20 22:42:55 +00004006std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00004007ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00004008 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004009 if (Constraint.size() == 1) {
4010 // GCC RS6000 Constraint Letters
4011 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004012 case 'l':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004013 if (Subtarget->isThumb1Only())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00004014 return std::make_pair(0U, ARM::tGPRRegisterClass);
4015 else
4016 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004017 case 'r':
4018 return std::make_pair(0U, ARM::GPRRegisterClass);
4019 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00004020 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004021 return std::make_pair(0U, ARM::SPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00004022 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004023 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00004024 if (VT.getSizeInBits() == 128)
4025 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004026 break;
Evan Chenga8e29892007-01-19 07:51:42 +00004027 }
4028 }
4029 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
4030}
4031
4032std::vector<unsigned> ARMTargetLowering::
4033getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00004034 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004035 if (Constraint.size() != 1)
4036 return std::vector<unsigned>();
4037
4038 switch (Constraint[0]) { // GCC ARM Constraint Letters
4039 default: break;
4040 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00004041 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
4042 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
4043 0);
Evan Chenga8e29892007-01-19 07:51:42 +00004044 case 'r':
4045 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
4046 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
4047 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
4048 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004049 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00004050 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004051 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
4052 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
4053 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
4054 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
4055 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
4056 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
4057 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
4058 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00004059 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004060 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
4061 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
4062 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
4063 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00004064 if (VT.getSizeInBits() == 128)
4065 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
4066 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004067 break;
Evan Chenga8e29892007-01-19 07:51:42 +00004068 }
4069
4070 return std::vector<unsigned>();
4071}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004072
4073/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
4074/// vector. If it is invalid, don't add anything to Ops.
4075void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
4076 char Constraint,
4077 bool hasMemory,
4078 std::vector<SDValue>&Ops,
4079 SelectionDAG &DAG) const {
4080 SDValue Result(0, 0);
4081
4082 switch (Constraint) {
4083 default: break;
4084 case 'I': case 'J': case 'K': case 'L':
4085 case 'M': case 'N': case 'O':
4086 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
4087 if (!C)
4088 return;
4089
4090 int64_t CVal64 = C->getSExtValue();
4091 int CVal = (int) CVal64;
4092 // None of these constraints allow values larger than 32 bits. Check
4093 // that the value fits in an int.
4094 if (CVal != CVal64)
4095 return;
4096
4097 switch (Constraint) {
4098 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004099 if (Subtarget->isThumb1Only()) {
4100 // This must be a constant between 0 and 255, for ADD
4101 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004102 if (CVal >= 0 && CVal <= 255)
4103 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004104 } else if (Subtarget->isThumb2()) {
4105 // A constant that can be used as an immediate value in a
4106 // data-processing instruction.
4107 if (ARM_AM::getT2SOImmVal(CVal) != -1)
4108 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004109 } else {
4110 // A constant that can be used as an immediate value in a
4111 // data-processing instruction.
4112 if (ARM_AM::getSOImmVal(CVal) != -1)
4113 break;
4114 }
4115 return;
4116
4117 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004118 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004119 // This must be a constant between -255 and -1, for negated ADD
4120 // immediates. This can be used in GCC with an "n" modifier that
4121 // prints the negated value, for use with SUB instructions. It is
4122 // not useful otherwise but is implemented for compatibility.
4123 if (CVal >= -255 && CVal <= -1)
4124 break;
4125 } else {
4126 // This must be a constant between -4095 and 4095. It is not clear
4127 // what this constraint is intended for. Implemented for
4128 // compatibility with GCC.
4129 if (CVal >= -4095 && CVal <= 4095)
4130 break;
4131 }
4132 return;
4133
4134 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004135 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004136 // A 32-bit value where only one byte has a nonzero value. Exclude
4137 // zero to match GCC. This constraint is used by GCC internally for
4138 // constants that can be loaded with a move/shift combination.
4139 // It is not useful otherwise but is implemented for compatibility.
4140 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
4141 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004142 } else if (Subtarget->isThumb2()) {
4143 // A constant whose bitwise inverse can be used as an immediate
4144 // value in a data-processing instruction. This can be used in GCC
4145 // with a "B" modifier that prints the inverted value, for use with
4146 // BIC and MVN instructions. It is not useful otherwise but is
4147 // implemented for compatibility.
4148 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
4149 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004150 } else {
4151 // A constant whose bitwise inverse can be used as an immediate
4152 // value in a data-processing instruction. This can be used in GCC
4153 // with a "B" modifier that prints the inverted value, for use with
4154 // BIC and MVN instructions. It is not useful otherwise but is
4155 // implemented for compatibility.
4156 if (ARM_AM::getSOImmVal(~CVal) != -1)
4157 break;
4158 }
4159 return;
4160
4161 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004162 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004163 // This must be a constant between -7 and 7,
4164 // for 3-operand ADD/SUB immediate instructions.
4165 if (CVal >= -7 && CVal < 7)
4166 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004167 } else if (Subtarget->isThumb2()) {
4168 // A constant whose negation can be used as an immediate value in a
4169 // data-processing instruction. This can be used in GCC with an "n"
4170 // modifier that prints the negated value, for use with SUB
4171 // instructions. It is not useful otherwise but is implemented for
4172 // compatibility.
4173 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
4174 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004175 } else {
4176 // A constant whose negation can be used as an immediate value in a
4177 // data-processing instruction. This can be used in GCC with an "n"
4178 // modifier that prints the negated value, for use with SUB
4179 // instructions. It is not useful otherwise but is implemented for
4180 // compatibility.
4181 if (ARM_AM::getSOImmVal(-CVal) != -1)
4182 break;
4183 }
4184 return;
4185
4186 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004187 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004188 // This must be a multiple of 4 between 0 and 1020, for
4189 // ADD sp + immediate.
4190 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
4191 break;
4192 } else {
4193 // A power of two or a constant between 0 and 32. This is used in
4194 // GCC for the shift amount on shifted register operands, but it is
4195 // useful in general for any shift amounts.
4196 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
4197 break;
4198 }
4199 return;
4200
4201 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004202 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004203 // This must be a constant between 0 and 31, for shift amounts.
4204 if (CVal >= 0 && CVal <= 31)
4205 break;
4206 }
4207 return;
4208
4209 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004210 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004211 // This must be a multiple of 4 between -508 and 508, for
4212 // ADD/SUB sp = sp + immediate.
4213 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
4214 break;
4215 }
4216 return;
4217 }
4218 Result = DAG.getTargetConstant(CVal, Op.getValueType());
4219 break;
4220 }
4221
4222 if (Result.getNode()) {
4223 Ops.push_back(Result);
4224 return;
4225 }
4226 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
4227 Ops, DAG);
4228}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00004229
4230bool
4231ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
4232 // The ARM target isn't yet aware of offsets.
4233 return false;
4234}
Evan Cheng39382422009-10-28 01:44:26 +00004235
4236int ARM::getVFPf32Imm(const APFloat &FPImm) {
4237 APInt Imm = FPImm.bitcastToAPInt();
4238 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
4239 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
4240 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
4241
4242 // We can handle 4 bits of mantissa.
4243 // mantissa = (16+UInt(e:f:g:h))/16.
4244 if (Mantissa & 0x7ffff)
4245 return -1;
4246 Mantissa >>= 19;
4247 if ((Mantissa & 0xf) != Mantissa)
4248 return -1;
4249
4250 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4251 if (Exp < -3 || Exp > 4)
4252 return -1;
4253 Exp = ((Exp+3) & 0x7) ^ 4;
4254
4255 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4256}
4257
4258int ARM::getVFPf64Imm(const APFloat &FPImm) {
4259 APInt Imm = FPImm.bitcastToAPInt();
4260 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
4261 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
4262 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
4263
4264 // We can handle 4 bits of mantissa.
4265 // mantissa = (16+UInt(e:f:g:h))/16.
4266 if (Mantissa & 0xffffffffffffLL)
4267 return -1;
4268 Mantissa >>= 48;
4269 if ((Mantissa & 0xf) != Mantissa)
4270 return -1;
4271
4272 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4273 if (Exp < -3 || Exp > 4)
4274 return -1;
4275 Exp = ((Exp+3) & 0x7) ^ 4;
4276
4277 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4278}
4279
4280/// isFPImmLegal - Returns true if the target can instruction select the
4281/// specified FP immediate natively. If false, the legalizer will
4282/// materialize the FP immediate as a load from a constant pool.
4283bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
4284 if (!Subtarget->hasVFP3())
4285 return false;
4286 if (VT == MVT::f32)
4287 return ARM::getVFPf32Imm(Imm) != -1;
4288 if (VT == MVT::f64)
4289 return ARM::getVFPf64Imm(Imm) != -1;
4290 return false;
4291}