blob: cfe840442dfaecf03a9d91753fd0cb1188c62a22 [file] [log] [blame]
Evan Chenga8e29892007-01-19 07:51:42 +00001//===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chenga8e29892007-01-19 07:51:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that ARM uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "ARM.h"
16#include "ARMAddressingModes.h"
17#include "ARMConstantPoolValue.h"
18#include "ARMISelLowering.h"
19#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +000020#include "ARMPerfectShuffle.h"
Evan Chenga8e29892007-01-19 07:51:42 +000021#include "ARMRegisterInfo.h"
22#include "ARMSubtarget.h"
23#include "ARMTargetMachine.h"
Chris Lattner80ec2792009-08-02 00:34:36 +000024#include "ARMTargetObjectFile.h"
Evan Chenga8e29892007-01-19 07:51:42 +000025#include "llvm/CallingConv.h"
26#include "llvm/Constants.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000027#include "llvm/Function.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000028#include "llvm/GlobalValue.h"
Evan Cheng27707472007-03-16 08:43:56 +000029#include "llvm/Instruction.h"
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +000030#include "llvm/Intrinsics.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000031#include "llvm/Type.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000032#include "llvm/CodeGen/CallingConvLower.h"
Evan Chenga8e29892007-01-19 07:51:42 +000033#include "llvm/CodeGen/MachineBasicBlock.h"
34#include "llvm/CodeGen/MachineFrameInfo.h"
35#include "llvm/CodeGen/MachineFunction.h"
36#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000038#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chenga8e29892007-01-19 07:51:42 +000039#include "llvm/CodeGen/SelectionDAG.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000040#include "llvm/Target/TargetOptions.h"
Evan Chenga8e29892007-01-19 07:51:42 +000041#include "llvm/ADT/VectorExtras.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000042#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000043#include "llvm/Support/MathExtras.h"
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000044#include <sstream>
Evan Chenga8e29892007-01-19 07:51:42 +000045using namespace llvm;
46
Owen Andersone50ed302009-08-10 22:56:29 +000047static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000048 CCValAssign::LocInfo &LocInfo,
49 ISD::ArgFlagsTy &ArgFlags,
50 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000051static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000052 CCValAssign::LocInfo &LocInfo,
53 ISD::ArgFlagsTy &ArgFlags,
54 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000055static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000056 CCValAssign::LocInfo &LocInfo,
57 ISD::ArgFlagsTy &ArgFlags,
58 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000059static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000060 CCValAssign::LocInfo &LocInfo,
61 ISD::ArgFlagsTy &ArgFlags,
62 CCState &State);
63
Owen Andersone50ed302009-08-10 22:56:29 +000064void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
65 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000066 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000067 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000068 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
69 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000070
Owen Anderson70671842009-08-10 20:18:46 +000071 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +000072 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +000073 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000074 }
75
Owen Andersone50ed302009-08-10 22:56:29 +000076 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +000077 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Owen Anderson70671842009-08-10 20:18:46 +000078 setOperationAction(ISD::VSETCC, VT.getSimpleVT(), Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +000079 if (ElemTy == MVT::i8 || ElemTy == MVT::i16)
Owen Anderson70671842009-08-10 20:18:46 +000080 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson0696fdf2009-09-16 20:20:44 +000081 if (ElemTy != MVT::i32) {
82 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
83 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
84 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
85 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
86 }
Owen Anderson70671842009-08-10 20:18:46 +000087 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
88 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Owen Anderson70671842009-08-10 20:18:46 +000089 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Custom);
Anton Korobeynikov8e6c2b92009-08-21 12:40:35 +000090 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +000091 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +000092 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
93 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
94 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +000095 }
96
97 // Promote all bit-wise operations.
98 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +000099 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000100 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
101 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000102 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000103 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000104 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000105 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000106 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000107 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000108 }
Bob Wilson16330762009-09-16 00:17:28 +0000109
110 // Neon does not support vector divide/remainder operations.
111 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
112 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
113 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
114 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
115 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
116 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000117}
118
Owen Andersone50ed302009-08-10 22:56:29 +0000119void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000120 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000121 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000122}
123
Owen Andersone50ed302009-08-10 22:56:29 +0000124void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000125 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000126 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000127}
128
Chris Lattnerf0144122009-07-28 03:13:23 +0000129static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
130 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000131 return new TargetLoweringObjectFileMachO();
Chris Lattner80ec2792009-08-02 00:34:36 +0000132 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000133}
134
Evan Chenga8e29892007-01-19 07:51:42 +0000135ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000136 : TargetLowering(TM, createTLOF(TM)), ARMPCLabelIndex(0) {
Evan Chenga8e29892007-01-19 07:51:42 +0000137 Subtarget = &TM.getSubtarget<ARMSubtarget>();
138
Evan Chengb1df8f22007-04-27 08:15:43 +0000139 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000140 // Uses VFP for Thumb libfuncs if available.
141 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
142 // Single-precision floating-point arithmetic.
143 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
144 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
145 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
146 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000147
Evan Chengb1df8f22007-04-27 08:15:43 +0000148 // Double-precision floating-point arithmetic.
149 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
150 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
151 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
152 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000153
Evan Chengb1df8f22007-04-27 08:15:43 +0000154 // Single-precision comparisons.
155 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
156 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
157 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
158 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
159 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
160 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
161 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
162 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000163
Evan Chengb1df8f22007-04-27 08:15:43 +0000164 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
165 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
166 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
167 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
168 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
169 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
170 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
171 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000172
Evan Chengb1df8f22007-04-27 08:15:43 +0000173 // Double-precision comparisons.
174 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
175 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
176 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
177 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
178 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
179 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
180 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
181 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000182
Evan Chengb1df8f22007-04-27 08:15:43 +0000183 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
184 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
185 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
186 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
187 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
188 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
189 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
190 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000191
Evan Chengb1df8f22007-04-27 08:15:43 +0000192 // Floating-point to integer conversions.
193 // i64 conversions are done via library routines even when generating VFP
194 // instructions, so use the same ones.
195 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
196 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
197 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
198 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000199
Evan Chengb1df8f22007-04-27 08:15:43 +0000200 // Conversions between floating types.
201 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
202 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
203
204 // Integer to floating-point conversions.
205 // i64 conversions are done via library routines even when generating VFP
206 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000207 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
208 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000209 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
210 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
211 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
212 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
213 }
Evan Chenga8e29892007-01-19 07:51:42 +0000214 }
215
Bob Wilson2f954612009-05-22 17:38:41 +0000216 // These libcalls are not available in 32-bit.
217 setLibcallName(RTLIB::SHL_I128, 0);
218 setLibcallName(RTLIB::SRL_I128, 0);
219 setLibcallName(RTLIB::SRA_I128, 0);
220
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000221 // Libcalls should use the AAPCS base standard ABI, even if hard float
222 // is in effect, as per the ARM RTABI specification, section 4.1.2.
223 if (Subtarget->isAAPCS_ABI()) {
224 for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i) {
225 setLibcallCallingConv(static_cast<RTLIB::Libcall>(i),
226 CallingConv::ARM_AAPCS);
227 }
228 }
229
David Goodwinf1daf7d2009-07-08 23:10:31 +0000230 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000231 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000232 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000233 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000234 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000235 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
236 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000237
Owen Anderson825b72b2009-08-11 20:47:22 +0000238 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000239 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000240
241 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000242 addDRTypeForNEON(MVT::v2f32);
243 addDRTypeForNEON(MVT::v8i8);
244 addDRTypeForNEON(MVT::v4i16);
245 addDRTypeForNEON(MVT::v2i32);
246 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000247
Owen Anderson825b72b2009-08-11 20:47:22 +0000248 addQRTypeForNEON(MVT::v4f32);
249 addQRTypeForNEON(MVT::v2f64);
250 addQRTypeForNEON(MVT::v16i8);
251 addQRTypeForNEON(MVT::v8i16);
252 addQRTypeForNEON(MVT::v4i32);
253 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000254
Bob Wilson74dc72e2009-09-15 23:55:57 +0000255 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
256 // neither Neon nor VFP support any arithmetic operations on it.
257 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
258 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
259 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
260 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
261 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
262 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
263 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
264 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
265 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
266 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
267 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
268 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
269 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
270 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
271 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
272 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
273 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
274 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
275 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
276 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
277 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
278 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
279 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
280 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
281
Bob Wilson642b3292009-09-16 00:32:15 +0000282 // Neon does not support some operations on v1i64 and v2i64 types.
283 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
284 setOperationAction(ISD::MUL, MVT::v2i64, Expand);
285 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
286 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
287
Bob Wilson5bafff32009-06-22 23:27:02 +0000288 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
289 setTargetDAGCombine(ISD::SHL);
290 setTargetDAGCombine(ISD::SRL);
291 setTargetDAGCombine(ISD::SRA);
292 setTargetDAGCombine(ISD::SIGN_EXTEND);
293 setTargetDAGCombine(ISD::ZERO_EXTEND);
294 setTargetDAGCombine(ISD::ANY_EXTEND);
295 }
296
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000297 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000298
299 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000300 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000301
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000302 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000303 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000304
Evan Chenga8e29892007-01-19 07:51:42 +0000305 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000306 if (!Subtarget->isThumb1Only()) {
307 for (unsigned im = (unsigned)ISD::PRE_INC;
308 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000309 setIndexedLoadAction(im, MVT::i1, Legal);
310 setIndexedLoadAction(im, MVT::i8, Legal);
311 setIndexedLoadAction(im, MVT::i16, Legal);
312 setIndexedLoadAction(im, MVT::i32, Legal);
313 setIndexedStoreAction(im, MVT::i1, Legal);
314 setIndexedStoreAction(im, MVT::i8, Legal);
315 setIndexedStoreAction(im, MVT::i16, Legal);
316 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000317 }
Evan Chenga8e29892007-01-19 07:51:42 +0000318 }
319
320 // i64 operation support.
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000321 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000322 setOperationAction(ISD::MUL, MVT::i64, Expand);
323 setOperationAction(ISD::MULHU, MVT::i32, Expand);
324 setOperationAction(ISD::MULHS, MVT::i32, Expand);
325 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
326 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000327 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000328 setOperationAction(ISD::MUL, MVT::i64, Expand);
329 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb6207242009-08-01 00:16:10 +0000330 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000331 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000332 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000333 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
334 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
335 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
336 setOperationAction(ISD::SRL, MVT::i64, Custom);
337 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000338
339 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000340 setOperationAction(ISD::ROTL, MVT::i32, Expand);
341 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
342 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000343 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000344 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000345
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000346 // Only ARMv6 has BSWAP.
347 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000348 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000349
Evan Chenga8e29892007-01-19 07:51:42 +0000350 // These are expanded into libcalls.
Owen Anderson825b72b2009-08-11 20:47:22 +0000351 setOperationAction(ISD::SDIV, MVT::i32, Expand);
352 setOperationAction(ISD::UDIV, MVT::i32, Expand);
353 setOperationAction(ISD::SREM, MVT::i32, Expand);
354 setOperationAction(ISD::UREM, MVT::i32, Expand);
355 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
356 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000357
Evan Chenga8e29892007-01-19 07:51:42 +0000358 // Support label based line numbers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000359 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
360 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000361
Owen Anderson825b72b2009-08-11 20:47:22 +0000362 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
363 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
364 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
365 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000366 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000367
Evan Chenga8e29892007-01-19 07:51:42 +0000368 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000369 setOperationAction(ISD::VASTART, MVT::Other, Custom);
370 setOperationAction(ISD::VAARG, MVT::Other, Expand);
371 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
372 setOperationAction(ISD::VAEND, MVT::Other, Expand);
373 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
374 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000375 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
376 // FIXME: Shouldn't need this, since no register is used, but the legalizer
377 // doesn't yet know how to not do that for SjLj.
378 setExceptionSelectorRegister(ARM::R0);
Evan Cheng86198642009-08-07 00:34:42 +0000379 if (Subtarget->isThumb())
Owen Anderson825b72b2009-08-11 20:47:22 +0000380 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Evan Cheng86198642009-08-07 00:34:42 +0000381 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000382 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
383 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000384
Evan Chengd27c9fc2009-07-03 01:43:10 +0000385 if (!Subtarget->hasV6Ops() && !Subtarget->isThumb2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000386 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
387 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000388 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000389 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000390
David Goodwinf1daf7d2009-07-08 23:10:31 +0000391 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only())
Evan Chengc7c77292008-11-04 19:57:48 +0000392 // Turn f64->i64 into FMRRD, i64 -> f64 to FMDRR iff target supports vfp2.
Owen Anderson825b72b2009-08-11 20:47:22 +0000393 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000394
395 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000396 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000397
Owen Anderson825b72b2009-08-11 20:47:22 +0000398 setOperationAction(ISD::SETCC, MVT::i32, Expand);
399 setOperationAction(ISD::SETCC, MVT::f32, Expand);
400 setOperationAction(ISD::SETCC, MVT::f64, Expand);
401 setOperationAction(ISD::SELECT, MVT::i32, Expand);
402 setOperationAction(ISD::SELECT, MVT::f32, Expand);
403 setOperationAction(ISD::SELECT, MVT::f64, Expand);
404 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
405 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
406 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000407
Owen Anderson825b72b2009-08-11 20:47:22 +0000408 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
409 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
410 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
411 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
412 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000413
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000414 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000415 setOperationAction(ISD::FSIN, MVT::f64, Expand);
416 setOperationAction(ISD::FSIN, MVT::f32, Expand);
417 setOperationAction(ISD::FCOS, MVT::f32, Expand);
418 setOperationAction(ISD::FCOS, MVT::f64, Expand);
419 setOperationAction(ISD::FREM, MVT::f64, Expand);
420 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000421 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000422 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
423 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000424 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000425 setOperationAction(ISD::FPOW, MVT::f64, Expand);
426 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000427
Evan Chenga8e29892007-01-19 07:51:42 +0000428 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
David Goodwinf1daf7d2009-07-08 23:10:31 +0000429 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000430 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
431 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
432 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
433 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000434 }
Evan Chenga8e29892007-01-19 07:51:42 +0000435
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000436 // We have target-specific dag combine patterns for the following nodes:
437 // ARMISD::FMRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000438 setTargetDAGCombine(ISD::ADD);
439 setTargetDAGCombine(ISD::SUB);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000440
Evan Chenga8e29892007-01-19 07:51:42 +0000441 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Chenga8e29892007-01-19 07:51:42 +0000442 setSchedulingPreference(SchedulingForRegPressure);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000443
Evan Chengbc9b7542009-08-15 07:59:10 +0000444 // FIXME: If-converter should use instruction latency to determine
445 // profitability rather than relying on fixed limits.
446 if (Subtarget->getCPUString() == "generic") {
447 // Generic (and overly aggressive) if-conversion limits.
448 setIfCvtBlockSizeLimit(10);
449 setIfCvtDupBlockSizeLimit(2);
450 } else if (Subtarget->hasV6Ops()) {
451 setIfCvtBlockSizeLimit(2);
452 setIfCvtDupBlockSizeLimit(1);
453 } else {
454 setIfCvtBlockSizeLimit(3);
455 setIfCvtDupBlockSizeLimit(2);
Evan Cheng8557c2b2009-06-19 01:51:50 +0000456 }
457
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000458 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Bob Wilsone6abdff2009-05-18 20:55:32 +0000459 // Do not enable CodePlacementOpt for now: it currently runs after the
460 // ARMConstantIslandPass and messes up branch relaxation and placement
461 // of constant islands.
462 // benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000463}
464
Evan Chenga8e29892007-01-19 07:51:42 +0000465const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
466 switch (Opcode) {
467 default: return 0;
468 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000469 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
470 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000471 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000472 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
473 case ARMISD::tCALL: return "ARMISD::tCALL";
474 case ARMISD::BRCOND: return "ARMISD::BRCOND";
475 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000476 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000477 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
478 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
479 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000480 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000481 case ARMISD::CMPFP: return "ARMISD::CMPFP";
482 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
483 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
484 case ARMISD::CMOV: return "ARMISD::CMOV";
485 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000486
Evan Chenga8e29892007-01-19 07:51:42 +0000487 case ARMISD::FTOSI: return "ARMISD::FTOSI";
488 case ARMISD::FTOUI: return "ARMISD::FTOUI";
489 case ARMISD::SITOF: return "ARMISD::SITOF";
490 case ARMISD::UITOF: return "ARMISD::UITOF";
Evan Chenga8e29892007-01-19 07:51:42 +0000491
492 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
493 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
494 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000495
Evan Chenga8e29892007-01-19 07:51:42 +0000496 case ARMISD::FMRRD: return "ARMISD::FMRRD";
497 case ARMISD::FMDRR: return "ARMISD::FMDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000498
Evan Chengc5942082009-10-28 06:55:03 +0000499 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
500 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
501
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000502 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000503
Evan Cheng86198642009-08-07 00:34:42 +0000504 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
505
Bob Wilson5bafff32009-06-22 23:27:02 +0000506 case ARMISD::VCEQ: return "ARMISD::VCEQ";
507 case ARMISD::VCGE: return "ARMISD::VCGE";
508 case ARMISD::VCGEU: return "ARMISD::VCGEU";
509 case ARMISD::VCGT: return "ARMISD::VCGT";
510 case ARMISD::VCGTU: return "ARMISD::VCGTU";
511 case ARMISD::VTST: return "ARMISD::VTST";
512
513 case ARMISD::VSHL: return "ARMISD::VSHL";
514 case ARMISD::VSHRs: return "ARMISD::VSHRs";
515 case ARMISD::VSHRu: return "ARMISD::VSHRu";
516 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
517 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
518 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
519 case ARMISD::VSHRN: return "ARMISD::VSHRN";
520 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
521 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
522 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
523 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
524 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
525 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
526 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
527 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
528 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
529 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
530 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
531 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
532 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
533 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000534 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000535 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000536 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000537 case ARMISD::VREV64: return "ARMISD::VREV64";
538 case ARMISD::VREV32: return "ARMISD::VREV32";
539 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000540 case ARMISD::VZIP: return "ARMISD::VZIP";
541 case ARMISD::VUZP: return "ARMISD::VUZP";
542 case ARMISD::VTRN: return "ARMISD::VTRN";
Evan Chenga8e29892007-01-19 07:51:42 +0000543 }
544}
545
Bill Wendlingb4202b82009-07-01 18:50:55 +0000546/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000547unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Evan Cheng048e36f2009-10-02 06:57:25 +0000548 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 0 : 1;
Bill Wendling20c568f2009-06-30 22:38:32 +0000549}
550
Evan Chenga8e29892007-01-19 07:51:42 +0000551//===----------------------------------------------------------------------===//
552// Lowering Code
553//===----------------------------------------------------------------------===//
554
Evan Chenga8e29892007-01-19 07:51:42 +0000555/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
556static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
557 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000558 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000559 case ISD::SETNE: return ARMCC::NE;
560 case ISD::SETEQ: return ARMCC::EQ;
561 case ISD::SETGT: return ARMCC::GT;
562 case ISD::SETGE: return ARMCC::GE;
563 case ISD::SETLT: return ARMCC::LT;
564 case ISD::SETLE: return ARMCC::LE;
565 case ISD::SETUGT: return ARMCC::HI;
566 case ISD::SETUGE: return ARMCC::HS;
567 case ISD::SETULT: return ARMCC::LO;
568 case ISD::SETULE: return ARMCC::LS;
569 }
570}
571
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000572/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
573static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000574 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000575 CondCode2 = ARMCC::AL;
576 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000577 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000578 case ISD::SETEQ:
579 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
580 case ISD::SETGT:
581 case ISD::SETOGT: CondCode = ARMCC::GT; break;
582 case ISD::SETGE:
583 case ISD::SETOGE: CondCode = ARMCC::GE; break;
584 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000585 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000586 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
587 case ISD::SETO: CondCode = ARMCC::VC; break;
588 case ISD::SETUO: CondCode = ARMCC::VS; break;
589 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
590 case ISD::SETUGT: CondCode = ARMCC::HI; break;
591 case ISD::SETUGE: CondCode = ARMCC::PL; break;
592 case ISD::SETLT:
593 case ISD::SETULT: CondCode = ARMCC::LT; break;
594 case ISD::SETLE:
595 case ISD::SETULE: CondCode = ARMCC::LE; break;
596 case ISD::SETNE:
597 case ISD::SETUNE: CondCode = ARMCC::NE; break;
598 }
Evan Chenga8e29892007-01-19 07:51:42 +0000599}
600
Bob Wilson1f595bb2009-04-17 19:07:39 +0000601//===----------------------------------------------------------------------===//
602// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +0000603//===----------------------------------------------------------------------===//
604
605#include "ARMGenCallingConv.inc"
606
607// APCS f64 is in register pairs, possibly split to stack
Owen Andersone50ed302009-08-10 22:56:29 +0000608static bool f64AssignAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000609 CCValAssign::LocInfo &LocInfo,
610 CCState &State, bool CanFail) {
611 static const unsigned RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
612
613 // Try to get the first register.
614 if (unsigned Reg = State.AllocateReg(RegList, 4))
615 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
616 else {
617 // For the 2nd half of a v2f64, do not fail.
618 if (CanFail)
619 return false;
620
621 // Put the whole thing on the stack.
622 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
623 State.AllocateStack(8, 4),
624 LocVT, LocInfo));
625 return true;
626 }
627
628 // Try to get the second register.
629 if (unsigned Reg = State.AllocateReg(RegList, 4))
630 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
631 else
632 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
633 State.AllocateStack(4, 4),
634 LocVT, LocInfo));
635 return true;
636}
637
Owen Andersone50ed302009-08-10 22:56:29 +0000638static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000639 CCValAssign::LocInfo &LocInfo,
640 ISD::ArgFlagsTy &ArgFlags,
641 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000642 if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
643 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000644 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000645 !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
646 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000647 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000648}
649
650// AAPCS f64 is in aligned register pairs
Owen Andersone50ed302009-08-10 22:56:29 +0000651static bool f64AssignAAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000652 CCValAssign::LocInfo &LocInfo,
653 CCState &State, bool CanFail) {
654 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
655 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
656
657 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
658 if (Reg == 0) {
659 // For the 2nd half of a v2f64, do not just fail.
660 if (CanFail)
661 return false;
662
663 // Put the whole thing on the stack.
664 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
665 State.AllocateStack(8, 8),
666 LocVT, LocInfo));
667 return true;
668 }
669
670 unsigned i;
671 for (i = 0; i < 2; ++i)
672 if (HiRegList[i] == Reg)
673 break;
674
675 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
676 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
677 LocVT, LocInfo));
678 return true;
679}
680
Owen Andersone50ed302009-08-10 22:56:29 +0000681static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000682 CCValAssign::LocInfo &LocInfo,
683 ISD::ArgFlagsTy &ArgFlags,
684 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000685 if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
686 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000687 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000688 !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
689 return false;
690 return true; // we handled it
691}
692
Owen Andersone50ed302009-08-10 22:56:29 +0000693static bool f64RetAssign(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000694 CCValAssign::LocInfo &LocInfo, CCState &State) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000695 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
696 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
697
Bob Wilsone65586b2009-04-17 20:40:45 +0000698 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
699 if (Reg == 0)
700 return false; // we didn't handle it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000701
Bob Wilsone65586b2009-04-17 20:40:45 +0000702 unsigned i;
703 for (i = 0; i < 2; ++i)
704 if (HiRegList[i] == Reg)
705 break;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000706
Bob Wilson5bafff32009-06-22 23:27:02 +0000707 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bob Wilsone65586b2009-04-17 20:40:45 +0000708 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000709 LocVT, LocInfo));
710 return true;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000711}
712
Owen Andersone50ed302009-08-10 22:56:29 +0000713static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000714 CCValAssign::LocInfo &LocInfo,
715 ISD::ArgFlagsTy &ArgFlags,
716 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000717 if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
718 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000719 if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
Bob Wilson5bafff32009-06-22 23:27:02 +0000720 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000721 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000722}
723
Owen Andersone50ed302009-08-10 22:56:29 +0000724static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000725 CCValAssign::LocInfo &LocInfo,
726 ISD::ArgFlagsTy &ArgFlags,
727 CCState &State) {
728 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
729 State);
730}
731
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000732/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
733/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000734CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000735 bool Return,
736 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000737 switch (CC) {
738 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000739 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000740 case CallingConv::C:
741 case CallingConv::Fast:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000742 // Use target triple & subtarget features to do actual dispatch.
743 if (Subtarget->isAAPCS_ABI()) {
744 if (Subtarget->hasVFP2() &&
745 FloatABIType == FloatABI::Hard && !isVarArg)
746 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
747 else
748 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
749 } else
750 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000751 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000752 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000753 case CallingConv::ARM_AAPCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000754 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000755 case CallingConv::ARM_APCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000756 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000757 }
758}
759
Dan Gohman98ca4f22009-08-05 01:29:28 +0000760/// LowerCallResult - Lower the result values of a call into the
761/// appropriate copies out of appropriate physical registers.
762SDValue
763ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000764 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000765 const SmallVectorImpl<ISD::InputArg> &Ins,
766 DebugLoc dl, SelectionDAG &DAG,
767 SmallVectorImpl<SDValue> &InVals) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000768
Bob Wilson1f595bb2009-04-17 19:07:39 +0000769 // Assign locations to each value returned by this call.
770 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000771 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000772 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000773 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000774 CCAssignFnForNode(CallConv, /* Return*/ true,
775 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000776
777 // Copy all of the result registers out of their specified physreg.
778 for (unsigned i = 0; i != RVLocs.size(); ++i) {
779 CCValAssign VA = RVLocs[i];
780
Bob Wilson80915242009-04-25 00:33:20 +0000781 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000782 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000783 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000784 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000785 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000786 Chain = Lo.getValue(1);
787 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000788 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000789 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000790 InFlag);
791 Chain = Hi.getValue(1);
792 InFlag = Hi.getValue(2);
Owen Anderson825b72b2009-08-11 20:47:22 +0000793 Val = DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +0000794
Owen Anderson825b72b2009-08-11 20:47:22 +0000795 if (VA.getLocVT() == MVT::v2f64) {
796 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
797 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
798 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000799
800 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000801 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000802 Chain = Lo.getValue(1);
803 InFlag = Lo.getValue(2);
804 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000805 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000806 Chain = Hi.getValue(1);
807 InFlag = Hi.getValue(2);
Owen Anderson825b72b2009-08-11 20:47:22 +0000808 Val = DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
809 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
810 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000811 }
Bob Wilson1f595bb2009-04-17 19:07:39 +0000812 } else {
Bob Wilson80915242009-04-25 00:33:20 +0000813 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
814 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000815 Chain = Val.getValue(1);
816 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000817 }
Bob Wilson80915242009-04-25 00:33:20 +0000818
819 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000820 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +0000821 case CCValAssign::Full: break;
822 case CCValAssign::BCvt:
823 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
824 break;
825 }
826
Dan Gohman98ca4f22009-08-05 01:29:28 +0000827 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000828 }
829
Dan Gohman98ca4f22009-08-05 01:29:28 +0000830 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000831}
832
833/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
834/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +0000835/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +0000836/// a byval function parameter.
837/// Sometimes what we are copying is the end of a larger object, the part that
838/// does not fit in registers.
839static SDValue
840CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
841 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
842 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000843 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000844 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
845 /*AlwaysInline=*/false, NULL, 0, NULL, 0);
846}
847
Bob Wilsondee46d72009-04-17 20:35:10 +0000848/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000849SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +0000850ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
851 SDValue StackPtr, SDValue Arg,
852 DebugLoc dl, SelectionDAG &DAG,
853 const CCValAssign &VA,
854 ISD::ArgFlagsTy Flags) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000855 unsigned LocMemOffset = VA.getLocMemOffset();
856 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
857 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
858 if (Flags.isByVal()) {
859 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
860 }
861 return DAG.getStore(Chain, dl, Arg, PtrOff,
862 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chenga8e29892007-01-19 07:51:42 +0000863}
864
Dan Gohman98ca4f22009-08-05 01:29:28 +0000865void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +0000866 SDValue Chain, SDValue &Arg,
867 RegsToPassVector &RegsToPass,
868 CCValAssign &VA, CCValAssign &NextVA,
869 SDValue &StackPtr,
870 SmallVector<SDValue, 8> &MemOpChains,
871 ISD::ArgFlagsTy Flags) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000872
873 SDValue fmrrd = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +0000874 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +0000875 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
876
877 if (NextVA.isRegLoc())
878 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
879 else {
880 assert(NextVA.isMemLoc());
881 if (StackPtr.getNode() == 0)
882 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
883
Dan Gohman98ca4f22009-08-05 01:29:28 +0000884 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
885 dl, DAG, NextVA,
886 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000887 }
888}
889
Dan Gohman98ca4f22009-08-05 01:29:28 +0000890/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +0000891/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
892/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000893SDValue
894ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000895 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000896 bool isTailCall,
897 const SmallVectorImpl<ISD::OutputArg> &Outs,
898 const SmallVectorImpl<ISD::InputArg> &Ins,
899 DebugLoc dl, SelectionDAG &DAG,
900 SmallVectorImpl<SDValue> &InVals) {
Evan Chenga8e29892007-01-19 07:51:42 +0000901
Bob Wilson1f595bb2009-04-17 19:07:39 +0000902 // Analyze operands of the call, assigning locations to each operand.
903 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000904 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
905 *DAG.getContext());
906 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000907 CCAssignFnForNode(CallConv, /* Return*/ false,
908 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +0000909
Bob Wilson1f595bb2009-04-17 19:07:39 +0000910 // Get a count of how many bytes are to be pushed on the stack.
911 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +0000912
913 // Adjust the stack pointer for the new arguments...
914 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattnere563bbc2008-10-11 22:08:30 +0000915 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +0000916
Owen Anderson825b72b2009-08-11 20:47:22 +0000917 SDValue StackPtr = DAG.getRegister(ARM::SP, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000918
Bob Wilson5bafff32009-06-22 23:27:02 +0000919 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000920 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +0000921
Bob Wilson1f595bb2009-04-17 19:07:39 +0000922 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +0000923 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000924 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
925 i != e;
926 ++i, ++realArgIdx) {
927 CCValAssign &VA = ArgLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +0000928 SDValue Arg = Outs[realArgIdx].Val;
929 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +0000930
Bob Wilson1f595bb2009-04-17 19:07:39 +0000931 // Promote the value if needed.
932 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000933 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +0000934 case CCValAssign::Full: break;
935 case CCValAssign::SExt:
936 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
937 break;
938 case CCValAssign::ZExt:
939 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
940 break;
941 case CCValAssign::AExt:
942 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
943 break;
944 case CCValAssign::BCvt:
945 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
946 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000947 }
948
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000949 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +0000950 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000951 if (VA.getLocVT() == MVT::v2f64) {
952 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
953 DAG.getConstant(0, MVT::i32));
954 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
955 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000956
Dan Gohman98ca4f22009-08-05 01:29:28 +0000957 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +0000958 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
959
960 VA = ArgLocs[++i]; // skip ahead to next loc
961 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000962 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +0000963 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
964 } else {
965 assert(VA.isMemLoc());
966 if (StackPtr.getNode() == 0)
967 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
968
Dan Gohman98ca4f22009-08-05 01:29:28 +0000969 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
970 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000971 }
972 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000973 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000974 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000975 }
976 } else if (VA.isRegLoc()) {
977 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
978 } else {
979 assert(VA.isMemLoc());
980 if (StackPtr.getNode() == 0)
981 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
982
Dan Gohman98ca4f22009-08-05 01:29:28 +0000983 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
984 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000985 }
Evan Chenga8e29892007-01-19 07:51:42 +0000986 }
987
988 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000989 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +0000990 &MemOpChains[0], MemOpChains.size());
991
992 // Build a sequence of copy-to-reg nodes chained together with token chain
993 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +0000994 SDValue InFlag;
Evan Chenga8e29892007-01-19 07:51:42 +0000995 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Bob Wilson2dc4f542009-03-20 22:42:55 +0000996 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000997 RegsToPass[i].second, InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +0000998 InFlag = Chain.getValue(1);
999 }
1000
Bill Wendling056292f2008-09-16 21:48:12 +00001001 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1002 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1003 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001004 bool isDirect = false;
1005 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001006 bool isLocalARMFunc = false;
Evan Chenga8e29892007-01-19 07:51:42 +00001007 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1008 GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001009 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001010 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001011 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001012 getTargetMachine().getRelocationModel() != Reloc::Static;
1013 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001014 // ARM call to a local ARM function is predicable.
1015 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
Evan Chengc60e76d2007-01-30 20:37:08 +00001016 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001017 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge4e4ed32009-08-28 23:18:09 +00001018 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001019 ARMPCLabelIndex,
1020 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001021 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001022 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001023 Callee = DAG.getLoad(getPointerTy(), dl,
1024 DAG.getEntryNode(), CPAddr, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001025 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001026 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001027 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001028 } else
1029 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +00001030 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001031 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001032 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001033 getTargetMachine().getRelocationModel() != Reloc::Static;
1034 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001035 // tBX takes a register source operand.
1036 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001037 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Owen Anderson1d0be152009-08-13 21:58:54 +00001038 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001039 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001040 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001041 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001042 Callee = DAG.getLoad(getPointerTy(), dl,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001043 DAG.getEntryNode(), CPAddr, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001044 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001045 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001046 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001047 } else
Bill Wendling056292f2008-09-16 21:48:12 +00001048 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001049 }
1050
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001051 // FIXME: handle tail calls differently.
1052 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001053 if (Subtarget->isThumb()) {
1054 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001055 CallOpc = ARMISD::CALL_NOLINK;
1056 else
1057 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1058 } else {
1059 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001060 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1061 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001062 }
David Goodwinf1daf7d2009-07-08 23:10:31 +00001063 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb1Only()) {
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +00001064 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Owen Anderson825b72b2009-08-11 20:47:22 +00001065 Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001066 InFlag = Chain.getValue(1);
1067 }
1068
Dan Gohman475871a2008-07-27 21:46:04 +00001069 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001070 Ops.push_back(Chain);
1071 Ops.push_back(Callee);
1072
1073 // Add argument registers to the end of the list so that they are known live
1074 // into the call.
1075 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1076 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1077 RegsToPass[i].second.getValueType()));
1078
Gabor Greifba36cb52008-08-28 21:40:38 +00001079 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001080 Ops.push_back(InFlag);
Duncan Sands4bdcb612008-07-02 17:40:58 +00001081 // Returns a chain and a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00001082 Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Flag),
Duncan Sands4bdcb612008-07-02 17:40:58 +00001083 &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001084 InFlag = Chain.getValue(1);
1085
Chris Lattnere563bbc2008-10-11 22:08:30 +00001086 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1087 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001088 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001089 InFlag = Chain.getValue(1);
1090
Bob Wilson1f595bb2009-04-17 19:07:39 +00001091 // Handle result values, copying them out of physregs into vregs that we
1092 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001093 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1094 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001095}
1096
Dan Gohman98ca4f22009-08-05 01:29:28 +00001097SDValue
1098ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001099 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001100 const SmallVectorImpl<ISD::OutputArg> &Outs,
1101 DebugLoc dl, SelectionDAG &DAG) {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001102
Bob Wilsondee46d72009-04-17 20:35:10 +00001103 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001104 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001105
Bob Wilsondee46d72009-04-17 20:35:10 +00001106 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001107 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1108 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001109
Dan Gohman98ca4f22009-08-05 01:29:28 +00001110 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001111 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1112 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001113
1114 // If this is the first return lowered for this function, add
1115 // the regs to the liveout set for the function.
1116 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1117 for (unsigned i = 0; i != RVLocs.size(); ++i)
1118 if (RVLocs[i].isRegLoc())
1119 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001120 }
1121
Bob Wilson1f595bb2009-04-17 19:07:39 +00001122 SDValue Flag;
1123
1124 // Copy the result values into the output registers.
1125 for (unsigned i = 0, realRVLocIdx = 0;
1126 i != RVLocs.size();
1127 ++i, ++realRVLocIdx) {
1128 CCValAssign &VA = RVLocs[i];
1129 assert(VA.isRegLoc() && "Can only return in registers!");
1130
Dan Gohman98ca4f22009-08-05 01:29:28 +00001131 SDValue Arg = Outs[realRVLocIdx].Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001132
1133 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001134 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001135 case CCValAssign::Full: break;
1136 case CCValAssign::BCvt:
1137 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1138 break;
1139 }
1140
Bob Wilson1f595bb2009-04-17 19:07:39 +00001141 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001142 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001143 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001144 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1145 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001146 SDValue HalfGPRs = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001147 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001148
1149 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1150 Flag = Chain.getValue(1);
1151 VA = RVLocs[++i]; // skip ahead to next loc
1152 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1153 HalfGPRs.getValue(1), Flag);
1154 Flag = Chain.getValue(1);
1155 VA = RVLocs[++i]; // skip ahead to next loc
1156
1157 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001158 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1159 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001160 }
1161 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1162 // available.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001163 SDValue fmrrd = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001164 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001165 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001166 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001167 VA = RVLocs[++i]; // skip ahead to next loc
1168 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1169 Flag);
1170 } else
1171 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1172
Bob Wilsondee46d72009-04-17 20:35:10 +00001173 // Guarantee that all emitted copies are
1174 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001175 Flag = Chain.getValue(1);
1176 }
1177
1178 SDValue result;
1179 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001180 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001181 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001182 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001183
1184 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001185}
1186
Bob Wilsonddb16df2009-10-30 05:45:42 +00001187// ConstantPool, BlockAddress, JumpTable, GlobalAddress, and ExternalSymbol are
1188// lowered as their target counterpart wrapped in the ARMISD::Wrapper
1189// node. Suppose N is one of the above mentioned nodes. It has to be wrapped
1190// because otherwise Select(N) returns N. So the raw TargetGlobalAddress
1191// nodes, etc. can only be used to form addressing mode. These wrapped nodes
1192// will be selected into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001193static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001194 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001195 // FIXME there is no actual debug info here
1196 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001197 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001198 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001199 if (CP->isMachineConstantPoolEntry())
1200 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1201 CP->getAlignment());
1202 else
1203 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1204 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001205 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001206}
1207
Bob Wilsonddb16df2009-10-30 05:45:42 +00001208SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) {
1209 DebugLoc DL = Op.getDebugLoc();
1210 BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1211 SDValue Result = DAG.getBlockAddress(BA, DL, /*isTarget=*/true);
1212 return DAG.getNode(ARMISD::Wrapper, DL, getPointerTy(), Result);
1213}
1214
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001215// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001216SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001217ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
1218 SelectionDAG &DAG) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001219 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001220 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001221 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1222 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001223 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001224 ARMCP::CPValue, PCAdj, "tlsgd", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001225 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001226 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001227 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001228 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001229
Owen Anderson825b72b2009-08-11 20:47:22 +00001230 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001231 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001232
1233 // call __tls_get_addr.
1234 ArgListTy Args;
1235 ArgListEntry Entry;
1236 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001237 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001238 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001239 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001240 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001241 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1242 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001243 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001244 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001245 return CallResult.first;
1246}
1247
1248// Lower ISD::GlobalTLSAddress using the "initial exec" or
1249// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001250SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001251ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001252 SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001253 GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001254 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001255 SDValue Offset;
1256 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001257 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001258 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001259 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001260
Chris Lattner4fb63d02009-07-15 04:12:33 +00001261 if (GV->isDeclaration()) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001262 // initial exec model
1263 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1264 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001265 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001266 ARMCP::CPValue, PCAdj, "gottpoff", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001267 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001268 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001269 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001270 Chain = Offset.getValue(1);
1271
Owen Anderson825b72b2009-08-11 20:47:22 +00001272 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001273 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001274
Dale Johannesen33c960f2009-02-04 20:06:27 +00001275 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001276 } else {
1277 // local exec model
Evan Chenge4e4ed32009-08-28 23:18:09 +00001278 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, "tpoff");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001279 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001280 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001281 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001282 }
1283
1284 // The address of the thread local variable is the add of the thread
1285 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001286 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001287}
1288
Dan Gohman475871a2008-07-27 21:46:04 +00001289SDValue
1290ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001291 // TODO: implement the "local dynamic" model
1292 assert(Subtarget->isTargetELF() &&
1293 "TLS not implemented for non-ELF targets");
1294 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1295 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1296 // otherwise use the "Local Exec" TLS Model
1297 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1298 return LowerToTLSGeneralDynamicModel(GA, DAG);
1299 else
1300 return LowerToTLSExecModels(GA, DAG);
1301}
1302
Dan Gohman475871a2008-07-27 21:46:04 +00001303SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001304 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001305 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001306 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001307 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1308 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1309 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001310 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001311 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001312 new ARMConstantPoolValue(GV, UseGOTOFF ? "GOTOFF" : "GOT");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001313 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001314 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001315 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001316 CPAddr,
1317 PseudoSourceValue::getConstantPool(), 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001318 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001319 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001320 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001321 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001322 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
1323 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001324 return Result;
1325 } else {
Evan Cheng1606e8e2009-03-13 07:51:59 +00001326 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001327 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001328 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1329 PseudoSourceValue::getConstantPool(), 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001330 }
1331}
1332
Dan Gohman475871a2008-07-27 21:46:04 +00001333SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001334 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001335 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001336 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001337 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1338 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Dan Gohman475871a2008-07-27 21:46:04 +00001339 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +00001340 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001341 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001342 else {
Evan Chenge4e4ed32009-08-28 23:18:09 +00001343 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
1344 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001345 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001346 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001347 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001348 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001349
Dale Johannesen33c960f2009-02-04 20:06:27 +00001350 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001351 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001352
1353 if (RelocM == Reloc::PIC_) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001354 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001355 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001356 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00001357
Evan Cheng63476a82009-09-03 07:04:02 +00001358 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Dale Johannesen33c960f2009-02-04 20:06:27 +00001359 Result = DAG.getLoad(PtrVT, dl, Chain, Result, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001360
1361 return Result;
1362}
1363
Dan Gohman475871a2008-07-27 21:46:04 +00001364SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001365 SelectionDAG &DAG){
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001366 assert(Subtarget->isTargetELF() &&
1367 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Owen Andersone50ed302009-08-10 22:56:29 +00001368 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001369 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001370 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00001371 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1372 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00001373 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001374 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001375 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001376 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1377 PseudoSourceValue::getConstantPool(), 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001378 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001379 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001380}
1381
Jim Grosbach0e0da732009-05-12 23:59:14 +00001382SDValue
1383ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001384 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001385 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001386 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001387 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00001388 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00001389 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00001390 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1391 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001392 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001393 MachineFunction &MF = DAG.getMachineFunction();
1394 EVT PtrVT = getPointerTy();
1395 DebugLoc dl = Op.getDebugLoc();
1396 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1397 SDValue CPAddr;
1398 unsigned PCAdj = (RelocM != Reloc::PIC_)
1399 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001400 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001401 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
1402 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001403 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001404 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001405 SDValue Result =
1406 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
1407 SDValue Chain = Result.getValue(1);
1408
1409 if (RelocM == Reloc::PIC_) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001410 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001411 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1412 }
1413 return Result;
1414 }
Jim Grosbachf9570122009-05-14 00:46:35 +00001415 case Intrinsic::eh_sjlj_setjmp:
Owen Anderson825b72b2009-08-11 20:47:22 +00001416 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(1));
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001417 }
1418}
1419
Dan Gohman475871a2008-07-27 21:46:04 +00001420static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001421 unsigned VarArgsFrameIndex) {
Evan Chenga8e29892007-01-19 07:51:42 +00001422 // vastart just stores the address of the VarArgsFrameIndex slot into the
1423 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001424 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001425 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001426 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001427 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001428 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001429}
1430
Dan Gohman475871a2008-07-27 21:46:04 +00001431SDValue
Evan Cheng86198642009-08-07 00:34:42 +00001432ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) {
1433 SDNode *Node = Op.getNode();
1434 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001435 EVT VT = Node->getValueType(0);
Evan Cheng86198642009-08-07 00:34:42 +00001436 SDValue Chain = Op.getOperand(0);
1437 SDValue Size = Op.getOperand(1);
1438 SDValue Align = Op.getOperand(2);
1439
1440 // Chain the dynamic stack allocation so that it doesn't modify the stack
1441 // pointer when other instructions are using the stack.
1442 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1443
1444 unsigned AlignVal = cast<ConstantSDNode>(Align)->getZExtValue();
1445 unsigned StackAlign = getTargetMachine().getFrameInfo()->getStackAlignment();
1446 if (AlignVal > StackAlign)
1447 // Do this now since selection pass cannot introduce new target
1448 // independent node.
1449 Align = DAG.getConstant(-(uint64_t)AlignVal, VT);
1450
1451 // In Thumb1 mode, there isn't a "sub r, sp, r" instruction, we will end up
1452 // using a "add r, sp, r" instead. Negate the size now so we don't have to
1453 // do even more horrible hack later.
1454 MachineFunction &MF = DAG.getMachineFunction();
1455 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1456 if (AFI->isThumb1OnlyFunction()) {
1457 bool Negate = true;
1458 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Size);
1459 if (C) {
1460 uint32_t Val = C->getZExtValue();
1461 if (Val <= 508 && ((Val & 3) == 0))
1462 Negate = false;
1463 }
1464 if (Negate)
1465 Size = DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(0, VT), Size);
1466 }
1467
Owen Anderson825b72b2009-08-11 20:47:22 +00001468 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
Evan Cheng86198642009-08-07 00:34:42 +00001469 SDValue Ops1[] = { Chain, Size, Align };
1470 SDValue Res = DAG.getNode(ARMISD::DYN_ALLOC, dl, VTList, Ops1, 3);
1471 Chain = Res.getValue(1);
1472 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1473 DAG.getIntPtrConstant(0, true), SDValue());
1474 SDValue Ops2[] = { Res, Chain };
1475 return DAG.getMergeValues(Ops2, 2, dl);
1476}
1477
1478SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00001479ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
1480 SDValue &Root, SelectionDAG &DAG,
1481 DebugLoc dl) {
1482 MachineFunction &MF = DAG.getMachineFunction();
1483 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1484
1485 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001486 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00001487 RC = ARM::tGPRRegisterClass;
1488 else
1489 RC = ARM::GPRRegisterClass;
1490
1491 // Transform the arguments stored in physical registers into virtual ones.
1492 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001493 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001494
1495 SDValue ArgValue2;
1496 if (NextVA.isMemLoc()) {
1497 unsigned ArgSize = NextVA.getLocVT().getSizeInBits()/8;
1498 MachineFrameInfo *MFI = MF.getFrameInfo();
1499 int FI = MFI->CreateFixedObject(ArgSize, NextVA.getLocMemOffset());
1500
1501 // Create load node to retrieve arguments from the stack.
1502 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00001503 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN, NULL, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00001504 } else {
1505 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001506 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001507 }
1508
Owen Anderson825b72b2009-08-11 20:47:22 +00001509 return DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00001510}
1511
1512SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001513ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001514 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001515 const SmallVectorImpl<ISD::InputArg>
1516 &Ins,
1517 DebugLoc dl, SelectionDAG &DAG,
1518 SmallVectorImpl<SDValue> &InVals) {
1519
Bob Wilson1f595bb2009-04-17 19:07:39 +00001520 MachineFunction &MF = DAG.getMachineFunction();
1521 MachineFrameInfo *MFI = MF.getFrameInfo();
1522
Bob Wilson1f595bb2009-04-17 19:07:39 +00001523 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1524
1525 // Assign locations to all of the incoming arguments.
1526 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001527 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1528 *DAG.getContext());
1529 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001530 CCAssignFnForNode(CallConv, /* Return*/ false,
1531 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001532
1533 SmallVector<SDValue, 16> ArgValues;
1534
1535 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1536 CCValAssign &VA = ArgLocs[i];
1537
Bob Wilsondee46d72009-04-17 20:35:10 +00001538 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001539 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001540 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00001541
Bob Wilson5bafff32009-06-22 23:27:02 +00001542 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001543 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001544 // f64 and vector types are split up into multiple registers or
1545 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00001546 RegVT = MVT::i32;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001547
Owen Anderson825b72b2009-08-11 20:47:22 +00001548 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001549 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001550 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00001551 VA = ArgLocs[++i]; // skip ahead to next loc
1552 SDValue ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001553 Chain, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001554 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1555 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001556 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00001557 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001558 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
1559 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001560 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001561
Bob Wilson5bafff32009-06-22 23:27:02 +00001562 } else {
1563 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001564
Owen Anderson825b72b2009-08-11 20:47:22 +00001565 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00001566 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001567 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00001568 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001569 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001570 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001571 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001572 RC = (AFI->isThumb1OnlyFunction() ?
1573 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00001574 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001575 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00001576
1577 // Transform the arguments in physical registers into virtual ones.
1578 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001579 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001580 }
1581
1582 // If this is an 8 or 16-bit value, it is really passed promoted
1583 // to 32 bits. Insert an assert[sz]ext to capture this, then
1584 // truncate to the right size.
1585 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001586 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001587 case CCValAssign::Full: break;
1588 case CCValAssign::BCvt:
1589 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1590 break;
1591 case CCValAssign::SExt:
1592 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
1593 DAG.getValueType(VA.getValVT()));
1594 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1595 break;
1596 case CCValAssign::ZExt:
1597 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
1598 DAG.getValueType(VA.getValVT()));
1599 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1600 break;
1601 }
1602
Dan Gohman98ca4f22009-08-05 01:29:28 +00001603 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001604
1605 } else { // VA.isRegLoc()
1606
1607 // sanity check
1608 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00001609 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001610
1611 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
1612 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset());
1613
Bob Wilsondee46d72009-04-17 20:35:10 +00001614 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001615 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001616 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN, NULL, 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001617 }
1618 }
1619
1620 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00001621 if (isVarArg) {
1622 static const unsigned GPRArgRegs[] = {
1623 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1624 };
1625
Bob Wilsondee46d72009-04-17 20:35:10 +00001626 unsigned NumGPRs = CCInfo.getFirstUnallocated
1627 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001628
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001629 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1630 unsigned VARegSize = (4 - NumGPRs) * 4;
1631 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Rafael Espindolac1382b72009-10-30 14:33:14 +00001632 unsigned ArgOffset = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001633 if (VARegSaveSize) {
1634 // If this function is vararg, store any remaining integer argument regs
1635 // to their spots on the stack so that they may be loaded by deferencing
1636 // the result of va_next.
1637 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001638 VarArgsFrameIndex = MFI->CreateFixedObject(VARegSaveSize, ArgOffset +
1639 VARegSaveSize - VARegSize);
Dan Gohman475871a2008-07-27 21:46:04 +00001640 SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001641
Dan Gohman475871a2008-07-27 21:46:04 +00001642 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00001643 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001644 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001645 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00001646 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001647 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00001648 RC = ARM::GPRRegisterClass;
1649
Bob Wilson998e1252009-04-20 18:36:57 +00001650 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001651 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001652 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001653 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001654 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00001655 DAG.getConstant(4, getPointerTy()));
1656 }
1657 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001658 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001659 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001660 } else
1661 // This will point to the next argument passed via stack.
1662 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1663 }
1664
Dan Gohman98ca4f22009-08-05 01:29:28 +00001665 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00001666}
1667
1668/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00001669static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00001670 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001671 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00001672 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00001673 // Maybe this has already been legalized into the constant pool?
1674 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00001675 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001676 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
1677 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001678 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001679 }
1680 }
1681 return false;
1682}
1683
David Goodwinf1daf7d2009-07-08 23:10:31 +00001684static bool isLegalCmpImmediate(unsigned C, bool isThumb1Only) {
1685 return ( isThumb1Only && (C & ~255U) == 0) ||
1686 (!isThumb1Only && ARM_AM::getSOImmVal(C) != -1);
Evan Chenga8e29892007-01-19 07:51:42 +00001687}
1688
1689/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1690/// the given operands.
Dan Gohman475871a2008-07-27 21:46:04 +00001691static SDValue getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
David Goodwinf1daf7d2009-07-08 23:10:31 +00001692 SDValue &ARMCC, SelectionDAG &DAG, bool isThumb1Only,
Dale Johannesende064702009-02-06 21:50:26 +00001693 DebugLoc dl) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001694 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001695 unsigned C = RHSC->getZExtValue();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001696 if (!isLegalCmpImmediate(C, isThumb1Only)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001697 // Constant does not fit, try adjusting it by one?
1698 switch (CC) {
1699 default: break;
1700 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00001701 case ISD::SETGE:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001702 if (isLegalCmpImmediate(C-1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001703 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001704 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001705 }
1706 break;
1707 case ISD::SETULT:
1708 case ISD::SETUGE:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001709 if (C > 0 && isLegalCmpImmediate(C-1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001710 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001711 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001712 }
1713 break;
1714 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00001715 case ISD::SETGT:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001716 if (isLegalCmpImmediate(C+1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001717 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001718 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001719 }
1720 break;
1721 case ISD::SETULE:
1722 case ISD::SETUGT:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001723 if (C < 0xffffffff && isLegalCmpImmediate(C+1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001724 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001725 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001726 }
1727 break;
1728 }
1729 }
1730 }
1731
1732 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001733 ARMISD::NodeType CompareType;
1734 switch (CondCode) {
1735 default:
1736 CompareType = ARMISD::CMP;
1737 break;
1738 case ARMCC::EQ:
1739 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00001740 // Uses only Z Flag
1741 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001742 break;
1743 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001744 ARMCC = DAG.getConstant(CondCode, MVT::i32);
1745 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001746}
1747
1748/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001749static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Dale Johannesende064702009-02-06 21:50:26 +00001750 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001751 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00001752 if (!isFloatingPointZero(RHS))
Owen Anderson825b72b2009-08-11 20:47:22 +00001753 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001754 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001755 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
1756 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001757}
1758
Dan Gohman475871a2008-07-27 21:46:04 +00001759static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001760 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00001761 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001762 SDValue LHS = Op.getOperand(0);
1763 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001764 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001765 SDValue TrueVal = Op.getOperand(2);
1766 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00001767 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001768
Owen Anderson825b72b2009-08-11 20:47:22 +00001769 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001770 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001771 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
David Goodwinf1daf7d2009-07-08 23:10:31 +00001772 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb1Only(), dl);
Dale Johannesende064702009-02-06 21:50:26 +00001773 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001774 }
1775
1776 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001777 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00001778
Owen Anderson825b72b2009-08-11 20:47:22 +00001779 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1780 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001781 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
1782 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00001783 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001784 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001785 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001786 // FIXME: Needs another CMP because flag can have but one use.
Dale Johannesende064702009-02-06 21:50:26 +00001787 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001788 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Dale Johannesende064702009-02-06 21:50:26 +00001789 Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00001790 }
1791 return Result;
1792}
1793
Dan Gohman475871a2008-07-27 21:46:04 +00001794static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001795 const ARMSubtarget *ST) {
Dan Gohman475871a2008-07-27 21:46:04 +00001796 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001797 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001798 SDValue LHS = Op.getOperand(2);
1799 SDValue RHS = Op.getOperand(3);
1800 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00001801 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001802
Owen Anderson825b72b2009-08-11 20:47:22 +00001803 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001804 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001805 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
David Goodwinf1daf7d2009-07-08 23:10:31 +00001806 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb1Only(), dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001807 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Dale Johannesende064702009-02-06 21:50:26 +00001808 Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001809 }
1810
Owen Anderson825b72b2009-08-11 20:47:22 +00001811 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Chenga8e29892007-01-19 07:51:42 +00001812 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001813 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001814
Dale Johannesende064702009-02-06 21:50:26 +00001815 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001816 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1817 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1818 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00001819 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00001820 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001821 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001822 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00001823 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00001824 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001825 }
1826 return Res;
1827}
1828
Dan Gohman475871a2008-07-27 21:46:04 +00001829SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) {
1830 SDValue Chain = Op.getOperand(0);
1831 SDValue Table = Op.getOperand(1);
1832 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001833 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001834
Owen Andersone50ed302009-08-10 22:56:29 +00001835 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00001836 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
1837 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00001838 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00001839 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00001840 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00001841 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
1842 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00001843 if (Subtarget->isThumb2()) {
1844 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
1845 // which does another jump to the destination. This also makes it easier
1846 // to translate it to TBB / TBH later.
1847 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00001848 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00001849 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001850 }
Evan Cheng66ac5312009-07-25 00:33:29 +00001851 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001852 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr, NULL, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00001853 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001854 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00001855 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001856 } else {
1857 Addr = DAG.getLoad(PTy, dl, Chain, Addr, NULL, 0);
1858 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00001859 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001860 }
Evan Chenga8e29892007-01-19 07:51:42 +00001861}
1862
Dan Gohman475871a2008-07-27 21:46:04 +00001863static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Dale Johannesende064702009-02-06 21:50:26 +00001864 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001865 unsigned Opc =
1866 Op.getOpcode() == ISD::FP_TO_SINT ? ARMISD::FTOSI : ARMISD::FTOUI;
Owen Anderson825b72b2009-08-11 20:47:22 +00001867 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
1868 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001869}
1870
Dan Gohman475871a2008-07-27 21:46:04 +00001871static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001872 EVT VT = Op.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001873 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001874 unsigned Opc =
1875 Op.getOpcode() == ISD::SINT_TO_FP ? ARMISD::SITOF : ARMISD::UITOF;
1876
Owen Anderson825b72b2009-08-11 20:47:22 +00001877 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
Dale Johannesende064702009-02-06 21:50:26 +00001878 return DAG.getNode(Opc, dl, VT, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001879}
1880
Dan Gohman475871a2008-07-27 21:46:04 +00001881static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00001882 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00001883 SDValue Tmp0 = Op.getOperand(0);
1884 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00001885 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001886 EVT VT = Op.getValueType();
1887 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001888 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
1889 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001890 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
1891 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001892 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001893}
1894
Jim Grosbach0e0da732009-05-12 23:59:14 +00001895SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
1896 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1897 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00001898 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001899 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
1900 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00001901 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00001902 ? ARM::R7 : ARM::R11;
1903 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
1904 while (Depth--)
1905 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
1906 return FrameAddr;
1907}
1908
Dan Gohman475871a2008-07-27 21:46:04 +00001909SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00001910ARMTargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman475871a2008-07-27 21:46:04 +00001911 SDValue Chain,
1912 SDValue Dst, SDValue Src,
1913 SDValue Size, unsigned Align,
Dan Gohman707e0182008-04-12 04:36:06 +00001914 bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00001915 const Value *DstSV, uint64_t DstSVOff,
1916 const Value *SrcSV, uint64_t SrcSVOff){
Evan Cheng4102eb52007-10-22 22:11:27 +00001917 // Do repeated 4-byte loads and stores. To be improved.
Dan Gohman707e0182008-04-12 04:36:06 +00001918 // This requires 4-byte alignment.
1919 if ((Align & 3) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00001920 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001921 // This requires the copy size to be a constant, preferrably
1922 // within a subtarget-specific limit.
1923 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
1924 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00001925 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001926 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001927 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00001928 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001929
1930 unsigned BytesLeft = SizeVal & 3;
1931 unsigned NumMemOps = SizeVal >> 2;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001932 unsigned EmittedNumMemOps = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001933 EVT VT = MVT::i32;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001934 unsigned VTSize = 4;
Evan Cheng4102eb52007-10-22 22:11:27 +00001935 unsigned i = 0;
Evan Chenge5e7ce42007-05-18 01:19:57 +00001936 const unsigned MAX_LOADS_IN_LDM = 6;
Dan Gohman475871a2008-07-27 21:46:04 +00001937 SDValue TFOps[MAX_LOADS_IN_LDM];
1938 SDValue Loads[MAX_LOADS_IN_LDM];
Dan Gohman1f13c682008-04-28 17:15:20 +00001939 uint64_t SrcOff = 0, DstOff = 0;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001940
Evan Cheng4102eb52007-10-22 22:11:27 +00001941 // Emit up to MAX_LOADS_IN_LDM loads, then a TokenFactor barrier, then the
1942 // same number of stores. The loads and stores will get combined into
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001943 // ldm/stm later on.
Evan Cheng4102eb52007-10-22 22:11:27 +00001944 while (EmittedNumMemOps < NumMemOps) {
1945 for (i = 0;
1946 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00001947 Loads[i] = DAG.getLoad(VT, dl, Chain,
Owen Anderson825b72b2009-08-11 20:47:22 +00001948 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
1949 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001950 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00001951 TFOps[i] = Loads[i].getValue(1);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001952 SrcOff += VTSize;
1953 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001954 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001955
Evan Cheng4102eb52007-10-22 22:11:27 +00001956 for (i = 0;
1957 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00001958 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Owen Anderson825b72b2009-08-11 20:47:22 +00001959 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
1960 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001961 DstSV, DstSVOff + DstOff);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001962 DstOff += VTSize;
1963 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001964 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00001965
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001966 EmittedNumMemOps += i;
1967 }
1968
Bob Wilson2dc4f542009-03-20 22:42:55 +00001969 if (BytesLeft == 0)
Evan Cheng4102eb52007-10-22 22:11:27 +00001970 return Chain;
1971
1972 // Issue loads / stores for the trailing (1 - 3) bytes.
1973 unsigned BytesLeftSave = BytesLeft;
1974 i = 0;
1975 while (BytesLeft) {
1976 if (BytesLeft >= 2) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001977 VT = MVT::i16;
Evan Cheng4102eb52007-10-22 22:11:27 +00001978 VTSize = 2;
1979 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001980 VT = MVT::i8;
Evan Cheng4102eb52007-10-22 22:11:27 +00001981 VTSize = 1;
1982 }
1983
Dale Johannesen0f502f62009-02-03 22:26:09 +00001984 Loads[i] = DAG.getLoad(VT, dl, Chain,
Owen Anderson825b72b2009-08-11 20:47:22 +00001985 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
1986 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001987 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00001988 TFOps[i] = Loads[i].getValue(1);
1989 ++i;
1990 SrcOff += VTSize;
1991 BytesLeft -= VTSize;
1992 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001993 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00001994
1995 i = 0;
1996 BytesLeft = BytesLeftSave;
1997 while (BytesLeft) {
1998 if (BytesLeft >= 2) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001999 VT = MVT::i16;
Evan Cheng4102eb52007-10-22 22:11:27 +00002000 VTSize = 2;
2001 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00002002 VT = MVT::i8;
Evan Cheng4102eb52007-10-22 22:11:27 +00002003 VTSize = 1;
2004 }
2005
Dale Johannesen0f502f62009-02-03 22:26:09 +00002006 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Owen Anderson825b72b2009-08-11 20:47:22 +00002007 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
2008 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00002009 DstSV, DstSVOff + DstOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00002010 ++i;
2011 DstOff += VTSize;
2012 BytesLeft -= VTSize;
2013 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002014 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002015}
2016
Duncan Sands1607f052008-12-01 11:39:25 +00002017static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00002018 SDValue Op = N->getOperand(0);
Dale Johannesende064702009-02-06 21:50:26 +00002019 DebugLoc dl = N->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00002020 if (N->getValueType(0) == MVT::f64) {
Evan Chengc7c77292008-11-04 19:57:48 +00002021 // Turn i64->f64 into FMDRR.
Owen Anderson825b72b2009-08-11 20:47:22 +00002022 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2023 DAG.getConstant(0, MVT::i32));
2024 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2025 DAG.getConstant(1, MVT::i32));
2026 return DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
Evan Chengc7c77292008-11-04 19:57:48 +00002027 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002028
Evan Chengc7c77292008-11-04 19:57:48 +00002029 // Turn f64->i64 into FMRRD.
Bob Wilson2dc4f542009-03-20 22:42:55 +00002030 SDValue Cvt = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00002031 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002032
Chris Lattner27a6c732007-11-24 07:07:01 +00002033 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002034 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
Chris Lattner27a6c732007-11-24 07:07:01 +00002035}
2036
Bob Wilson5bafff32009-06-22 23:27:02 +00002037/// getZeroVector - Returns a vector of specified type with all zero elements.
2038///
Owen Andersone50ed302009-08-10 22:56:29 +00002039static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002040 assert(VT.isVector() && "Expected a vector type");
2041
2042 // Zero vectors are used to represent vector negation and in those cases
2043 // will be implemented with the NEON VNEG instruction. However, VNEG does
2044 // not support i64 elements, so sometimes the zero vectors will need to be
2045 // explicitly constructed. For those cases, and potentially other uses in
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002046 // the future, always build zero vectors as <16 x i8> or <8 x i8> bitcasted
Bob Wilson5bafff32009-06-22 23:27:02 +00002047 // to their dest type. This ensures they get CSE'd.
2048 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002049 SDValue Cst = DAG.getTargetConstant(0, MVT::i8);
2050 SmallVector<SDValue, 8> Ops;
2051 MVT TVT;
2052
2053 if (VT.getSizeInBits() == 64) {
2054 Ops.assign(8, Cst); TVT = MVT::v8i8;
2055 } else {
2056 Ops.assign(16, Cst); TVT = MVT::v16i8;
2057 }
2058 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002059
2060 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2061}
2062
2063/// getOnesVector - Returns a vector of specified type with all bits set.
2064///
Owen Andersone50ed302009-08-10 22:56:29 +00002065static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002066 assert(VT.isVector() && "Expected a vector type");
2067
Bob Wilson929ffa22009-10-30 20:13:25 +00002068 // Always build ones vectors as <16 x i8> or <8 x i8> bitcasted to their
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002069 // dest type. This ensures they get CSE'd.
Bob Wilson5bafff32009-06-22 23:27:02 +00002070 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002071 SDValue Cst = DAG.getTargetConstant(0xFF, MVT::i8);
2072 SmallVector<SDValue, 8> Ops;
2073 MVT TVT;
2074
2075 if (VT.getSizeInBits() == 64) {
2076 Ops.assign(8, Cst); TVT = MVT::v8i8;
2077 } else {
2078 Ops.assign(16, Cst); TVT = MVT::v16i8;
2079 }
2080 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002081
2082 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2083}
2084
2085static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2086 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00002087 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002088 DebugLoc dl = N->getDebugLoc();
2089
2090 // Lower vector shifts on NEON to use VSHL.
2091 if (VT.isVector()) {
2092 assert(ST->hasNEON() && "unexpected vector shift");
2093
2094 // Left shifts translate directly to the vshiftu intrinsic.
2095 if (N->getOpcode() == ISD::SHL)
2096 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002097 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002098 N->getOperand(0), N->getOperand(1));
2099
2100 assert((N->getOpcode() == ISD::SRA ||
2101 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2102
2103 // NEON uses the same intrinsics for both left and right shifts. For
2104 // right shifts, the shift amounts are negative, so negate the vector of
2105 // shift amounts.
Owen Andersone50ed302009-08-10 22:56:29 +00002106 EVT ShiftVT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002107 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2108 getZeroVector(ShiftVT, DAG, dl),
2109 N->getOperand(1));
2110 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2111 Intrinsic::arm_neon_vshifts :
2112 Intrinsic::arm_neon_vshiftu);
2113 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002114 DAG.getConstant(vshiftInt, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002115 N->getOperand(0), NegatedCount);
2116 }
2117
Eli Friedmance392eb2009-08-22 03:13:10 +00002118 // We can get here for a node like i32 = ISD::SHL i32, i64
2119 if (VT != MVT::i64)
2120 return SDValue();
2121
2122 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00002123 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00002124
Chris Lattner27a6c732007-11-24 07:07:01 +00002125 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2126 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002127 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00002128 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002129
Chris Lattner27a6c732007-11-24 07:07:01 +00002130 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00002131 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002132
Chris Lattner27a6c732007-11-24 07:07:01 +00002133 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00002134 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2135 DAG.getConstant(0, MVT::i32));
2136 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2137 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002138
Chris Lattner27a6c732007-11-24 07:07:01 +00002139 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2140 // captures the result into a carry flag.
2141 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Owen Anderson825b72b2009-08-11 20:47:22 +00002142 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002143
Chris Lattner27a6c732007-11-24 07:07:01 +00002144 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00002145 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002146
Chris Lattner27a6c732007-11-24 07:07:01 +00002147 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002148 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00002149}
2150
Bob Wilson5bafff32009-06-22 23:27:02 +00002151static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2152 SDValue TmpOp0, TmpOp1;
2153 bool Invert = false;
2154 bool Swap = false;
2155 unsigned Opc = 0;
2156
2157 SDValue Op0 = Op.getOperand(0);
2158 SDValue Op1 = Op.getOperand(1);
2159 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00002160 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002161 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2162 DebugLoc dl = Op.getDebugLoc();
2163
2164 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2165 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002166 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002167 case ISD::SETUNE:
2168 case ISD::SETNE: Invert = true; // Fallthrough
2169 case ISD::SETOEQ:
2170 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2171 case ISD::SETOLT:
2172 case ISD::SETLT: Swap = true; // Fallthrough
2173 case ISD::SETOGT:
2174 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2175 case ISD::SETOLE:
2176 case ISD::SETLE: Swap = true; // Fallthrough
2177 case ISD::SETOGE:
2178 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2179 case ISD::SETUGE: Swap = true; // Fallthrough
2180 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2181 case ISD::SETUGT: Swap = true; // Fallthrough
2182 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2183 case ISD::SETUEQ: Invert = true; // Fallthrough
2184 case ISD::SETONE:
2185 // Expand this to (OLT | OGT).
2186 TmpOp0 = Op0;
2187 TmpOp1 = Op1;
2188 Opc = ISD::OR;
2189 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2190 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2191 break;
2192 case ISD::SETUO: Invert = true; // Fallthrough
2193 case ISD::SETO:
2194 // Expand this to (OLT | OGE).
2195 TmpOp0 = Op0;
2196 TmpOp1 = Op1;
2197 Opc = ISD::OR;
2198 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2199 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2200 break;
2201 }
2202 } else {
2203 // Integer comparisons.
2204 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002205 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002206 case ISD::SETNE: Invert = true;
2207 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2208 case ISD::SETLT: Swap = true;
2209 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2210 case ISD::SETLE: Swap = true;
2211 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2212 case ISD::SETULT: Swap = true;
2213 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2214 case ISD::SETULE: Swap = true;
2215 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2216 }
2217
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002218 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00002219 if (Opc == ARMISD::VCEQ) {
2220
2221 SDValue AndOp;
2222 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2223 AndOp = Op0;
2224 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2225 AndOp = Op1;
2226
2227 // Ignore bitconvert.
2228 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2229 AndOp = AndOp.getOperand(0);
2230
2231 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2232 Opc = ARMISD::VTST;
2233 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2234 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2235 Invert = !Invert;
2236 }
2237 }
2238 }
2239
2240 if (Swap)
2241 std::swap(Op0, Op1);
2242
2243 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
2244
2245 if (Invert)
2246 Result = DAG.getNOT(dl, Result, VT);
2247
2248 return Result;
2249}
2250
2251/// isVMOVSplat - Check if the specified splat value corresponds to an immediate
2252/// VMOV instruction, and if so, return the constant being splatted.
2253static SDValue isVMOVSplat(uint64_t SplatBits, uint64_t SplatUndef,
2254 unsigned SplatBitSize, SelectionDAG &DAG) {
2255 switch (SplatBitSize) {
2256 case 8:
2257 // Any 1-byte value is OK.
2258 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Owen Anderson825b72b2009-08-11 20:47:22 +00002259 return DAG.getTargetConstant(SplatBits, MVT::i8);
Bob Wilson5bafff32009-06-22 23:27:02 +00002260
2261 case 16:
2262 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
2263 if ((SplatBits & ~0xff) == 0 ||
2264 (SplatBits & ~0xff00) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002265 return DAG.getTargetConstant(SplatBits, MVT::i16);
Bob Wilson5bafff32009-06-22 23:27:02 +00002266 break;
2267
2268 case 32:
2269 // NEON's 32-bit VMOV supports splat values where:
2270 // * only one byte is nonzero, or
2271 // * the least significant byte is 0xff and the second byte is nonzero, or
2272 // * the least significant 2 bytes are 0xff and the third is nonzero.
2273 if ((SplatBits & ~0xff) == 0 ||
2274 (SplatBits & ~0xff00) == 0 ||
2275 (SplatBits & ~0xff0000) == 0 ||
2276 (SplatBits & ~0xff000000) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002277 return DAG.getTargetConstant(SplatBits, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002278
2279 if ((SplatBits & ~0xffff) == 0 &&
2280 ((SplatBits | SplatUndef) & 0xff) == 0xff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002281 return DAG.getTargetConstant(SplatBits | 0xff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002282
2283 if ((SplatBits & ~0xffffff) == 0 &&
2284 ((SplatBits | SplatUndef) & 0xffff) == 0xffff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002285 return DAG.getTargetConstant(SplatBits | 0xffff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002286
2287 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
2288 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
2289 // VMOV.I32. A (very) minor optimization would be to replicate the value
2290 // and fall through here to test for a valid 64-bit splat. But, then the
2291 // caller would also need to check and handle the change in size.
2292 break;
2293
2294 case 64: {
2295 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
2296 uint64_t BitMask = 0xff;
2297 uint64_t Val = 0;
2298 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
2299 if (((SplatBits | SplatUndef) & BitMask) == BitMask)
2300 Val |= BitMask;
2301 else if ((SplatBits & BitMask) != 0)
2302 return SDValue();
2303 BitMask <<= 8;
2304 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002305 return DAG.getTargetConstant(Val, MVT::i64);
Bob Wilson5bafff32009-06-22 23:27:02 +00002306 }
2307
2308 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002309 llvm_unreachable("unexpected size for isVMOVSplat");
Bob Wilson5bafff32009-06-22 23:27:02 +00002310 break;
2311 }
2312
2313 return SDValue();
2314}
2315
2316/// getVMOVImm - If this is a build_vector of constants which can be
2317/// formed by using a VMOV instruction of the specified element size,
2318/// return the constant being splatted. The ByteSize field indicates the
2319/// number of bytes of each element [1248].
2320SDValue ARM::getVMOVImm(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
2321 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
2322 APInt SplatBits, SplatUndef;
2323 unsigned SplatBitSize;
2324 bool HasAnyUndefs;
2325 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
2326 HasAnyUndefs, ByteSize * 8))
2327 return SDValue();
2328
2329 if (SplatBitSize > ByteSize * 8)
2330 return SDValue();
2331
2332 return isVMOVSplat(SplatBits.getZExtValue(), SplatUndef.getZExtValue(),
2333 SplatBitSize, DAG);
2334}
2335
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002336static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
2337 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002338 unsigned NumElts = VT.getVectorNumElements();
2339 ReverseVEXT = false;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002340 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002341
2342 // If this is a VEXT shuffle, the immediate value is the index of the first
2343 // element. The other shuffle indices must be the successive elements after
2344 // the first one.
2345 unsigned ExpectedElt = Imm;
2346 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002347 // Increment the expected index. If it wraps around, it may still be
2348 // a VEXT but the source vectors must be swapped.
2349 ExpectedElt += 1;
2350 if (ExpectedElt == NumElts * 2) {
2351 ExpectedElt = 0;
2352 ReverseVEXT = true;
2353 }
2354
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002355 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002356 return false;
2357 }
2358
2359 // Adjust the index value if the source operands will be swapped.
2360 if (ReverseVEXT)
2361 Imm -= NumElts;
2362
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002363 return true;
2364}
2365
Bob Wilson8bb9e482009-07-26 00:39:34 +00002366/// isVREVMask - Check if a vector shuffle corresponds to a VREV
2367/// instruction with the specified blocksize. (The order of the elements
2368/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002369static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
2370 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00002371 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
2372 "Only possible block sizes for VREV are: 16, 32, 64");
2373
Bob Wilson8bb9e482009-07-26 00:39:34 +00002374 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00002375 if (EltSz == 64)
2376 return false;
2377
2378 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002379 unsigned BlockElts = M[0] + 1;
Bob Wilson8bb9e482009-07-26 00:39:34 +00002380
2381 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
2382 return false;
2383
2384 for (unsigned i = 0; i < NumElts; ++i) {
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002385 if ((unsigned) M[i] !=
Bob Wilson8bb9e482009-07-26 00:39:34 +00002386 (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
2387 return false;
2388 }
2389
2390 return true;
2391}
2392
Bob Wilsonc692cb72009-08-21 20:54:19 +00002393static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
2394 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002395 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2396 if (EltSz == 64)
2397 return false;
2398
Bob Wilsonc692cb72009-08-21 20:54:19 +00002399 unsigned NumElts = VT.getVectorNumElements();
2400 WhichResult = (M[0] == 0 ? 0 : 1);
2401 for (unsigned i = 0; i < NumElts; i += 2) {
2402 if ((unsigned) M[i] != i + WhichResult ||
2403 (unsigned) M[i+1] != i + NumElts + WhichResult)
2404 return false;
2405 }
2406 return true;
2407}
2408
2409static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
2410 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002411 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2412 if (EltSz == 64)
2413 return false;
2414
Bob Wilsonc692cb72009-08-21 20:54:19 +00002415 unsigned NumElts = VT.getVectorNumElements();
2416 WhichResult = (M[0] == 0 ? 0 : 1);
2417 for (unsigned i = 0; i != NumElts; ++i) {
2418 if ((unsigned) M[i] != 2 * i + WhichResult)
2419 return false;
2420 }
2421
2422 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00002423 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002424 return false;
2425
2426 return true;
2427}
2428
2429static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
2430 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002431 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2432 if (EltSz == 64)
2433 return false;
2434
Bob Wilsonc692cb72009-08-21 20:54:19 +00002435 unsigned NumElts = VT.getVectorNumElements();
2436 WhichResult = (M[0] == 0 ? 0 : 1);
2437 unsigned Idx = WhichResult * NumElts / 2;
2438 for (unsigned i = 0; i != NumElts; i += 2) {
2439 if ((unsigned) M[i] != Idx ||
2440 (unsigned) M[i+1] != Idx + NumElts)
2441 return false;
2442 Idx += 1;
2443 }
2444
2445 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00002446 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002447 return false;
2448
2449 return true;
2450}
2451
Owen Andersone50ed302009-08-10 22:56:29 +00002452static SDValue BuildSplat(SDValue Val, EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002453 // Canonicalize all-zeros and all-ones vectors.
Bob Wilsond06791f2009-08-13 01:57:47 +00002454 ConstantSDNode *ConstVal = cast<ConstantSDNode>(Val.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002455 if (ConstVal->isNullValue())
2456 return getZeroVector(VT, DAG, dl);
2457 if (ConstVal->isAllOnesValue())
2458 return getOnesVector(VT, DAG, dl);
2459
Owen Andersone50ed302009-08-10 22:56:29 +00002460 EVT CanonicalVT;
Bob Wilson5bafff32009-06-22 23:27:02 +00002461 if (VT.is64BitVector()) {
2462 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002463 case 8: CanonicalVT = MVT::v8i8; break;
2464 case 16: CanonicalVT = MVT::v4i16; break;
2465 case 32: CanonicalVT = MVT::v2i32; break;
2466 case 64: CanonicalVT = MVT::v1i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002467 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002468 }
2469 } else {
2470 assert(VT.is128BitVector() && "unknown splat vector size");
2471 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002472 case 8: CanonicalVT = MVT::v16i8; break;
2473 case 16: CanonicalVT = MVT::v8i16; break;
2474 case 32: CanonicalVT = MVT::v4i32; break;
2475 case 64: CanonicalVT = MVT::v2i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002476 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002477 }
2478 }
2479
2480 // Build a canonical splat for this value.
2481 SmallVector<SDValue, 8> Ops;
2482 Ops.assign(CanonicalVT.getVectorNumElements(), Val);
2483 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, &Ops[0],
2484 Ops.size());
2485 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Res);
2486}
2487
2488// If this is a case we can't handle, return null and let the default
2489// expansion code take care of it.
2490static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bob Wilsond06791f2009-08-13 01:57:47 +00002491 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002492 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002493 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002494
2495 APInt SplatBits, SplatUndef;
2496 unsigned SplatBitSize;
2497 bool HasAnyUndefs;
2498 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00002499 if (SplatBitSize <= 64) {
2500 SDValue Val = isVMOVSplat(SplatBits.getZExtValue(),
2501 SplatUndef.getZExtValue(), SplatBitSize, DAG);
2502 if (Val.getNode())
2503 return BuildSplat(Val, VT, DAG, dl);
2504 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00002505 }
2506
2507 // If there are only 2 elements in a 128-bit vector, insert them into an
2508 // undef vector. This handles the common case for 128-bit vector argument
2509 // passing, where the insertions should be translated to subreg accesses
2510 // with no real instructions.
2511 if (VT.is128BitVector() && Op.getNumOperands() == 2) {
2512 SDValue Val = DAG.getUNDEF(VT);
2513 SDValue Op0 = Op.getOperand(0);
2514 SDValue Op1 = Op.getOperand(1);
2515 if (Op0.getOpcode() != ISD::UNDEF)
2516 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op0,
2517 DAG.getIntPtrConstant(0));
2518 if (Op1.getOpcode() != ISD::UNDEF)
2519 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op1,
2520 DAG.getIntPtrConstant(1));
2521 return Val;
Bob Wilson5bafff32009-06-22 23:27:02 +00002522 }
2523
2524 return SDValue();
2525}
2526
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002527/// isShuffleMaskLegal - Targets can use this to indicate that they only
2528/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
2529/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
2530/// are assumed to be legal.
2531bool
2532ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
2533 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002534 if (VT.getVectorNumElements() == 4 &&
2535 (VT.is128BitVector() || VT.is64BitVector())) {
2536 unsigned PFIndexes[4];
2537 for (unsigned i = 0; i != 4; ++i) {
2538 if (M[i] < 0)
2539 PFIndexes[i] = 8;
2540 else
2541 PFIndexes[i] = M[i];
2542 }
2543
2544 // Compute the index in the perfect shuffle table.
2545 unsigned PFTableIndex =
2546 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2547 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2548 unsigned Cost = (PFEntry >> 30);
2549
2550 if (Cost <= 4)
2551 return true;
2552 }
2553
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002554 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00002555 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002556
2557 return (ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
2558 isVREVMask(M, VT, 64) ||
2559 isVREVMask(M, VT, 32) ||
2560 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00002561 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
2562 isVTRNMask(M, VT, WhichResult) ||
2563 isVUZPMask(M, VT, WhichResult) ||
2564 isVZIPMask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002565}
2566
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002567/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
2568/// the specified operations to build the shuffle.
2569static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
2570 SDValue RHS, SelectionDAG &DAG,
2571 DebugLoc dl) {
2572 unsigned OpNum = (PFEntry >> 26) & 0x0F;
2573 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
2574 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
2575
2576 enum {
2577 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
2578 OP_VREV,
2579 OP_VDUP0,
2580 OP_VDUP1,
2581 OP_VDUP2,
2582 OP_VDUP3,
2583 OP_VEXT1,
2584 OP_VEXT2,
2585 OP_VEXT3,
2586 OP_VUZPL, // VUZP, left result
2587 OP_VUZPR, // VUZP, right result
2588 OP_VZIPL, // VZIP, left result
2589 OP_VZIPR, // VZIP, right result
2590 OP_VTRNL, // VTRN, left result
2591 OP_VTRNR // VTRN, right result
2592 };
2593
2594 if (OpNum == OP_COPY) {
2595 if (LHSID == (1*9+2)*9+3) return LHS;
2596 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
2597 return RHS;
2598 }
2599
2600 SDValue OpLHS, OpRHS;
2601 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
2602 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
2603 EVT VT = OpLHS.getValueType();
2604
2605 switch (OpNum) {
2606 default: llvm_unreachable("Unknown shuffle opcode!");
2607 case OP_VREV:
2608 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
2609 case OP_VDUP0:
2610 case OP_VDUP1:
2611 case OP_VDUP2:
2612 case OP_VDUP3:
2613 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002614 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002615 case OP_VEXT1:
2616 case OP_VEXT2:
2617 case OP_VEXT3:
2618 return DAG.getNode(ARMISD::VEXT, dl, VT,
2619 OpLHS, OpRHS,
2620 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
2621 case OP_VUZPL:
2622 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002623 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002624 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
2625 case OP_VZIPL:
2626 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002627 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002628 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
2629 case OP_VTRNL:
2630 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002631 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2632 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002633 }
2634}
2635
Bob Wilson5bafff32009-06-22 23:27:02 +00002636static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002637 SDValue V1 = Op.getOperand(0);
2638 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00002639 DebugLoc dl = Op.getDebugLoc();
2640 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002641 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002642 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00002643
Bob Wilson28865062009-08-13 02:13:04 +00002644 // Convert shuffles that are directly supported on NEON to target-specific
2645 // DAG nodes, instead of keeping them as shuffles and matching them again
2646 // during code selection. This is more efficient and avoids the possibility
2647 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00002648 // FIXME: floating-point vectors should be canonicalized to integer vectors
2649 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002650 SVN->getMask(ShuffleMask);
2651
2652 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
Bob Wilson0ce37102009-08-14 05:08:32 +00002653 int Lane = SVN->getSplatIndex();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002654 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
2655 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00002656 }
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002657 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002658 DAG.getConstant(Lane, MVT::i32));
Bob Wilson0ce37102009-08-14 05:08:32 +00002659 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002660
2661 bool ReverseVEXT;
2662 unsigned Imm;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002663 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002664 if (ReverseVEXT)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002665 std::swap(V1, V2);
2666 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002667 DAG.getConstant(Imm, MVT::i32));
2668 }
2669
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002670 if (isVREVMask(ShuffleMask, VT, 64))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002671 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002672 if (isVREVMask(ShuffleMask, VT, 32))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002673 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002674 if (isVREVMask(ShuffleMask, VT, 16))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002675 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
2676
Bob Wilsonc692cb72009-08-21 20:54:19 +00002677 // Check for Neon shuffles that modify both input vectors in place.
2678 // If both results are used, i.e., if there are two shuffles with the same
2679 // source operands and with masks corresponding to both results of one of
2680 // these operations, DAG memoization will ensure that a single node is
2681 // used for both shuffles.
2682 unsigned WhichResult;
2683 if (isVTRNMask(ShuffleMask, VT, WhichResult))
2684 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2685 V1, V2).getValue(WhichResult);
2686 if (isVUZPMask(ShuffleMask, VT, WhichResult))
2687 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
2688 V1, V2).getValue(WhichResult);
2689 if (isVZIPMask(ShuffleMask, VT, WhichResult))
2690 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
2691 V1, V2).getValue(WhichResult);
2692
2693 // If the shuffle is not directly supported and it has 4 elements, use
2694 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002695 if (VT.getVectorNumElements() == 4 &&
2696 (VT.is128BitVector() || VT.is64BitVector())) {
2697 unsigned PFIndexes[4];
2698 for (unsigned i = 0; i != 4; ++i) {
2699 if (ShuffleMask[i] < 0)
2700 PFIndexes[i] = 8;
2701 else
2702 PFIndexes[i] = ShuffleMask[i];
2703 }
2704
2705 // Compute the index in the perfect shuffle table.
2706 unsigned PFTableIndex =
2707 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2708
2709 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2710 unsigned Cost = (PFEntry >> 30);
2711
2712 if (Cost <= 4)
2713 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
2714 }
Bob Wilsond8e17572009-08-12 22:31:50 +00002715
Bob Wilson22cac0d2009-08-14 05:16:33 +00002716 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00002717}
2718
Bob Wilson5bafff32009-06-22 23:27:02 +00002719static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002720 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002721 DebugLoc dl = Op.getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00002722 SDValue Vec = Op.getOperand(0);
2723 SDValue Lane = Op.getOperand(1);
Bob Wilson934f98b2009-10-15 23:12:05 +00002724 assert(VT == MVT::i32 &&
2725 Vec.getValueType().getVectorElementType().getSizeInBits() < 32 &&
2726 "unexpected type for custom-lowering vector extract");
2727 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
Bob Wilson5bafff32009-06-22 23:27:02 +00002728}
2729
Bob Wilsona6d65862009-08-03 20:36:38 +00002730static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
2731 // The only time a CONCAT_VECTORS operation can have legal types is when
2732 // two 64-bit vectors are concatenated to a 128-bit vector.
2733 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
2734 "unexpected CONCAT_VECTORS");
2735 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00002736 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00002737 SDValue Op0 = Op.getOperand(0);
2738 SDValue Op1 = Op.getOperand(1);
2739 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00002740 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
2741 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00002742 DAG.getIntPtrConstant(0));
2743 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00002744 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
2745 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00002746 DAG.getIntPtrConstant(1));
2747 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00002748}
2749
Dan Gohman475871a2008-07-27 21:46:04 +00002750SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00002751 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002752 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00002753 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00002754 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002755 case ISD::GlobalAddress:
2756 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
2757 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002758 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002759 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, Subtarget);
2760 case ISD::BR_CC: return LowerBR_CC(Op, DAG, Subtarget);
2761 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Evan Cheng86198642009-08-07 00:34:42 +00002762 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002763 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
2764 case ISD::SINT_TO_FP:
2765 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
2766 case ISD::FP_TO_SINT:
2767 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
2768 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00002769 case ISD::RETURNADDR: break;
Jim Grosbach0e0da732009-05-12 23:59:14 +00002770 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002771 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002772 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00002773 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00002774 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00002775 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00002776 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
2777 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
2778 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2779 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00002780 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00002781 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002782 }
Dan Gohman475871a2008-07-27 21:46:04 +00002783 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00002784}
2785
Duncan Sands1607f052008-12-01 11:39:25 +00002786/// ReplaceNodeResults - Replace the results of node with an illegal result
2787/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00002788void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
2789 SmallVectorImpl<SDValue>&Results,
2790 SelectionDAG &DAG) {
Chris Lattner27a6c732007-11-24 07:07:01 +00002791 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00002792 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002793 llvm_unreachable("Don't know how to custom expand this!");
Duncan Sands1607f052008-12-01 11:39:25 +00002794 return;
2795 case ISD::BIT_CONVERT:
2796 Results.push_back(ExpandBIT_CONVERT(N, DAG));
2797 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00002798 case ISD::SRL:
Duncan Sands1607f052008-12-01 11:39:25 +00002799 case ISD::SRA: {
Bob Wilson5bafff32009-06-22 23:27:02 +00002800 SDValue Res = LowerShift(N, DAG, Subtarget);
Duncan Sands1607f052008-12-01 11:39:25 +00002801 if (Res.getNode())
2802 Results.push_back(Res);
2803 return;
2804 }
Chris Lattner27a6c732007-11-24 07:07:01 +00002805 }
2806}
Chris Lattner27a6c732007-11-24 07:07:01 +00002807
Evan Chenga8e29892007-01-19 07:51:42 +00002808//===----------------------------------------------------------------------===//
2809// ARM Scheduler Hooks
2810//===----------------------------------------------------------------------===//
2811
2812MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00002813ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +00002814 MachineBasicBlock *BB,
2815 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002816 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00002817 DebugLoc dl = MI->getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002818 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00002819 default:
2820 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng007ea272009-08-12 05:17:19 +00002821 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00002822 // To "insert" a SELECT_CC instruction, we actually have to insert the
2823 // diamond control-flow pattern. The incoming instruction knows the
2824 // destination vreg to set, the condition code register to branch on, the
2825 // true/false values to select between, and a branch opcode to use.
2826 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002827 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00002828 ++It;
2829
2830 // thisMBB:
2831 // ...
2832 // TrueVal = ...
2833 // cmpTY ccX, r1, r2
2834 // bCC copy1MBB
2835 // fallthrough --> copy0MBB
2836 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002837 MachineFunction *F = BB->getParent();
2838 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
2839 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenb6728402009-02-13 02:25:56 +00002840 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00002841 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002842 F->insert(It, copy0MBB);
2843 F->insert(It, sinkMBB);
Evan Chenga8e29892007-01-19 07:51:42 +00002844 // Update machine-CFG edges by first adding all successors of the current
2845 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00002846 // Also inform sdisel of the edge changes.
2847 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
2848 E = BB->succ_end(); I != E; ++I) {
2849 EM->insert(std::make_pair(*I, sinkMBB));
2850 sinkMBB->addSuccessor(*I);
2851 }
Evan Chenga8e29892007-01-19 07:51:42 +00002852 // Next, remove all successors of the current block, and add the true
2853 // and fallthrough blocks as its successors.
Evan Chengce319102009-09-19 09:51:03 +00002854 while (!BB->succ_empty())
Evan Chenga8e29892007-01-19 07:51:42 +00002855 BB->removeSuccessor(BB->succ_begin());
2856 BB->addSuccessor(copy0MBB);
2857 BB->addSuccessor(sinkMBB);
2858
2859 // copy0MBB:
2860 // %FalseValue = ...
2861 // # fallthrough to sinkMBB
2862 BB = copy0MBB;
2863
2864 // Update machine-CFG edges
2865 BB->addSuccessor(sinkMBB);
2866
2867 // sinkMBB:
2868 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2869 // ...
2870 BB = sinkMBB;
Dale Johannesenb6728402009-02-13 02:25:56 +00002871 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00002872 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
2873 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
2874
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002875 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00002876 return BB;
2877 }
Evan Cheng86198642009-08-07 00:34:42 +00002878
2879 case ARM::tANDsp:
2880 case ARM::tADDspr_:
2881 case ARM::tSUBspi_:
2882 case ARM::t2SUBrSPi_:
2883 case ARM::t2SUBrSPi12_:
2884 case ARM::t2SUBrSPs_: {
2885 MachineFunction *MF = BB->getParent();
2886 unsigned DstReg = MI->getOperand(0).getReg();
2887 unsigned SrcReg = MI->getOperand(1).getReg();
2888 bool DstIsDead = MI->getOperand(0).isDead();
2889 bool SrcIsKill = MI->getOperand(1).isKill();
2890
2891 if (SrcReg != ARM::SP) {
2892 // Copy the source to SP from virtual register.
2893 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(SrcReg);
2894 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
2895 ? ARM::tMOVtgpr2gpr : ARM::tMOVgpr2gpr;
2896 BuildMI(BB, dl, TII->get(CopyOpc), ARM::SP)
2897 .addReg(SrcReg, getKillRegState(SrcIsKill));
2898 }
2899
2900 unsigned OpOpc = 0;
2901 bool NeedPred = false, NeedCC = false, NeedOp3 = false;
2902 switch (MI->getOpcode()) {
2903 default:
2904 llvm_unreachable("Unexpected pseudo instruction!");
2905 case ARM::tANDsp:
2906 OpOpc = ARM::tAND;
2907 NeedPred = true;
2908 break;
2909 case ARM::tADDspr_:
2910 OpOpc = ARM::tADDspr;
2911 break;
2912 case ARM::tSUBspi_:
2913 OpOpc = ARM::tSUBspi;
2914 break;
2915 case ARM::t2SUBrSPi_:
2916 OpOpc = ARM::t2SUBrSPi;
2917 NeedPred = true; NeedCC = true;
2918 break;
2919 case ARM::t2SUBrSPi12_:
2920 OpOpc = ARM::t2SUBrSPi12;
2921 NeedPred = true;
2922 break;
2923 case ARM::t2SUBrSPs_:
2924 OpOpc = ARM::t2SUBrSPs;
2925 NeedPred = true; NeedCC = true; NeedOp3 = true;
2926 break;
2927 }
2928 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(OpOpc), ARM::SP);
2929 if (OpOpc == ARM::tAND)
2930 AddDefaultT1CC(MIB);
2931 MIB.addReg(ARM::SP);
2932 MIB.addOperand(MI->getOperand(2));
2933 if (NeedOp3)
2934 MIB.addOperand(MI->getOperand(3));
2935 if (NeedPred)
2936 AddDefaultPred(MIB);
2937 if (NeedCC)
2938 AddDefaultCC(MIB);
2939
2940 // Copy the result from SP to virtual register.
2941 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(DstReg);
2942 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
2943 ? ARM::tMOVgpr2tgpr : ARM::tMOVgpr2gpr;
2944 BuildMI(BB, dl, TII->get(CopyOpc))
2945 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
2946 .addReg(ARM::SP);
2947 MF->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
2948 return BB;
2949 }
Evan Chenga8e29892007-01-19 07:51:42 +00002950 }
2951}
2952
2953//===----------------------------------------------------------------------===//
2954// ARM Optimization Hooks
2955//===----------------------------------------------------------------------===//
2956
Chris Lattnerd1980a52009-03-12 06:52:53 +00002957static
2958SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
2959 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00002960 SelectionDAG &DAG = DCI.DAG;
2961 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00002962 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00002963 unsigned Opc = N->getOpcode();
2964 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
2965 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
2966 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
2967 ISD::CondCode CC = ISD::SETCC_INVALID;
2968
2969 if (isSlctCC) {
2970 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
2971 } else {
2972 SDValue CCOp = Slct.getOperand(0);
2973 if (CCOp.getOpcode() == ISD::SETCC)
2974 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
2975 }
2976
2977 bool DoXform = false;
2978 bool InvCC = false;
2979 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
2980 "Bad input!");
2981
2982 if (LHS.getOpcode() == ISD::Constant &&
2983 cast<ConstantSDNode>(LHS)->isNullValue()) {
2984 DoXform = true;
2985 } else if (CC != ISD::SETCC_INVALID &&
2986 RHS.getOpcode() == ISD::Constant &&
2987 cast<ConstantSDNode>(RHS)->isNullValue()) {
2988 std::swap(LHS, RHS);
2989 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00002990 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00002991 Op0.getOperand(0).getValueType();
2992 bool isInt = OpVT.isInteger();
2993 CC = ISD::getSetCCInverse(CC, isInt);
2994
2995 if (!TLI.isCondCodeLegal(CC, OpVT))
2996 return SDValue(); // Inverse operator isn't legal.
2997
2998 DoXform = true;
2999 InvCC = true;
3000 }
3001
3002 if (DoXform) {
3003 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
3004 if (isSlctCC)
3005 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
3006 Slct.getOperand(0), Slct.getOperand(1), CC);
3007 SDValue CCOp = Slct.getOperand(0);
3008 if (InvCC)
3009 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
3010 CCOp.getOperand(0), CCOp.getOperand(1), CC);
3011 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
3012 CCOp, OtherOp, Result);
3013 }
3014 return SDValue();
3015}
3016
3017/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
3018static SDValue PerformADDCombine(SDNode *N,
3019 TargetLowering::DAGCombinerInfo &DCI) {
3020 // added by evan in r37685 with no testcase.
3021 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003022
Chris Lattnerd1980a52009-03-12 06:52:53 +00003023 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
3024 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
3025 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
3026 if (Result.getNode()) return Result;
3027 }
3028 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3029 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3030 if (Result.getNode()) return Result;
3031 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003032
Chris Lattnerd1980a52009-03-12 06:52:53 +00003033 return SDValue();
3034}
3035
3036/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
3037static SDValue PerformSUBCombine(SDNode *N,
3038 TargetLowering::DAGCombinerInfo &DCI) {
3039 // added by evan in r37685 with no testcase.
3040 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003041
Chris Lattnerd1980a52009-03-12 06:52:53 +00003042 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
3043 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3044 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3045 if (Result.getNode()) return Result;
3046 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003047
Chris Lattnerd1980a52009-03-12 06:52:53 +00003048 return SDValue();
3049}
3050
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003051/// PerformFMRRDCombine - Target-specific dag combine xforms for ARMISD::FMRRD.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003052static SDValue PerformFMRRDCombine(SDNode *N,
3053 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003054 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00003055 SDValue InDouble = N->getOperand(0);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003056 if (InDouble.getOpcode() == ARMISD::FMDRR)
3057 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003058 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003059}
3060
Bob Wilson5bafff32009-06-22 23:27:02 +00003061/// getVShiftImm - Check if this is a valid build_vector for the immediate
3062/// operand of a vector shift operation, where all the elements of the
3063/// build_vector must have the same constant integer value.
3064static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
3065 // Ignore bit_converts.
3066 while (Op.getOpcode() == ISD::BIT_CONVERT)
3067 Op = Op.getOperand(0);
3068 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
3069 APInt SplatBits, SplatUndef;
3070 unsigned SplatBitSize;
3071 bool HasAnyUndefs;
3072 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
3073 HasAnyUndefs, ElementBits) ||
3074 SplatBitSize > ElementBits)
3075 return false;
3076 Cnt = SplatBits.getSExtValue();
3077 return true;
3078}
3079
3080/// isVShiftLImm - Check if this is a valid build_vector for the immediate
3081/// operand of a vector shift left operation. That value must be in the range:
3082/// 0 <= Value < ElementBits for a left shift; or
3083/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003084static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003085 assert(VT.isVector() && "vector shift count is not a vector type");
3086 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3087 if (! getVShiftImm(Op, ElementBits, Cnt))
3088 return false;
3089 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
3090}
3091
3092/// isVShiftRImm - Check if this is a valid build_vector for the immediate
3093/// operand of a vector shift right operation. For a shift opcode, the value
3094/// is positive, but for an intrinsic the value count must be negative. The
3095/// absolute value must be in the range:
3096/// 1 <= |Value| <= ElementBits for a right shift; or
3097/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003098static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00003099 int64_t &Cnt) {
3100 assert(VT.isVector() && "vector shift count is not a vector type");
3101 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3102 if (! getVShiftImm(Op, ElementBits, Cnt))
3103 return false;
3104 if (isIntrinsic)
3105 Cnt = -Cnt;
3106 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
3107}
3108
3109/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
3110static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
3111 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
3112 switch (IntNo) {
3113 default:
3114 // Don't do anything for most intrinsics.
3115 break;
3116
3117 // Vector shifts: check for immediate versions and lower them.
3118 // Note: This is done during DAG combining instead of DAG legalizing because
3119 // the build_vectors for 64-bit vector element shift counts are generally
3120 // not legal, and it is hard to see their values after they get legalized to
3121 // loads from a constant pool.
3122 case Intrinsic::arm_neon_vshifts:
3123 case Intrinsic::arm_neon_vshiftu:
3124 case Intrinsic::arm_neon_vshiftls:
3125 case Intrinsic::arm_neon_vshiftlu:
3126 case Intrinsic::arm_neon_vshiftn:
3127 case Intrinsic::arm_neon_vrshifts:
3128 case Intrinsic::arm_neon_vrshiftu:
3129 case Intrinsic::arm_neon_vrshiftn:
3130 case Intrinsic::arm_neon_vqshifts:
3131 case Intrinsic::arm_neon_vqshiftu:
3132 case Intrinsic::arm_neon_vqshiftsu:
3133 case Intrinsic::arm_neon_vqshiftns:
3134 case Intrinsic::arm_neon_vqshiftnu:
3135 case Intrinsic::arm_neon_vqshiftnsu:
3136 case Intrinsic::arm_neon_vqrshiftns:
3137 case Intrinsic::arm_neon_vqrshiftnu:
3138 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00003139 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003140 int64_t Cnt;
3141 unsigned VShiftOpc = 0;
3142
3143 switch (IntNo) {
3144 case Intrinsic::arm_neon_vshifts:
3145 case Intrinsic::arm_neon_vshiftu:
3146 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
3147 VShiftOpc = ARMISD::VSHL;
3148 break;
3149 }
3150 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
3151 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
3152 ARMISD::VSHRs : ARMISD::VSHRu);
3153 break;
3154 }
3155 return SDValue();
3156
3157 case Intrinsic::arm_neon_vshiftls:
3158 case Intrinsic::arm_neon_vshiftlu:
3159 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
3160 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003161 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003162
3163 case Intrinsic::arm_neon_vrshifts:
3164 case Intrinsic::arm_neon_vrshiftu:
3165 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
3166 break;
3167 return SDValue();
3168
3169 case Intrinsic::arm_neon_vqshifts:
3170 case Intrinsic::arm_neon_vqshiftu:
3171 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3172 break;
3173 return SDValue();
3174
3175 case Intrinsic::arm_neon_vqshiftsu:
3176 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3177 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003178 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003179
3180 case Intrinsic::arm_neon_vshiftn:
3181 case Intrinsic::arm_neon_vrshiftn:
3182 case Intrinsic::arm_neon_vqshiftns:
3183 case Intrinsic::arm_neon_vqshiftnu:
3184 case Intrinsic::arm_neon_vqshiftnsu:
3185 case Intrinsic::arm_neon_vqrshiftns:
3186 case Intrinsic::arm_neon_vqrshiftnu:
3187 case Intrinsic::arm_neon_vqrshiftnsu:
3188 // Narrowing shifts require an immediate right shift.
3189 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
3190 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003191 llvm_unreachable("invalid shift count for narrowing vector shift intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003192
3193 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003194 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003195 }
3196
3197 switch (IntNo) {
3198 case Intrinsic::arm_neon_vshifts:
3199 case Intrinsic::arm_neon_vshiftu:
3200 // Opcode already set above.
3201 break;
3202 case Intrinsic::arm_neon_vshiftls:
3203 case Intrinsic::arm_neon_vshiftlu:
3204 if (Cnt == VT.getVectorElementType().getSizeInBits())
3205 VShiftOpc = ARMISD::VSHLLi;
3206 else
3207 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
3208 ARMISD::VSHLLs : ARMISD::VSHLLu);
3209 break;
3210 case Intrinsic::arm_neon_vshiftn:
3211 VShiftOpc = ARMISD::VSHRN; break;
3212 case Intrinsic::arm_neon_vrshifts:
3213 VShiftOpc = ARMISD::VRSHRs; break;
3214 case Intrinsic::arm_neon_vrshiftu:
3215 VShiftOpc = ARMISD::VRSHRu; break;
3216 case Intrinsic::arm_neon_vrshiftn:
3217 VShiftOpc = ARMISD::VRSHRN; break;
3218 case Intrinsic::arm_neon_vqshifts:
3219 VShiftOpc = ARMISD::VQSHLs; break;
3220 case Intrinsic::arm_neon_vqshiftu:
3221 VShiftOpc = ARMISD::VQSHLu; break;
3222 case Intrinsic::arm_neon_vqshiftsu:
3223 VShiftOpc = ARMISD::VQSHLsu; break;
3224 case Intrinsic::arm_neon_vqshiftns:
3225 VShiftOpc = ARMISD::VQSHRNs; break;
3226 case Intrinsic::arm_neon_vqshiftnu:
3227 VShiftOpc = ARMISD::VQSHRNu; break;
3228 case Intrinsic::arm_neon_vqshiftnsu:
3229 VShiftOpc = ARMISD::VQSHRNsu; break;
3230 case Intrinsic::arm_neon_vqrshiftns:
3231 VShiftOpc = ARMISD::VQRSHRNs; break;
3232 case Intrinsic::arm_neon_vqrshiftnu:
3233 VShiftOpc = ARMISD::VQRSHRNu; break;
3234 case Intrinsic::arm_neon_vqrshiftnsu:
3235 VShiftOpc = ARMISD::VQRSHRNsu; break;
3236 }
3237
3238 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003239 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003240 }
3241
3242 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00003243 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003244 int64_t Cnt;
3245 unsigned VShiftOpc = 0;
3246
3247 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
3248 VShiftOpc = ARMISD::VSLI;
3249 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
3250 VShiftOpc = ARMISD::VSRI;
3251 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00003252 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003253 }
3254
3255 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
3256 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00003257 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003258 }
3259
3260 case Intrinsic::arm_neon_vqrshifts:
3261 case Intrinsic::arm_neon_vqrshiftu:
3262 // No immediate versions of these to check for.
3263 break;
3264 }
3265
3266 return SDValue();
3267}
3268
3269/// PerformShiftCombine - Checks for immediate versions of vector shifts and
3270/// lowers them. As with the vector shift intrinsics, this is done during DAG
3271/// combining instead of DAG legalizing because the build_vectors for 64-bit
3272/// vector element shift counts are generally not legal, and it is hard to see
3273/// their values after they get legalized to loads from a constant pool.
3274static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
3275 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003276 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003277
3278 // Nothing to be done for scalar shifts.
3279 if (! VT.isVector())
3280 return SDValue();
3281
3282 assert(ST->hasNEON() && "unexpected vector shift");
3283 int64_t Cnt;
3284
3285 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003286 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003287
3288 case ISD::SHL:
3289 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
3290 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003291 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003292 break;
3293
3294 case ISD::SRA:
3295 case ISD::SRL:
3296 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
3297 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
3298 ARMISD::VSHRs : ARMISD::VSHRu);
3299 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003300 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003301 }
3302 }
3303 return SDValue();
3304}
3305
3306/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
3307/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
3308static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
3309 const ARMSubtarget *ST) {
3310 SDValue N0 = N->getOperand(0);
3311
3312 // Check for sign- and zero-extensions of vector extract operations of 8-
3313 // and 16-bit vector elements. NEON supports these directly. They are
3314 // handled during DAG combining because type legalization will promote them
3315 // to 32-bit types and it is messy to recognize the operations after that.
3316 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
3317 SDValue Vec = N0.getOperand(0);
3318 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00003319 EVT VT = N->getValueType(0);
3320 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003321 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3322
Owen Anderson825b72b2009-08-11 20:47:22 +00003323 if (VT == MVT::i32 &&
3324 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00003325 TLI.isTypeLegal(Vec.getValueType())) {
3326
3327 unsigned Opc = 0;
3328 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003329 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003330 case ISD::SIGN_EXTEND:
3331 Opc = ARMISD::VGETLANEs;
3332 break;
3333 case ISD::ZERO_EXTEND:
3334 case ISD::ANY_EXTEND:
3335 Opc = ARMISD::VGETLANEu;
3336 break;
3337 }
3338 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
3339 }
3340 }
3341
3342 return SDValue();
3343}
3344
Dan Gohman475871a2008-07-27 21:46:04 +00003345SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003346 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003347 switch (N->getOpcode()) {
3348 default: break;
Chris Lattnerd1980a52009-03-12 06:52:53 +00003349 case ISD::ADD: return PerformADDCombine(N, DCI);
3350 case ISD::SUB: return PerformSUBCombine(N, DCI);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003351 case ARMISD::FMRRD: return PerformFMRRDCombine(N, DCI);
Bob Wilson5bafff32009-06-22 23:27:02 +00003352 case ISD::INTRINSIC_WO_CHAIN:
3353 return PerformIntrinsicCombine(N, DCI.DAG);
3354 case ISD::SHL:
3355 case ISD::SRA:
3356 case ISD::SRL:
3357 return PerformShiftCombine(N, DCI.DAG, Subtarget);
3358 case ISD::SIGN_EXTEND:
3359 case ISD::ZERO_EXTEND:
3360 case ISD::ANY_EXTEND:
3361 return PerformExtendCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003362 }
Dan Gohman475871a2008-07-27 21:46:04 +00003363 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003364}
3365
Bill Wendlingaf566342009-08-15 21:21:19 +00003366bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
3367 if (!Subtarget->hasV6Ops())
3368 // Pre-v6 does not support unaligned mem access.
3369 return false;
3370 else if (!Subtarget->hasV6Ops()) {
3371 // v6 may or may not support unaligned mem access.
3372 if (!Subtarget->isTargetDarwin())
3373 return false;
3374 }
3375
3376 switch (VT.getSimpleVT().SimpleTy) {
3377 default:
3378 return false;
3379 case MVT::i8:
3380 case MVT::i16:
3381 case MVT::i32:
3382 return true;
3383 // FIXME: VLD1 etc with standard alignment is legal.
3384 }
3385}
3386
Evan Chenge6c835f2009-08-14 20:09:37 +00003387static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
3388 if (V < 0)
3389 return false;
3390
3391 unsigned Scale = 1;
3392 switch (VT.getSimpleVT().SimpleTy) {
3393 default: return false;
3394 case MVT::i1:
3395 case MVT::i8:
3396 // Scale == 1;
3397 break;
3398 case MVT::i16:
3399 // Scale == 2;
3400 Scale = 2;
3401 break;
3402 case MVT::i32:
3403 // Scale == 4;
3404 Scale = 4;
3405 break;
3406 }
3407
3408 if ((V & (Scale - 1)) != 0)
3409 return false;
3410 V /= Scale;
3411 return V == (V & ((1LL << 5) - 1));
3412}
3413
3414static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
3415 const ARMSubtarget *Subtarget) {
3416 bool isNeg = false;
3417 if (V < 0) {
3418 isNeg = true;
3419 V = - V;
3420 }
3421
3422 switch (VT.getSimpleVT().SimpleTy) {
3423 default: return false;
3424 case MVT::i1:
3425 case MVT::i8:
3426 case MVT::i16:
3427 case MVT::i32:
3428 // + imm12 or - imm8
3429 if (isNeg)
3430 return V == (V & ((1LL << 8) - 1));
3431 return V == (V & ((1LL << 12) - 1));
3432 case MVT::f32:
3433 case MVT::f64:
3434 // Same as ARM mode. FIXME: NEON?
3435 if (!Subtarget->hasVFP2())
3436 return false;
3437 if ((V & 3) != 0)
3438 return false;
3439 V >>= 2;
3440 return V == (V & ((1LL << 8) - 1));
3441 }
3442}
3443
Evan Chengb01fad62007-03-12 23:30:29 +00003444/// isLegalAddressImmediate - Return true if the integer value can be used
3445/// as the offset of the target addressing mode for load / store of the
3446/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00003447static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00003448 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00003449 if (V == 0)
3450 return true;
3451
Evan Cheng65011532009-03-09 19:15:00 +00003452 if (!VT.isSimple())
3453 return false;
3454
Evan Chenge6c835f2009-08-14 20:09:37 +00003455 if (Subtarget->isThumb1Only())
3456 return isLegalT1AddressImmediate(V, VT);
3457 else if (Subtarget->isThumb2())
3458 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00003459
Evan Chenge6c835f2009-08-14 20:09:37 +00003460 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00003461 if (V < 0)
3462 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00003463 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00003464 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00003465 case MVT::i1:
3466 case MVT::i8:
3467 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00003468 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003469 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003470 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00003471 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003472 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003473 case MVT::f32:
3474 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00003475 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00003476 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00003477 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00003478 return false;
3479 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003480 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00003481 }
Evan Chenga8e29892007-01-19 07:51:42 +00003482}
3483
Evan Chenge6c835f2009-08-14 20:09:37 +00003484bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
3485 EVT VT) const {
3486 int Scale = AM.Scale;
3487 if (Scale < 0)
3488 return false;
3489
3490 switch (VT.getSimpleVT().SimpleTy) {
3491 default: return false;
3492 case MVT::i1:
3493 case MVT::i8:
3494 case MVT::i16:
3495 case MVT::i32:
3496 if (Scale == 1)
3497 return true;
3498 // r + r << imm
3499 Scale = Scale & ~1;
3500 return Scale == 2 || Scale == 4 || Scale == 8;
3501 case MVT::i64:
3502 // r + r
3503 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
3504 return true;
3505 return false;
3506 case MVT::isVoid:
3507 // Note, we allow "void" uses (basically, uses that aren't loads or
3508 // stores), because arm allows folding a scale into many arithmetic
3509 // operations. This should be made more precise and revisited later.
3510
3511 // Allow r << imm, but the imm has to be a multiple of two.
3512 if (Scale & 1) return false;
3513 return isPowerOf2_32(Scale);
3514 }
3515}
3516
Chris Lattner37caf8c2007-04-09 23:33:39 +00003517/// isLegalAddressingMode - Return true if the addressing mode represented
3518/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003519bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00003520 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00003521 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00003522 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00003523 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003524
Chris Lattner37caf8c2007-04-09 23:33:39 +00003525 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003526 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003527 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003528
Chris Lattner37caf8c2007-04-09 23:33:39 +00003529 switch (AM.Scale) {
3530 case 0: // no scale reg, must be "r+i" or "r", or "i".
3531 break;
3532 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00003533 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00003534 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00003535 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00003536 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00003537 // ARM doesn't support any R+R*scale+imm addr modes.
3538 if (AM.BaseOffs)
3539 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003540
Bob Wilson2c7dab12009-04-08 17:55:28 +00003541 if (!VT.isSimple())
3542 return false;
3543
Evan Chenge6c835f2009-08-14 20:09:37 +00003544 if (Subtarget->isThumb2())
3545 return isLegalT2ScaledAddressingMode(AM, VT);
3546
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003547 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00003548 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00003549 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00003550 case MVT::i1:
3551 case MVT::i8:
3552 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003553 if (Scale < 0) Scale = -Scale;
3554 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003555 return true;
3556 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00003557 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003558 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00003559 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00003560 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003561 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003562 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00003563 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003564
Owen Anderson825b72b2009-08-11 20:47:22 +00003565 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00003566 // Note, we allow "void" uses (basically, uses that aren't loads or
3567 // stores), because arm allows folding a scale into many arithmetic
3568 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003569
Chris Lattner37caf8c2007-04-09 23:33:39 +00003570 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00003571 if (Scale & 1) return false;
3572 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00003573 }
3574 break;
Evan Chengb01fad62007-03-12 23:30:29 +00003575 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00003576 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00003577}
3578
Owen Andersone50ed302009-08-10 22:56:29 +00003579static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00003580 bool isSEXTLoad, SDValue &Base,
3581 SDValue &Offset, bool &isInc,
3582 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00003583 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
3584 return false;
3585
Owen Anderson825b72b2009-08-11 20:47:22 +00003586 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00003587 // AddressingMode 3
3588 Base = Ptr->getOperand(0);
3589 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003590 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003591 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003592 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00003593 isInc = false;
3594 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3595 return true;
3596 }
3597 }
3598 isInc = (Ptr->getOpcode() == ISD::ADD);
3599 Offset = Ptr->getOperand(1);
3600 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00003601 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00003602 // AddressingMode 2
3603 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003604 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003605 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003606 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00003607 isInc = false;
3608 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3609 Base = Ptr->getOperand(0);
3610 return true;
3611 }
3612 }
3613
3614 if (Ptr->getOpcode() == ISD::ADD) {
3615 isInc = true;
3616 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
3617 if (ShOpcVal != ARM_AM::no_shift) {
3618 Base = Ptr->getOperand(1);
3619 Offset = Ptr->getOperand(0);
3620 } else {
3621 Base = Ptr->getOperand(0);
3622 Offset = Ptr->getOperand(1);
3623 }
3624 return true;
3625 }
3626
3627 isInc = (Ptr->getOpcode() == ISD::ADD);
3628 Base = Ptr->getOperand(0);
3629 Offset = Ptr->getOperand(1);
3630 return true;
3631 }
3632
3633 // FIXME: Use FLDM / FSTM to emulate indexed FP load / store.
3634 return false;
3635}
3636
Owen Andersone50ed302009-08-10 22:56:29 +00003637static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00003638 bool isSEXTLoad, SDValue &Base,
3639 SDValue &Offset, bool &isInc,
3640 SelectionDAG &DAG) {
3641 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
3642 return false;
3643
3644 Base = Ptr->getOperand(0);
3645 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
3646 int RHSC = (int)RHS->getZExtValue();
3647 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
3648 assert(Ptr->getOpcode() == ISD::ADD);
3649 isInc = false;
3650 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3651 return true;
3652 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
3653 isInc = Ptr->getOpcode() == ISD::ADD;
3654 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
3655 return true;
3656 }
3657 }
3658
3659 return false;
3660}
3661
Evan Chenga8e29892007-01-19 07:51:42 +00003662/// getPreIndexedAddressParts - returns true by value, base pointer and
3663/// offset pointer and addressing mode by reference if the node's address
3664/// can be legally represented as pre-indexed load / store address.
3665bool
Dan Gohman475871a2008-07-27 21:46:04 +00003666ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
3667 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003668 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00003669 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003670 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00003671 return false;
3672
Owen Andersone50ed302009-08-10 22:56:29 +00003673 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00003674 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00003675 bool isSEXTLoad = false;
3676 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
3677 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003678 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003679 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
3680 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
3681 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003682 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003683 } else
3684 return false;
3685
3686 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00003687 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00003688 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00003689 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
3690 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00003691 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00003692 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00003693 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00003694 if (!isLegal)
3695 return false;
3696
3697 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
3698 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00003699}
3700
3701/// getPostIndexedAddressParts - returns true by value, base pointer and
3702/// offset pointer and addressing mode by reference if this node can be
3703/// combined with a load / store to form a post-indexed load / store.
3704bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00003705 SDValue &Base,
3706 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003707 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00003708 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003709 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00003710 return false;
3711
Owen Andersone50ed302009-08-10 22:56:29 +00003712 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00003713 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00003714 bool isSEXTLoad = false;
3715 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003716 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003717 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
3718 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003719 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003720 } else
3721 return false;
3722
3723 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00003724 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00003725 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00003726 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003727 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00003728 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00003729 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
3730 isInc, DAG);
3731 if (!isLegal)
3732 return false;
3733
3734 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
3735 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00003736}
3737
Dan Gohman475871a2008-07-27 21:46:04 +00003738void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00003739 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003740 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003741 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00003742 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00003743 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003744 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00003745 switch (Op.getOpcode()) {
3746 default: break;
3747 case ARMISD::CMOV: {
3748 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00003749 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00003750 if (KnownZero == 0 && KnownOne == 0) return;
3751
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003752 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00003753 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
3754 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00003755 KnownZero &= KnownZeroRHS;
3756 KnownOne &= KnownOneRHS;
3757 return;
3758 }
3759 }
3760}
3761
3762//===----------------------------------------------------------------------===//
3763// ARM Inline Assembly Support
3764//===----------------------------------------------------------------------===//
3765
3766/// getConstraintType - Given a constraint letter, return the type of
3767/// constraint it is for this target.
3768ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00003769ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
3770 if (Constraint.size() == 1) {
3771 switch (Constraint[0]) {
3772 default: break;
3773 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003774 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00003775 }
Evan Chenga8e29892007-01-19 07:51:42 +00003776 }
Chris Lattner4234f572007-03-25 02:14:49 +00003777 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00003778}
3779
Bob Wilson2dc4f542009-03-20 22:42:55 +00003780std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00003781ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00003782 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003783 if (Constraint.size() == 1) {
3784 // GCC RS6000 Constraint Letters
3785 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003786 case 'l':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003787 if (Subtarget->isThumb1Only())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00003788 return std::make_pair(0U, ARM::tGPRRegisterClass);
3789 else
3790 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003791 case 'r':
3792 return std::make_pair(0U, ARM::GPRRegisterClass);
3793 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00003794 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003795 return std::make_pair(0U, ARM::SPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00003796 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003797 return std::make_pair(0U, ARM::DPRRegisterClass);
3798 break;
Evan Chenga8e29892007-01-19 07:51:42 +00003799 }
3800 }
3801 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3802}
3803
3804std::vector<unsigned> ARMTargetLowering::
3805getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00003806 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003807 if (Constraint.size() != 1)
3808 return std::vector<unsigned>();
3809
3810 switch (Constraint[0]) { // GCC ARM Constraint Letters
3811 default: break;
3812 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00003813 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
3814 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
3815 0);
Evan Chenga8e29892007-01-19 07:51:42 +00003816 case 'r':
3817 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
3818 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
3819 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
3820 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003821 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00003822 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003823 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
3824 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
3825 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
3826 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
3827 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
3828 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
3829 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
3830 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00003831 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003832 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
3833 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
3834 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
3835 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
3836 break;
Evan Chenga8e29892007-01-19 07:51:42 +00003837 }
3838
3839 return std::vector<unsigned>();
3840}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003841
3842/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3843/// vector. If it is invalid, don't add anything to Ops.
3844void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3845 char Constraint,
3846 bool hasMemory,
3847 std::vector<SDValue>&Ops,
3848 SelectionDAG &DAG) const {
3849 SDValue Result(0, 0);
3850
3851 switch (Constraint) {
3852 default: break;
3853 case 'I': case 'J': case 'K': case 'L':
3854 case 'M': case 'N': case 'O':
3855 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
3856 if (!C)
3857 return;
3858
3859 int64_t CVal64 = C->getSExtValue();
3860 int CVal = (int) CVal64;
3861 // None of these constraints allow values larger than 32 bits. Check
3862 // that the value fits in an int.
3863 if (CVal != CVal64)
3864 return;
3865
3866 switch (Constraint) {
3867 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003868 if (Subtarget->isThumb1Only()) {
3869 // This must be a constant between 0 and 255, for ADD
3870 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003871 if (CVal >= 0 && CVal <= 255)
3872 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00003873 } else if (Subtarget->isThumb2()) {
3874 // A constant that can be used as an immediate value in a
3875 // data-processing instruction.
3876 if (ARM_AM::getT2SOImmVal(CVal) != -1)
3877 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003878 } else {
3879 // A constant that can be used as an immediate value in a
3880 // data-processing instruction.
3881 if (ARM_AM::getSOImmVal(CVal) != -1)
3882 break;
3883 }
3884 return;
3885
3886 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003887 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003888 // This must be a constant between -255 and -1, for negated ADD
3889 // immediates. This can be used in GCC with an "n" modifier that
3890 // prints the negated value, for use with SUB instructions. It is
3891 // not useful otherwise but is implemented for compatibility.
3892 if (CVal >= -255 && CVal <= -1)
3893 break;
3894 } else {
3895 // This must be a constant between -4095 and 4095. It is not clear
3896 // what this constraint is intended for. Implemented for
3897 // compatibility with GCC.
3898 if (CVal >= -4095 && CVal <= 4095)
3899 break;
3900 }
3901 return;
3902
3903 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003904 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003905 // A 32-bit value where only one byte has a nonzero value. Exclude
3906 // zero to match GCC. This constraint is used by GCC internally for
3907 // constants that can be loaded with a move/shift combination.
3908 // It is not useful otherwise but is implemented for compatibility.
3909 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
3910 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00003911 } else if (Subtarget->isThumb2()) {
3912 // A constant whose bitwise inverse can be used as an immediate
3913 // value in a data-processing instruction. This can be used in GCC
3914 // with a "B" modifier that prints the inverted value, for use with
3915 // BIC and MVN instructions. It is not useful otherwise but is
3916 // implemented for compatibility.
3917 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
3918 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003919 } else {
3920 // A constant whose bitwise inverse can be used as an immediate
3921 // value in a data-processing instruction. This can be used in GCC
3922 // with a "B" modifier that prints the inverted value, for use with
3923 // BIC and MVN instructions. It is not useful otherwise but is
3924 // implemented for compatibility.
3925 if (ARM_AM::getSOImmVal(~CVal) != -1)
3926 break;
3927 }
3928 return;
3929
3930 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003931 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003932 // This must be a constant between -7 and 7,
3933 // for 3-operand ADD/SUB immediate instructions.
3934 if (CVal >= -7 && CVal < 7)
3935 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00003936 } else if (Subtarget->isThumb2()) {
3937 // A constant whose negation can be used as an immediate value in a
3938 // data-processing instruction. This can be used in GCC with an "n"
3939 // modifier that prints the negated value, for use with SUB
3940 // instructions. It is not useful otherwise but is implemented for
3941 // compatibility.
3942 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
3943 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003944 } else {
3945 // A constant whose negation can be used as an immediate value in a
3946 // data-processing instruction. This can be used in GCC with an "n"
3947 // modifier that prints the negated value, for use with SUB
3948 // instructions. It is not useful otherwise but is implemented for
3949 // compatibility.
3950 if (ARM_AM::getSOImmVal(-CVal) != -1)
3951 break;
3952 }
3953 return;
3954
3955 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003956 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003957 // This must be a multiple of 4 between 0 and 1020, for
3958 // ADD sp + immediate.
3959 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
3960 break;
3961 } else {
3962 // A power of two or a constant between 0 and 32. This is used in
3963 // GCC for the shift amount on shifted register operands, but it is
3964 // useful in general for any shift amounts.
3965 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
3966 break;
3967 }
3968 return;
3969
3970 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003971 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003972 // This must be a constant between 0 and 31, for shift amounts.
3973 if (CVal >= 0 && CVal <= 31)
3974 break;
3975 }
3976 return;
3977
3978 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003979 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003980 // This must be a multiple of 4 between -508 and 508, for
3981 // ADD/SUB sp = sp + immediate.
3982 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
3983 break;
3984 }
3985 return;
3986 }
3987 Result = DAG.getTargetConstant(CVal, Op.getValueType());
3988 break;
3989 }
3990
3991 if (Result.getNode()) {
3992 Ops.push_back(Result);
3993 return;
3994 }
3995 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
3996 Ops, DAG);
3997}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00003998
3999bool
4000ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
4001 // The ARM target isn't yet aware of offsets.
4002 return false;
4003}
Evan Cheng39382422009-10-28 01:44:26 +00004004
4005int ARM::getVFPf32Imm(const APFloat &FPImm) {
4006 APInt Imm = FPImm.bitcastToAPInt();
4007 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
4008 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
4009 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
4010
4011 // We can handle 4 bits of mantissa.
4012 // mantissa = (16+UInt(e:f:g:h))/16.
4013 if (Mantissa & 0x7ffff)
4014 return -1;
4015 Mantissa >>= 19;
4016 if ((Mantissa & 0xf) != Mantissa)
4017 return -1;
4018
4019 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4020 if (Exp < -3 || Exp > 4)
4021 return -1;
4022 Exp = ((Exp+3) & 0x7) ^ 4;
4023
4024 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4025}
4026
4027int ARM::getVFPf64Imm(const APFloat &FPImm) {
4028 APInt Imm = FPImm.bitcastToAPInt();
4029 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
4030 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
4031 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
4032
4033 // We can handle 4 bits of mantissa.
4034 // mantissa = (16+UInt(e:f:g:h))/16.
4035 if (Mantissa & 0xffffffffffffLL)
4036 return -1;
4037 Mantissa >>= 48;
4038 if ((Mantissa & 0xf) != Mantissa)
4039 return -1;
4040
4041 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4042 if (Exp < -3 || Exp > 4)
4043 return -1;
4044 Exp = ((Exp+3) & 0x7) ^ 4;
4045
4046 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4047}
4048
4049/// isFPImmLegal - Returns true if the target can instruction select the
4050/// specified FP immediate natively. If false, the legalizer will
4051/// materialize the FP immediate as a load from a constant pool.
4052bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
4053 if (!Subtarget->hasVFP3())
4054 return false;
4055 if (VT == MVT::f32)
4056 return ARM::getVFPf32Imm(Imm) != -1;
4057 if (VT == MVT::f64)
4058 return ARM::getVFPf64Imm(Imm) != -1;
4059 return false;
4060}