blob: 6a264fdfc44f5ef48e8122e8c0f2f98be99cadd6 [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);
Evan Chenga8e29892007-01-19 07:51:42 +0000366
Evan Chenga8e29892007-01-19 07:51:42 +0000367 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000368 setOperationAction(ISD::VASTART, MVT::Other, Custom);
369 setOperationAction(ISD::VAARG, MVT::Other, Expand);
370 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
371 setOperationAction(ISD::VAEND, MVT::Other, Expand);
372 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
373 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000374 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
375 // FIXME: Shouldn't need this, since no register is used, but the legalizer
376 // doesn't yet know how to not do that for SjLj.
377 setExceptionSelectorRegister(ARM::R0);
Evan Cheng86198642009-08-07 00:34:42 +0000378 if (Subtarget->isThumb())
Owen Anderson825b72b2009-08-11 20:47:22 +0000379 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Evan Cheng86198642009-08-07 00:34:42 +0000380 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000381 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
382 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000383
Evan Chengd27c9fc2009-07-03 01:43:10 +0000384 if (!Subtarget->hasV6Ops() && !Subtarget->isThumb2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000385 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
386 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000387 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000388 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000389
David Goodwinf1daf7d2009-07-08 23:10:31 +0000390 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only())
Evan Chengc7c77292008-11-04 19:57:48 +0000391 // Turn f64->i64 into FMRRD, i64 -> f64 to FMDRR iff target supports vfp2.
Owen Anderson825b72b2009-08-11 20:47:22 +0000392 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000393
394 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000395 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000396
Owen Anderson825b72b2009-08-11 20:47:22 +0000397 setOperationAction(ISD::SETCC, MVT::i32, Expand);
398 setOperationAction(ISD::SETCC, MVT::f32, Expand);
399 setOperationAction(ISD::SETCC, MVT::f64, Expand);
400 setOperationAction(ISD::SELECT, MVT::i32, Expand);
401 setOperationAction(ISD::SELECT, MVT::f32, Expand);
402 setOperationAction(ISD::SELECT, MVT::f64, Expand);
403 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
404 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
405 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000406
Owen Anderson825b72b2009-08-11 20:47:22 +0000407 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
408 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
409 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
410 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
411 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000412
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000413 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000414 setOperationAction(ISD::FSIN, MVT::f64, Expand);
415 setOperationAction(ISD::FSIN, MVT::f32, Expand);
416 setOperationAction(ISD::FCOS, MVT::f32, Expand);
417 setOperationAction(ISD::FCOS, MVT::f64, Expand);
418 setOperationAction(ISD::FREM, MVT::f64, Expand);
419 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000420 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000421 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
422 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000423 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000424 setOperationAction(ISD::FPOW, MVT::f64, Expand);
425 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000426
Evan Chenga8e29892007-01-19 07:51:42 +0000427 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
David Goodwinf1daf7d2009-07-08 23:10:31 +0000428 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000429 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
430 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
431 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
432 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000433 }
Evan Chenga8e29892007-01-19 07:51:42 +0000434
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000435 // We have target-specific dag combine patterns for the following nodes:
436 // ARMISD::FMRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000437 setTargetDAGCombine(ISD::ADD);
438 setTargetDAGCombine(ISD::SUB);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000439
Evan Chenga8e29892007-01-19 07:51:42 +0000440 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Chenga8e29892007-01-19 07:51:42 +0000441 setSchedulingPreference(SchedulingForRegPressure);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000442
Evan Chengbc9b7542009-08-15 07:59:10 +0000443 // FIXME: If-converter should use instruction latency to determine
444 // profitability rather than relying on fixed limits.
445 if (Subtarget->getCPUString() == "generic") {
446 // Generic (and overly aggressive) if-conversion limits.
447 setIfCvtBlockSizeLimit(10);
448 setIfCvtDupBlockSizeLimit(2);
449 } else if (Subtarget->hasV6Ops()) {
450 setIfCvtBlockSizeLimit(2);
451 setIfCvtDupBlockSizeLimit(1);
452 } else {
453 setIfCvtBlockSizeLimit(3);
454 setIfCvtDupBlockSizeLimit(2);
Evan Cheng8557c2b2009-06-19 01:51:50 +0000455 }
456
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000457 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Bob Wilsone6abdff2009-05-18 20:55:32 +0000458 // Do not enable CodePlacementOpt for now: it currently runs after the
459 // ARMConstantIslandPass and messes up branch relaxation and placement
460 // of constant islands.
461 // benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000462}
463
Evan Chenga8e29892007-01-19 07:51:42 +0000464const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
465 switch (Opcode) {
466 default: return 0;
467 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000468 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
469 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000470 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000471 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
472 case ARMISD::tCALL: return "ARMISD::tCALL";
473 case ARMISD::BRCOND: return "ARMISD::BRCOND";
474 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000475 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000476 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
477 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
478 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000479 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000480 case ARMISD::CMPFP: return "ARMISD::CMPFP";
481 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
482 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
483 case ARMISD::CMOV: return "ARMISD::CMOV";
484 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000485
Evan Chenga8e29892007-01-19 07:51:42 +0000486 case ARMISD::FTOSI: return "ARMISD::FTOSI";
487 case ARMISD::FTOUI: return "ARMISD::FTOUI";
488 case ARMISD::SITOF: return "ARMISD::SITOF";
489 case ARMISD::UITOF: return "ARMISD::UITOF";
Evan Chenga8e29892007-01-19 07:51:42 +0000490
491 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
492 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
493 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000494
Evan Chenga8e29892007-01-19 07:51:42 +0000495 case ARMISD::FMRRD: return "ARMISD::FMRRD";
496 case ARMISD::FMDRR: return "ARMISD::FMDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000497
498 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000499
Evan Cheng86198642009-08-07 00:34:42 +0000500 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
501
Bob Wilson5bafff32009-06-22 23:27:02 +0000502 case ARMISD::VCEQ: return "ARMISD::VCEQ";
503 case ARMISD::VCGE: return "ARMISD::VCGE";
504 case ARMISD::VCGEU: return "ARMISD::VCGEU";
505 case ARMISD::VCGT: return "ARMISD::VCGT";
506 case ARMISD::VCGTU: return "ARMISD::VCGTU";
507 case ARMISD::VTST: return "ARMISD::VTST";
508
509 case ARMISD::VSHL: return "ARMISD::VSHL";
510 case ARMISD::VSHRs: return "ARMISD::VSHRs";
511 case ARMISD::VSHRu: return "ARMISD::VSHRu";
512 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
513 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
514 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
515 case ARMISD::VSHRN: return "ARMISD::VSHRN";
516 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
517 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
518 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
519 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
520 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
521 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
522 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
523 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
524 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
525 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
526 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
527 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
528 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
529 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000530 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000531 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000532 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000533 case ARMISD::VREV64: return "ARMISD::VREV64";
534 case ARMISD::VREV32: return "ARMISD::VREV32";
535 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000536 case ARMISD::VZIP: return "ARMISD::VZIP";
537 case ARMISD::VUZP: return "ARMISD::VUZP";
538 case ARMISD::VTRN: return "ARMISD::VTRN";
Evan Chenga8e29892007-01-19 07:51:42 +0000539 }
540}
541
Bill Wendlingb4202b82009-07-01 18:50:55 +0000542/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000543unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Evan Cheng048e36f2009-10-02 06:57:25 +0000544 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 0 : 1;
Bill Wendling20c568f2009-06-30 22:38:32 +0000545}
546
Evan Chenga8e29892007-01-19 07:51:42 +0000547//===----------------------------------------------------------------------===//
548// Lowering Code
549//===----------------------------------------------------------------------===//
550
Evan Chenga8e29892007-01-19 07:51:42 +0000551/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
552static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
553 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000554 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000555 case ISD::SETNE: return ARMCC::NE;
556 case ISD::SETEQ: return ARMCC::EQ;
557 case ISD::SETGT: return ARMCC::GT;
558 case ISD::SETGE: return ARMCC::GE;
559 case ISD::SETLT: return ARMCC::LT;
560 case ISD::SETLE: return ARMCC::LE;
561 case ISD::SETUGT: return ARMCC::HI;
562 case ISD::SETUGE: return ARMCC::HS;
563 case ISD::SETULT: return ARMCC::LO;
564 case ISD::SETULE: return ARMCC::LS;
565 }
566}
567
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000568/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
569static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000570 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000571 CondCode2 = ARMCC::AL;
572 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000573 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000574 case ISD::SETEQ:
575 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
576 case ISD::SETGT:
577 case ISD::SETOGT: CondCode = ARMCC::GT; break;
578 case ISD::SETGE:
579 case ISD::SETOGE: CondCode = ARMCC::GE; break;
580 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000581 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000582 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
583 case ISD::SETO: CondCode = ARMCC::VC; break;
584 case ISD::SETUO: CondCode = ARMCC::VS; break;
585 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
586 case ISD::SETUGT: CondCode = ARMCC::HI; break;
587 case ISD::SETUGE: CondCode = ARMCC::PL; break;
588 case ISD::SETLT:
589 case ISD::SETULT: CondCode = ARMCC::LT; break;
590 case ISD::SETLE:
591 case ISD::SETULE: CondCode = ARMCC::LE; break;
592 case ISD::SETNE:
593 case ISD::SETUNE: CondCode = ARMCC::NE; break;
594 }
Evan Chenga8e29892007-01-19 07:51:42 +0000595}
596
Bob Wilson1f595bb2009-04-17 19:07:39 +0000597//===----------------------------------------------------------------------===//
598// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +0000599//===----------------------------------------------------------------------===//
600
601#include "ARMGenCallingConv.inc"
602
603// APCS f64 is in register pairs, possibly split to stack
Owen Andersone50ed302009-08-10 22:56:29 +0000604static bool f64AssignAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000605 CCValAssign::LocInfo &LocInfo,
606 CCState &State, bool CanFail) {
607 static const unsigned RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
608
609 // Try to get the first register.
610 if (unsigned Reg = State.AllocateReg(RegList, 4))
611 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
612 else {
613 // For the 2nd half of a v2f64, do not fail.
614 if (CanFail)
615 return false;
616
617 // Put the whole thing on the stack.
618 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
619 State.AllocateStack(8, 4),
620 LocVT, LocInfo));
621 return true;
622 }
623
624 // Try to get the second register.
625 if (unsigned Reg = State.AllocateReg(RegList, 4))
626 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
627 else
628 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
629 State.AllocateStack(4, 4),
630 LocVT, LocInfo));
631 return true;
632}
633
Owen Andersone50ed302009-08-10 22:56:29 +0000634static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000635 CCValAssign::LocInfo &LocInfo,
636 ISD::ArgFlagsTy &ArgFlags,
637 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000638 if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
639 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000640 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000641 !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
642 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000643 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000644}
645
646// AAPCS f64 is in aligned register pairs
Owen Andersone50ed302009-08-10 22:56:29 +0000647static bool f64AssignAAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000648 CCValAssign::LocInfo &LocInfo,
649 CCState &State, bool CanFail) {
650 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
651 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
652
653 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
654 if (Reg == 0) {
655 // For the 2nd half of a v2f64, do not just fail.
656 if (CanFail)
657 return false;
658
659 // Put the whole thing on the stack.
660 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
661 State.AllocateStack(8, 8),
662 LocVT, LocInfo));
663 return true;
664 }
665
666 unsigned i;
667 for (i = 0; i < 2; ++i)
668 if (HiRegList[i] == Reg)
669 break;
670
671 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
672 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
673 LocVT, LocInfo));
674 return true;
675}
676
Owen Andersone50ed302009-08-10 22:56:29 +0000677static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000678 CCValAssign::LocInfo &LocInfo,
679 ISD::ArgFlagsTy &ArgFlags,
680 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000681 if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
682 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000683 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000684 !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
685 return false;
686 return true; // we handled it
687}
688
Owen Andersone50ed302009-08-10 22:56:29 +0000689static bool f64RetAssign(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000690 CCValAssign::LocInfo &LocInfo, CCState &State) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000691 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
692 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
693
Bob Wilsone65586b2009-04-17 20:40:45 +0000694 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
695 if (Reg == 0)
696 return false; // we didn't handle it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000697
Bob Wilsone65586b2009-04-17 20:40:45 +0000698 unsigned i;
699 for (i = 0; i < 2; ++i)
700 if (HiRegList[i] == Reg)
701 break;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000702
Bob Wilson5bafff32009-06-22 23:27:02 +0000703 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bob Wilsone65586b2009-04-17 20:40:45 +0000704 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000705 LocVT, LocInfo));
706 return true;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000707}
708
Owen Andersone50ed302009-08-10 22:56:29 +0000709static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000710 CCValAssign::LocInfo &LocInfo,
711 ISD::ArgFlagsTy &ArgFlags,
712 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000713 if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
714 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000715 if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
Bob Wilson5bafff32009-06-22 23:27:02 +0000716 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000717 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000718}
719
Owen Andersone50ed302009-08-10 22:56:29 +0000720static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000721 CCValAssign::LocInfo &LocInfo,
722 ISD::ArgFlagsTy &ArgFlags,
723 CCState &State) {
724 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
725 State);
726}
727
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000728/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
729/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000730CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000731 bool Return,
732 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000733 switch (CC) {
734 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000735 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000736 case CallingConv::C:
737 case CallingConv::Fast:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000738 // Use target triple & subtarget features to do actual dispatch.
739 if (Subtarget->isAAPCS_ABI()) {
740 if (Subtarget->hasVFP2() &&
741 FloatABIType == FloatABI::Hard && !isVarArg)
742 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
743 else
744 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
745 } else
746 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000747 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000748 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000749 case CallingConv::ARM_AAPCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000750 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000751 case CallingConv::ARM_APCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000752 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000753 }
754}
755
Dan Gohman98ca4f22009-08-05 01:29:28 +0000756/// LowerCallResult - Lower the result values of a call into the
757/// appropriate copies out of appropriate physical registers.
758SDValue
759ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000760 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000761 const SmallVectorImpl<ISD::InputArg> &Ins,
762 DebugLoc dl, SelectionDAG &DAG,
763 SmallVectorImpl<SDValue> &InVals) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000764
Bob Wilson1f595bb2009-04-17 19:07:39 +0000765 // Assign locations to each value returned by this call.
766 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000767 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000768 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000769 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000770 CCAssignFnForNode(CallConv, /* Return*/ true,
771 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000772
773 // Copy all of the result registers out of their specified physreg.
774 for (unsigned i = 0; i != RVLocs.size(); ++i) {
775 CCValAssign VA = RVLocs[i];
776
Bob Wilson80915242009-04-25 00:33:20 +0000777 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000778 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000779 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000780 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000781 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000782 Chain = Lo.getValue(1);
783 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000784 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000785 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000786 InFlag);
787 Chain = Hi.getValue(1);
788 InFlag = Hi.getValue(2);
Owen Anderson825b72b2009-08-11 20:47:22 +0000789 Val = DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +0000790
Owen Anderson825b72b2009-08-11 20:47:22 +0000791 if (VA.getLocVT() == MVT::v2f64) {
792 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
793 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
794 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000795
796 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000797 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000798 Chain = Lo.getValue(1);
799 InFlag = Lo.getValue(2);
800 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000801 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000802 Chain = Hi.getValue(1);
803 InFlag = Hi.getValue(2);
Owen Anderson825b72b2009-08-11 20:47:22 +0000804 Val = DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
805 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
806 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000807 }
Bob Wilson1f595bb2009-04-17 19:07:39 +0000808 } else {
Bob Wilson80915242009-04-25 00:33:20 +0000809 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
810 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000811 Chain = Val.getValue(1);
812 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000813 }
Bob Wilson80915242009-04-25 00:33:20 +0000814
815 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000816 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +0000817 case CCValAssign::Full: break;
818 case CCValAssign::BCvt:
819 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
820 break;
821 }
822
Dan Gohman98ca4f22009-08-05 01:29:28 +0000823 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000824 }
825
Dan Gohman98ca4f22009-08-05 01:29:28 +0000826 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000827}
828
829/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
830/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +0000831/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +0000832/// a byval function parameter.
833/// Sometimes what we are copying is the end of a larger object, the part that
834/// does not fit in registers.
835static SDValue
836CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
837 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
838 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000839 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000840 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
841 /*AlwaysInline=*/false, NULL, 0, NULL, 0);
842}
843
Bob Wilsondee46d72009-04-17 20:35:10 +0000844/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000845SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +0000846ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
847 SDValue StackPtr, SDValue Arg,
848 DebugLoc dl, SelectionDAG &DAG,
849 const CCValAssign &VA,
850 ISD::ArgFlagsTy Flags) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000851 unsigned LocMemOffset = VA.getLocMemOffset();
852 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
853 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
854 if (Flags.isByVal()) {
855 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
856 }
857 return DAG.getStore(Chain, dl, Arg, PtrOff,
858 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chenga8e29892007-01-19 07:51:42 +0000859}
860
Dan Gohman98ca4f22009-08-05 01:29:28 +0000861void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +0000862 SDValue Chain, SDValue &Arg,
863 RegsToPassVector &RegsToPass,
864 CCValAssign &VA, CCValAssign &NextVA,
865 SDValue &StackPtr,
866 SmallVector<SDValue, 8> &MemOpChains,
867 ISD::ArgFlagsTy Flags) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000868
869 SDValue fmrrd = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +0000870 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +0000871 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
872
873 if (NextVA.isRegLoc())
874 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
875 else {
876 assert(NextVA.isMemLoc());
877 if (StackPtr.getNode() == 0)
878 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
879
Dan Gohman98ca4f22009-08-05 01:29:28 +0000880 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
881 dl, DAG, NextVA,
882 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000883 }
884}
885
Dan Gohman98ca4f22009-08-05 01:29:28 +0000886/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +0000887/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
888/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000889SDValue
890ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000891 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000892 bool isTailCall,
893 const SmallVectorImpl<ISD::OutputArg> &Outs,
894 const SmallVectorImpl<ISD::InputArg> &Ins,
895 DebugLoc dl, SelectionDAG &DAG,
896 SmallVectorImpl<SDValue> &InVals) {
Evan Chenga8e29892007-01-19 07:51:42 +0000897
Bob Wilson1f595bb2009-04-17 19:07:39 +0000898 // Analyze operands of the call, assigning locations to each operand.
899 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000900 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
901 *DAG.getContext());
902 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000903 CCAssignFnForNode(CallConv, /* Return*/ false,
904 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +0000905
Bob Wilson1f595bb2009-04-17 19:07:39 +0000906 // Get a count of how many bytes are to be pushed on the stack.
907 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +0000908
909 // Adjust the stack pointer for the new arguments...
910 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattnere563bbc2008-10-11 22:08:30 +0000911 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +0000912
Owen Anderson825b72b2009-08-11 20:47:22 +0000913 SDValue StackPtr = DAG.getRegister(ARM::SP, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000914
Bob Wilson5bafff32009-06-22 23:27:02 +0000915 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000916 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +0000917
Bob Wilson1f595bb2009-04-17 19:07:39 +0000918 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +0000919 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000920 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
921 i != e;
922 ++i, ++realArgIdx) {
923 CCValAssign &VA = ArgLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +0000924 SDValue Arg = Outs[realArgIdx].Val;
925 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +0000926
Bob Wilson1f595bb2009-04-17 19:07:39 +0000927 // Promote the value if needed.
928 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000929 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +0000930 case CCValAssign::Full: break;
931 case CCValAssign::SExt:
932 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
933 break;
934 case CCValAssign::ZExt:
935 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
936 break;
937 case CCValAssign::AExt:
938 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
939 break;
940 case CCValAssign::BCvt:
941 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
942 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000943 }
944
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000945 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +0000946 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000947 if (VA.getLocVT() == MVT::v2f64) {
948 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
949 DAG.getConstant(0, MVT::i32));
950 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
951 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000952
Dan Gohman98ca4f22009-08-05 01:29:28 +0000953 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +0000954 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
955
956 VA = ArgLocs[++i]; // skip ahead to next loc
957 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000958 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +0000959 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
960 } else {
961 assert(VA.isMemLoc());
962 if (StackPtr.getNode() == 0)
963 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
964
Dan Gohman98ca4f22009-08-05 01:29:28 +0000965 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
966 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000967 }
968 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000969 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000970 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000971 }
972 } else if (VA.isRegLoc()) {
973 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
974 } else {
975 assert(VA.isMemLoc());
976 if (StackPtr.getNode() == 0)
977 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
978
Dan Gohman98ca4f22009-08-05 01:29:28 +0000979 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
980 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000981 }
Evan Chenga8e29892007-01-19 07:51:42 +0000982 }
983
984 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000985 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +0000986 &MemOpChains[0], MemOpChains.size());
987
988 // Build a sequence of copy-to-reg nodes chained together with token chain
989 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +0000990 SDValue InFlag;
Evan Chenga8e29892007-01-19 07:51:42 +0000991 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Bob Wilson2dc4f542009-03-20 22:42:55 +0000992 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000993 RegsToPass[i].second, InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +0000994 InFlag = Chain.getValue(1);
995 }
996
Bill Wendling056292f2008-09-16 21:48:12 +0000997 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
998 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
999 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001000 bool isDirect = false;
1001 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001002 bool isLocalARMFunc = false;
Evan Chenga8e29892007-01-19 07:51:42 +00001003 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1004 GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001005 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001006 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001007 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001008 getTargetMachine().getRelocationModel() != Reloc::Static;
1009 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001010 // ARM call to a local ARM function is predicable.
1011 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
Evan Chengc60e76d2007-01-30 20:37:08 +00001012 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001013 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge4e4ed32009-08-28 23:18:09 +00001014 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001015 ARMPCLabelIndex,
1016 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001017 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001018 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001019 Callee = DAG.getLoad(getPointerTy(), dl,
1020 DAG.getEntryNode(), CPAddr, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001021 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001022 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001023 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001024 } else
1025 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +00001026 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001027 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001028 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001029 getTargetMachine().getRelocationModel() != Reloc::Static;
1030 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001031 // tBX takes a register source operand.
1032 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001033 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Owen Anderson1d0be152009-08-13 21:58:54 +00001034 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001035 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001036 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001037 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001038 Callee = DAG.getLoad(getPointerTy(), dl,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001039 DAG.getEntryNode(), CPAddr, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001040 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001041 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001042 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001043 } else
Bill Wendling056292f2008-09-16 21:48:12 +00001044 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001045 }
1046
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001047 // FIXME: handle tail calls differently.
1048 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001049 if (Subtarget->isThumb()) {
1050 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001051 CallOpc = ARMISD::CALL_NOLINK;
1052 else
1053 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1054 } else {
1055 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001056 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1057 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001058 }
David Goodwinf1daf7d2009-07-08 23:10:31 +00001059 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb1Only()) {
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +00001060 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Owen Anderson825b72b2009-08-11 20:47:22 +00001061 Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001062 InFlag = Chain.getValue(1);
1063 }
1064
Dan Gohman475871a2008-07-27 21:46:04 +00001065 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001066 Ops.push_back(Chain);
1067 Ops.push_back(Callee);
1068
1069 // Add argument registers to the end of the list so that they are known live
1070 // into the call.
1071 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1072 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1073 RegsToPass[i].second.getValueType()));
1074
Gabor Greifba36cb52008-08-28 21:40:38 +00001075 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001076 Ops.push_back(InFlag);
Duncan Sands4bdcb612008-07-02 17:40:58 +00001077 // Returns a chain and a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00001078 Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Flag),
Duncan Sands4bdcb612008-07-02 17:40:58 +00001079 &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001080 InFlag = Chain.getValue(1);
1081
Chris Lattnere563bbc2008-10-11 22:08:30 +00001082 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1083 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001084 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001085 InFlag = Chain.getValue(1);
1086
Bob Wilson1f595bb2009-04-17 19:07:39 +00001087 // Handle result values, copying them out of physregs into vregs that we
1088 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001089 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1090 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001091}
1092
Dan Gohman98ca4f22009-08-05 01:29:28 +00001093SDValue
1094ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001095 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001096 const SmallVectorImpl<ISD::OutputArg> &Outs,
1097 DebugLoc dl, SelectionDAG &DAG) {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001098
Bob Wilsondee46d72009-04-17 20:35:10 +00001099 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001100 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001101
Bob Wilsondee46d72009-04-17 20:35:10 +00001102 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001103 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1104 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001105
Dan Gohman98ca4f22009-08-05 01:29:28 +00001106 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001107 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1108 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001109
1110 // If this is the first return lowered for this function, add
1111 // the regs to the liveout set for the function.
1112 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1113 for (unsigned i = 0; i != RVLocs.size(); ++i)
1114 if (RVLocs[i].isRegLoc())
1115 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001116 }
1117
Bob Wilson1f595bb2009-04-17 19:07:39 +00001118 SDValue Flag;
1119
1120 // Copy the result values into the output registers.
1121 for (unsigned i = 0, realRVLocIdx = 0;
1122 i != RVLocs.size();
1123 ++i, ++realRVLocIdx) {
1124 CCValAssign &VA = RVLocs[i];
1125 assert(VA.isRegLoc() && "Can only return in registers!");
1126
Dan Gohman98ca4f22009-08-05 01:29:28 +00001127 SDValue Arg = Outs[realRVLocIdx].Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001128
1129 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001130 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001131 case CCValAssign::Full: break;
1132 case CCValAssign::BCvt:
1133 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1134 break;
1135 }
1136
Bob Wilson1f595bb2009-04-17 19:07:39 +00001137 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001138 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001139 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001140 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1141 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001142 SDValue HalfGPRs = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001143 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001144
1145 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1146 Flag = Chain.getValue(1);
1147 VA = RVLocs[++i]; // skip ahead to next loc
1148 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1149 HalfGPRs.getValue(1), Flag);
1150 Flag = Chain.getValue(1);
1151 VA = RVLocs[++i]; // skip ahead to next loc
1152
1153 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001154 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1155 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001156 }
1157 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1158 // available.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001159 SDValue fmrrd = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001160 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001161 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001162 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001163 VA = RVLocs[++i]; // skip ahead to next loc
1164 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1165 Flag);
1166 } else
1167 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1168
Bob Wilsondee46d72009-04-17 20:35:10 +00001169 // Guarantee that all emitted copies are
1170 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001171 Flag = Chain.getValue(1);
1172 }
1173
1174 SDValue result;
1175 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001176 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001177 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001178 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001179
1180 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001181}
1182
Bob Wilson2dc4f542009-03-20 22:42:55 +00001183// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
Bob Wilsond2559bf2009-07-13 18:11:36 +00001184// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
Bill Wendling056292f2008-09-16 21:48:12 +00001185// one of the above mentioned nodes. It has to be wrapped because otherwise
1186// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1187// be used to form addressing mode. These wrapped nodes will be selected
1188// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001189static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001190 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001191 // FIXME there is no actual debug info here
1192 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001193 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001194 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001195 if (CP->isMachineConstantPoolEntry())
1196 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1197 CP->getAlignment());
1198 else
1199 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1200 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001201 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001202}
1203
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001204// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001205SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001206ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
1207 SelectionDAG &DAG) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001208 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001209 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001210 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1211 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001212 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001213 ARMCP::CPValue, PCAdj, "tlsgd", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001214 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001215 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001216 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001217 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001218
Owen Anderson825b72b2009-08-11 20:47:22 +00001219 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001220 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001221
1222 // call __tls_get_addr.
1223 ArgListTy Args;
1224 ArgListEntry Entry;
1225 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001226 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001227 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001228 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001229 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001230 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1231 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001232 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001233 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001234 return CallResult.first;
1235}
1236
1237// Lower ISD::GlobalTLSAddress using the "initial exec" or
1238// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001239SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001240ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001241 SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001242 GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001243 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001244 SDValue Offset;
1245 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001246 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001247 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001248 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001249
Chris Lattner4fb63d02009-07-15 04:12:33 +00001250 if (GV->isDeclaration()) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001251 // initial exec model
1252 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1253 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001254 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001255 ARMCP::CPValue, PCAdj, "gottpoff", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001256 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001257 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001258 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001259 Chain = Offset.getValue(1);
1260
Owen Anderson825b72b2009-08-11 20:47:22 +00001261 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001262 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001263
Dale Johannesen33c960f2009-02-04 20:06:27 +00001264 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001265 } else {
1266 // local exec model
Evan Chenge4e4ed32009-08-28 23:18:09 +00001267 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, "tpoff");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001268 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001269 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001270 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001271 }
1272
1273 // The address of the thread local variable is the add of the thread
1274 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001275 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001276}
1277
Dan Gohman475871a2008-07-27 21:46:04 +00001278SDValue
1279ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001280 // TODO: implement the "local dynamic" model
1281 assert(Subtarget->isTargetELF() &&
1282 "TLS not implemented for non-ELF targets");
1283 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1284 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1285 // otherwise use the "Local Exec" TLS Model
1286 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1287 return LowerToTLSGeneralDynamicModel(GA, DAG);
1288 else
1289 return LowerToTLSExecModels(GA, DAG);
1290}
1291
Dan Gohman475871a2008-07-27 21:46:04 +00001292SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001293 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001294 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001295 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001296 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1297 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1298 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001299 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001300 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001301 new ARMConstantPoolValue(GV, UseGOTOFF ? "GOTOFF" : "GOT");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001302 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001303 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001304 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001305 CPAddr,
1306 PseudoSourceValue::getConstantPool(), 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001307 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001308 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001309 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001310 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001311 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
1312 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001313 return Result;
1314 } else {
Evan Cheng1606e8e2009-03-13 07:51:59 +00001315 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001316 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001317 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1318 PseudoSourceValue::getConstantPool(), 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001319 }
1320}
1321
Dan Gohman475871a2008-07-27 21:46:04 +00001322SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001323 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001324 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001325 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001326 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1327 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Dan Gohman475871a2008-07-27 21:46:04 +00001328 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +00001329 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001330 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001331 else {
Evan Chenge4e4ed32009-08-28 23:18:09 +00001332 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
1333 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001334 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001335 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001336 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001337 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001338
Dale Johannesen33c960f2009-02-04 20:06:27 +00001339 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001340 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001341
1342 if (RelocM == Reloc::PIC_) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001343 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001344 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001345 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00001346
Evan Cheng63476a82009-09-03 07:04:02 +00001347 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Dale Johannesen33c960f2009-02-04 20:06:27 +00001348 Result = DAG.getLoad(PtrVT, dl, Chain, Result, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001349
1350 return Result;
1351}
1352
Dan Gohman475871a2008-07-27 21:46:04 +00001353SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001354 SelectionDAG &DAG){
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001355 assert(Subtarget->isTargetELF() &&
1356 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Owen Andersone50ed302009-08-10 22:56:29 +00001357 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001358 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001359 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00001360 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1361 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00001362 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001363 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001364 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001365 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1366 PseudoSourceValue::getConstantPool(), 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001367 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001368 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001369}
1370
Jim Grosbach0e0da732009-05-12 23:59:14 +00001371SDValue
1372ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001373 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001374 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001375 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001376 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00001377 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00001378 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00001379 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1380 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001381 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001382 MachineFunction &MF = DAG.getMachineFunction();
1383 EVT PtrVT = getPointerTy();
1384 DebugLoc dl = Op.getDebugLoc();
1385 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1386 SDValue CPAddr;
1387 unsigned PCAdj = (RelocM != Reloc::PIC_)
1388 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001389 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001390 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
1391 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001392 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001393 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001394 SDValue Result =
1395 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
1396 SDValue Chain = Result.getValue(1);
1397
1398 if (RelocM == Reloc::PIC_) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001399 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001400 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1401 }
1402 return Result;
1403 }
Jim Grosbachf9570122009-05-14 00:46:35 +00001404 case Intrinsic::eh_sjlj_setjmp:
Owen Anderson825b72b2009-08-11 20:47:22 +00001405 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(1));
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001406 }
1407}
1408
Dan Gohman475871a2008-07-27 21:46:04 +00001409static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001410 unsigned VarArgsFrameIndex) {
Evan Chenga8e29892007-01-19 07:51:42 +00001411 // vastart just stores the address of the VarArgsFrameIndex slot into the
1412 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001413 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001414 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001415 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001416 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001417 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001418}
1419
Dan Gohman475871a2008-07-27 21:46:04 +00001420SDValue
Evan Cheng86198642009-08-07 00:34:42 +00001421ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) {
1422 SDNode *Node = Op.getNode();
1423 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001424 EVT VT = Node->getValueType(0);
Evan Cheng86198642009-08-07 00:34:42 +00001425 SDValue Chain = Op.getOperand(0);
1426 SDValue Size = Op.getOperand(1);
1427 SDValue Align = Op.getOperand(2);
1428
1429 // Chain the dynamic stack allocation so that it doesn't modify the stack
1430 // pointer when other instructions are using the stack.
1431 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1432
1433 unsigned AlignVal = cast<ConstantSDNode>(Align)->getZExtValue();
1434 unsigned StackAlign = getTargetMachine().getFrameInfo()->getStackAlignment();
1435 if (AlignVal > StackAlign)
1436 // Do this now since selection pass cannot introduce new target
1437 // independent node.
1438 Align = DAG.getConstant(-(uint64_t)AlignVal, VT);
1439
1440 // In Thumb1 mode, there isn't a "sub r, sp, r" instruction, we will end up
1441 // using a "add r, sp, r" instead. Negate the size now so we don't have to
1442 // do even more horrible hack later.
1443 MachineFunction &MF = DAG.getMachineFunction();
1444 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1445 if (AFI->isThumb1OnlyFunction()) {
1446 bool Negate = true;
1447 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Size);
1448 if (C) {
1449 uint32_t Val = C->getZExtValue();
1450 if (Val <= 508 && ((Val & 3) == 0))
1451 Negate = false;
1452 }
1453 if (Negate)
1454 Size = DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(0, VT), Size);
1455 }
1456
Owen Anderson825b72b2009-08-11 20:47:22 +00001457 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
Evan Cheng86198642009-08-07 00:34:42 +00001458 SDValue Ops1[] = { Chain, Size, Align };
1459 SDValue Res = DAG.getNode(ARMISD::DYN_ALLOC, dl, VTList, Ops1, 3);
1460 Chain = Res.getValue(1);
1461 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1462 DAG.getIntPtrConstant(0, true), SDValue());
1463 SDValue Ops2[] = { Res, Chain };
1464 return DAG.getMergeValues(Ops2, 2, dl);
1465}
1466
1467SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00001468ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
1469 SDValue &Root, SelectionDAG &DAG,
1470 DebugLoc dl) {
1471 MachineFunction &MF = DAG.getMachineFunction();
1472 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1473
1474 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001475 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00001476 RC = ARM::tGPRRegisterClass;
1477 else
1478 RC = ARM::GPRRegisterClass;
1479
1480 // Transform the arguments stored in physical registers into virtual ones.
1481 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001482 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001483
1484 SDValue ArgValue2;
1485 if (NextVA.isMemLoc()) {
1486 unsigned ArgSize = NextVA.getLocVT().getSizeInBits()/8;
1487 MachineFrameInfo *MFI = MF.getFrameInfo();
1488 int FI = MFI->CreateFixedObject(ArgSize, NextVA.getLocMemOffset());
1489
1490 // Create load node to retrieve arguments from the stack.
1491 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00001492 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN, NULL, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00001493 } else {
1494 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001495 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001496 }
1497
Owen Anderson825b72b2009-08-11 20:47:22 +00001498 return DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00001499}
1500
1501SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001502ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001503 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001504 const SmallVectorImpl<ISD::InputArg>
1505 &Ins,
1506 DebugLoc dl, SelectionDAG &DAG,
1507 SmallVectorImpl<SDValue> &InVals) {
1508
Bob Wilson1f595bb2009-04-17 19:07:39 +00001509 MachineFunction &MF = DAG.getMachineFunction();
1510 MachineFrameInfo *MFI = MF.getFrameInfo();
1511
Bob Wilson1f595bb2009-04-17 19:07:39 +00001512 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1513
1514 // Assign locations to all of the incoming arguments.
1515 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001516 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1517 *DAG.getContext());
1518 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001519 CCAssignFnForNode(CallConv, /* Return*/ false,
1520 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001521
1522 SmallVector<SDValue, 16> ArgValues;
1523
1524 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1525 CCValAssign &VA = ArgLocs[i];
1526
Bob Wilsondee46d72009-04-17 20:35:10 +00001527 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001528 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001529 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00001530
Bob Wilson5bafff32009-06-22 23:27:02 +00001531 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001532 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001533 // f64 and vector types are split up into multiple registers or
1534 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00001535 RegVT = MVT::i32;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001536
Owen Anderson825b72b2009-08-11 20:47:22 +00001537 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001538 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001539 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00001540 VA = ArgLocs[++i]; // skip ahead to next loc
1541 SDValue ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001542 Chain, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001543 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1544 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001545 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00001546 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001547 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
1548 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001549 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001550
Bob Wilson5bafff32009-06-22 23:27:02 +00001551 } else {
1552 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001553
Owen Anderson825b72b2009-08-11 20:47:22 +00001554 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00001555 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001556 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00001557 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001558 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001559 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001560 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001561 RC = (AFI->isThumb1OnlyFunction() ?
1562 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00001563 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001564 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00001565
1566 // Transform the arguments in physical registers into virtual ones.
1567 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001568 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001569 }
1570
1571 // If this is an 8 or 16-bit value, it is really passed promoted
1572 // to 32 bits. Insert an assert[sz]ext to capture this, then
1573 // truncate to the right size.
1574 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001575 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001576 case CCValAssign::Full: break;
1577 case CCValAssign::BCvt:
1578 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1579 break;
1580 case CCValAssign::SExt:
1581 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
1582 DAG.getValueType(VA.getValVT()));
1583 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1584 break;
1585 case CCValAssign::ZExt:
1586 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
1587 DAG.getValueType(VA.getValVT()));
1588 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1589 break;
1590 }
1591
Dan Gohman98ca4f22009-08-05 01:29:28 +00001592 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001593
1594 } else { // VA.isRegLoc()
1595
1596 // sanity check
1597 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00001598 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001599
1600 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
1601 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset());
1602
Bob Wilsondee46d72009-04-17 20:35:10 +00001603 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001604 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001605 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN, NULL, 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001606 }
1607 }
1608
1609 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00001610 if (isVarArg) {
1611 static const unsigned GPRArgRegs[] = {
1612 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1613 };
1614
Bob Wilsondee46d72009-04-17 20:35:10 +00001615 unsigned NumGPRs = CCInfo.getFirstUnallocated
1616 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001617
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001618 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1619 unsigned VARegSize = (4 - NumGPRs) * 4;
1620 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001621 unsigned ArgOffset = 0;
Evan Chenga8e29892007-01-19 07:51:42 +00001622 if (VARegSaveSize) {
1623 // If this function is vararg, store any remaining integer argument regs
1624 // to their spots on the stack so that they may be loaded by deferencing
1625 // the result of va_next.
1626 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001627 ArgOffset = CCInfo.getNextStackOffset();
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001628 VarArgsFrameIndex = MFI->CreateFixedObject(VARegSaveSize, ArgOffset +
1629 VARegSaveSize - VARegSize);
Dan Gohman475871a2008-07-27 21:46:04 +00001630 SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001631
Dan Gohman475871a2008-07-27 21:46:04 +00001632 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00001633 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001634 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001635 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00001636 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001637 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00001638 RC = ARM::GPRRegisterClass;
1639
Bob Wilson998e1252009-04-20 18:36:57 +00001640 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001641 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001642 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001643 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001644 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00001645 DAG.getConstant(4, getPointerTy()));
1646 }
1647 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001648 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001649 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001650 } else
1651 // This will point to the next argument passed via stack.
1652 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1653 }
1654
Dan Gohman98ca4f22009-08-05 01:29:28 +00001655 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00001656}
1657
1658/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00001659static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00001660 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001661 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00001662 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00001663 // Maybe this has already been legalized into the constant pool?
1664 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00001665 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001666 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
1667 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001668 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001669 }
1670 }
1671 return false;
1672}
1673
David Goodwinf1daf7d2009-07-08 23:10:31 +00001674static bool isLegalCmpImmediate(unsigned C, bool isThumb1Only) {
1675 return ( isThumb1Only && (C & ~255U) == 0) ||
1676 (!isThumb1Only && ARM_AM::getSOImmVal(C) != -1);
Evan Chenga8e29892007-01-19 07:51:42 +00001677}
1678
1679/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1680/// the given operands.
Dan Gohman475871a2008-07-27 21:46:04 +00001681static SDValue getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
David Goodwinf1daf7d2009-07-08 23:10:31 +00001682 SDValue &ARMCC, SelectionDAG &DAG, bool isThumb1Only,
Dale Johannesende064702009-02-06 21:50:26 +00001683 DebugLoc dl) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001684 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001685 unsigned C = RHSC->getZExtValue();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001686 if (!isLegalCmpImmediate(C, isThumb1Only)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001687 // Constant does not fit, try adjusting it by one?
1688 switch (CC) {
1689 default: break;
1690 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00001691 case ISD::SETGE:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001692 if (isLegalCmpImmediate(C-1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001693 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001694 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001695 }
1696 break;
1697 case ISD::SETULT:
1698 case ISD::SETUGE:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001699 if (C > 0 && isLegalCmpImmediate(C-1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001700 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001701 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001702 }
1703 break;
1704 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00001705 case ISD::SETGT:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001706 if (isLegalCmpImmediate(C+1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001707 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001708 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001709 }
1710 break;
1711 case ISD::SETULE:
1712 case ISD::SETUGT:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001713 if (C < 0xffffffff && isLegalCmpImmediate(C+1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001714 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001715 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001716 }
1717 break;
1718 }
1719 }
1720 }
1721
1722 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001723 ARMISD::NodeType CompareType;
1724 switch (CondCode) {
1725 default:
1726 CompareType = ARMISD::CMP;
1727 break;
1728 case ARMCC::EQ:
1729 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00001730 // Uses only Z Flag
1731 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001732 break;
1733 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001734 ARMCC = DAG.getConstant(CondCode, MVT::i32);
1735 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001736}
1737
1738/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001739static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Dale Johannesende064702009-02-06 21:50:26 +00001740 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001741 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00001742 if (!isFloatingPointZero(RHS))
Owen Anderson825b72b2009-08-11 20:47:22 +00001743 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001744 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001745 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
1746 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001747}
1748
Dan Gohman475871a2008-07-27 21:46:04 +00001749static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001750 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00001751 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001752 SDValue LHS = Op.getOperand(0);
1753 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001754 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001755 SDValue TrueVal = Op.getOperand(2);
1756 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00001757 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001758
Owen Anderson825b72b2009-08-11 20:47:22 +00001759 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001760 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001761 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
David Goodwinf1daf7d2009-07-08 23:10:31 +00001762 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb1Only(), dl);
Dale Johannesende064702009-02-06 21:50:26 +00001763 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001764 }
1765
1766 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001767 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00001768
Owen Anderson825b72b2009-08-11 20:47:22 +00001769 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1770 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001771 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
1772 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00001773 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001774 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001775 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001776 // FIXME: Needs another CMP because flag can have but one use.
Dale Johannesende064702009-02-06 21:50:26 +00001777 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001778 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Dale Johannesende064702009-02-06 21:50:26 +00001779 Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00001780 }
1781 return Result;
1782}
1783
Dan Gohman475871a2008-07-27 21:46:04 +00001784static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001785 const ARMSubtarget *ST) {
Dan Gohman475871a2008-07-27 21:46:04 +00001786 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001787 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001788 SDValue LHS = Op.getOperand(2);
1789 SDValue RHS = Op.getOperand(3);
1790 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00001791 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001792
Owen Anderson825b72b2009-08-11 20:47:22 +00001793 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001794 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001795 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
David Goodwinf1daf7d2009-07-08 23:10:31 +00001796 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb1Only(), dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001797 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Dale Johannesende064702009-02-06 21:50:26 +00001798 Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001799 }
1800
Owen Anderson825b72b2009-08-11 20:47:22 +00001801 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Chenga8e29892007-01-19 07:51:42 +00001802 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001803 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001804
Dale Johannesende064702009-02-06 21:50:26 +00001805 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001806 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1807 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1808 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00001809 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00001810 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001811 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001812 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00001813 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00001814 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001815 }
1816 return Res;
1817}
1818
Dan Gohman475871a2008-07-27 21:46:04 +00001819SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) {
1820 SDValue Chain = Op.getOperand(0);
1821 SDValue Table = Op.getOperand(1);
1822 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001823 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001824
Owen Andersone50ed302009-08-10 22:56:29 +00001825 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00001826 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
1827 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00001828 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00001829 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00001830 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00001831 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
1832 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00001833 if (Subtarget->isThumb2()) {
1834 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
1835 // which does another jump to the destination. This also makes it easier
1836 // to translate it to TBB / TBH later.
1837 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00001838 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00001839 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001840 }
Evan Cheng66ac5312009-07-25 00:33:29 +00001841 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001842 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr, NULL, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00001843 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001844 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00001845 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001846 } else {
1847 Addr = DAG.getLoad(PTy, dl, Chain, Addr, NULL, 0);
1848 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00001849 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001850 }
Evan Chenga8e29892007-01-19 07:51:42 +00001851}
1852
Dan Gohman475871a2008-07-27 21:46:04 +00001853static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Dale Johannesende064702009-02-06 21:50:26 +00001854 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001855 unsigned Opc =
1856 Op.getOpcode() == ISD::FP_TO_SINT ? ARMISD::FTOSI : ARMISD::FTOUI;
Owen Anderson825b72b2009-08-11 20:47:22 +00001857 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
1858 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001859}
1860
Dan Gohman475871a2008-07-27 21:46:04 +00001861static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001862 EVT VT = Op.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001863 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001864 unsigned Opc =
1865 Op.getOpcode() == ISD::SINT_TO_FP ? ARMISD::SITOF : ARMISD::UITOF;
1866
Owen Anderson825b72b2009-08-11 20:47:22 +00001867 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
Dale Johannesende064702009-02-06 21:50:26 +00001868 return DAG.getNode(Opc, dl, VT, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001869}
1870
Dan Gohman475871a2008-07-27 21:46:04 +00001871static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00001872 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00001873 SDValue Tmp0 = Op.getOperand(0);
1874 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00001875 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001876 EVT VT = Op.getValueType();
1877 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001878 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
1879 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001880 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
1881 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001882 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001883}
1884
Jim Grosbach0e0da732009-05-12 23:59:14 +00001885SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
1886 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1887 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00001888 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001889 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
1890 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00001891 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00001892 ? ARM::R7 : ARM::R11;
1893 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
1894 while (Depth--)
1895 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
1896 return FrameAddr;
1897}
1898
Dan Gohman475871a2008-07-27 21:46:04 +00001899SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00001900ARMTargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman475871a2008-07-27 21:46:04 +00001901 SDValue Chain,
1902 SDValue Dst, SDValue Src,
1903 SDValue Size, unsigned Align,
Dan Gohman707e0182008-04-12 04:36:06 +00001904 bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00001905 const Value *DstSV, uint64_t DstSVOff,
1906 const Value *SrcSV, uint64_t SrcSVOff){
Evan Cheng4102eb52007-10-22 22:11:27 +00001907 // Do repeated 4-byte loads and stores. To be improved.
Dan Gohman707e0182008-04-12 04:36:06 +00001908 // This requires 4-byte alignment.
1909 if ((Align & 3) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00001910 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001911 // This requires the copy size to be a constant, preferrably
1912 // within a subtarget-specific limit.
1913 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
1914 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00001915 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001916 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001917 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00001918 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001919
1920 unsigned BytesLeft = SizeVal & 3;
1921 unsigned NumMemOps = SizeVal >> 2;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001922 unsigned EmittedNumMemOps = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001923 EVT VT = MVT::i32;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001924 unsigned VTSize = 4;
Evan Cheng4102eb52007-10-22 22:11:27 +00001925 unsigned i = 0;
Evan Chenge5e7ce42007-05-18 01:19:57 +00001926 const unsigned MAX_LOADS_IN_LDM = 6;
Dan Gohman475871a2008-07-27 21:46:04 +00001927 SDValue TFOps[MAX_LOADS_IN_LDM];
1928 SDValue Loads[MAX_LOADS_IN_LDM];
Dan Gohman1f13c682008-04-28 17:15:20 +00001929 uint64_t SrcOff = 0, DstOff = 0;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001930
Evan Cheng4102eb52007-10-22 22:11:27 +00001931 // Emit up to MAX_LOADS_IN_LDM loads, then a TokenFactor barrier, then the
1932 // same number of stores. The loads and stores will get combined into
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001933 // ldm/stm later on.
Evan Cheng4102eb52007-10-22 22:11:27 +00001934 while (EmittedNumMemOps < NumMemOps) {
1935 for (i = 0;
1936 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00001937 Loads[i] = DAG.getLoad(VT, dl, Chain,
Owen Anderson825b72b2009-08-11 20:47:22 +00001938 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
1939 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001940 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00001941 TFOps[i] = Loads[i].getValue(1);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001942 SrcOff += VTSize;
1943 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001944 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001945
Evan Cheng4102eb52007-10-22 22:11:27 +00001946 for (i = 0;
1947 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00001948 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Owen Anderson825b72b2009-08-11 20:47:22 +00001949 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
1950 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001951 DstSV, DstSVOff + DstOff);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001952 DstOff += VTSize;
1953 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001954 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00001955
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001956 EmittedNumMemOps += i;
1957 }
1958
Bob Wilson2dc4f542009-03-20 22:42:55 +00001959 if (BytesLeft == 0)
Evan Cheng4102eb52007-10-22 22:11:27 +00001960 return Chain;
1961
1962 // Issue loads / stores for the trailing (1 - 3) bytes.
1963 unsigned BytesLeftSave = BytesLeft;
1964 i = 0;
1965 while (BytesLeft) {
1966 if (BytesLeft >= 2) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001967 VT = MVT::i16;
Evan Cheng4102eb52007-10-22 22:11:27 +00001968 VTSize = 2;
1969 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001970 VT = MVT::i8;
Evan Cheng4102eb52007-10-22 22:11:27 +00001971 VTSize = 1;
1972 }
1973
Dale Johannesen0f502f62009-02-03 22:26:09 +00001974 Loads[i] = DAG.getLoad(VT, dl, Chain,
Owen Anderson825b72b2009-08-11 20:47:22 +00001975 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
1976 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001977 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00001978 TFOps[i] = Loads[i].getValue(1);
1979 ++i;
1980 SrcOff += VTSize;
1981 BytesLeft -= VTSize;
1982 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001983 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00001984
1985 i = 0;
1986 BytesLeft = BytesLeftSave;
1987 while (BytesLeft) {
1988 if (BytesLeft >= 2) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001989 VT = MVT::i16;
Evan Cheng4102eb52007-10-22 22:11:27 +00001990 VTSize = 2;
1991 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001992 VT = MVT::i8;
Evan Cheng4102eb52007-10-22 22:11:27 +00001993 VTSize = 1;
1994 }
1995
Dale Johannesen0f502f62009-02-03 22:26:09 +00001996 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Owen Anderson825b72b2009-08-11 20:47:22 +00001997 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
1998 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001999 DstSV, DstSVOff + DstOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00002000 ++i;
2001 DstOff += VTSize;
2002 BytesLeft -= VTSize;
2003 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002004 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002005}
2006
Duncan Sands1607f052008-12-01 11:39:25 +00002007static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00002008 SDValue Op = N->getOperand(0);
Dale Johannesende064702009-02-06 21:50:26 +00002009 DebugLoc dl = N->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00002010 if (N->getValueType(0) == MVT::f64) {
Evan Chengc7c77292008-11-04 19:57:48 +00002011 // Turn i64->f64 into FMDRR.
Owen Anderson825b72b2009-08-11 20:47:22 +00002012 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2013 DAG.getConstant(0, MVT::i32));
2014 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2015 DAG.getConstant(1, MVT::i32));
2016 return DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
Evan Chengc7c77292008-11-04 19:57:48 +00002017 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002018
Evan Chengc7c77292008-11-04 19:57:48 +00002019 // Turn f64->i64 into FMRRD.
Bob Wilson2dc4f542009-03-20 22:42:55 +00002020 SDValue Cvt = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00002021 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002022
Chris Lattner27a6c732007-11-24 07:07:01 +00002023 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002024 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
Chris Lattner27a6c732007-11-24 07:07:01 +00002025}
2026
Bob Wilson5bafff32009-06-22 23:27:02 +00002027/// getZeroVector - Returns a vector of specified type with all zero elements.
2028///
Owen Andersone50ed302009-08-10 22:56:29 +00002029static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002030 assert(VT.isVector() && "Expected a vector type");
2031
2032 // Zero vectors are used to represent vector negation and in those cases
2033 // will be implemented with the NEON VNEG instruction. However, VNEG does
2034 // not support i64 elements, so sometimes the zero vectors will need to be
2035 // explicitly constructed. For those cases, and potentially other uses in
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002036 // the future, always build zero vectors as <16 x i8> or <8 x i8> bitcasted
Bob Wilson5bafff32009-06-22 23:27:02 +00002037 // to their dest type. This ensures they get CSE'd.
2038 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002039 SDValue Cst = DAG.getTargetConstant(0, MVT::i8);
2040 SmallVector<SDValue, 8> Ops;
2041 MVT TVT;
2042
2043 if (VT.getSizeInBits() == 64) {
2044 Ops.assign(8, Cst); TVT = MVT::v8i8;
2045 } else {
2046 Ops.assign(16, Cst); TVT = MVT::v16i8;
2047 }
2048 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002049
2050 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2051}
2052
2053/// getOnesVector - Returns a vector of specified type with all bits set.
2054///
Owen Andersone50ed302009-08-10 22:56:29 +00002055static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002056 assert(VT.isVector() && "Expected a vector type");
2057
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002058 // Always build ones vectors as <16 x i32> or <8 x i32> bitcasted to their
2059 // dest type. This ensures they get CSE'd.
Bob Wilson5bafff32009-06-22 23:27:02 +00002060 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002061 SDValue Cst = DAG.getTargetConstant(0xFF, MVT::i8);
2062 SmallVector<SDValue, 8> Ops;
2063 MVT TVT;
2064
2065 if (VT.getSizeInBits() == 64) {
2066 Ops.assign(8, Cst); TVT = MVT::v8i8;
2067 } else {
2068 Ops.assign(16, Cst); TVT = MVT::v16i8;
2069 }
2070 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002071
2072 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2073}
2074
2075static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2076 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00002077 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002078 DebugLoc dl = N->getDebugLoc();
2079
2080 // Lower vector shifts on NEON to use VSHL.
2081 if (VT.isVector()) {
2082 assert(ST->hasNEON() && "unexpected vector shift");
2083
2084 // Left shifts translate directly to the vshiftu intrinsic.
2085 if (N->getOpcode() == ISD::SHL)
2086 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002087 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002088 N->getOperand(0), N->getOperand(1));
2089
2090 assert((N->getOpcode() == ISD::SRA ||
2091 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2092
2093 // NEON uses the same intrinsics for both left and right shifts. For
2094 // right shifts, the shift amounts are negative, so negate the vector of
2095 // shift amounts.
Owen Andersone50ed302009-08-10 22:56:29 +00002096 EVT ShiftVT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002097 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2098 getZeroVector(ShiftVT, DAG, dl),
2099 N->getOperand(1));
2100 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2101 Intrinsic::arm_neon_vshifts :
2102 Intrinsic::arm_neon_vshiftu);
2103 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002104 DAG.getConstant(vshiftInt, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002105 N->getOperand(0), NegatedCount);
2106 }
2107
Eli Friedmance392eb2009-08-22 03:13:10 +00002108 // We can get here for a node like i32 = ISD::SHL i32, i64
2109 if (VT != MVT::i64)
2110 return SDValue();
2111
2112 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00002113 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00002114
Chris Lattner27a6c732007-11-24 07:07:01 +00002115 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2116 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002117 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00002118 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002119
Chris Lattner27a6c732007-11-24 07:07:01 +00002120 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00002121 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002122
Chris Lattner27a6c732007-11-24 07:07:01 +00002123 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00002124 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2125 DAG.getConstant(0, MVT::i32));
2126 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2127 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002128
Chris Lattner27a6c732007-11-24 07:07:01 +00002129 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2130 // captures the result into a carry flag.
2131 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Owen Anderson825b72b2009-08-11 20:47:22 +00002132 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002133
Chris Lattner27a6c732007-11-24 07:07:01 +00002134 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00002135 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002136
Chris Lattner27a6c732007-11-24 07:07:01 +00002137 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002138 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00002139}
2140
Bob Wilson5bafff32009-06-22 23:27:02 +00002141static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2142 SDValue TmpOp0, TmpOp1;
2143 bool Invert = false;
2144 bool Swap = false;
2145 unsigned Opc = 0;
2146
2147 SDValue Op0 = Op.getOperand(0);
2148 SDValue Op1 = Op.getOperand(1);
2149 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00002150 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002151 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2152 DebugLoc dl = Op.getDebugLoc();
2153
2154 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2155 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002156 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002157 case ISD::SETUNE:
2158 case ISD::SETNE: Invert = true; // Fallthrough
2159 case ISD::SETOEQ:
2160 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2161 case ISD::SETOLT:
2162 case ISD::SETLT: Swap = true; // Fallthrough
2163 case ISD::SETOGT:
2164 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2165 case ISD::SETOLE:
2166 case ISD::SETLE: Swap = true; // Fallthrough
2167 case ISD::SETOGE:
2168 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2169 case ISD::SETUGE: Swap = true; // Fallthrough
2170 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2171 case ISD::SETUGT: Swap = true; // Fallthrough
2172 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2173 case ISD::SETUEQ: Invert = true; // Fallthrough
2174 case ISD::SETONE:
2175 // Expand this to (OLT | OGT).
2176 TmpOp0 = Op0;
2177 TmpOp1 = Op1;
2178 Opc = ISD::OR;
2179 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2180 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2181 break;
2182 case ISD::SETUO: Invert = true; // Fallthrough
2183 case ISD::SETO:
2184 // Expand this to (OLT | OGE).
2185 TmpOp0 = Op0;
2186 TmpOp1 = Op1;
2187 Opc = ISD::OR;
2188 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2189 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2190 break;
2191 }
2192 } else {
2193 // Integer comparisons.
2194 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002195 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002196 case ISD::SETNE: Invert = true;
2197 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2198 case ISD::SETLT: Swap = true;
2199 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2200 case ISD::SETLE: Swap = true;
2201 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2202 case ISD::SETULT: Swap = true;
2203 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2204 case ISD::SETULE: Swap = true;
2205 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2206 }
2207
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002208 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00002209 if (Opc == ARMISD::VCEQ) {
2210
2211 SDValue AndOp;
2212 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2213 AndOp = Op0;
2214 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2215 AndOp = Op1;
2216
2217 // Ignore bitconvert.
2218 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2219 AndOp = AndOp.getOperand(0);
2220
2221 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2222 Opc = ARMISD::VTST;
2223 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2224 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2225 Invert = !Invert;
2226 }
2227 }
2228 }
2229
2230 if (Swap)
2231 std::swap(Op0, Op1);
2232
2233 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
2234
2235 if (Invert)
2236 Result = DAG.getNOT(dl, Result, VT);
2237
2238 return Result;
2239}
2240
2241/// isVMOVSplat - Check if the specified splat value corresponds to an immediate
2242/// VMOV instruction, and if so, return the constant being splatted.
2243static SDValue isVMOVSplat(uint64_t SplatBits, uint64_t SplatUndef,
2244 unsigned SplatBitSize, SelectionDAG &DAG) {
2245 switch (SplatBitSize) {
2246 case 8:
2247 // Any 1-byte value is OK.
2248 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Owen Anderson825b72b2009-08-11 20:47:22 +00002249 return DAG.getTargetConstant(SplatBits, MVT::i8);
Bob Wilson5bafff32009-06-22 23:27:02 +00002250
2251 case 16:
2252 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
2253 if ((SplatBits & ~0xff) == 0 ||
2254 (SplatBits & ~0xff00) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002255 return DAG.getTargetConstant(SplatBits, MVT::i16);
Bob Wilson5bafff32009-06-22 23:27:02 +00002256 break;
2257
2258 case 32:
2259 // NEON's 32-bit VMOV supports splat values where:
2260 // * only one byte is nonzero, or
2261 // * the least significant byte is 0xff and the second byte is nonzero, or
2262 // * the least significant 2 bytes are 0xff and the third is nonzero.
2263 if ((SplatBits & ~0xff) == 0 ||
2264 (SplatBits & ~0xff00) == 0 ||
2265 (SplatBits & ~0xff0000) == 0 ||
2266 (SplatBits & ~0xff000000) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002267 return DAG.getTargetConstant(SplatBits, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002268
2269 if ((SplatBits & ~0xffff) == 0 &&
2270 ((SplatBits | SplatUndef) & 0xff) == 0xff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002271 return DAG.getTargetConstant(SplatBits | 0xff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002272
2273 if ((SplatBits & ~0xffffff) == 0 &&
2274 ((SplatBits | SplatUndef) & 0xffff) == 0xffff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002275 return DAG.getTargetConstant(SplatBits | 0xffff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002276
2277 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
2278 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
2279 // VMOV.I32. A (very) minor optimization would be to replicate the value
2280 // and fall through here to test for a valid 64-bit splat. But, then the
2281 // caller would also need to check and handle the change in size.
2282 break;
2283
2284 case 64: {
2285 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
2286 uint64_t BitMask = 0xff;
2287 uint64_t Val = 0;
2288 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
2289 if (((SplatBits | SplatUndef) & BitMask) == BitMask)
2290 Val |= BitMask;
2291 else if ((SplatBits & BitMask) != 0)
2292 return SDValue();
2293 BitMask <<= 8;
2294 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002295 return DAG.getTargetConstant(Val, MVT::i64);
Bob Wilson5bafff32009-06-22 23:27:02 +00002296 }
2297
2298 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002299 llvm_unreachable("unexpected size for isVMOVSplat");
Bob Wilson5bafff32009-06-22 23:27:02 +00002300 break;
2301 }
2302
2303 return SDValue();
2304}
2305
2306/// getVMOVImm - If this is a build_vector of constants which can be
2307/// formed by using a VMOV instruction of the specified element size,
2308/// return the constant being splatted. The ByteSize field indicates the
2309/// number of bytes of each element [1248].
2310SDValue ARM::getVMOVImm(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
2311 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
2312 APInt SplatBits, SplatUndef;
2313 unsigned SplatBitSize;
2314 bool HasAnyUndefs;
2315 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
2316 HasAnyUndefs, ByteSize * 8))
2317 return SDValue();
2318
2319 if (SplatBitSize > ByteSize * 8)
2320 return SDValue();
2321
2322 return isVMOVSplat(SplatBits.getZExtValue(), SplatUndef.getZExtValue(),
2323 SplatBitSize, DAG);
2324}
2325
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002326static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
2327 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002328 unsigned NumElts = VT.getVectorNumElements();
2329 ReverseVEXT = false;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002330 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002331
2332 // If this is a VEXT shuffle, the immediate value is the index of the first
2333 // element. The other shuffle indices must be the successive elements after
2334 // the first one.
2335 unsigned ExpectedElt = Imm;
2336 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002337 // Increment the expected index. If it wraps around, it may still be
2338 // a VEXT but the source vectors must be swapped.
2339 ExpectedElt += 1;
2340 if (ExpectedElt == NumElts * 2) {
2341 ExpectedElt = 0;
2342 ReverseVEXT = true;
2343 }
2344
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002345 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002346 return false;
2347 }
2348
2349 // Adjust the index value if the source operands will be swapped.
2350 if (ReverseVEXT)
2351 Imm -= NumElts;
2352
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002353 return true;
2354}
2355
Bob Wilson8bb9e482009-07-26 00:39:34 +00002356/// isVREVMask - Check if a vector shuffle corresponds to a VREV
2357/// instruction with the specified blocksize. (The order of the elements
2358/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002359static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
2360 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00002361 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
2362 "Only possible block sizes for VREV are: 16, 32, 64");
2363
Bob Wilson8bb9e482009-07-26 00:39:34 +00002364 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00002365 if (EltSz == 64)
2366 return false;
2367
2368 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002369 unsigned BlockElts = M[0] + 1;
Bob Wilson8bb9e482009-07-26 00:39:34 +00002370
2371 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
2372 return false;
2373
2374 for (unsigned i = 0; i < NumElts; ++i) {
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002375 if ((unsigned) M[i] !=
Bob Wilson8bb9e482009-07-26 00:39:34 +00002376 (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
2377 return false;
2378 }
2379
2380 return true;
2381}
2382
Bob Wilsonc692cb72009-08-21 20:54:19 +00002383static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
2384 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002385 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2386 if (EltSz == 64)
2387 return false;
2388
Bob Wilsonc692cb72009-08-21 20:54:19 +00002389 unsigned NumElts = VT.getVectorNumElements();
2390 WhichResult = (M[0] == 0 ? 0 : 1);
2391 for (unsigned i = 0; i < NumElts; i += 2) {
2392 if ((unsigned) M[i] != i + WhichResult ||
2393 (unsigned) M[i+1] != i + NumElts + WhichResult)
2394 return false;
2395 }
2396 return true;
2397}
2398
2399static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
2400 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002401 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2402 if (EltSz == 64)
2403 return false;
2404
Bob Wilsonc692cb72009-08-21 20:54:19 +00002405 unsigned NumElts = VT.getVectorNumElements();
2406 WhichResult = (M[0] == 0 ? 0 : 1);
2407 for (unsigned i = 0; i != NumElts; ++i) {
2408 if ((unsigned) M[i] != 2 * i + WhichResult)
2409 return false;
2410 }
2411
2412 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00002413 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002414 return false;
2415
2416 return true;
2417}
2418
2419static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
2420 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002421 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2422 if (EltSz == 64)
2423 return false;
2424
Bob Wilsonc692cb72009-08-21 20:54:19 +00002425 unsigned NumElts = VT.getVectorNumElements();
2426 WhichResult = (M[0] == 0 ? 0 : 1);
2427 unsigned Idx = WhichResult * NumElts / 2;
2428 for (unsigned i = 0; i != NumElts; i += 2) {
2429 if ((unsigned) M[i] != Idx ||
2430 (unsigned) M[i+1] != Idx + NumElts)
2431 return false;
2432 Idx += 1;
2433 }
2434
2435 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00002436 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002437 return false;
2438
2439 return true;
2440}
2441
Owen Andersone50ed302009-08-10 22:56:29 +00002442static SDValue BuildSplat(SDValue Val, EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002443 // Canonicalize all-zeros and all-ones vectors.
Bob Wilsond06791f2009-08-13 01:57:47 +00002444 ConstantSDNode *ConstVal = cast<ConstantSDNode>(Val.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002445 if (ConstVal->isNullValue())
2446 return getZeroVector(VT, DAG, dl);
2447 if (ConstVal->isAllOnesValue())
2448 return getOnesVector(VT, DAG, dl);
2449
Owen Andersone50ed302009-08-10 22:56:29 +00002450 EVT CanonicalVT;
Bob Wilson5bafff32009-06-22 23:27:02 +00002451 if (VT.is64BitVector()) {
2452 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002453 case 8: CanonicalVT = MVT::v8i8; break;
2454 case 16: CanonicalVT = MVT::v4i16; break;
2455 case 32: CanonicalVT = MVT::v2i32; break;
2456 case 64: CanonicalVT = MVT::v1i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002457 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002458 }
2459 } else {
2460 assert(VT.is128BitVector() && "unknown splat vector size");
2461 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002462 case 8: CanonicalVT = MVT::v16i8; break;
2463 case 16: CanonicalVT = MVT::v8i16; break;
2464 case 32: CanonicalVT = MVT::v4i32; break;
2465 case 64: CanonicalVT = MVT::v2i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002466 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002467 }
2468 }
2469
2470 // Build a canonical splat for this value.
2471 SmallVector<SDValue, 8> Ops;
2472 Ops.assign(CanonicalVT.getVectorNumElements(), Val);
2473 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, &Ops[0],
2474 Ops.size());
2475 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Res);
2476}
2477
2478// If this is a case we can't handle, return null and let the default
2479// expansion code take care of it.
2480static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bob Wilsond06791f2009-08-13 01:57:47 +00002481 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002482 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002483 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002484
2485 APInt SplatBits, SplatUndef;
2486 unsigned SplatBitSize;
2487 bool HasAnyUndefs;
2488 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00002489 if (SplatBitSize <= 64) {
2490 SDValue Val = isVMOVSplat(SplatBits.getZExtValue(),
2491 SplatUndef.getZExtValue(), SplatBitSize, DAG);
2492 if (Val.getNode())
2493 return BuildSplat(Val, VT, DAG, dl);
2494 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00002495 }
2496
2497 // If there are only 2 elements in a 128-bit vector, insert them into an
2498 // undef vector. This handles the common case for 128-bit vector argument
2499 // passing, where the insertions should be translated to subreg accesses
2500 // with no real instructions.
2501 if (VT.is128BitVector() && Op.getNumOperands() == 2) {
2502 SDValue Val = DAG.getUNDEF(VT);
2503 SDValue Op0 = Op.getOperand(0);
2504 SDValue Op1 = Op.getOperand(1);
2505 if (Op0.getOpcode() != ISD::UNDEF)
2506 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op0,
2507 DAG.getIntPtrConstant(0));
2508 if (Op1.getOpcode() != ISD::UNDEF)
2509 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op1,
2510 DAG.getIntPtrConstant(1));
2511 return Val;
Bob Wilson5bafff32009-06-22 23:27:02 +00002512 }
2513
2514 return SDValue();
2515}
2516
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002517/// isShuffleMaskLegal - Targets can use this to indicate that they only
2518/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
2519/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
2520/// are assumed to be legal.
2521bool
2522ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
2523 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002524 if (VT.getVectorNumElements() == 4 &&
2525 (VT.is128BitVector() || VT.is64BitVector())) {
2526 unsigned PFIndexes[4];
2527 for (unsigned i = 0; i != 4; ++i) {
2528 if (M[i] < 0)
2529 PFIndexes[i] = 8;
2530 else
2531 PFIndexes[i] = M[i];
2532 }
2533
2534 // Compute the index in the perfect shuffle table.
2535 unsigned PFTableIndex =
2536 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2537 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2538 unsigned Cost = (PFEntry >> 30);
2539
2540 if (Cost <= 4)
2541 return true;
2542 }
2543
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002544 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00002545 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002546
2547 return (ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
2548 isVREVMask(M, VT, 64) ||
2549 isVREVMask(M, VT, 32) ||
2550 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00002551 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
2552 isVTRNMask(M, VT, WhichResult) ||
2553 isVUZPMask(M, VT, WhichResult) ||
2554 isVZIPMask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002555}
2556
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002557/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
2558/// the specified operations to build the shuffle.
2559static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
2560 SDValue RHS, SelectionDAG &DAG,
2561 DebugLoc dl) {
2562 unsigned OpNum = (PFEntry >> 26) & 0x0F;
2563 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
2564 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
2565
2566 enum {
2567 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
2568 OP_VREV,
2569 OP_VDUP0,
2570 OP_VDUP1,
2571 OP_VDUP2,
2572 OP_VDUP3,
2573 OP_VEXT1,
2574 OP_VEXT2,
2575 OP_VEXT3,
2576 OP_VUZPL, // VUZP, left result
2577 OP_VUZPR, // VUZP, right result
2578 OP_VZIPL, // VZIP, left result
2579 OP_VZIPR, // VZIP, right result
2580 OP_VTRNL, // VTRN, left result
2581 OP_VTRNR // VTRN, right result
2582 };
2583
2584 if (OpNum == OP_COPY) {
2585 if (LHSID == (1*9+2)*9+3) return LHS;
2586 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
2587 return RHS;
2588 }
2589
2590 SDValue OpLHS, OpRHS;
2591 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
2592 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
2593 EVT VT = OpLHS.getValueType();
2594
2595 switch (OpNum) {
2596 default: llvm_unreachable("Unknown shuffle opcode!");
2597 case OP_VREV:
2598 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
2599 case OP_VDUP0:
2600 case OP_VDUP1:
2601 case OP_VDUP2:
2602 case OP_VDUP3:
2603 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002604 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002605 case OP_VEXT1:
2606 case OP_VEXT2:
2607 case OP_VEXT3:
2608 return DAG.getNode(ARMISD::VEXT, dl, VT,
2609 OpLHS, OpRHS,
2610 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
2611 case OP_VUZPL:
2612 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002613 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002614 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
2615 case OP_VZIPL:
2616 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002617 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002618 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
2619 case OP_VTRNL:
2620 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002621 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2622 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002623 }
2624}
2625
Bob Wilson5bafff32009-06-22 23:27:02 +00002626static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002627 SDValue V1 = Op.getOperand(0);
2628 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00002629 DebugLoc dl = Op.getDebugLoc();
2630 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002631 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002632 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00002633
Bob Wilson28865062009-08-13 02:13:04 +00002634 // Convert shuffles that are directly supported on NEON to target-specific
2635 // DAG nodes, instead of keeping them as shuffles and matching them again
2636 // during code selection. This is more efficient and avoids the possibility
2637 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00002638 // FIXME: floating-point vectors should be canonicalized to integer vectors
2639 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002640 SVN->getMask(ShuffleMask);
2641
2642 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
Bob Wilson0ce37102009-08-14 05:08:32 +00002643 int Lane = SVN->getSplatIndex();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002644 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
2645 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00002646 }
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002647 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002648 DAG.getConstant(Lane, MVT::i32));
Bob Wilson0ce37102009-08-14 05:08:32 +00002649 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002650
2651 bool ReverseVEXT;
2652 unsigned Imm;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002653 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002654 if (ReverseVEXT)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002655 std::swap(V1, V2);
2656 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002657 DAG.getConstant(Imm, MVT::i32));
2658 }
2659
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002660 if (isVREVMask(ShuffleMask, VT, 64))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002661 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002662 if (isVREVMask(ShuffleMask, VT, 32))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002663 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002664 if (isVREVMask(ShuffleMask, VT, 16))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002665 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
2666
Bob Wilsonc692cb72009-08-21 20:54:19 +00002667 // Check for Neon shuffles that modify both input vectors in place.
2668 // If both results are used, i.e., if there are two shuffles with the same
2669 // source operands and with masks corresponding to both results of one of
2670 // these operations, DAG memoization will ensure that a single node is
2671 // used for both shuffles.
2672 unsigned WhichResult;
2673 if (isVTRNMask(ShuffleMask, VT, WhichResult))
2674 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2675 V1, V2).getValue(WhichResult);
2676 if (isVUZPMask(ShuffleMask, VT, WhichResult))
2677 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
2678 V1, V2).getValue(WhichResult);
2679 if (isVZIPMask(ShuffleMask, VT, WhichResult))
2680 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
2681 V1, V2).getValue(WhichResult);
2682
2683 // If the shuffle is not directly supported and it has 4 elements, use
2684 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002685 if (VT.getVectorNumElements() == 4 &&
2686 (VT.is128BitVector() || VT.is64BitVector())) {
2687 unsigned PFIndexes[4];
2688 for (unsigned i = 0; i != 4; ++i) {
2689 if (ShuffleMask[i] < 0)
2690 PFIndexes[i] = 8;
2691 else
2692 PFIndexes[i] = ShuffleMask[i];
2693 }
2694
2695 // Compute the index in the perfect shuffle table.
2696 unsigned PFTableIndex =
2697 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2698
2699 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2700 unsigned Cost = (PFEntry >> 30);
2701
2702 if (Cost <= 4)
2703 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
2704 }
Bob Wilsond8e17572009-08-12 22:31:50 +00002705
Bob Wilson22cac0d2009-08-14 05:16:33 +00002706 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00002707}
2708
Bob Wilson5bafff32009-06-22 23:27:02 +00002709static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002710 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002711 DebugLoc dl = Op.getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00002712 SDValue Vec = Op.getOperand(0);
2713 SDValue Lane = Op.getOperand(1);
Bob Wilson934f98b2009-10-15 23:12:05 +00002714 assert(VT == MVT::i32 &&
2715 Vec.getValueType().getVectorElementType().getSizeInBits() < 32 &&
2716 "unexpected type for custom-lowering vector extract");
2717 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
Bob Wilson5bafff32009-06-22 23:27:02 +00002718}
2719
Bob Wilsona6d65862009-08-03 20:36:38 +00002720static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
2721 // The only time a CONCAT_VECTORS operation can have legal types is when
2722 // two 64-bit vectors are concatenated to a 128-bit vector.
2723 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
2724 "unexpected CONCAT_VECTORS");
2725 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00002726 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00002727 SDValue Op0 = Op.getOperand(0);
2728 SDValue Op1 = Op.getOperand(1);
2729 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00002730 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
2731 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00002732 DAG.getIntPtrConstant(0));
2733 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00002734 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
2735 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00002736 DAG.getIntPtrConstant(1));
2737 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00002738}
2739
Dan Gohman475871a2008-07-27 21:46:04 +00002740SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00002741 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002742 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00002743 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002744 case ISD::GlobalAddress:
2745 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
2746 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002747 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002748 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, Subtarget);
2749 case ISD::BR_CC: return LowerBR_CC(Op, DAG, Subtarget);
2750 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Evan Cheng86198642009-08-07 00:34:42 +00002751 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002752 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
2753 case ISD::SINT_TO_FP:
2754 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
2755 case ISD::FP_TO_SINT:
2756 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
2757 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00002758 case ISD::RETURNADDR: break;
Jim Grosbach0e0da732009-05-12 23:59:14 +00002759 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002760 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002761 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00002762 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00002763 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00002764 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00002765 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
2766 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
2767 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2768 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00002769 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00002770 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002771 }
Dan Gohman475871a2008-07-27 21:46:04 +00002772 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00002773}
2774
Duncan Sands1607f052008-12-01 11:39:25 +00002775/// ReplaceNodeResults - Replace the results of node with an illegal result
2776/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00002777void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
2778 SmallVectorImpl<SDValue>&Results,
2779 SelectionDAG &DAG) {
Chris Lattner27a6c732007-11-24 07:07:01 +00002780 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00002781 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002782 llvm_unreachable("Don't know how to custom expand this!");
Duncan Sands1607f052008-12-01 11:39:25 +00002783 return;
2784 case ISD::BIT_CONVERT:
2785 Results.push_back(ExpandBIT_CONVERT(N, DAG));
2786 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00002787 case ISD::SRL:
Duncan Sands1607f052008-12-01 11:39:25 +00002788 case ISD::SRA: {
Bob Wilson5bafff32009-06-22 23:27:02 +00002789 SDValue Res = LowerShift(N, DAG, Subtarget);
Duncan Sands1607f052008-12-01 11:39:25 +00002790 if (Res.getNode())
2791 Results.push_back(Res);
2792 return;
2793 }
Chris Lattner27a6c732007-11-24 07:07:01 +00002794 }
2795}
Chris Lattner27a6c732007-11-24 07:07:01 +00002796
Evan Chenga8e29892007-01-19 07:51:42 +00002797//===----------------------------------------------------------------------===//
2798// ARM Scheduler Hooks
2799//===----------------------------------------------------------------------===//
2800
2801MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00002802ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +00002803 MachineBasicBlock *BB,
2804 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002805 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00002806 DebugLoc dl = MI->getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002807 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00002808 default:
2809 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng007ea272009-08-12 05:17:19 +00002810 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00002811 // To "insert" a SELECT_CC instruction, we actually have to insert the
2812 // diamond control-flow pattern. The incoming instruction knows the
2813 // destination vreg to set, the condition code register to branch on, the
2814 // true/false values to select between, and a branch opcode to use.
2815 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002816 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00002817 ++It;
2818
2819 // thisMBB:
2820 // ...
2821 // TrueVal = ...
2822 // cmpTY ccX, r1, r2
2823 // bCC copy1MBB
2824 // fallthrough --> copy0MBB
2825 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002826 MachineFunction *F = BB->getParent();
2827 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
2828 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenb6728402009-02-13 02:25:56 +00002829 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00002830 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002831 F->insert(It, copy0MBB);
2832 F->insert(It, sinkMBB);
Evan Chenga8e29892007-01-19 07:51:42 +00002833 // Update machine-CFG edges by first adding all successors of the current
2834 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00002835 // Also inform sdisel of the edge changes.
2836 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
2837 E = BB->succ_end(); I != E; ++I) {
2838 EM->insert(std::make_pair(*I, sinkMBB));
2839 sinkMBB->addSuccessor(*I);
2840 }
Evan Chenga8e29892007-01-19 07:51:42 +00002841 // Next, remove all successors of the current block, and add the true
2842 // and fallthrough blocks as its successors.
Evan Chengce319102009-09-19 09:51:03 +00002843 while (!BB->succ_empty())
Evan Chenga8e29892007-01-19 07:51:42 +00002844 BB->removeSuccessor(BB->succ_begin());
2845 BB->addSuccessor(copy0MBB);
2846 BB->addSuccessor(sinkMBB);
2847
2848 // copy0MBB:
2849 // %FalseValue = ...
2850 // # fallthrough to sinkMBB
2851 BB = copy0MBB;
2852
2853 // Update machine-CFG edges
2854 BB->addSuccessor(sinkMBB);
2855
2856 // sinkMBB:
2857 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2858 // ...
2859 BB = sinkMBB;
Dale Johannesenb6728402009-02-13 02:25:56 +00002860 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00002861 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
2862 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
2863
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002864 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00002865 return BB;
2866 }
Evan Cheng86198642009-08-07 00:34:42 +00002867
2868 case ARM::tANDsp:
2869 case ARM::tADDspr_:
2870 case ARM::tSUBspi_:
2871 case ARM::t2SUBrSPi_:
2872 case ARM::t2SUBrSPi12_:
2873 case ARM::t2SUBrSPs_: {
2874 MachineFunction *MF = BB->getParent();
2875 unsigned DstReg = MI->getOperand(0).getReg();
2876 unsigned SrcReg = MI->getOperand(1).getReg();
2877 bool DstIsDead = MI->getOperand(0).isDead();
2878 bool SrcIsKill = MI->getOperand(1).isKill();
2879
2880 if (SrcReg != ARM::SP) {
2881 // Copy the source to SP from virtual register.
2882 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(SrcReg);
2883 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
2884 ? ARM::tMOVtgpr2gpr : ARM::tMOVgpr2gpr;
2885 BuildMI(BB, dl, TII->get(CopyOpc), ARM::SP)
2886 .addReg(SrcReg, getKillRegState(SrcIsKill));
2887 }
2888
2889 unsigned OpOpc = 0;
2890 bool NeedPred = false, NeedCC = false, NeedOp3 = false;
2891 switch (MI->getOpcode()) {
2892 default:
2893 llvm_unreachable("Unexpected pseudo instruction!");
2894 case ARM::tANDsp:
2895 OpOpc = ARM::tAND;
2896 NeedPred = true;
2897 break;
2898 case ARM::tADDspr_:
2899 OpOpc = ARM::tADDspr;
2900 break;
2901 case ARM::tSUBspi_:
2902 OpOpc = ARM::tSUBspi;
2903 break;
2904 case ARM::t2SUBrSPi_:
2905 OpOpc = ARM::t2SUBrSPi;
2906 NeedPred = true; NeedCC = true;
2907 break;
2908 case ARM::t2SUBrSPi12_:
2909 OpOpc = ARM::t2SUBrSPi12;
2910 NeedPred = true;
2911 break;
2912 case ARM::t2SUBrSPs_:
2913 OpOpc = ARM::t2SUBrSPs;
2914 NeedPred = true; NeedCC = true; NeedOp3 = true;
2915 break;
2916 }
2917 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(OpOpc), ARM::SP);
2918 if (OpOpc == ARM::tAND)
2919 AddDefaultT1CC(MIB);
2920 MIB.addReg(ARM::SP);
2921 MIB.addOperand(MI->getOperand(2));
2922 if (NeedOp3)
2923 MIB.addOperand(MI->getOperand(3));
2924 if (NeedPred)
2925 AddDefaultPred(MIB);
2926 if (NeedCC)
2927 AddDefaultCC(MIB);
2928
2929 // Copy the result from SP to virtual register.
2930 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(DstReg);
2931 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
2932 ? ARM::tMOVgpr2tgpr : ARM::tMOVgpr2gpr;
2933 BuildMI(BB, dl, TII->get(CopyOpc))
2934 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
2935 .addReg(ARM::SP);
2936 MF->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
2937 return BB;
2938 }
Evan Chenga8e29892007-01-19 07:51:42 +00002939 }
2940}
2941
2942//===----------------------------------------------------------------------===//
2943// ARM Optimization Hooks
2944//===----------------------------------------------------------------------===//
2945
Chris Lattnerd1980a52009-03-12 06:52:53 +00002946static
2947SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
2948 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00002949 SelectionDAG &DAG = DCI.DAG;
2950 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00002951 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00002952 unsigned Opc = N->getOpcode();
2953 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
2954 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
2955 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
2956 ISD::CondCode CC = ISD::SETCC_INVALID;
2957
2958 if (isSlctCC) {
2959 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
2960 } else {
2961 SDValue CCOp = Slct.getOperand(0);
2962 if (CCOp.getOpcode() == ISD::SETCC)
2963 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
2964 }
2965
2966 bool DoXform = false;
2967 bool InvCC = false;
2968 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
2969 "Bad input!");
2970
2971 if (LHS.getOpcode() == ISD::Constant &&
2972 cast<ConstantSDNode>(LHS)->isNullValue()) {
2973 DoXform = true;
2974 } else if (CC != ISD::SETCC_INVALID &&
2975 RHS.getOpcode() == ISD::Constant &&
2976 cast<ConstantSDNode>(RHS)->isNullValue()) {
2977 std::swap(LHS, RHS);
2978 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00002979 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00002980 Op0.getOperand(0).getValueType();
2981 bool isInt = OpVT.isInteger();
2982 CC = ISD::getSetCCInverse(CC, isInt);
2983
2984 if (!TLI.isCondCodeLegal(CC, OpVT))
2985 return SDValue(); // Inverse operator isn't legal.
2986
2987 DoXform = true;
2988 InvCC = true;
2989 }
2990
2991 if (DoXform) {
2992 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
2993 if (isSlctCC)
2994 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
2995 Slct.getOperand(0), Slct.getOperand(1), CC);
2996 SDValue CCOp = Slct.getOperand(0);
2997 if (InvCC)
2998 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
2999 CCOp.getOperand(0), CCOp.getOperand(1), CC);
3000 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
3001 CCOp, OtherOp, Result);
3002 }
3003 return SDValue();
3004}
3005
3006/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
3007static SDValue PerformADDCombine(SDNode *N,
3008 TargetLowering::DAGCombinerInfo &DCI) {
3009 // added by evan in r37685 with no testcase.
3010 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003011
Chris Lattnerd1980a52009-03-12 06:52:53 +00003012 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
3013 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
3014 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
3015 if (Result.getNode()) return Result;
3016 }
3017 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3018 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3019 if (Result.getNode()) return Result;
3020 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003021
Chris Lattnerd1980a52009-03-12 06:52:53 +00003022 return SDValue();
3023}
3024
3025/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
3026static SDValue PerformSUBCombine(SDNode *N,
3027 TargetLowering::DAGCombinerInfo &DCI) {
3028 // added by evan in r37685 with no testcase.
3029 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003030
Chris Lattnerd1980a52009-03-12 06:52:53 +00003031 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
3032 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3033 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3034 if (Result.getNode()) return Result;
3035 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003036
Chris Lattnerd1980a52009-03-12 06:52:53 +00003037 return SDValue();
3038}
3039
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003040/// PerformFMRRDCombine - Target-specific dag combine xforms for ARMISD::FMRRD.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003041static SDValue PerformFMRRDCombine(SDNode *N,
3042 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003043 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00003044 SDValue InDouble = N->getOperand(0);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003045 if (InDouble.getOpcode() == ARMISD::FMDRR)
3046 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003047 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003048}
3049
Bob Wilson5bafff32009-06-22 23:27:02 +00003050/// getVShiftImm - Check if this is a valid build_vector for the immediate
3051/// operand of a vector shift operation, where all the elements of the
3052/// build_vector must have the same constant integer value.
3053static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
3054 // Ignore bit_converts.
3055 while (Op.getOpcode() == ISD::BIT_CONVERT)
3056 Op = Op.getOperand(0);
3057 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
3058 APInt SplatBits, SplatUndef;
3059 unsigned SplatBitSize;
3060 bool HasAnyUndefs;
3061 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
3062 HasAnyUndefs, ElementBits) ||
3063 SplatBitSize > ElementBits)
3064 return false;
3065 Cnt = SplatBits.getSExtValue();
3066 return true;
3067}
3068
3069/// isVShiftLImm - Check if this is a valid build_vector for the immediate
3070/// operand of a vector shift left operation. That value must be in the range:
3071/// 0 <= Value < ElementBits for a left shift; or
3072/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003073static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003074 assert(VT.isVector() && "vector shift count is not a vector type");
3075 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3076 if (! getVShiftImm(Op, ElementBits, Cnt))
3077 return false;
3078 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
3079}
3080
3081/// isVShiftRImm - Check if this is a valid build_vector for the immediate
3082/// operand of a vector shift right operation. For a shift opcode, the value
3083/// is positive, but for an intrinsic the value count must be negative. The
3084/// absolute value must be in the range:
3085/// 1 <= |Value| <= ElementBits for a right shift; or
3086/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003087static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00003088 int64_t &Cnt) {
3089 assert(VT.isVector() && "vector shift count is not a vector type");
3090 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3091 if (! getVShiftImm(Op, ElementBits, Cnt))
3092 return false;
3093 if (isIntrinsic)
3094 Cnt = -Cnt;
3095 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
3096}
3097
3098/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
3099static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
3100 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
3101 switch (IntNo) {
3102 default:
3103 // Don't do anything for most intrinsics.
3104 break;
3105
3106 // Vector shifts: check for immediate versions and lower them.
3107 // Note: This is done during DAG combining instead of DAG legalizing because
3108 // the build_vectors for 64-bit vector element shift counts are generally
3109 // not legal, and it is hard to see their values after they get legalized to
3110 // loads from a constant pool.
3111 case Intrinsic::arm_neon_vshifts:
3112 case Intrinsic::arm_neon_vshiftu:
3113 case Intrinsic::arm_neon_vshiftls:
3114 case Intrinsic::arm_neon_vshiftlu:
3115 case Intrinsic::arm_neon_vshiftn:
3116 case Intrinsic::arm_neon_vrshifts:
3117 case Intrinsic::arm_neon_vrshiftu:
3118 case Intrinsic::arm_neon_vrshiftn:
3119 case Intrinsic::arm_neon_vqshifts:
3120 case Intrinsic::arm_neon_vqshiftu:
3121 case Intrinsic::arm_neon_vqshiftsu:
3122 case Intrinsic::arm_neon_vqshiftns:
3123 case Intrinsic::arm_neon_vqshiftnu:
3124 case Intrinsic::arm_neon_vqshiftnsu:
3125 case Intrinsic::arm_neon_vqrshiftns:
3126 case Intrinsic::arm_neon_vqrshiftnu:
3127 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00003128 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003129 int64_t Cnt;
3130 unsigned VShiftOpc = 0;
3131
3132 switch (IntNo) {
3133 case Intrinsic::arm_neon_vshifts:
3134 case Intrinsic::arm_neon_vshiftu:
3135 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
3136 VShiftOpc = ARMISD::VSHL;
3137 break;
3138 }
3139 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
3140 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
3141 ARMISD::VSHRs : ARMISD::VSHRu);
3142 break;
3143 }
3144 return SDValue();
3145
3146 case Intrinsic::arm_neon_vshiftls:
3147 case Intrinsic::arm_neon_vshiftlu:
3148 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
3149 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003150 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003151
3152 case Intrinsic::arm_neon_vrshifts:
3153 case Intrinsic::arm_neon_vrshiftu:
3154 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
3155 break;
3156 return SDValue();
3157
3158 case Intrinsic::arm_neon_vqshifts:
3159 case Intrinsic::arm_neon_vqshiftu:
3160 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3161 break;
3162 return SDValue();
3163
3164 case Intrinsic::arm_neon_vqshiftsu:
3165 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3166 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003167 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003168
3169 case Intrinsic::arm_neon_vshiftn:
3170 case Intrinsic::arm_neon_vrshiftn:
3171 case Intrinsic::arm_neon_vqshiftns:
3172 case Intrinsic::arm_neon_vqshiftnu:
3173 case Intrinsic::arm_neon_vqshiftnsu:
3174 case Intrinsic::arm_neon_vqrshiftns:
3175 case Intrinsic::arm_neon_vqrshiftnu:
3176 case Intrinsic::arm_neon_vqrshiftnsu:
3177 // Narrowing shifts require an immediate right shift.
3178 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
3179 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003180 llvm_unreachable("invalid shift count for narrowing vector shift intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003181
3182 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003183 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003184 }
3185
3186 switch (IntNo) {
3187 case Intrinsic::arm_neon_vshifts:
3188 case Intrinsic::arm_neon_vshiftu:
3189 // Opcode already set above.
3190 break;
3191 case Intrinsic::arm_neon_vshiftls:
3192 case Intrinsic::arm_neon_vshiftlu:
3193 if (Cnt == VT.getVectorElementType().getSizeInBits())
3194 VShiftOpc = ARMISD::VSHLLi;
3195 else
3196 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
3197 ARMISD::VSHLLs : ARMISD::VSHLLu);
3198 break;
3199 case Intrinsic::arm_neon_vshiftn:
3200 VShiftOpc = ARMISD::VSHRN; break;
3201 case Intrinsic::arm_neon_vrshifts:
3202 VShiftOpc = ARMISD::VRSHRs; break;
3203 case Intrinsic::arm_neon_vrshiftu:
3204 VShiftOpc = ARMISD::VRSHRu; break;
3205 case Intrinsic::arm_neon_vrshiftn:
3206 VShiftOpc = ARMISD::VRSHRN; break;
3207 case Intrinsic::arm_neon_vqshifts:
3208 VShiftOpc = ARMISD::VQSHLs; break;
3209 case Intrinsic::arm_neon_vqshiftu:
3210 VShiftOpc = ARMISD::VQSHLu; break;
3211 case Intrinsic::arm_neon_vqshiftsu:
3212 VShiftOpc = ARMISD::VQSHLsu; break;
3213 case Intrinsic::arm_neon_vqshiftns:
3214 VShiftOpc = ARMISD::VQSHRNs; break;
3215 case Intrinsic::arm_neon_vqshiftnu:
3216 VShiftOpc = ARMISD::VQSHRNu; break;
3217 case Intrinsic::arm_neon_vqshiftnsu:
3218 VShiftOpc = ARMISD::VQSHRNsu; break;
3219 case Intrinsic::arm_neon_vqrshiftns:
3220 VShiftOpc = ARMISD::VQRSHRNs; break;
3221 case Intrinsic::arm_neon_vqrshiftnu:
3222 VShiftOpc = ARMISD::VQRSHRNu; break;
3223 case Intrinsic::arm_neon_vqrshiftnsu:
3224 VShiftOpc = ARMISD::VQRSHRNsu; break;
3225 }
3226
3227 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003228 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003229 }
3230
3231 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00003232 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003233 int64_t Cnt;
3234 unsigned VShiftOpc = 0;
3235
3236 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
3237 VShiftOpc = ARMISD::VSLI;
3238 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
3239 VShiftOpc = ARMISD::VSRI;
3240 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00003241 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003242 }
3243
3244 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
3245 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00003246 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003247 }
3248
3249 case Intrinsic::arm_neon_vqrshifts:
3250 case Intrinsic::arm_neon_vqrshiftu:
3251 // No immediate versions of these to check for.
3252 break;
3253 }
3254
3255 return SDValue();
3256}
3257
3258/// PerformShiftCombine - Checks for immediate versions of vector shifts and
3259/// lowers them. As with the vector shift intrinsics, this is done during DAG
3260/// combining instead of DAG legalizing because the build_vectors for 64-bit
3261/// vector element shift counts are generally not legal, and it is hard to see
3262/// their values after they get legalized to loads from a constant pool.
3263static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
3264 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003265 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003266
3267 // Nothing to be done for scalar shifts.
3268 if (! VT.isVector())
3269 return SDValue();
3270
3271 assert(ST->hasNEON() && "unexpected vector shift");
3272 int64_t Cnt;
3273
3274 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003275 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003276
3277 case ISD::SHL:
3278 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
3279 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003280 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003281 break;
3282
3283 case ISD::SRA:
3284 case ISD::SRL:
3285 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
3286 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
3287 ARMISD::VSHRs : ARMISD::VSHRu);
3288 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003289 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003290 }
3291 }
3292 return SDValue();
3293}
3294
3295/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
3296/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
3297static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
3298 const ARMSubtarget *ST) {
3299 SDValue N0 = N->getOperand(0);
3300
3301 // Check for sign- and zero-extensions of vector extract operations of 8-
3302 // and 16-bit vector elements. NEON supports these directly. They are
3303 // handled during DAG combining because type legalization will promote them
3304 // to 32-bit types and it is messy to recognize the operations after that.
3305 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
3306 SDValue Vec = N0.getOperand(0);
3307 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00003308 EVT VT = N->getValueType(0);
3309 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003310 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3311
Owen Anderson825b72b2009-08-11 20:47:22 +00003312 if (VT == MVT::i32 &&
3313 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00003314 TLI.isTypeLegal(Vec.getValueType())) {
3315
3316 unsigned Opc = 0;
3317 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003318 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003319 case ISD::SIGN_EXTEND:
3320 Opc = ARMISD::VGETLANEs;
3321 break;
3322 case ISD::ZERO_EXTEND:
3323 case ISD::ANY_EXTEND:
3324 Opc = ARMISD::VGETLANEu;
3325 break;
3326 }
3327 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
3328 }
3329 }
3330
3331 return SDValue();
3332}
3333
Dan Gohman475871a2008-07-27 21:46:04 +00003334SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003335 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003336 switch (N->getOpcode()) {
3337 default: break;
Chris Lattnerd1980a52009-03-12 06:52:53 +00003338 case ISD::ADD: return PerformADDCombine(N, DCI);
3339 case ISD::SUB: return PerformSUBCombine(N, DCI);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003340 case ARMISD::FMRRD: return PerformFMRRDCombine(N, DCI);
Bob Wilson5bafff32009-06-22 23:27:02 +00003341 case ISD::INTRINSIC_WO_CHAIN:
3342 return PerformIntrinsicCombine(N, DCI.DAG);
3343 case ISD::SHL:
3344 case ISD::SRA:
3345 case ISD::SRL:
3346 return PerformShiftCombine(N, DCI.DAG, Subtarget);
3347 case ISD::SIGN_EXTEND:
3348 case ISD::ZERO_EXTEND:
3349 case ISD::ANY_EXTEND:
3350 return PerformExtendCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003351 }
Dan Gohman475871a2008-07-27 21:46:04 +00003352 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003353}
3354
Bill Wendlingaf566342009-08-15 21:21:19 +00003355bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
3356 if (!Subtarget->hasV6Ops())
3357 // Pre-v6 does not support unaligned mem access.
3358 return false;
3359 else if (!Subtarget->hasV6Ops()) {
3360 // v6 may or may not support unaligned mem access.
3361 if (!Subtarget->isTargetDarwin())
3362 return false;
3363 }
3364
3365 switch (VT.getSimpleVT().SimpleTy) {
3366 default:
3367 return false;
3368 case MVT::i8:
3369 case MVT::i16:
3370 case MVT::i32:
3371 return true;
3372 // FIXME: VLD1 etc with standard alignment is legal.
3373 }
3374}
3375
Evan Chenge6c835f2009-08-14 20:09:37 +00003376static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
3377 if (V < 0)
3378 return false;
3379
3380 unsigned Scale = 1;
3381 switch (VT.getSimpleVT().SimpleTy) {
3382 default: return false;
3383 case MVT::i1:
3384 case MVT::i8:
3385 // Scale == 1;
3386 break;
3387 case MVT::i16:
3388 // Scale == 2;
3389 Scale = 2;
3390 break;
3391 case MVT::i32:
3392 // Scale == 4;
3393 Scale = 4;
3394 break;
3395 }
3396
3397 if ((V & (Scale - 1)) != 0)
3398 return false;
3399 V /= Scale;
3400 return V == (V & ((1LL << 5) - 1));
3401}
3402
3403static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
3404 const ARMSubtarget *Subtarget) {
3405 bool isNeg = false;
3406 if (V < 0) {
3407 isNeg = true;
3408 V = - V;
3409 }
3410
3411 switch (VT.getSimpleVT().SimpleTy) {
3412 default: return false;
3413 case MVT::i1:
3414 case MVT::i8:
3415 case MVT::i16:
3416 case MVT::i32:
3417 // + imm12 or - imm8
3418 if (isNeg)
3419 return V == (V & ((1LL << 8) - 1));
3420 return V == (V & ((1LL << 12) - 1));
3421 case MVT::f32:
3422 case MVT::f64:
3423 // Same as ARM mode. FIXME: NEON?
3424 if (!Subtarget->hasVFP2())
3425 return false;
3426 if ((V & 3) != 0)
3427 return false;
3428 V >>= 2;
3429 return V == (V & ((1LL << 8) - 1));
3430 }
3431}
3432
Evan Chengb01fad62007-03-12 23:30:29 +00003433/// isLegalAddressImmediate - Return true if the integer value can be used
3434/// as the offset of the target addressing mode for load / store of the
3435/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00003436static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00003437 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00003438 if (V == 0)
3439 return true;
3440
Evan Cheng65011532009-03-09 19:15:00 +00003441 if (!VT.isSimple())
3442 return false;
3443
Evan Chenge6c835f2009-08-14 20:09:37 +00003444 if (Subtarget->isThumb1Only())
3445 return isLegalT1AddressImmediate(V, VT);
3446 else if (Subtarget->isThumb2())
3447 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00003448
Evan Chenge6c835f2009-08-14 20:09:37 +00003449 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00003450 if (V < 0)
3451 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00003452 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00003453 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00003454 case MVT::i1:
3455 case MVT::i8:
3456 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00003457 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003458 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003459 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00003460 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003461 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003462 case MVT::f32:
3463 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00003464 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00003465 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00003466 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00003467 return false;
3468 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003469 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00003470 }
Evan Chenga8e29892007-01-19 07:51:42 +00003471}
3472
Evan Chenge6c835f2009-08-14 20:09:37 +00003473bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
3474 EVT VT) const {
3475 int Scale = AM.Scale;
3476 if (Scale < 0)
3477 return false;
3478
3479 switch (VT.getSimpleVT().SimpleTy) {
3480 default: return false;
3481 case MVT::i1:
3482 case MVT::i8:
3483 case MVT::i16:
3484 case MVT::i32:
3485 if (Scale == 1)
3486 return true;
3487 // r + r << imm
3488 Scale = Scale & ~1;
3489 return Scale == 2 || Scale == 4 || Scale == 8;
3490 case MVT::i64:
3491 // r + r
3492 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
3493 return true;
3494 return false;
3495 case MVT::isVoid:
3496 // Note, we allow "void" uses (basically, uses that aren't loads or
3497 // stores), because arm allows folding a scale into many arithmetic
3498 // operations. This should be made more precise and revisited later.
3499
3500 // Allow r << imm, but the imm has to be a multiple of two.
3501 if (Scale & 1) return false;
3502 return isPowerOf2_32(Scale);
3503 }
3504}
3505
Chris Lattner37caf8c2007-04-09 23:33:39 +00003506/// isLegalAddressingMode - Return true if the addressing mode represented
3507/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003508bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00003509 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00003510 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00003511 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00003512 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003513
Chris Lattner37caf8c2007-04-09 23:33:39 +00003514 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003515 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003516 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003517
Chris Lattner37caf8c2007-04-09 23:33:39 +00003518 switch (AM.Scale) {
3519 case 0: // no scale reg, must be "r+i" or "r", or "i".
3520 break;
3521 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00003522 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00003523 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00003524 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00003525 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00003526 // ARM doesn't support any R+R*scale+imm addr modes.
3527 if (AM.BaseOffs)
3528 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003529
Bob Wilson2c7dab12009-04-08 17:55:28 +00003530 if (!VT.isSimple())
3531 return false;
3532
Evan Chenge6c835f2009-08-14 20:09:37 +00003533 if (Subtarget->isThumb2())
3534 return isLegalT2ScaledAddressingMode(AM, VT);
3535
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003536 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00003537 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00003538 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00003539 case MVT::i1:
3540 case MVT::i8:
3541 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003542 if (Scale < 0) Scale = -Scale;
3543 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003544 return true;
3545 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00003546 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003547 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00003548 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00003549 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003550 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003551 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00003552 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003553
Owen Anderson825b72b2009-08-11 20:47:22 +00003554 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00003555 // Note, we allow "void" uses (basically, uses that aren't loads or
3556 // stores), because arm allows folding a scale into many arithmetic
3557 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003558
Chris Lattner37caf8c2007-04-09 23:33:39 +00003559 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00003560 if (Scale & 1) return false;
3561 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00003562 }
3563 break;
Evan Chengb01fad62007-03-12 23:30:29 +00003564 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00003565 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00003566}
3567
Owen Andersone50ed302009-08-10 22:56:29 +00003568static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00003569 bool isSEXTLoad, SDValue &Base,
3570 SDValue &Offset, bool &isInc,
3571 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00003572 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
3573 return false;
3574
Owen Anderson825b72b2009-08-11 20:47:22 +00003575 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00003576 // AddressingMode 3
3577 Base = Ptr->getOperand(0);
3578 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003579 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003580 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003581 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00003582 isInc = false;
3583 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3584 return true;
3585 }
3586 }
3587 isInc = (Ptr->getOpcode() == ISD::ADD);
3588 Offset = Ptr->getOperand(1);
3589 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00003590 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00003591 // AddressingMode 2
3592 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003593 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003594 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003595 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00003596 isInc = false;
3597 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3598 Base = Ptr->getOperand(0);
3599 return true;
3600 }
3601 }
3602
3603 if (Ptr->getOpcode() == ISD::ADD) {
3604 isInc = true;
3605 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
3606 if (ShOpcVal != ARM_AM::no_shift) {
3607 Base = Ptr->getOperand(1);
3608 Offset = Ptr->getOperand(0);
3609 } else {
3610 Base = Ptr->getOperand(0);
3611 Offset = Ptr->getOperand(1);
3612 }
3613 return true;
3614 }
3615
3616 isInc = (Ptr->getOpcode() == ISD::ADD);
3617 Base = Ptr->getOperand(0);
3618 Offset = Ptr->getOperand(1);
3619 return true;
3620 }
3621
3622 // FIXME: Use FLDM / FSTM to emulate indexed FP load / store.
3623 return false;
3624}
3625
Owen Andersone50ed302009-08-10 22:56:29 +00003626static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00003627 bool isSEXTLoad, SDValue &Base,
3628 SDValue &Offset, bool &isInc,
3629 SelectionDAG &DAG) {
3630 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
3631 return false;
3632
3633 Base = Ptr->getOperand(0);
3634 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
3635 int RHSC = (int)RHS->getZExtValue();
3636 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
3637 assert(Ptr->getOpcode() == ISD::ADD);
3638 isInc = false;
3639 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3640 return true;
3641 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
3642 isInc = Ptr->getOpcode() == ISD::ADD;
3643 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
3644 return true;
3645 }
3646 }
3647
3648 return false;
3649}
3650
Evan Chenga8e29892007-01-19 07:51:42 +00003651/// getPreIndexedAddressParts - returns true by value, base pointer and
3652/// offset pointer and addressing mode by reference if the node's address
3653/// can be legally represented as pre-indexed load / store address.
3654bool
Dan Gohman475871a2008-07-27 21:46:04 +00003655ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
3656 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003657 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00003658 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003659 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00003660 return false;
3661
Owen Andersone50ed302009-08-10 22:56:29 +00003662 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00003663 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00003664 bool isSEXTLoad = false;
3665 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
3666 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003667 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003668 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
3669 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
3670 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003671 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003672 } else
3673 return false;
3674
3675 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00003676 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00003677 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00003678 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
3679 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00003680 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00003681 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00003682 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00003683 if (!isLegal)
3684 return false;
3685
3686 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
3687 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00003688}
3689
3690/// getPostIndexedAddressParts - returns true by value, base pointer and
3691/// offset pointer and addressing mode by reference if this node can be
3692/// combined with a load / store to form a post-indexed load / store.
3693bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00003694 SDValue &Base,
3695 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003696 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00003697 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003698 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00003699 return false;
3700
Owen Andersone50ed302009-08-10 22:56:29 +00003701 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00003702 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00003703 bool isSEXTLoad = false;
3704 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003705 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003706 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
3707 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003708 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003709 } else
3710 return false;
3711
3712 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00003713 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00003714 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00003715 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003716 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00003717 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00003718 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
3719 isInc, DAG);
3720 if (!isLegal)
3721 return false;
3722
3723 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
3724 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00003725}
3726
Dan Gohman475871a2008-07-27 21:46:04 +00003727void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00003728 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003729 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003730 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00003731 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00003732 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003733 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00003734 switch (Op.getOpcode()) {
3735 default: break;
3736 case ARMISD::CMOV: {
3737 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00003738 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00003739 if (KnownZero == 0 && KnownOne == 0) return;
3740
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003741 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00003742 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
3743 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00003744 KnownZero &= KnownZeroRHS;
3745 KnownOne &= KnownOneRHS;
3746 return;
3747 }
3748 }
3749}
3750
3751//===----------------------------------------------------------------------===//
3752// ARM Inline Assembly Support
3753//===----------------------------------------------------------------------===//
3754
3755/// getConstraintType - Given a constraint letter, return the type of
3756/// constraint it is for this target.
3757ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00003758ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
3759 if (Constraint.size() == 1) {
3760 switch (Constraint[0]) {
3761 default: break;
3762 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003763 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00003764 }
Evan Chenga8e29892007-01-19 07:51:42 +00003765 }
Chris Lattner4234f572007-03-25 02:14:49 +00003766 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00003767}
3768
Bob Wilson2dc4f542009-03-20 22:42:55 +00003769std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00003770ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00003771 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003772 if (Constraint.size() == 1) {
3773 // GCC RS6000 Constraint Letters
3774 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003775 case 'l':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003776 if (Subtarget->isThumb1Only())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00003777 return std::make_pair(0U, ARM::tGPRRegisterClass);
3778 else
3779 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003780 case 'r':
3781 return std::make_pair(0U, ARM::GPRRegisterClass);
3782 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00003783 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003784 return std::make_pair(0U, ARM::SPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00003785 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003786 return std::make_pair(0U, ARM::DPRRegisterClass);
3787 break;
Evan Chenga8e29892007-01-19 07:51:42 +00003788 }
3789 }
3790 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3791}
3792
3793std::vector<unsigned> ARMTargetLowering::
3794getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00003795 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003796 if (Constraint.size() != 1)
3797 return std::vector<unsigned>();
3798
3799 switch (Constraint[0]) { // GCC ARM Constraint Letters
3800 default: break;
3801 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00003802 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
3803 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
3804 0);
Evan Chenga8e29892007-01-19 07:51:42 +00003805 case 'r':
3806 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
3807 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
3808 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
3809 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003810 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00003811 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003812 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
3813 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
3814 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
3815 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
3816 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
3817 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
3818 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
3819 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00003820 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003821 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
3822 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
3823 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
3824 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
3825 break;
Evan Chenga8e29892007-01-19 07:51:42 +00003826 }
3827
3828 return std::vector<unsigned>();
3829}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003830
3831/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3832/// vector. If it is invalid, don't add anything to Ops.
3833void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3834 char Constraint,
3835 bool hasMemory,
3836 std::vector<SDValue>&Ops,
3837 SelectionDAG &DAG) const {
3838 SDValue Result(0, 0);
3839
3840 switch (Constraint) {
3841 default: break;
3842 case 'I': case 'J': case 'K': case 'L':
3843 case 'M': case 'N': case 'O':
3844 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
3845 if (!C)
3846 return;
3847
3848 int64_t CVal64 = C->getSExtValue();
3849 int CVal = (int) CVal64;
3850 // None of these constraints allow values larger than 32 bits. Check
3851 // that the value fits in an int.
3852 if (CVal != CVal64)
3853 return;
3854
3855 switch (Constraint) {
3856 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003857 if (Subtarget->isThumb1Only()) {
3858 // This must be a constant between 0 and 255, for ADD
3859 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003860 if (CVal >= 0 && CVal <= 255)
3861 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00003862 } else if (Subtarget->isThumb2()) {
3863 // A constant that can be used as an immediate value in a
3864 // data-processing instruction.
3865 if (ARM_AM::getT2SOImmVal(CVal) != -1)
3866 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003867 } else {
3868 // A constant that can be used as an immediate value in a
3869 // data-processing instruction.
3870 if (ARM_AM::getSOImmVal(CVal) != -1)
3871 break;
3872 }
3873 return;
3874
3875 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003876 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003877 // This must be a constant between -255 and -1, for negated ADD
3878 // immediates. This can be used in GCC with an "n" modifier that
3879 // prints the negated value, for use with SUB instructions. It is
3880 // not useful otherwise but is implemented for compatibility.
3881 if (CVal >= -255 && CVal <= -1)
3882 break;
3883 } else {
3884 // This must be a constant between -4095 and 4095. It is not clear
3885 // what this constraint is intended for. Implemented for
3886 // compatibility with GCC.
3887 if (CVal >= -4095 && CVal <= 4095)
3888 break;
3889 }
3890 return;
3891
3892 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003893 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003894 // A 32-bit value where only one byte has a nonzero value. Exclude
3895 // zero to match GCC. This constraint is used by GCC internally for
3896 // constants that can be loaded with a move/shift combination.
3897 // It is not useful otherwise but is implemented for compatibility.
3898 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
3899 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00003900 } else if (Subtarget->isThumb2()) {
3901 // A constant whose bitwise inverse can be used as an immediate
3902 // value in a data-processing instruction. This can be used in GCC
3903 // with a "B" modifier that prints the inverted value, for use with
3904 // BIC and MVN instructions. It is not useful otherwise but is
3905 // implemented for compatibility.
3906 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
3907 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003908 } else {
3909 // A constant whose bitwise inverse can be used as an immediate
3910 // value in a data-processing instruction. This can be used in GCC
3911 // with a "B" modifier that prints the inverted value, for use with
3912 // BIC and MVN instructions. It is not useful otherwise but is
3913 // implemented for compatibility.
3914 if (ARM_AM::getSOImmVal(~CVal) != -1)
3915 break;
3916 }
3917 return;
3918
3919 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003920 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003921 // This must be a constant between -7 and 7,
3922 // for 3-operand ADD/SUB immediate instructions.
3923 if (CVal >= -7 && CVal < 7)
3924 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00003925 } else if (Subtarget->isThumb2()) {
3926 // A constant whose negation can be used as an immediate value in a
3927 // data-processing instruction. This can be used in GCC with an "n"
3928 // modifier that prints the negated value, for use with SUB
3929 // instructions. It is not useful otherwise but is implemented for
3930 // compatibility.
3931 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
3932 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003933 } else {
3934 // A constant whose negation can be used as an immediate value in a
3935 // data-processing instruction. This can be used in GCC with an "n"
3936 // modifier that prints the negated value, for use with SUB
3937 // instructions. It is not useful otherwise but is implemented for
3938 // compatibility.
3939 if (ARM_AM::getSOImmVal(-CVal) != -1)
3940 break;
3941 }
3942 return;
3943
3944 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003945 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003946 // This must be a multiple of 4 between 0 and 1020, for
3947 // ADD sp + immediate.
3948 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
3949 break;
3950 } else {
3951 // A power of two or a constant between 0 and 32. This is used in
3952 // GCC for the shift amount on shifted register operands, but it is
3953 // useful in general for any shift amounts.
3954 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
3955 break;
3956 }
3957 return;
3958
3959 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003960 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003961 // This must be a constant between 0 and 31, for shift amounts.
3962 if (CVal >= 0 && CVal <= 31)
3963 break;
3964 }
3965 return;
3966
3967 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003968 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003969 // This must be a multiple of 4 between -508 and 508, for
3970 // ADD/SUB sp = sp + immediate.
3971 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
3972 break;
3973 }
3974 return;
3975 }
3976 Result = DAG.getTargetConstant(CVal, Op.getValueType());
3977 break;
3978 }
3979
3980 if (Result.getNode()) {
3981 Ops.push_back(Result);
3982 return;
3983 }
3984 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
3985 Ops, DAG);
3986}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00003987
3988bool
3989ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3990 // The ARM target isn't yet aware of offsets.
3991 return false;
3992}