blob: 4a482fb6ec27f07a9780d916606edb9c31155975 [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
Evan Chengc5942082009-10-28 06:55:03 +0000498 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
499 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
500
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000501 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000502
Evan Cheng86198642009-08-07 00:34:42 +0000503 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
504
Bob Wilson5bafff32009-06-22 23:27:02 +0000505 case ARMISD::VCEQ: return "ARMISD::VCEQ";
506 case ARMISD::VCGE: return "ARMISD::VCGE";
507 case ARMISD::VCGEU: return "ARMISD::VCGEU";
508 case ARMISD::VCGT: return "ARMISD::VCGT";
509 case ARMISD::VCGTU: return "ARMISD::VCGTU";
510 case ARMISD::VTST: return "ARMISD::VTST";
511
512 case ARMISD::VSHL: return "ARMISD::VSHL";
513 case ARMISD::VSHRs: return "ARMISD::VSHRs";
514 case ARMISD::VSHRu: return "ARMISD::VSHRu";
515 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
516 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
517 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
518 case ARMISD::VSHRN: return "ARMISD::VSHRN";
519 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
520 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
521 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
522 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
523 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
524 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
525 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
526 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
527 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
528 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
529 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
530 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
531 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
532 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000533 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000534 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000535 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000536 case ARMISD::VREV64: return "ARMISD::VREV64";
537 case ARMISD::VREV32: return "ARMISD::VREV32";
538 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000539 case ARMISD::VZIP: return "ARMISD::VZIP";
540 case ARMISD::VUZP: return "ARMISD::VUZP";
541 case ARMISD::VTRN: return "ARMISD::VTRN";
Evan Chenga8e29892007-01-19 07:51:42 +0000542 }
543}
544
Bill Wendlingb4202b82009-07-01 18:50:55 +0000545/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000546unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Evan Cheng048e36f2009-10-02 06:57:25 +0000547 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 0 : 1;
Bill Wendling20c568f2009-06-30 22:38:32 +0000548}
549
Evan Chenga8e29892007-01-19 07:51:42 +0000550//===----------------------------------------------------------------------===//
551// Lowering Code
552//===----------------------------------------------------------------------===//
553
Evan Chenga8e29892007-01-19 07:51:42 +0000554/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
555static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
556 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000557 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000558 case ISD::SETNE: return ARMCC::NE;
559 case ISD::SETEQ: return ARMCC::EQ;
560 case ISD::SETGT: return ARMCC::GT;
561 case ISD::SETGE: return ARMCC::GE;
562 case ISD::SETLT: return ARMCC::LT;
563 case ISD::SETLE: return ARMCC::LE;
564 case ISD::SETUGT: return ARMCC::HI;
565 case ISD::SETUGE: return ARMCC::HS;
566 case ISD::SETULT: return ARMCC::LO;
567 case ISD::SETULE: return ARMCC::LS;
568 }
569}
570
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000571/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
572static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000573 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000574 CondCode2 = ARMCC::AL;
575 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000576 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000577 case ISD::SETEQ:
578 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
579 case ISD::SETGT:
580 case ISD::SETOGT: CondCode = ARMCC::GT; break;
581 case ISD::SETGE:
582 case ISD::SETOGE: CondCode = ARMCC::GE; break;
583 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000584 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000585 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
586 case ISD::SETO: CondCode = ARMCC::VC; break;
587 case ISD::SETUO: CondCode = ARMCC::VS; break;
588 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
589 case ISD::SETUGT: CondCode = ARMCC::HI; break;
590 case ISD::SETUGE: CondCode = ARMCC::PL; break;
591 case ISD::SETLT:
592 case ISD::SETULT: CondCode = ARMCC::LT; break;
593 case ISD::SETLE:
594 case ISD::SETULE: CondCode = ARMCC::LE; break;
595 case ISD::SETNE:
596 case ISD::SETUNE: CondCode = ARMCC::NE; break;
597 }
Evan Chenga8e29892007-01-19 07:51:42 +0000598}
599
Bob Wilson1f595bb2009-04-17 19:07:39 +0000600//===----------------------------------------------------------------------===//
601// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +0000602//===----------------------------------------------------------------------===//
603
604#include "ARMGenCallingConv.inc"
605
606// APCS f64 is in register pairs, possibly split to stack
Owen Andersone50ed302009-08-10 22:56:29 +0000607static bool f64AssignAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000608 CCValAssign::LocInfo &LocInfo,
609 CCState &State, bool CanFail) {
610 static const unsigned RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
611
612 // Try to get the first register.
613 if (unsigned Reg = State.AllocateReg(RegList, 4))
614 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
615 else {
616 // For the 2nd half of a v2f64, do not fail.
617 if (CanFail)
618 return false;
619
620 // Put the whole thing on the stack.
621 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
622 State.AllocateStack(8, 4),
623 LocVT, LocInfo));
624 return true;
625 }
626
627 // Try to get the second register.
628 if (unsigned Reg = State.AllocateReg(RegList, 4))
629 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
630 else
631 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
632 State.AllocateStack(4, 4),
633 LocVT, LocInfo));
634 return true;
635}
636
Owen Andersone50ed302009-08-10 22:56:29 +0000637static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000638 CCValAssign::LocInfo &LocInfo,
639 ISD::ArgFlagsTy &ArgFlags,
640 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000641 if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
642 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000643 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000644 !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
645 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000646 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000647}
648
649// AAPCS f64 is in aligned register pairs
Owen Andersone50ed302009-08-10 22:56:29 +0000650static bool f64AssignAAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000651 CCValAssign::LocInfo &LocInfo,
652 CCState &State, bool CanFail) {
653 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
654 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
655
656 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
657 if (Reg == 0) {
658 // For the 2nd half of a v2f64, do not just fail.
659 if (CanFail)
660 return false;
661
662 // Put the whole thing on the stack.
663 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
664 State.AllocateStack(8, 8),
665 LocVT, LocInfo));
666 return true;
667 }
668
669 unsigned i;
670 for (i = 0; i < 2; ++i)
671 if (HiRegList[i] == Reg)
672 break;
673
674 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
675 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
676 LocVT, LocInfo));
677 return true;
678}
679
Owen Andersone50ed302009-08-10 22:56:29 +0000680static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000681 CCValAssign::LocInfo &LocInfo,
682 ISD::ArgFlagsTy &ArgFlags,
683 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000684 if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
685 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000686 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000687 !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
688 return false;
689 return true; // we handled it
690}
691
Owen Andersone50ed302009-08-10 22:56:29 +0000692static bool f64RetAssign(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000693 CCValAssign::LocInfo &LocInfo, CCState &State) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000694 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
695 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
696
Bob Wilsone65586b2009-04-17 20:40:45 +0000697 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
698 if (Reg == 0)
699 return false; // we didn't handle it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000700
Bob Wilsone65586b2009-04-17 20:40:45 +0000701 unsigned i;
702 for (i = 0; i < 2; ++i)
703 if (HiRegList[i] == Reg)
704 break;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000705
Bob Wilson5bafff32009-06-22 23:27:02 +0000706 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bob Wilsone65586b2009-04-17 20:40:45 +0000707 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000708 LocVT, LocInfo));
709 return true;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000710}
711
Owen Andersone50ed302009-08-10 22:56:29 +0000712static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000713 CCValAssign::LocInfo &LocInfo,
714 ISD::ArgFlagsTy &ArgFlags,
715 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000716 if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
717 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000718 if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
Bob Wilson5bafff32009-06-22 23:27:02 +0000719 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000720 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000721}
722
Owen Andersone50ed302009-08-10 22:56:29 +0000723static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000724 CCValAssign::LocInfo &LocInfo,
725 ISD::ArgFlagsTy &ArgFlags,
726 CCState &State) {
727 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
728 State);
729}
730
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000731/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
732/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000733CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000734 bool Return,
735 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000736 switch (CC) {
737 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000738 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000739 case CallingConv::C:
740 case CallingConv::Fast:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000741 // Use target triple & subtarget features to do actual dispatch.
742 if (Subtarget->isAAPCS_ABI()) {
743 if (Subtarget->hasVFP2() &&
744 FloatABIType == FloatABI::Hard && !isVarArg)
745 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
746 else
747 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
748 } else
749 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000750 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000751 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000752 case CallingConv::ARM_AAPCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000753 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000754 case CallingConv::ARM_APCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000755 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000756 }
757}
758
Dan Gohman98ca4f22009-08-05 01:29:28 +0000759/// LowerCallResult - Lower the result values of a call into the
760/// appropriate copies out of appropriate physical registers.
761SDValue
762ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000763 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000764 const SmallVectorImpl<ISD::InputArg> &Ins,
765 DebugLoc dl, SelectionDAG &DAG,
766 SmallVectorImpl<SDValue> &InVals) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000767
Bob Wilson1f595bb2009-04-17 19:07:39 +0000768 // Assign locations to each value returned by this call.
769 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000770 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000771 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000772 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000773 CCAssignFnForNode(CallConv, /* Return*/ true,
774 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000775
776 // Copy all of the result registers out of their specified physreg.
777 for (unsigned i = 0; i != RVLocs.size(); ++i) {
778 CCValAssign VA = RVLocs[i];
779
Bob Wilson80915242009-04-25 00:33:20 +0000780 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000781 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000782 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000783 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000784 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000785 Chain = Lo.getValue(1);
786 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000787 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000788 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000789 InFlag);
790 Chain = Hi.getValue(1);
791 InFlag = Hi.getValue(2);
Owen Anderson825b72b2009-08-11 20:47:22 +0000792 Val = DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +0000793
Owen Anderson825b72b2009-08-11 20:47:22 +0000794 if (VA.getLocVT() == MVT::v2f64) {
795 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
796 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
797 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000798
799 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000800 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000801 Chain = Lo.getValue(1);
802 InFlag = Lo.getValue(2);
803 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000804 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000805 Chain = Hi.getValue(1);
806 InFlag = Hi.getValue(2);
Owen Anderson825b72b2009-08-11 20:47:22 +0000807 Val = DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
808 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
809 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000810 }
Bob Wilson1f595bb2009-04-17 19:07:39 +0000811 } else {
Bob Wilson80915242009-04-25 00:33:20 +0000812 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
813 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000814 Chain = Val.getValue(1);
815 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000816 }
Bob Wilson80915242009-04-25 00:33:20 +0000817
818 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000819 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +0000820 case CCValAssign::Full: break;
821 case CCValAssign::BCvt:
822 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
823 break;
824 }
825
Dan Gohman98ca4f22009-08-05 01:29:28 +0000826 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000827 }
828
Dan Gohman98ca4f22009-08-05 01:29:28 +0000829 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000830}
831
832/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
833/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +0000834/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +0000835/// a byval function parameter.
836/// Sometimes what we are copying is the end of a larger object, the part that
837/// does not fit in registers.
838static SDValue
839CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
840 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
841 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000842 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000843 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
844 /*AlwaysInline=*/false, NULL, 0, NULL, 0);
845}
846
Bob Wilsondee46d72009-04-17 20:35:10 +0000847/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000848SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +0000849ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
850 SDValue StackPtr, SDValue Arg,
851 DebugLoc dl, SelectionDAG &DAG,
852 const CCValAssign &VA,
853 ISD::ArgFlagsTy Flags) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000854 unsigned LocMemOffset = VA.getLocMemOffset();
855 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
856 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
857 if (Flags.isByVal()) {
858 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
859 }
860 return DAG.getStore(Chain, dl, Arg, PtrOff,
861 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chenga8e29892007-01-19 07:51:42 +0000862}
863
Dan Gohman98ca4f22009-08-05 01:29:28 +0000864void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +0000865 SDValue Chain, SDValue &Arg,
866 RegsToPassVector &RegsToPass,
867 CCValAssign &VA, CCValAssign &NextVA,
868 SDValue &StackPtr,
869 SmallVector<SDValue, 8> &MemOpChains,
870 ISD::ArgFlagsTy Flags) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000871
872 SDValue fmrrd = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +0000873 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +0000874 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
875
876 if (NextVA.isRegLoc())
877 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
878 else {
879 assert(NextVA.isMemLoc());
880 if (StackPtr.getNode() == 0)
881 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
882
Dan Gohman98ca4f22009-08-05 01:29:28 +0000883 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
884 dl, DAG, NextVA,
885 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000886 }
887}
888
Dan Gohman98ca4f22009-08-05 01:29:28 +0000889/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +0000890/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
891/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000892SDValue
893ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000894 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000895 bool isTailCall,
896 const SmallVectorImpl<ISD::OutputArg> &Outs,
897 const SmallVectorImpl<ISD::InputArg> &Ins,
898 DebugLoc dl, SelectionDAG &DAG,
899 SmallVectorImpl<SDValue> &InVals) {
Evan Chenga8e29892007-01-19 07:51:42 +0000900
Bob Wilson1f595bb2009-04-17 19:07:39 +0000901 // Analyze operands of the call, assigning locations to each operand.
902 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000903 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
904 *DAG.getContext());
905 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000906 CCAssignFnForNode(CallConv, /* Return*/ false,
907 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +0000908
Bob Wilson1f595bb2009-04-17 19:07:39 +0000909 // Get a count of how many bytes are to be pushed on the stack.
910 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +0000911
912 // Adjust the stack pointer for the new arguments...
913 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattnere563bbc2008-10-11 22:08:30 +0000914 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +0000915
Owen Anderson825b72b2009-08-11 20:47:22 +0000916 SDValue StackPtr = DAG.getRegister(ARM::SP, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000917
Bob Wilson5bafff32009-06-22 23:27:02 +0000918 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000919 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +0000920
Bob Wilson1f595bb2009-04-17 19:07:39 +0000921 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +0000922 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000923 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
924 i != e;
925 ++i, ++realArgIdx) {
926 CCValAssign &VA = ArgLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +0000927 SDValue Arg = Outs[realArgIdx].Val;
928 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +0000929
Bob Wilson1f595bb2009-04-17 19:07:39 +0000930 // Promote the value if needed.
931 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000932 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +0000933 case CCValAssign::Full: break;
934 case CCValAssign::SExt:
935 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
936 break;
937 case CCValAssign::ZExt:
938 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
939 break;
940 case CCValAssign::AExt:
941 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
942 break;
943 case CCValAssign::BCvt:
944 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
945 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000946 }
947
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000948 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +0000949 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000950 if (VA.getLocVT() == MVT::v2f64) {
951 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
952 DAG.getConstant(0, MVT::i32));
953 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
954 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000955
Dan Gohman98ca4f22009-08-05 01:29:28 +0000956 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +0000957 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
958
959 VA = ArgLocs[++i]; // skip ahead to next loc
960 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000961 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +0000962 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
963 } else {
964 assert(VA.isMemLoc());
965 if (StackPtr.getNode() == 0)
966 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
967
Dan Gohman98ca4f22009-08-05 01:29:28 +0000968 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
969 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000970 }
971 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000972 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000973 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000974 }
975 } else if (VA.isRegLoc()) {
976 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
977 } else {
978 assert(VA.isMemLoc());
979 if (StackPtr.getNode() == 0)
980 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
981
Dan Gohman98ca4f22009-08-05 01:29:28 +0000982 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
983 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000984 }
Evan Chenga8e29892007-01-19 07:51:42 +0000985 }
986
987 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000988 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +0000989 &MemOpChains[0], MemOpChains.size());
990
991 // Build a sequence of copy-to-reg nodes chained together with token chain
992 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +0000993 SDValue InFlag;
Evan Chenga8e29892007-01-19 07:51:42 +0000994 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Bob Wilson2dc4f542009-03-20 22:42:55 +0000995 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000996 RegsToPass[i].second, InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +0000997 InFlag = Chain.getValue(1);
998 }
999
Bill Wendling056292f2008-09-16 21:48:12 +00001000 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1001 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1002 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001003 bool isDirect = false;
1004 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001005 bool isLocalARMFunc = false;
Evan Chenga8e29892007-01-19 07:51:42 +00001006 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1007 GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001008 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001009 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001010 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001011 getTargetMachine().getRelocationModel() != Reloc::Static;
1012 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001013 // ARM call to a local ARM function is predicable.
1014 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
Evan Chengc60e76d2007-01-30 20:37:08 +00001015 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001016 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge4e4ed32009-08-28 23:18:09 +00001017 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001018 ARMPCLabelIndex,
1019 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001020 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001021 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001022 Callee = DAG.getLoad(getPointerTy(), dl,
1023 DAG.getEntryNode(), CPAddr, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001024 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001025 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001026 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001027 } else
1028 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +00001029 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001030 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001031 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001032 getTargetMachine().getRelocationModel() != Reloc::Static;
1033 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001034 // tBX takes a register source operand.
1035 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001036 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Owen Anderson1d0be152009-08-13 21:58:54 +00001037 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001038 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001039 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001040 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001041 Callee = DAG.getLoad(getPointerTy(), dl,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001042 DAG.getEntryNode(), CPAddr, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001043 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001044 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001045 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001046 } else
Bill Wendling056292f2008-09-16 21:48:12 +00001047 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001048 }
1049
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001050 // FIXME: handle tail calls differently.
1051 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001052 if (Subtarget->isThumb()) {
1053 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001054 CallOpc = ARMISD::CALL_NOLINK;
1055 else
1056 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1057 } else {
1058 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001059 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1060 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001061 }
David Goodwinf1daf7d2009-07-08 23:10:31 +00001062 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb1Only()) {
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +00001063 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Owen Anderson825b72b2009-08-11 20:47:22 +00001064 Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001065 InFlag = Chain.getValue(1);
1066 }
1067
Dan Gohman475871a2008-07-27 21:46:04 +00001068 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001069 Ops.push_back(Chain);
1070 Ops.push_back(Callee);
1071
1072 // Add argument registers to the end of the list so that they are known live
1073 // into the call.
1074 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1075 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1076 RegsToPass[i].second.getValueType()));
1077
Gabor Greifba36cb52008-08-28 21:40:38 +00001078 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001079 Ops.push_back(InFlag);
Duncan Sands4bdcb612008-07-02 17:40:58 +00001080 // Returns a chain and a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00001081 Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Flag),
Duncan Sands4bdcb612008-07-02 17:40:58 +00001082 &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001083 InFlag = Chain.getValue(1);
1084
Chris Lattnere563bbc2008-10-11 22:08:30 +00001085 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1086 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001087 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001088 InFlag = Chain.getValue(1);
1089
Bob Wilson1f595bb2009-04-17 19:07:39 +00001090 // Handle result values, copying them out of physregs into vregs that we
1091 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001092 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1093 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001094}
1095
Dan Gohman98ca4f22009-08-05 01:29:28 +00001096SDValue
1097ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001098 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001099 const SmallVectorImpl<ISD::OutputArg> &Outs,
1100 DebugLoc dl, SelectionDAG &DAG) {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001101
Bob Wilsondee46d72009-04-17 20:35:10 +00001102 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001103 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001104
Bob Wilsondee46d72009-04-17 20:35:10 +00001105 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001106 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1107 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001108
Dan Gohman98ca4f22009-08-05 01:29:28 +00001109 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001110 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1111 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001112
1113 // If this is the first return lowered for this function, add
1114 // the regs to the liveout set for the function.
1115 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1116 for (unsigned i = 0; i != RVLocs.size(); ++i)
1117 if (RVLocs[i].isRegLoc())
1118 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001119 }
1120
Bob Wilson1f595bb2009-04-17 19:07:39 +00001121 SDValue Flag;
1122
1123 // Copy the result values into the output registers.
1124 for (unsigned i = 0, realRVLocIdx = 0;
1125 i != RVLocs.size();
1126 ++i, ++realRVLocIdx) {
1127 CCValAssign &VA = RVLocs[i];
1128 assert(VA.isRegLoc() && "Can only return in registers!");
1129
Dan Gohman98ca4f22009-08-05 01:29:28 +00001130 SDValue Arg = Outs[realRVLocIdx].Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001131
1132 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001133 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001134 case CCValAssign::Full: break;
1135 case CCValAssign::BCvt:
1136 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1137 break;
1138 }
1139
Bob Wilson1f595bb2009-04-17 19:07:39 +00001140 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001141 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001142 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001143 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1144 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001145 SDValue HalfGPRs = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001146 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001147
1148 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1149 Flag = Chain.getValue(1);
1150 VA = RVLocs[++i]; // skip ahead to next loc
1151 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1152 HalfGPRs.getValue(1), Flag);
1153 Flag = Chain.getValue(1);
1154 VA = RVLocs[++i]; // skip ahead to next loc
1155
1156 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001157 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1158 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001159 }
1160 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1161 // available.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001162 SDValue fmrrd = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001163 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001164 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001165 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001166 VA = RVLocs[++i]; // skip ahead to next loc
1167 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1168 Flag);
1169 } else
1170 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1171
Bob Wilsondee46d72009-04-17 20:35:10 +00001172 // Guarantee that all emitted copies are
1173 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001174 Flag = Chain.getValue(1);
1175 }
1176
1177 SDValue result;
1178 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001179 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001180 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001181 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001182
1183 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001184}
1185
Bob Wilson2dc4f542009-03-20 22:42:55 +00001186// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
Bob Wilsond2559bf2009-07-13 18:11:36 +00001187// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
Bill Wendling056292f2008-09-16 21:48:12 +00001188// one of the above mentioned nodes. It has to be wrapped because otherwise
1189// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1190// be used to form addressing mode. These wrapped nodes will be selected
1191// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001192static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001193 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001194 // FIXME there is no actual debug info here
1195 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001196 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001197 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001198 if (CP->isMachineConstantPoolEntry())
1199 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1200 CP->getAlignment());
1201 else
1202 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1203 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001204 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001205}
1206
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001207// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001208SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001209ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
1210 SelectionDAG &DAG) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001211 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001212 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001213 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1214 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001215 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001216 ARMCP::CPValue, PCAdj, "tlsgd", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001217 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001218 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001219 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001220 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001221
Owen Anderson825b72b2009-08-11 20:47:22 +00001222 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001223 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001224
1225 // call __tls_get_addr.
1226 ArgListTy Args;
1227 ArgListEntry Entry;
1228 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001229 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001230 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001231 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001232 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001233 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1234 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001235 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001236 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001237 return CallResult.first;
1238}
1239
1240// Lower ISD::GlobalTLSAddress using the "initial exec" or
1241// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001242SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001243ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001244 SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001245 GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001246 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001247 SDValue Offset;
1248 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001249 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001250 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001251 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001252
Chris Lattner4fb63d02009-07-15 04:12:33 +00001253 if (GV->isDeclaration()) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001254 // initial exec model
1255 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1256 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001257 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001258 ARMCP::CPValue, PCAdj, "gottpoff", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001259 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001260 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001261 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001262 Chain = Offset.getValue(1);
1263
Owen Anderson825b72b2009-08-11 20:47:22 +00001264 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001265 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001266
Dale Johannesen33c960f2009-02-04 20:06:27 +00001267 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001268 } else {
1269 // local exec model
Evan Chenge4e4ed32009-08-28 23:18:09 +00001270 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, "tpoff");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001271 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001272 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001273 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001274 }
1275
1276 // The address of the thread local variable is the add of the thread
1277 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001278 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001279}
1280
Dan Gohman475871a2008-07-27 21:46:04 +00001281SDValue
1282ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001283 // TODO: implement the "local dynamic" model
1284 assert(Subtarget->isTargetELF() &&
1285 "TLS not implemented for non-ELF targets");
1286 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1287 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1288 // otherwise use the "Local Exec" TLS Model
1289 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1290 return LowerToTLSGeneralDynamicModel(GA, DAG);
1291 else
1292 return LowerToTLSExecModels(GA, DAG);
1293}
1294
Dan Gohman475871a2008-07-27 21:46:04 +00001295SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001296 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001297 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001298 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001299 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1300 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1301 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001302 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001303 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001304 new ARMConstantPoolValue(GV, UseGOTOFF ? "GOTOFF" : "GOT");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001305 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001306 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001307 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001308 CPAddr,
1309 PseudoSourceValue::getConstantPool(), 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001310 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001311 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001312 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001313 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001314 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
1315 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001316 return Result;
1317 } else {
Evan Cheng1606e8e2009-03-13 07:51:59 +00001318 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001319 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001320 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1321 PseudoSourceValue::getConstantPool(), 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001322 }
1323}
1324
Dan Gohman475871a2008-07-27 21:46:04 +00001325SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001326 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001327 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001328 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001329 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1330 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Dan Gohman475871a2008-07-27 21:46:04 +00001331 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +00001332 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001333 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001334 else {
Evan Chenge4e4ed32009-08-28 23:18:09 +00001335 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
1336 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001337 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001338 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001339 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001340 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001341
Dale Johannesen33c960f2009-02-04 20:06:27 +00001342 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001343 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001344
1345 if (RelocM == Reloc::PIC_) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001346 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001347 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001348 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00001349
Evan Cheng63476a82009-09-03 07:04:02 +00001350 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Dale Johannesen33c960f2009-02-04 20:06:27 +00001351 Result = DAG.getLoad(PtrVT, dl, Chain, Result, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001352
1353 return Result;
1354}
1355
Dan Gohman475871a2008-07-27 21:46:04 +00001356SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001357 SelectionDAG &DAG){
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001358 assert(Subtarget->isTargetELF() &&
1359 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Owen Andersone50ed302009-08-10 22:56:29 +00001360 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001361 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001362 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00001363 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1364 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00001365 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001366 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001367 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001368 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1369 PseudoSourceValue::getConstantPool(), 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001370 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001371 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001372}
1373
Jim Grosbach0e0da732009-05-12 23:59:14 +00001374SDValue
1375ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001376 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001377 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001378 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001379 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00001380 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00001381 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00001382 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1383 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001384 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001385 MachineFunction &MF = DAG.getMachineFunction();
1386 EVT PtrVT = getPointerTy();
1387 DebugLoc dl = Op.getDebugLoc();
1388 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1389 SDValue CPAddr;
1390 unsigned PCAdj = (RelocM != Reloc::PIC_)
1391 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001392 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001393 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
1394 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001395 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001396 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001397 SDValue Result =
1398 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
1399 SDValue Chain = Result.getValue(1);
1400
1401 if (RelocM == Reloc::PIC_) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001402 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001403 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1404 }
1405 return Result;
1406 }
Jim Grosbachf9570122009-05-14 00:46:35 +00001407 case Intrinsic::eh_sjlj_setjmp:
Owen Anderson825b72b2009-08-11 20:47:22 +00001408 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(1));
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001409 }
1410}
1411
Dan Gohman475871a2008-07-27 21:46:04 +00001412static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001413 unsigned VarArgsFrameIndex) {
Evan Chenga8e29892007-01-19 07:51:42 +00001414 // vastart just stores the address of the VarArgsFrameIndex slot into the
1415 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001416 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001417 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001418 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001419 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001420 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001421}
1422
Dan Gohman475871a2008-07-27 21:46:04 +00001423SDValue
Evan Cheng86198642009-08-07 00:34:42 +00001424ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) {
1425 SDNode *Node = Op.getNode();
1426 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001427 EVT VT = Node->getValueType(0);
Evan Cheng86198642009-08-07 00:34:42 +00001428 SDValue Chain = Op.getOperand(0);
1429 SDValue Size = Op.getOperand(1);
1430 SDValue Align = Op.getOperand(2);
1431
1432 // Chain the dynamic stack allocation so that it doesn't modify the stack
1433 // pointer when other instructions are using the stack.
1434 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1435
1436 unsigned AlignVal = cast<ConstantSDNode>(Align)->getZExtValue();
1437 unsigned StackAlign = getTargetMachine().getFrameInfo()->getStackAlignment();
1438 if (AlignVal > StackAlign)
1439 // Do this now since selection pass cannot introduce new target
1440 // independent node.
1441 Align = DAG.getConstant(-(uint64_t)AlignVal, VT);
1442
1443 // In Thumb1 mode, there isn't a "sub r, sp, r" instruction, we will end up
1444 // using a "add r, sp, r" instead. Negate the size now so we don't have to
1445 // do even more horrible hack later.
1446 MachineFunction &MF = DAG.getMachineFunction();
1447 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1448 if (AFI->isThumb1OnlyFunction()) {
1449 bool Negate = true;
1450 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Size);
1451 if (C) {
1452 uint32_t Val = C->getZExtValue();
1453 if (Val <= 508 && ((Val & 3) == 0))
1454 Negate = false;
1455 }
1456 if (Negate)
1457 Size = DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(0, VT), Size);
1458 }
1459
Owen Anderson825b72b2009-08-11 20:47:22 +00001460 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
Evan Cheng86198642009-08-07 00:34:42 +00001461 SDValue Ops1[] = { Chain, Size, Align };
1462 SDValue Res = DAG.getNode(ARMISD::DYN_ALLOC, dl, VTList, Ops1, 3);
1463 Chain = Res.getValue(1);
1464 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1465 DAG.getIntPtrConstant(0, true), SDValue());
1466 SDValue Ops2[] = { Res, Chain };
1467 return DAG.getMergeValues(Ops2, 2, dl);
1468}
1469
1470SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00001471ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
1472 SDValue &Root, SelectionDAG &DAG,
1473 DebugLoc dl) {
1474 MachineFunction &MF = DAG.getMachineFunction();
1475 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1476
1477 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001478 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00001479 RC = ARM::tGPRRegisterClass;
1480 else
1481 RC = ARM::GPRRegisterClass;
1482
1483 // Transform the arguments stored in physical registers into virtual ones.
1484 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001485 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001486
1487 SDValue ArgValue2;
1488 if (NextVA.isMemLoc()) {
1489 unsigned ArgSize = NextVA.getLocVT().getSizeInBits()/8;
1490 MachineFrameInfo *MFI = MF.getFrameInfo();
1491 int FI = MFI->CreateFixedObject(ArgSize, NextVA.getLocMemOffset());
1492
1493 // Create load node to retrieve arguments from the stack.
1494 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00001495 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN, NULL, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00001496 } else {
1497 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001498 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001499 }
1500
Owen Anderson825b72b2009-08-11 20:47:22 +00001501 return DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00001502}
1503
1504SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001505ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001506 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001507 const SmallVectorImpl<ISD::InputArg>
1508 &Ins,
1509 DebugLoc dl, SelectionDAG &DAG,
1510 SmallVectorImpl<SDValue> &InVals) {
1511
Bob Wilson1f595bb2009-04-17 19:07:39 +00001512 MachineFunction &MF = DAG.getMachineFunction();
1513 MachineFrameInfo *MFI = MF.getFrameInfo();
1514
Bob Wilson1f595bb2009-04-17 19:07:39 +00001515 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1516
1517 // Assign locations to all of the incoming arguments.
1518 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001519 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1520 *DAG.getContext());
1521 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001522 CCAssignFnForNode(CallConv, /* Return*/ false,
1523 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001524
1525 SmallVector<SDValue, 16> ArgValues;
1526
1527 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1528 CCValAssign &VA = ArgLocs[i];
1529
Bob Wilsondee46d72009-04-17 20:35:10 +00001530 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001531 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001532 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00001533
Bob Wilson5bafff32009-06-22 23:27:02 +00001534 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001535 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001536 // f64 and vector types are split up into multiple registers or
1537 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00001538 RegVT = MVT::i32;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001539
Owen Anderson825b72b2009-08-11 20:47:22 +00001540 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001541 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001542 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00001543 VA = ArgLocs[++i]; // skip ahead to next loc
1544 SDValue ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001545 Chain, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001546 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1547 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001548 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00001549 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001550 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
1551 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001552 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001553
Bob Wilson5bafff32009-06-22 23:27:02 +00001554 } else {
1555 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001556
Owen Anderson825b72b2009-08-11 20:47:22 +00001557 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00001558 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001559 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00001560 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001561 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001562 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001563 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001564 RC = (AFI->isThumb1OnlyFunction() ?
1565 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00001566 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001567 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00001568
1569 // Transform the arguments in physical registers into virtual ones.
1570 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001571 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001572 }
1573
1574 // If this is an 8 or 16-bit value, it is really passed promoted
1575 // to 32 bits. Insert an assert[sz]ext to capture this, then
1576 // truncate to the right size.
1577 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001578 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001579 case CCValAssign::Full: break;
1580 case CCValAssign::BCvt:
1581 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1582 break;
1583 case CCValAssign::SExt:
1584 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
1585 DAG.getValueType(VA.getValVT()));
1586 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1587 break;
1588 case CCValAssign::ZExt:
1589 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
1590 DAG.getValueType(VA.getValVT()));
1591 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1592 break;
1593 }
1594
Dan Gohman98ca4f22009-08-05 01:29:28 +00001595 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001596
1597 } else { // VA.isRegLoc()
1598
1599 // sanity check
1600 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00001601 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001602
1603 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
1604 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset());
1605
Bob Wilsondee46d72009-04-17 20:35:10 +00001606 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001607 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001608 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN, NULL, 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001609 }
1610 }
1611
1612 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00001613 if (isVarArg) {
1614 static const unsigned GPRArgRegs[] = {
1615 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1616 };
1617
Bob Wilsondee46d72009-04-17 20:35:10 +00001618 unsigned NumGPRs = CCInfo.getFirstUnallocated
1619 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001620
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001621 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1622 unsigned VARegSize = (4 - NumGPRs) * 4;
1623 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001624 unsigned ArgOffset = 0;
Evan Chenga8e29892007-01-19 07:51:42 +00001625 if (VARegSaveSize) {
1626 // If this function is vararg, store any remaining integer argument regs
1627 // to their spots on the stack so that they may be loaded by deferencing
1628 // the result of va_next.
1629 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001630 ArgOffset = CCInfo.getNextStackOffset();
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001631 VarArgsFrameIndex = MFI->CreateFixedObject(VARegSaveSize, ArgOffset +
1632 VARegSaveSize - VARegSize);
Dan Gohman475871a2008-07-27 21:46:04 +00001633 SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001634
Dan Gohman475871a2008-07-27 21:46:04 +00001635 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00001636 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001637 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001638 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00001639 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001640 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00001641 RC = ARM::GPRRegisterClass;
1642
Bob Wilson998e1252009-04-20 18:36:57 +00001643 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001644 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001645 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001646 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001647 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00001648 DAG.getConstant(4, getPointerTy()));
1649 }
1650 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001651 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001652 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001653 } else
1654 // This will point to the next argument passed via stack.
1655 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1656 }
1657
Dan Gohman98ca4f22009-08-05 01:29:28 +00001658 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00001659}
1660
1661/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00001662static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00001663 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001664 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00001665 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00001666 // Maybe this has already been legalized into the constant pool?
1667 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00001668 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001669 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
1670 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001671 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001672 }
1673 }
1674 return false;
1675}
1676
David Goodwinf1daf7d2009-07-08 23:10:31 +00001677static bool isLegalCmpImmediate(unsigned C, bool isThumb1Only) {
1678 return ( isThumb1Only && (C & ~255U) == 0) ||
1679 (!isThumb1Only && ARM_AM::getSOImmVal(C) != -1);
Evan Chenga8e29892007-01-19 07:51:42 +00001680}
1681
1682/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1683/// the given operands.
Dan Gohman475871a2008-07-27 21:46:04 +00001684static SDValue getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
David Goodwinf1daf7d2009-07-08 23:10:31 +00001685 SDValue &ARMCC, SelectionDAG &DAG, bool isThumb1Only,
Dale Johannesende064702009-02-06 21:50:26 +00001686 DebugLoc dl) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001687 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001688 unsigned C = RHSC->getZExtValue();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001689 if (!isLegalCmpImmediate(C, isThumb1Only)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001690 // Constant does not fit, try adjusting it by one?
1691 switch (CC) {
1692 default: break;
1693 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00001694 case ISD::SETGE:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001695 if (isLegalCmpImmediate(C-1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001696 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001697 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001698 }
1699 break;
1700 case ISD::SETULT:
1701 case ISD::SETUGE:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001702 if (C > 0 && isLegalCmpImmediate(C-1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001703 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001704 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001705 }
1706 break;
1707 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00001708 case ISD::SETGT:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001709 if (isLegalCmpImmediate(C+1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001710 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001711 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001712 }
1713 break;
1714 case ISD::SETULE:
1715 case ISD::SETUGT:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001716 if (C < 0xffffffff && isLegalCmpImmediate(C+1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001717 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001718 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001719 }
1720 break;
1721 }
1722 }
1723 }
1724
1725 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001726 ARMISD::NodeType CompareType;
1727 switch (CondCode) {
1728 default:
1729 CompareType = ARMISD::CMP;
1730 break;
1731 case ARMCC::EQ:
1732 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00001733 // Uses only Z Flag
1734 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001735 break;
1736 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001737 ARMCC = DAG.getConstant(CondCode, MVT::i32);
1738 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001739}
1740
1741/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001742static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Dale Johannesende064702009-02-06 21:50:26 +00001743 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001744 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00001745 if (!isFloatingPointZero(RHS))
Owen Anderson825b72b2009-08-11 20:47:22 +00001746 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001747 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001748 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
1749 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001750}
1751
Dan Gohman475871a2008-07-27 21:46:04 +00001752static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001753 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00001754 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001755 SDValue LHS = Op.getOperand(0);
1756 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001757 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001758 SDValue TrueVal = Op.getOperand(2);
1759 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00001760 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001761
Owen Anderson825b72b2009-08-11 20:47:22 +00001762 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001763 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001764 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
David Goodwinf1daf7d2009-07-08 23:10:31 +00001765 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb1Only(), dl);
Dale Johannesende064702009-02-06 21:50:26 +00001766 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001767 }
1768
1769 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001770 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00001771
Owen Anderson825b72b2009-08-11 20:47:22 +00001772 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1773 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001774 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
1775 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00001776 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001777 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001778 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001779 // FIXME: Needs another CMP because flag can have but one use.
Dale Johannesende064702009-02-06 21:50:26 +00001780 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001781 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Dale Johannesende064702009-02-06 21:50:26 +00001782 Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00001783 }
1784 return Result;
1785}
1786
Dan Gohman475871a2008-07-27 21:46:04 +00001787static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001788 const ARMSubtarget *ST) {
Dan Gohman475871a2008-07-27 21:46:04 +00001789 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001790 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001791 SDValue LHS = Op.getOperand(2);
1792 SDValue RHS = Op.getOperand(3);
1793 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00001794 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001795
Owen Anderson825b72b2009-08-11 20:47:22 +00001796 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001797 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001798 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
David Goodwinf1daf7d2009-07-08 23:10:31 +00001799 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb1Only(), dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001800 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Dale Johannesende064702009-02-06 21:50:26 +00001801 Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001802 }
1803
Owen Anderson825b72b2009-08-11 20:47:22 +00001804 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Chenga8e29892007-01-19 07:51:42 +00001805 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001806 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001807
Dale Johannesende064702009-02-06 21:50:26 +00001808 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001809 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1810 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1811 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00001812 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00001813 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001814 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001815 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00001816 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00001817 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001818 }
1819 return Res;
1820}
1821
Dan Gohman475871a2008-07-27 21:46:04 +00001822SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) {
1823 SDValue Chain = Op.getOperand(0);
1824 SDValue Table = Op.getOperand(1);
1825 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001826 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001827
Owen Andersone50ed302009-08-10 22:56:29 +00001828 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00001829 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
1830 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00001831 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00001832 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00001833 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00001834 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
1835 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00001836 if (Subtarget->isThumb2()) {
1837 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
1838 // which does another jump to the destination. This also makes it easier
1839 // to translate it to TBB / TBH later.
1840 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00001841 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00001842 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001843 }
Evan Cheng66ac5312009-07-25 00:33:29 +00001844 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001845 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr, NULL, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00001846 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001847 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00001848 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001849 } else {
1850 Addr = DAG.getLoad(PTy, dl, Chain, Addr, NULL, 0);
1851 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00001852 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001853 }
Evan Chenga8e29892007-01-19 07:51:42 +00001854}
1855
Dan Gohman475871a2008-07-27 21:46:04 +00001856static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Dale Johannesende064702009-02-06 21:50:26 +00001857 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001858 unsigned Opc =
1859 Op.getOpcode() == ISD::FP_TO_SINT ? ARMISD::FTOSI : ARMISD::FTOUI;
Owen Anderson825b72b2009-08-11 20:47:22 +00001860 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
1861 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001862}
1863
Dan Gohman475871a2008-07-27 21:46:04 +00001864static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001865 EVT VT = Op.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001866 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001867 unsigned Opc =
1868 Op.getOpcode() == ISD::SINT_TO_FP ? ARMISD::SITOF : ARMISD::UITOF;
1869
Owen Anderson825b72b2009-08-11 20:47:22 +00001870 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
Dale Johannesende064702009-02-06 21:50:26 +00001871 return DAG.getNode(Opc, dl, VT, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001872}
1873
Dan Gohman475871a2008-07-27 21:46:04 +00001874static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00001875 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00001876 SDValue Tmp0 = Op.getOperand(0);
1877 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00001878 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001879 EVT VT = Op.getValueType();
1880 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001881 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
1882 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001883 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
1884 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001885 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001886}
1887
Jim Grosbach0e0da732009-05-12 23:59:14 +00001888SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
1889 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1890 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00001891 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001892 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
1893 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00001894 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00001895 ? ARM::R7 : ARM::R11;
1896 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
1897 while (Depth--)
1898 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
1899 return FrameAddr;
1900}
1901
Dan Gohman475871a2008-07-27 21:46:04 +00001902SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00001903ARMTargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman475871a2008-07-27 21:46:04 +00001904 SDValue Chain,
1905 SDValue Dst, SDValue Src,
1906 SDValue Size, unsigned Align,
Dan Gohman707e0182008-04-12 04:36:06 +00001907 bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00001908 const Value *DstSV, uint64_t DstSVOff,
1909 const Value *SrcSV, uint64_t SrcSVOff){
Evan Cheng4102eb52007-10-22 22:11:27 +00001910 // Do repeated 4-byte loads and stores. To be improved.
Dan Gohman707e0182008-04-12 04:36:06 +00001911 // This requires 4-byte alignment.
1912 if ((Align & 3) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00001913 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001914 // This requires the copy size to be a constant, preferrably
1915 // within a subtarget-specific limit.
1916 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
1917 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00001918 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001919 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001920 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00001921 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001922
1923 unsigned BytesLeft = SizeVal & 3;
1924 unsigned NumMemOps = SizeVal >> 2;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001925 unsigned EmittedNumMemOps = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001926 EVT VT = MVT::i32;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001927 unsigned VTSize = 4;
Evan Cheng4102eb52007-10-22 22:11:27 +00001928 unsigned i = 0;
Evan Chenge5e7ce42007-05-18 01:19:57 +00001929 const unsigned MAX_LOADS_IN_LDM = 6;
Dan Gohman475871a2008-07-27 21:46:04 +00001930 SDValue TFOps[MAX_LOADS_IN_LDM];
1931 SDValue Loads[MAX_LOADS_IN_LDM];
Dan Gohman1f13c682008-04-28 17:15:20 +00001932 uint64_t SrcOff = 0, DstOff = 0;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001933
Evan Cheng4102eb52007-10-22 22:11:27 +00001934 // Emit up to MAX_LOADS_IN_LDM loads, then a TokenFactor barrier, then the
1935 // same number of stores. The loads and stores will get combined into
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001936 // ldm/stm later on.
Evan Cheng4102eb52007-10-22 22:11:27 +00001937 while (EmittedNumMemOps < NumMemOps) {
1938 for (i = 0;
1939 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00001940 Loads[i] = DAG.getLoad(VT, dl, Chain,
Owen Anderson825b72b2009-08-11 20:47:22 +00001941 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
1942 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001943 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00001944 TFOps[i] = Loads[i].getValue(1);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001945 SrcOff += VTSize;
1946 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001947 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001948
Evan Cheng4102eb52007-10-22 22:11:27 +00001949 for (i = 0;
1950 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00001951 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Owen Anderson825b72b2009-08-11 20:47:22 +00001952 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
1953 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001954 DstSV, DstSVOff + DstOff);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001955 DstOff += VTSize;
1956 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001957 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00001958
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001959 EmittedNumMemOps += i;
1960 }
1961
Bob Wilson2dc4f542009-03-20 22:42:55 +00001962 if (BytesLeft == 0)
Evan Cheng4102eb52007-10-22 22:11:27 +00001963 return Chain;
1964
1965 // Issue loads / stores for the trailing (1 - 3) bytes.
1966 unsigned BytesLeftSave = BytesLeft;
1967 i = 0;
1968 while (BytesLeft) {
1969 if (BytesLeft >= 2) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001970 VT = MVT::i16;
Evan Cheng4102eb52007-10-22 22:11:27 +00001971 VTSize = 2;
1972 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001973 VT = MVT::i8;
Evan Cheng4102eb52007-10-22 22:11:27 +00001974 VTSize = 1;
1975 }
1976
Dale Johannesen0f502f62009-02-03 22:26:09 +00001977 Loads[i] = DAG.getLoad(VT, dl, Chain,
Owen Anderson825b72b2009-08-11 20:47:22 +00001978 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
1979 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001980 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00001981 TFOps[i] = Loads[i].getValue(1);
1982 ++i;
1983 SrcOff += VTSize;
1984 BytesLeft -= VTSize;
1985 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001986 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00001987
1988 i = 0;
1989 BytesLeft = BytesLeftSave;
1990 while (BytesLeft) {
1991 if (BytesLeft >= 2) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001992 VT = MVT::i16;
Evan Cheng4102eb52007-10-22 22:11:27 +00001993 VTSize = 2;
1994 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001995 VT = MVT::i8;
Evan Cheng4102eb52007-10-22 22:11:27 +00001996 VTSize = 1;
1997 }
1998
Dale Johannesen0f502f62009-02-03 22:26:09 +00001999 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Owen Anderson825b72b2009-08-11 20:47:22 +00002000 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
2001 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00002002 DstSV, DstSVOff + DstOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00002003 ++i;
2004 DstOff += VTSize;
2005 BytesLeft -= VTSize;
2006 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002007 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002008}
2009
Duncan Sands1607f052008-12-01 11:39:25 +00002010static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00002011 SDValue Op = N->getOperand(0);
Dale Johannesende064702009-02-06 21:50:26 +00002012 DebugLoc dl = N->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00002013 if (N->getValueType(0) == MVT::f64) {
Evan Chengc7c77292008-11-04 19:57:48 +00002014 // Turn i64->f64 into FMDRR.
Owen Anderson825b72b2009-08-11 20:47:22 +00002015 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2016 DAG.getConstant(0, MVT::i32));
2017 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2018 DAG.getConstant(1, MVT::i32));
2019 return DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
Evan Chengc7c77292008-11-04 19:57:48 +00002020 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002021
Evan Chengc7c77292008-11-04 19:57:48 +00002022 // Turn f64->i64 into FMRRD.
Bob Wilson2dc4f542009-03-20 22:42:55 +00002023 SDValue Cvt = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00002024 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002025
Chris Lattner27a6c732007-11-24 07:07:01 +00002026 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002027 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
Chris Lattner27a6c732007-11-24 07:07:01 +00002028}
2029
Bob Wilson5bafff32009-06-22 23:27:02 +00002030/// getZeroVector - Returns a vector of specified type with all zero elements.
2031///
Owen Andersone50ed302009-08-10 22:56:29 +00002032static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002033 assert(VT.isVector() && "Expected a vector type");
2034
2035 // Zero vectors are used to represent vector negation and in those cases
2036 // will be implemented with the NEON VNEG instruction. However, VNEG does
2037 // not support i64 elements, so sometimes the zero vectors will need to be
2038 // explicitly constructed. For those cases, and potentially other uses in
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002039 // the future, always build zero vectors as <16 x i8> or <8 x i8> bitcasted
Bob Wilson5bafff32009-06-22 23:27:02 +00002040 // to their dest type. This ensures they get CSE'd.
2041 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002042 SDValue Cst = DAG.getTargetConstant(0, MVT::i8);
2043 SmallVector<SDValue, 8> Ops;
2044 MVT TVT;
2045
2046 if (VT.getSizeInBits() == 64) {
2047 Ops.assign(8, Cst); TVT = MVT::v8i8;
2048 } else {
2049 Ops.assign(16, Cst); TVT = MVT::v16i8;
2050 }
2051 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002052
2053 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2054}
2055
2056/// getOnesVector - Returns a vector of specified type with all bits set.
2057///
Owen Andersone50ed302009-08-10 22:56:29 +00002058static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002059 assert(VT.isVector() && "Expected a vector type");
2060
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002061 // Always build ones vectors as <16 x i32> or <8 x i32> bitcasted to their
2062 // dest type. This ensures they get CSE'd.
Bob Wilson5bafff32009-06-22 23:27:02 +00002063 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002064 SDValue Cst = DAG.getTargetConstant(0xFF, MVT::i8);
2065 SmallVector<SDValue, 8> Ops;
2066 MVT TVT;
2067
2068 if (VT.getSizeInBits() == 64) {
2069 Ops.assign(8, Cst); TVT = MVT::v8i8;
2070 } else {
2071 Ops.assign(16, Cst); TVT = MVT::v16i8;
2072 }
2073 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002074
2075 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2076}
2077
2078static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2079 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00002080 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002081 DebugLoc dl = N->getDebugLoc();
2082
2083 // Lower vector shifts on NEON to use VSHL.
2084 if (VT.isVector()) {
2085 assert(ST->hasNEON() && "unexpected vector shift");
2086
2087 // Left shifts translate directly to the vshiftu intrinsic.
2088 if (N->getOpcode() == ISD::SHL)
2089 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002090 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002091 N->getOperand(0), N->getOperand(1));
2092
2093 assert((N->getOpcode() == ISD::SRA ||
2094 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2095
2096 // NEON uses the same intrinsics for both left and right shifts. For
2097 // right shifts, the shift amounts are negative, so negate the vector of
2098 // shift amounts.
Owen Andersone50ed302009-08-10 22:56:29 +00002099 EVT ShiftVT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002100 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2101 getZeroVector(ShiftVT, DAG, dl),
2102 N->getOperand(1));
2103 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2104 Intrinsic::arm_neon_vshifts :
2105 Intrinsic::arm_neon_vshiftu);
2106 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002107 DAG.getConstant(vshiftInt, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002108 N->getOperand(0), NegatedCount);
2109 }
2110
Eli Friedmance392eb2009-08-22 03:13:10 +00002111 // We can get here for a node like i32 = ISD::SHL i32, i64
2112 if (VT != MVT::i64)
2113 return SDValue();
2114
2115 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00002116 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00002117
Chris Lattner27a6c732007-11-24 07:07:01 +00002118 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2119 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002120 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00002121 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002122
Chris Lattner27a6c732007-11-24 07:07:01 +00002123 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00002124 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002125
Chris Lattner27a6c732007-11-24 07:07:01 +00002126 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00002127 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2128 DAG.getConstant(0, MVT::i32));
2129 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2130 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002131
Chris Lattner27a6c732007-11-24 07:07:01 +00002132 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2133 // captures the result into a carry flag.
2134 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Owen Anderson825b72b2009-08-11 20:47:22 +00002135 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002136
Chris Lattner27a6c732007-11-24 07:07:01 +00002137 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00002138 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002139
Chris Lattner27a6c732007-11-24 07:07:01 +00002140 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002141 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00002142}
2143
Bob Wilson5bafff32009-06-22 23:27:02 +00002144static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2145 SDValue TmpOp0, TmpOp1;
2146 bool Invert = false;
2147 bool Swap = false;
2148 unsigned Opc = 0;
2149
2150 SDValue Op0 = Op.getOperand(0);
2151 SDValue Op1 = Op.getOperand(1);
2152 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00002153 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002154 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2155 DebugLoc dl = Op.getDebugLoc();
2156
2157 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2158 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002159 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002160 case ISD::SETUNE:
2161 case ISD::SETNE: Invert = true; // Fallthrough
2162 case ISD::SETOEQ:
2163 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2164 case ISD::SETOLT:
2165 case ISD::SETLT: Swap = true; // Fallthrough
2166 case ISD::SETOGT:
2167 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2168 case ISD::SETOLE:
2169 case ISD::SETLE: Swap = true; // Fallthrough
2170 case ISD::SETOGE:
2171 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2172 case ISD::SETUGE: Swap = true; // Fallthrough
2173 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2174 case ISD::SETUGT: Swap = true; // Fallthrough
2175 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2176 case ISD::SETUEQ: Invert = true; // Fallthrough
2177 case ISD::SETONE:
2178 // Expand this to (OLT | OGT).
2179 TmpOp0 = Op0;
2180 TmpOp1 = Op1;
2181 Opc = ISD::OR;
2182 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2183 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2184 break;
2185 case ISD::SETUO: Invert = true; // Fallthrough
2186 case ISD::SETO:
2187 // Expand this to (OLT | OGE).
2188 TmpOp0 = Op0;
2189 TmpOp1 = Op1;
2190 Opc = ISD::OR;
2191 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2192 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2193 break;
2194 }
2195 } else {
2196 // Integer comparisons.
2197 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002198 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002199 case ISD::SETNE: Invert = true;
2200 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2201 case ISD::SETLT: Swap = true;
2202 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2203 case ISD::SETLE: Swap = true;
2204 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2205 case ISD::SETULT: Swap = true;
2206 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2207 case ISD::SETULE: Swap = true;
2208 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2209 }
2210
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002211 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00002212 if (Opc == ARMISD::VCEQ) {
2213
2214 SDValue AndOp;
2215 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2216 AndOp = Op0;
2217 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2218 AndOp = Op1;
2219
2220 // Ignore bitconvert.
2221 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2222 AndOp = AndOp.getOperand(0);
2223
2224 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2225 Opc = ARMISD::VTST;
2226 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2227 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2228 Invert = !Invert;
2229 }
2230 }
2231 }
2232
2233 if (Swap)
2234 std::swap(Op0, Op1);
2235
2236 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
2237
2238 if (Invert)
2239 Result = DAG.getNOT(dl, Result, VT);
2240
2241 return Result;
2242}
2243
2244/// isVMOVSplat - Check if the specified splat value corresponds to an immediate
2245/// VMOV instruction, and if so, return the constant being splatted.
2246static SDValue isVMOVSplat(uint64_t SplatBits, uint64_t SplatUndef,
2247 unsigned SplatBitSize, SelectionDAG &DAG) {
2248 switch (SplatBitSize) {
2249 case 8:
2250 // Any 1-byte value is OK.
2251 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Owen Anderson825b72b2009-08-11 20:47:22 +00002252 return DAG.getTargetConstant(SplatBits, MVT::i8);
Bob Wilson5bafff32009-06-22 23:27:02 +00002253
2254 case 16:
2255 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
2256 if ((SplatBits & ~0xff) == 0 ||
2257 (SplatBits & ~0xff00) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002258 return DAG.getTargetConstant(SplatBits, MVT::i16);
Bob Wilson5bafff32009-06-22 23:27:02 +00002259 break;
2260
2261 case 32:
2262 // NEON's 32-bit VMOV supports splat values where:
2263 // * only one byte is nonzero, or
2264 // * the least significant byte is 0xff and the second byte is nonzero, or
2265 // * the least significant 2 bytes are 0xff and the third is nonzero.
2266 if ((SplatBits & ~0xff) == 0 ||
2267 (SplatBits & ~0xff00) == 0 ||
2268 (SplatBits & ~0xff0000) == 0 ||
2269 (SplatBits & ~0xff000000) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002270 return DAG.getTargetConstant(SplatBits, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002271
2272 if ((SplatBits & ~0xffff) == 0 &&
2273 ((SplatBits | SplatUndef) & 0xff) == 0xff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002274 return DAG.getTargetConstant(SplatBits | 0xff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002275
2276 if ((SplatBits & ~0xffffff) == 0 &&
2277 ((SplatBits | SplatUndef) & 0xffff) == 0xffff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002278 return DAG.getTargetConstant(SplatBits | 0xffff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002279
2280 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
2281 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
2282 // VMOV.I32. A (very) minor optimization would be to replicate the value
2283 // and fall through here to test for a valid 64-bit splat. But, then the
2284 // caller would also need to check and handle the change in size.
2285 break;
2286
2287 case 64: {
2288 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
2289 uint64_t BitMask = 0xff;
2290 uint64_t Val = 0;
2291 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
2292 if (((SplatBits | SplatUndef) & BitMask) == BitMask)
2293 Val |= BitMask;
2294 else if ((SplatBits & BitMask) != 0)
2295 return SDValue();
2296 BitMask <<= 8;
2297 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002298 return DAG.getTargetConstant(Val, MVT::i64);
Bob Wilson5bafff32009-06-22 23:27:02 +00002299 }
2300
2301 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002302 llvm_unreachable("unexpected size for isVMOVSplat");
Bob Wilson5bafff32009-06-22 23:27:02 +00002303 break;
2304 }
2305
2306 return SDValue();
2307}
2308
2309/// getVMOVImm - If this is a build_vector of constants which can be
2310/// formed by using a VMOV instruction of the specified element size,
2311/// return the constant being splatted. The ByteSize field indicates the
2312/// number of bytes of each element [1248].
2313SDValue ARM::getVMOVImm(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
2314 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
2315 APInt SplatBits, SplatUndef;
2316 unsigned SplatBitSize;
2317 bool HasAnyUndefs;
2318 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
2319 HasAnyUndefs, ByteSize * 8))
2320 return SDValue();
2321
2322 if (SplatBitSize > ByteSize * 8)
2323 return SDValue();
2324
2325 return isVMOVSplat(SplatBits.getZExtValue(), SplatUndef.getZExtValue(),
2326 SplatBitSize, DAG);
2327}
2328
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002329static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
2330 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002331 unsigned NumElts = VT.getVectorNumElements();
2332 ReverseVEXT = false;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002333 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002334
2335 // If this is a VEXT shuffle, the immediate value is the index of the first
2336 // element. The other shuffle indices must be the successive elements after
2337 // the first one.
2338 unsigned ExpectedElt = Imm;
2339 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002340 // Increment the expected index. If it wraps around, it may still be
2341 // a VEXT but the source vectors must be swapped.
2342 ExpectedElt += 1;
2343 if (ExpectedElt == NumElts * 2) {
2344 ExpectedElt = 0;
2345 ReverseVEXT = true;
2346 }
2347
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002348 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002349 return false;
2350 }
2351
2352 // Adjust the index value if the source operands will be swapped.
2353 if (ReverseVEXT)
2354 Imm -= NumElts;
2355
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002356 return true;
2357}
2358
Bob Wilson8bb9e482009-07-26 00:39:34 +00002359/// isVREVMask - Check if a vector shuffle corresponds to a VREV
2360/// instruction with the specified blocksize. (The order of the elements
2361/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002362static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
2363 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00002364 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
2365 "Only possible block sizes for VREV are: 16, 32, 64");
2366
Bob Wilson8bb9e482009-07-26 00:39:34 +00002367 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00002368 if (EltSz == 64)
2369 return false;
2370
2371 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002372 unsigned BlockElts = M[0] + 1;
Bob Wilson8bb9e482009-07-26 00:39:34 +00002373
2374 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
2375 return false;
2376
2377 for (unsigned i = 0; i < NumElts; ++i) {
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002378 if ((unsigned) M[i] !=
Bob Wilson8bb9e482009-07-26 00:39:34 +00002379 (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
2380 return false;
2381 }
2382
2383 return true;
2384}
2385
Bob Wilsonc692cb72009-08-21 20:54:19 +00002386static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
2387 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002388 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2389 if (EltSz == 64)
2390 return false;
2391
Bob Wilsonc692cb72009-08-21 20:54:19 +00002392 unsigned NumElts = VT.getVectorNumElements();
2393 WhichResult = (M[0] == 0 ? 0 : 1);
2394 for (unsigned i = 0; i < NumElts; i += 2) {
2395 if ((unsigned) M[i] != i + WhichResult ||
2396 (unsigned) M[i+1] != i + NumElts + WhichResult)
2397 return false;
2398 }
2399 return true;
2400}
2401
2402static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
2403 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002404 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2405 if (EltSz == 64)
2406 return false;
2407
Bob Wilsonc692cb72009-08-21 20:54:19 +00002408 unsigned NumElts = VT.getVectorNumElements();
2409 WhichResult = (M[0] == 0 ? 0 : 1);
2410 for (unsigned i = 0; i != NumElts; ++i) {
2411 if ((unsigned) M[i] != 2 * i + WhichResult)
2412 return false;
2413 }
2414
2415 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00002416 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002417 return false;
2418
2419 return true;
2420}
2421
2422static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
2423 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002424 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2425 if (EltSz == 64)
2426 return false;
2427
Bob Wilsonc692cb72009-08-21 20:54:19 +00002428 unsigned NumElts = VT.getVectorNumElements();
2429 WhichResult = (M[0] == 0 ? 0 : 1);
2430 unsigned Idx = WhichResult * NumElts / 2;
2431 for (unsigned i = 0; i != NumElts; i += 2) {
2432 if ((unsigned) M[i] != Idx ||
2433 (unsigned) M[i+1] != Idx + NumElts)
2434 return false;
2435 Idx += 1;
2436 }
2437
2438 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00002439 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002440 return false;
2441
2442 return true;
2443}
2444
Owen Andersone50ed302009-08-10 22:56:29 +00002445static SDValue BuildSplat(SDValue Val, EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002446 // Canonicalize all-zeros and all-ones vectors.
Bob Wilsond06791f2009-08-13 01:57:47 +00002447 ConstantSDNode *ConstVal = cast<ConstantSDNode>(Val.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002448 if (ConstVal->isNullValue())
2449 return getZeroVector(VT, DAG, dl);
2450 if (ConstVal->isAllOnesValue())
2451 return getOnesVector(VT, DAG, dl);
2452
Owen Andersone50ed302009-08-10 22:56:29 +00002453 EVT CanonicalVT;
Bob Wilson5bafff32009-06-22 23:27:02 +00002454 if (VT.is64BitVector()) {
2455 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002456 case 8: CanonicalVT = MVT::v8i8; break;
2457 case 16: CanonicalVT = MVT::v4i16; break;
2458 case 32: CanonicalVT = MVT::v2i32; break;
2459 case 64: CanonicalVT = MVT::v1i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002460 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002461 }
2462 } else {
2463 assert(VT.is128BitVector() && "unknown splat vector size");
2464 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002465 case 8: CanonicalVT = MVT::v16i8; break;
2466 case 16: CanonicalVT = MVT::v8i16; break;
2467 case 32: CanonicalVT = MVT::v4i32; break;
2468 case 64: CanonicalVT = MVT::v2i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002469 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002470 }
2471 }
2472
2473 // Build a canonical splat for this value.
2474 SmallVector<SDValue, 8> Ops;
2475 Ops.assign(CanonicalVT.getVectorNumElements(), Val);
2476 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, &Ops[0],
2477 Ops.size());
2478 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Res);
2479}
2480
2481// If this is a case we can't handle, return null and let the default
2482// expansion code take care of it.
2483static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bob Wilsond06791f2009-08-13 01:57:47 +00002484 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002485 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002486 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002487
2488 APInt SplatBits, SplatUndef;
2489 unsigned SplatBitSize;
2490 bool HasAnyUndefs;
2491 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00002492 if (SplatBitSize <= 64) {
2493 SDValue Val = isVMOVSplat(SplatBits.getZExtValue(),
2494 SplatUndef.getZExtValue(), SplatBitSize, DAG);
2495 if (Val.getNode())
2496 return BuildSplat(Val, VT, DAG, dl);
2497 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00002498 }
2499
2500 // If there are only 2 elements in a 128-bit vector, insert them into an
2501 // undef vector. This handles the common case for 128-bit vector argument
2502 // passing, where the insertions should be translated to subreg accesses
2503 // with no real instructions.
2504 if (VT.is128BitVector() && Op.getNumOperands() == 2) {
2505 SDValue Val = DAG.getUNDEF(VT);
2506 SDValue Op0 = Op.getOperand(0);
2507 SDValue Op1 = Op.getOperand(1);
2508 if (Op0.getOpcode() != ISD::UNDEF)
2509 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op0,
2510 DAG.getIntPtrConstant(0));
2511 if (Op1.getOpcode() != ISD::UNDEF)
2512 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op1,
2513 DAG.getIntPtrConstant(1));
2514 return Val;
Bob Wilson5bafff32009-06-22 23:27:02 +00002515 }
2516
2517 return SDValue();
2518}
2519
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002520/// isShuffleMaskLegal - Targets can use this to indicate that they only
2521/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
2522/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
2523/// are assumed to be legal.
2524bool
2525ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
2526 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002527 if (VT.getVectorNumElements() == 4 &&
2528 (VT.is128BitVector() || VT.is64BitVector())) {
2529 unsigned PFIndexes[4];
2530 for (unsigned i = 0; i != 4; ++i) {
2531 if (M[i] < 0)
2532 PFIndexes[i] = 8;
2533 else
2534 PFIndexes[i] = M[i];
2535 }
2536
2537 // Compute the index in the perfect shuffle table.
2538 unsigned PFTableIndex =
2539 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2540 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2541 unsigned Cost = (PFEntry >> 30);
2542
2543 if (Cost <= 4)
2544 return true;
2545 }
2546
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002547 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00002548 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002549
2550 return (ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
2551 isVREVMask(M, VT, 64) ||
2552 isVREVMask(M, VT, 32) ||
2553 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00002554 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
2555 isVTRNMask(M, VT, WhichResult) ||
2556 isVUZPMask(M, VT, WhichResult) ||
2557 isVZIPMask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002558}
2559
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002560/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
2561/// the specified operations to build the shuffle.
2562static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
2563 SDValue RHS, SelectionDAG &DAG,
2564 DebugLoc dl) {
2565 unsigned OpNum = (PFEntry >> 26) & 0x0F;
2566 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
2567 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
2568
2569 enum {
2570 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
2571 OP_VREV,
2572 OP_VDUP0,
2573 OP_VDUP1,
2574 OP_VDUP2,
2575 OP_VDUP3,
2576 OP_VEXT1,
2577 OP_VEXT2,
2578 OP_VEXT3,
2579 OP_VUZPL, // VUZP, left result
2580 OP_VUZPR, // VUZP, right result
2581 OP_VZIPL, // VZIP, left result
2582 OP_VZIPR, // VZIP, right result
2583 OP_VTRNL, // VTRN, left result
2584 OP_VTRNR // VTRN, right result
2585 };
2586
2587 if (OpNum == OP_COPY) {
2588 if (LHSID == (1*9+2)*9+3) return LHS;
2589 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
2590 return RHS;
2591 }
2592
2593 SDValue OpLHS, OpRHS;
2594 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
2595 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
2596 EVT VT = OpLHS.getValueType();
2597
2598 switch (OpNum) {
2599 default: llvm_unreachable("Unknown shuffle opcode!");
2600 case OP_VREV:
2601 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
2602 case OP_VDUP0:
2603 case OP_VDUP1:
2604 case OP_VDUP2:
2605 case OP_VDUP3:
2606 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002607 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002608 case OP_VEXT1:
2609 case OP_VEXT2:
2610 case OP_VEXT3:
2611 return DAG.getNode(ARMISD::VEXT, dl, VT,
2612 OpLHS, OpRHS,
2613 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
2614 case OP_VUZPL:
2615 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002616 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002617 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
2618 case OP_VZIPL:
2619 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002620 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002621 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
2622 case OP_VTRNL:
2623 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002624 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2625 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002626 }
2627}
2628
Bob Wilson5bafff32009-06-22 23:27:02 +00002629static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002630 SDValue V1 = Op.getOperand(0);
2631 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00002632 DebugLoc dl = Op.getDebugLoc();
2633 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002634 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002635 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00002636
Bob Wilson28865062009-08-13 02:13:04 +00002637 // Convert shuffles that are directly supported on NEON to target-specific
2638 // DAG nodes, instead of keeping them as shuffles and matching them again
2639 // during code selection. This is more efficient and avoids the possibility
2640 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00002641 // FIXME: floating-point vectors should be canonicalized to integer vectors
2642 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002643 SVN->getMask(ShuffleMask);
2644
2645 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
Bob Wilson0ce37102009-08-14 05:08:32 +00002646 int Lane = SVN->getSplatIndex();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002647 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
2648 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00002649 }
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002650 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002651 DAG.getConstant(Lane, MVT::i32));
Bob Wilson0ce37102009-08-14 05:08:32 +00002652 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002653
2654 bool ReverseVEXT;
2655 unsigned Imm;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002656 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002657 if (ReverseVEXT)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002658 std::swap(V1, V2);
2659 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002660 DAG.getConstant(Imm, MVT::i32));
2661 }
2662
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002663 if (isVREVMask(ShuffleMask, VT, 64))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002664 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002665 if (isVREVMask(ShuffleMask, VT, 32))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002666 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002667 if (isVREVMask(ShuffleMask, VT, 16))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002668 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
2669
Bob Wilsonc692cb72009-08-21 20:54:19 +00002670 // Check for Neon shuffles that modify both input vectors in place.
2671 // If both results are used, i.e., if there are two shuffles with the same
2672 // source operands and with masks corresponding to both results of one of
2673 // these operations, DAG memoization will ensure that a single node is
2674 // used for both shuffles.
2675 unsigned WhichResult;
2676 if (isVTRNMask(ShuffleMask, VT, WhichResult))
2677 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2678 V1, V2).getValue(WhichResult);
2679 if (isVUZPMask(ShuffleMask, VT, WhichResult))
2680 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
2681 V1, V2).getValue(WhichResult);
2682 if (isVZIPMask(ShuffleMask, VT, WhichResult))
2683 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
2684 V1, V2).getValue(WhichResult);
2685
2686 // If the shuffle is not directly supported and it has 4 elements, use
2687 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002688 if (VT.getVectorNumElements() == 4 &&
2689 (VT.is128BitVector() || VT.is64BitVector())) {
2690 unsigned PFIndexes[4];
2691 for (unsigned i = 0; i != 4; ++i) {
2692 if (ShuffleMask[i] < 0)
2693 PFIndexes[i] = 8;
2694 else
2695 PFIndexes[i] = ShuffleMask[i];
2696 }
2697
2698 // Compute the index in the perfect shuffle table.
2699 unsigned PFTableIndex =
2700 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2701
2702 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2703 unsigned Cost = (PFEntry >> 30);
2704
2705 if (Cost <= 4)
2706 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
2707 }
Bob Wilsond8e17572009-08-12 22:31:50 +00002708
Bob Wilson22cac0d2009-08-14 05:16:33 +00002709 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00002710}
2711
Bob Wilson5bafff32009-06-22 23:27:02 +00002712static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002713 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002714 DebugLoc dl = Op.getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00002715 SDValue Vec = Op.getOperand(0);
2716 SDValue Lane = Op.getOperand(1);
Bob Wilson934f98b2009-10-15 23:12:05 +00002717 assert(VT == MVT::i32 &&
2718 Vec.getValueType().getVectorElementType().getSizeInBits() < 32 &&
2719 "unexpected type for custom-lowering vector extract");
2720 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
Bob Wilson5bafff32009-06-22 23:27:02 +00002721}
2722
Bob Wilsona6d65862009-08-03 20:36:38 +00002723static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
2724 // The only time a CONCAT_VECTORS operation can have legal types is when
2725 // two 64-bit vectors are concatenated to a 128-bit vector.
2726 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
2727 "unexpected CONCAT_VECTORS");
2728 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00002729 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00002730 SDValue Op0 = Op.getOperand(0);
2731 SDValue Op1 = Op.getOperand(1);
2732 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00002733 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
2734 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00002735 DAG.getIntPtrConstant(0));
2736 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00002737 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
2738 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00002739 DAG.getIntPtrConstant(1));
2740 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00002741}
2742
Dan Gohman475871a2008-07-27 21:46:04 +00002743SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00002744 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002745 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00002746 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002747 case ISD::GlobalAddress:
2748 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
2749 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002750 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002751 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, Subtarget);
2752 case ISD::BR_CC: return LowerBR_CC(Op, DAG, Subtarget);
2753 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Evan Cheng86198642009-08-07 00:34:42 +00002754 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002755 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
2756 case ISD::SINT_TO_FP:
2757 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
2758 case ISD::FP_TO_SINT:
2759 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
2760 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00002761 case ISD::RETURNADDR: break;
Jim Grosbach0e0da732009-05-12 23:59:14 +00002762 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002763 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002764 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00002765 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00002766 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00002767 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00002768 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
2769 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
2770 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2771 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00002772 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00002773 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002774 }
Dan Gohman475871a2008-07-27 21:46:04 +00002775 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00002776}
2777
Duncan Sands1607f052008-12-01 11:39:25 +00002778/// ReplaceNodeResults - Replace the results of node with an illegal result
2779/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00002780void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
2781 SmallVectorImpl<SDValue>&Results,
2782 SelectionDAG &DAG) {
Chris Lattner27a6c732007-11-24 07:07:01 +00002783 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00002784 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002785 llvm_unreachable("Don't know how to custom expand this!");
Duncan Sands1607f052008-12-01 11:39:25 +00002786 return;
2787 case ISD::BIT_CONVERT:
2788 Results.push_back(ExpandBIT_CONVERT(N, DAG));
2789 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00002790 case ISD::SRL:
Duncan Sands1607f052008-12-01 11:39:25 +00002791 case ISD::SRA: {
Bob Wilson5bafff32009-06-22 23:27:02 +00002792 SDValue Res = LowerShift(N, DAG, Subtarget);
Duncan Sands1607f052008-12-01 11:39:25 +00002793 if (Res.getNode())
2794 Results.push_back(Res);
2795 return;
2796 }
Chris Lattner27a6c732007-11-24 07:07:01 +00002797 }
2798}
Chris Lattner27a6c732007-11-24 07:07:01 +00002799
Evan Chenga8e29892007-01-19 07:51:42 +00002800//===----------------------------------------------------------------------===//
2801// ARM Scheduler Hooks
2802//===----------------------------------------------------------------------===//
2803
2804MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00002805ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +00002806 MachineBasicBlock *BB,
2807 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002808 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00002809 DebugLoc dl = MI->getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002810 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00002811 default:
2812 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng007ea272009-08-12 05:17:19 +00002813 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00002814 // To "insert" a SELECT_CC instruction, we actually have to insert the
2815 // diamond control-flow pattern. The incoming instruction knows the
2816 // destination vreg to set, the condition code register to branch on, the
2817 // true/false values to select between, and a branch opcode to use.
2818 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002819 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00002820 ++It;
2821
2822 // thisMBB:
2823 // ...
2824 // TrueVal = ...
2825 // cmpTY ccX, r1, r2
2826 // bCC copy1MBB
2827 // fallthrough --> copy0MBB
2828 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002829 MachineFunction *F = BB->getParent();
2830 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
2831 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenb6728402009-02-13 02:25:56 +00002832 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00002833 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002834 F->insert(It, copy0MBB);
2835 F->insert(It, sinkMBB);
Evan Chenga8e29892007-01-19 07:51:42 +00002836 // Update machine-CFG edges by first adding all successors of the current
2837 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00002838 // Also inform sdisel of the edge changes.
2839 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
2840 E = BB->succ_end(); I != E; ++I) {
2841 EM->insert(std::make_pair(*I, sinkMBB));
2842 sinkMBB->addSuccessor(*I);
2843 }
Evan Chenga8e29892007-01-19 07:51:42 +00002844 // Next, remove all successors of the current block, and add the true
2845 // and fallthrough blocks as its successors.
Evan Chengce319102009-09-19 09:51:03 +00002846 while (!BB->succ_empty())
Evan Chenga8e29892007-01-19 07:51:42 +00002847 BB->removeSuccessor(BB->succ_begin());
2848 BB->addSuccessor(copy0MBB);
2849 BB->addSuccessor(sinkMBB);
2850
2851 // copy0MBB:
2852 // %FalseValue = ...
2853 // # fallthrough to sinkMBB
2854 BB = copy0MBB;
2855
2856 // Update machine-CFG edges
2857 BB->addSuccessor(sinkMBB);
2858
2859 // sinkMBB:
2860 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2861 // ...
2862 BB = sinkMBB;
Dale Johannesenb6728402009-02-13 02:25:56 +00002863 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00002864 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
2865 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
2866
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002867 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00002868 return BB;
2869 }
Evan Cheng86198642009-08-07 00:34:42 +00002870
2871 case ARM::tANDsp:
2872 case ARM::tADDspr_:
2873 case ARM::tSUBspi_:
2874 case ARM::t2SUBrSPi_:
2875 case ARM::t2SUBrSPi12_:
2876 case ARM::t2SUBrSPs_: {
2877 MachineFunction *MF = BB->getParent();
2878 unsigned DstReg = MI->getOperand(0).getReg();
2879 unsigned SrcReg = MI->getOperand(1).getReg();
2880 bool DstIsDead = MI->getOperand(0).isDead();
2881 bool SrcIsKill = MI->getOperand(1).isKill();
2882
2883 if (SrcReg != ARM::SP) {
2884 // Copy the source to SP from virtual register.
2885 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(SrcReg);
2886 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
2887 ? ARM::tMOVtgpr2gpr : ARM::tMOVgpr2gpr;
2888 BuildMI(BB, dl, TII->get(CopyOpc), ARM::SP)
2889 .addReg(SrcReg, getKillRegState(SrcIsKill));
2890 }
2891
2892 unsigned OpOpc = 0;
2893 bool NeedPred = false, NeedCC = false, NeedOp3 = false;
2894 switch (MI->getOpcode()) {
2895 default:
2896 llvm_unreachable("Unexpected pseudo instruction!");
2897 case ARM::tANDsp:
2898 OpOpc = ARM::tAND;
2899 NeedPred = true;
2900 break;
2901 case ARM::tADDspr_:
2902 OpOpc = ARM::tADDspr;
2903 break;
2904 case ARM::tSUBspi_:
2905 OpOpc = ARM::tSUBspi;
2906 break;
2907 case ARM::t2SUBrSPi_:
2908 OpOpc = ARM::t2SUBrSPi;
2909 NeedPred = true; NeedCC = true;
2910 break;
2911 case ARM::t2SUBrSPi12_:
2912 OpOpc = ARM::t2SUBrSPi12;
2913 NeedPred = true;
2914 break;
2915 case ARM::t2SUBrSPs_:
2916 OpOpc = ARM::t2SUBrSPs;
2917 NeedPred = true; NeedCC = true; NeedOp3 = true;
2918 break;
2919 }
2920 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(OpOpc), ARM::SP);
2921 if (OpOpc == ARM::tAND)
2922 AddDefaultT1CC(MIB);
2923 MIB.addReg(ARM::SP);
2924 MIB.addOperand(MI->getOperand(2));
2925 if (NeedOp3)
2926 MIB.addOperand(MI->getOperand(3));
2927 if (NeedPred)
2928 AddDefaultPred(MIB);
2929 if (NeedCC)
2930 AddDefaultCC(MIB);
2931
2932 // Copy the result from SP to virtual register.
2933 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(DstReg);
2934 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
2935 ? ARM::tMOVgpr2tgpr : ARM::tMOVgpr2gpr;
2936 BuildMI(BB, dl, TII->get(CopyOpc))
2937 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
2938 .addReg(ARM::SP);
2939 MF->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
2940 return BB;
2941 }
Evan Chenga8e29892007-01-19 07:51:42 +00002942 }
2943}
2944
2945//===----------------------------------------------------------------------===//
2946// ARM Optimization Hooks
2947//===----------------------------------------------------------------------===//
2948
Chris Lattnerd1980a52009-03-12 06:52:53 +00002949static
2950SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
2951 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00002952 SelectionDAG &DAG = DCI.DAG;
2953 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00002954 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00002955 unsigned Opc = N->getOpcode();
2956 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
2957 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
2958 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
2959 ISD::CondCode CC = ISD::SETCC_INVALID;
2960
2961 if (isSlctCC) {
2962 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
2963 } else {
2964 SDValue CCOp = Slct.getOperand(0);
2965 if (CCOp.getOpcode() == ISD::SETCC)
2966 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
2967 }
2968
2969 bool DoXform = false;
2970 bool InvCC = false;
2971 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
2972 "Bad input!");
2973
2974 if (LHS.getOpcode() == ISD::Constant &&
2975 cast<ConstantSDNode>(LHS)->isNullValue()) {
2976 DoXform = true;
2977 } else if (CC != ISD::SETCC_INVALID &&
2978 RHS.getOpcode() == ISD::Constant &&
2979 cast<ConstantSDNode>(RHS)->isNullValue()) {
2980 std::swap(LHS, RHS);
2981 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00002982 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00002983 Op0.getOperand(0).getValueType();
2984 bool isInt = OpVT.isInteger();
2985 CC = ISD::getSetCCInverse(CC, isInt);
2986
2987 if (!TLI.isCondCodeLegal(CC, OpVT))
2988 return SDValue(); // Inverse operator isn't legal.
2989
2990 DoXform = true;
2991 InvCC = true;
2992 }
2993
2994 if (DoXform) {
2995 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
2996 if (isSlctCC)
2997 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
2998 Slct.getOperand(0), Slct.getOperand(1), CC);
2999 SDValue CCOp = Slct.getOperand(0);
3000 if (InvCC)
3001 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
3002 CCOp.getOperand(0), CCOp.getOperand(1), CC);
3003 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
3004 CCOp, OtherOp, Result);
3005 }
3006 return SDValue();
3007}
3008
3009/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
3010static SDValue PerformADDCombine(SDNode *N,
3011 TargetLowering::DAGCombinerInfo &DCI) {
3012 // added by evan in r37685 with no testcase.
3013 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003014
Chris Lattnerd1980a52009-03-12 06:52:53 +00003015 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
3016 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
3017 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
3018 if (Result.getNode()) return Result;
3019 }
3020 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3021 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3022 if (Result.getNode()) return Result;
3023 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003024
Chris Lattnerd1980a52009-03-12 06:52:53 +00003025 return SDValue();
3026}
3027
3028/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
3029static SDValue PerformSUBCombine(SDNode *N,
3030 TargetLowering::DAGCombinerInfo &DCI) {
3031 // added by evan in r37685 with no testcase.
3032 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003033
Chris Lattnerd1980a52009-03-12 06:52:53 +00003034 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
3035 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3036 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3037 if (Result.getNode()) return Result;
3038 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003039
Chris Lattnerd1980a52009-03-12 06:52:53 +00003040 return SDValue();
3041}
3042
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003043/// PerformFMRRDCombine - Target-specific dag combine xforms for ARMISD::FMRRD.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003044static SDValue PerformFMRRDCombine(SDNode *N,
3045 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003046 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00003047 SDValue InDouble = N->getOperand(0);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003048 if (InDouble.getOpcode() == ARMISD::FMDRR)
3049 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003050 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003051}
3052
Bob Wilson5bafff32009-06-22 23:27:02 +00003053/// getVShiftImm - Check if this is a valid build_vector for the immediate
3054/// operand of a vector shift operation, where all the elements of the
3055/// build_vector must have the same constant integer value.
3056static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
3057 // Ignore bit_converts.
3058 while (Op.getOpcode() == ISD::BIT_CONVERT)
3059 Op = Op.getOperand(0);
3060 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
3061 APInt SplatBits, SplatUndef;
3062 unsigned SplatBitSize;
3063 bool HasAnyUndefs;
3064 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
3065 HasAnyUndefs, ElementBits) ||
3066 SplatBitSize > ElementBits)
3067 return false;
3068 Cnt = SplatBits.getSExtValue();
3069 return true;
3070}
3071
3072/// isVShiftLImm - Check if this is a valid build_vector for the immediate
3073/// operand of a vector shift left operation. That value must be in the range:
3074/// 0 <= Value < ElementBits for a left shift; or
3075/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003076static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003077 assert(VT.isVector() && "vector shift count is not a vector type");
3078 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3079 if (! getVShiftImm(Op, ElementBits, Cnt))
3080 return false;
3081 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
3082}
3083
3084/// isVShiftRImm - Check if this is a valid build_vector for the immediate
3085/// operand of a vector shift right operation. For a shift opcode, the value
3086/// is positive, but for an intrinsic the value count must be negative. The
3087/// absolute value must be in the range:
3088/// 1 <= |Value| <= ElementBits for a right shift; or
3089/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003090static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00003091 int64_t &Cnt) {
3092 assert(VT.isVector() && "vector shift count is not a vector type");
3093 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3094 if (! getVShiftImm(Op, ElementBits, Cnt))
3095 return false;
3096 if (isIntrinsic)
3097 Cnt = -Cnt;
3098 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
3099}
3100
3101/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
3102static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
3103 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
3104 switch (IntNo) {
3105 default:
3106 // Don't do anything for most intrinsics.
3107 break;
3108
3109 // Vector shifts: check for immediate versions and lower them.
3110 // Note: This is done during DAG combining instead of DAG legalizing because
3111 // the build_vectors for 64-bit vector element shift counts are generally
3112 // not legal, and it is hard to see their values after they get legalized to
3113 // loads from a constant pool.
3114 case Intrinsic::arm_neon_vshifts:
3115 case Intrinsic::arm_neon_vshiftu:
3116 case Intrinsic::arm_neon_vshiftls:
3117 case Intrinsic::arm_neon_vshiftlu:
3118 case Intrinsic::arm_neon_vshiftn:
3119 case Intrinsic::arm_neon_vrshifts:
3120 case Intrinsic::arm_neon_vrshiftu:
3121 case Intrinsic::arm_neon_vrshiftn:
3122 case Intrinsic::arm_neon_vqshifts:
3123 case Intrinsic::arm_neon_vqshiftu:
3124 case Intrinsic::arm_neon_vqshiftsu:
3125 case Intrinsic::arm_neon_vqshiftns:
3126 case Intrinsic::arm_neon_vqshiftnu:
3127 case Intrinsic::arm_neon_vqshiftnsu:
3128 case Intrinsic::arm_neon_vqrshiftns:
3129 case Intrinsic::arm_neon_vqrshiftnu:
3130 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00003131 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003132 int64_t Cnt;
3133 unsigned VShiftOpc = 0;
3134
3135 switch (IntNo) {
3136 case Intrinsic::arm_neon_vshifts:
3137 case Intrinsic::arm_neon_vshiftu:
3138 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
3139 VShiftOpc = ARMISD::VSHL;
3140 break;
3141 }
3142 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
3143 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
3144 ARMISD::VSHRs : ARMISD::VSHRu);
3145 break;
3146 }
3147 return SDValue();
3148
3149 case Intrinsic::arm_neon_vshiftls:
3150 case Intrinsic::arm_neon_vshiftlu:
3151 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
3152 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003153 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003154
3155 case Intrinsic::arm_neon_vrshifts:
3156 case Intrinsic::arm_neon_vrshiftu:
3157 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
3158 break;
3159 return SDValue();
3160
3161 case Intrinsic::arm_neon_vqshifts:
3162 case Intrinsic::arm_neon_vqshiftu:
3163 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3164 break;
3165 return SDValue();
3166
3167 case Intrinsic::arm_neon_vqshiftsu:
3168 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3169 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003170 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003171
3172 case Intrinsic::arm_neon_vshiftn:
3173 case Intrinsic::arm_neon_vrshiftn:
3174 case Intrinsic::arm_neon_vqshiftns:
3175 case Intrinsic::arm_neon_vqshiftnu:
3176 case Intrinsic::arm_neon_vqshiftnsu:
3177 case Intrinsic::arm_neon_vqrshiftns:
3178 case Intrinsic::arm_neon_vqrshiftnu:
3179 case Intrinsic::arm_neon_vqrshiftnsu:
3180 // Narrowing shifts require an immediate right shift.
3181 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
3182 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003183 llvm_unreachable("invalid shift count for narrowing vector shift intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003184
3185 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003186 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003187 }
3188
3189 switch (IntNo) {
3190 case Intrinsic::arm_neon_vshifts:
3191 case Intrinsic::arm_neon_vshiftu:
3192 // Opcode already set above.
3193 break;
3194 case Intrinsic::arm_neon_vshiftls:
3195 case Intrinsic::arm_neon_vshiftlu:
3196 if (Cnt == VT.getVectorElementType().getSizeInBits())
3197 VShiftOpc = ARMISD::VSHLLi;
3198 else
3199 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
3200 ARMISD::VSHLLs : ARMISD::VSHLLu);
3201 break;
3202 case Intrinsic::arm_neon_vshiftn:
3203 VShiftOpc = ARMISD::VSHRN; break;
3204 case Intrinsic::arm_neon_vrshifts:
3205 VShiftOpc = ARMISD::VRSHRs; break;
3206 case Intrinsic::arm_neon_vrshiftu:
3207 VShiftOpc = ARMISD::VRSHRu; break;
3208 case Intrinsic::arm_neon_vrshiftn:
3209 VShiftOpc = ARMISD::VRSHRN; break;
3210 case Intrinsic::arm_neon_vqshifts:
3211 VShiftOpc = ARMISD::VQSHLs; break;
3212 case Intrinsic::arm_neon_vqshiftu:
3213 VShiftOpc = ARMISD::VQSHLu; break;
3214 case Intrinsic::arm_neon_vqshiftsu:
3215 VShiftOpc = ARMISD::VQSHLsu; break;
3216 case Intrinsic::arm_neon_vqshiftns:
3217 VShiftOpc = ARMISD::VQSHRNs; break;
3218 case Intrinsic::arm_neon_vqshiftnu:
3219 VShiftOpc = ARMISD::VQSHRNu; break;
3220 case Intrinsic::arm_neon_vqshiftnsu:
3221 VShiftOpc = ARMISD::VQSHRNsu; break;
3222 case Intrinsic::arm_neon_vqrshiftns:
3223 VShiftOpc = ARMISD::VQRSHRNs; break;
3224 case Intrinsic::arm_neon_vqrshiftnu:
3225 VShiftOpc = ARMISD::VQRSHRNu; break;
3226 case Intrinsic::arm_neon_vqrshiftnsu:
3227 VShiftOpc = ARMISD::VQRSHRNsu; break;
3228 }
3229
3230 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003231 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003232 }
3233
3234 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00003235 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003236 int64_t Cnt;
3237 unsigned VShiftOpc = 0;
3238
3239 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
3240 VShiftOpc = ARMISD::VSLI;
3241 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
3242 VShiftOpc = ARMISD::VSRI;
3243 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00003244 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003245 }
3246
3247 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
3248 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00003249 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003250 }
3251
3252 case Intrinsic::arm_neon_vqrshifts:
3253 case Intrinsic::arm_neon_vqrshiftu:
3254 // No immediate versions of these to check for.
3255 break;
3256 }
3257
3258 return SDValue();
3259}
3260
3261/// PerformShiftCombine - Checks for immediate versions of vector shifts and
3262/// lowers them. As with the vector shift intrinsics, this is done during DAG
3263/// combining instead of DAG legalizing because the build_vectors for 64-bit
3264/// vector element shift counts are generally not legal, and it is hard to see
3265/// their values after they get legalized to loads from a constant pool.
3266static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
3267 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003268 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003269
3270 // Nothing to be done for scalar shifts.
3271 if (! VT.isVector())
3272 return SDValue();
3273
3274 assert(ST->hasNEON() && "unexpected vector shift");
3275 int64_t Cnt;
3276
3277 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003278 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003279
3280 case ISD::SHL:
3281 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
3282 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003283 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003284 break;
3285
3286 case ISD::SRA:
3287 case ISD::SRL:
3288 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
3289 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
3290 ARMISD::VSHRs : ARMISD::VSHRu);
3291 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003292 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003293 }
3294 }
3295 return SDValue();
3296}
3297
3298/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
3299/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
3300static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
3301 const ARMSubtarget *ST) {
3302 SDValue N0 = N->getOperand(0);
3303
3304 // Check for sign- and zero-extensions of vector extract operations of 8-
3305 // and 16-bit vector elements. NEON supports these directly. They are
3306 // handled during DAG combining because type legalization will promote them
3307 // to 32-bit types and it is messy to recognize the operations after that.
3308 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
3309 SDValue Vec = N0.getOperand(0);
3310 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00003311 EVT VT = N->getValueType(0);
3312 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003313 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3314
Owen Anderson825b72b2009-08-11 20:47:22 +00003315 if (VT == MVT::i32 &&
3316 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00003317 TLI.isTypeLegal(Vec.getValueType())) {
3318
3319 unsigned Opc = 0;
3320 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003321 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003322 case ISD::SIGN_EXTEND:
3323 Opc = ARMISD::VGETLANEs;
3324 break;
3325 case ISD::ZERO_EXTEND:
3326 case ISD::ANY_EXTEND:
3327 Opc = ARMISD::VGETLANEu;
3328 break;
3329 }
3330 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
3331 }
3332 }
3333
3334 return SDValue();
3335}
3336
Dan Gohman475871a2008-07-27 21:46:04 +00003337SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003338 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003339 switch (N->getOpcode()) {
3340 default: break;
Chris Lattnerd1980a52009-03-12 06:52:53 +00003341 case ISD::ADD: return PerformADDCombine(N, DCI);
3342 case ISD::SUB: return PerformSUBCombine(N, DCI);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003343 case ARMISD::FMRRD: return PerformFMRRDCombine(N, DCI);
Bob Wilson5bafff32009-06-22 23:27:02 +00003344 case ISD::INTRINSIC_WO_CHAIN:
3345 return PerformIntrinsicCombine(N, DCI.DAG);
3346 case ISD::SHL:
3347 case ISD::SRA:
3348 case ISD::SRL:
3349 return PerformShiftCombine(N, DCI.DAG, Subtarget);
3350 case ISD::SIGN_EXTEND:
3351 case ISD::ZERO_EXTEND:
3352 case ISD::ANY_EXTEND:
3353 return PerformExtendCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003354 }
Dan Gohman475871a2008-07-27 21:46:04 +00003355 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003356}
3357
Bill Wendlingaf566342009-08-15 21:21:19 +00003358bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
3359 if (!Subtarget->hasV6Ops())
3360 // Pre-v6 does not support unaligned mem access.
3361 return false;
3362 else if (!Subtarget->hasV6Ops()) {
3363 // v6 may or may not support unaligned mem access.
3364 if (!Subtarget->isTargetDarwin())
3365 return false;
3366 }
3367
3368 switch (VT.getSimpleVT().SimpleTy) {
3369 default:
3370 return false;
3371 case MVT::i8:
3372 case MVT::i16:
3373 case MVT::i32:
3374 return true;
3375 // FIXME: VLD1 etc with standard alignment is legal.
3376 }
3377}
3378
Evan Chenge6c835f2009-08-14 20:09:37 +00003379static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
3380 if (V < 0)
3381 return false;
3382
3383 unsigned Scale = 1;
3384 switch (VT.getSimpleVT().SimpleTy) {
3385 default: return false;
3386 case MVT::i1:
3387 case MVT::i8:
3388 // Scale == 1;
3389 break;
3390 case MVT::i16:
3391 // Scale == 2;
3392 Scale = 2;
3393 break;
3394 case MVT::i32:
3395 // Scale == 4;
3396 Scale = 4;
3397 break;
3398 }
3399
3400 if ((V & (Scale - 1)) != 0)
3401 return false;
3402 V /= Scale;
3403 return V == (V & ((1LL << 5) - 1));
3404}
3405
3406static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
3407 const ARMSubtarget *Subtarget) {
3408 bool isNeg = false;
3409 if (V < 0) {
3410 isNeg = true;
3411 V = - V;
3412 }
3413
3414 switch (VT.getSimpleVT().SimpleTy) {
3415 default: return false;
3416 case MVT::i1:
3417 case MVT::i8:
3418 case MVT::i16:
3419 case MVT::i32:
3420 // + imm12 or - imm8
3421 if (isNeg)
3422 return V == (V & ((1LL << 8) - 1));
3423 return V == (V & ((1LL << 12) - 1));
3424 case MVT::f32:
3425 case MVT::f64:
3426 // Same as ARM mode. FIXME: NEON?
3427 if (!Subtarget->hasVFP2())
3428 return false;
3429 if ((V & 3) != 0)
3430 return false;
3431 V >>= 2;
3432 return V == (V & ((1LL << 8) - 1));
3433 }
3434}
3435
Evan Chengb01fad62007-03-12 23:30:29 +00003436/// isLegalAddressImmediate - Return true if the integer value can be used
3437/// as the offset of the target addressing mode for load / store of the
3438/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00003439static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00003440 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00003441 if (V == 0)
3442 return true;
3443
Evan Cheng65011532009-03-09 19:15:00 +00003444 if (!VT.isSimple())
3445 return false;
3446
Evan Chenge6c835f2009-08-14 20:09:37 +00003447 if (Subtarget->isThumb1Only())
3448 return isLegalT1AddressImmediate(V, VT);
3449 else if (Subtarget->isThumb2())
3450 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00003451
Evan Chenge6c835f2009-08-14 20:09:37 +00003452 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00003453 if (V < 0)
3454 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00003455 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00003456 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00003457 case MVT::i1:
3458 case MVT::i8:
3459 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00003460 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003461 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003462 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00003463 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003464 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003465 case MVT::f32:
3466 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00003467 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00003468 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00003469 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00003470 return false;
3471 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003472 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00003473 }
Evan Chenga8e29892007-01-19 07:51:42 +00003474}
3475
Evan Chenge6c835f2009-08-14 20:09:37 +00003476bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
3477 EVT VT) const {
3478 int Scale = AM.Scale;
3479 if (Scale < 0)
3480 return false;
3481
3482 switch (VT.getSimpleVT().SimpleTy) {
3483 default: return false;
3484 case MVT::i1:
3485 case MVT::i8:
3486 case MVT::i16:
3487 case MVT::i32:
3488 if (Scale == 1)
3489 return true;
3490 // r + r << imm
3491 Scale = Scale & ~1;
3492 return Scale == 2 || Scale == 4 || Scale == 8;
3493 case MVT::i64:
3494 // r + r
3495 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
3496 return true;
3497 return false;
3498 case MVT::isVoid:
3499 // Note, we allow "void" uses (basically, uses that aren't loads or
3500 // stores), because arm allows folding a scale into many arithmetic
3501 // operations. This should be made more precise and revisited later.
3502
3503 // Allow r << imm, but the imm has to be a multiple of two.
3504 if (Scale & 1) return false;
3505 return isPowerOf2_32(Scale);
3506 }
3507}
3508
Chris Lattner37caf8c2007-04-09 23:33:39 +00003509/// isLegalAddressingMode - Return true if the addressing mode represented
3510/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003511bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00003512 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00003513 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00003514 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00003515 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003516
Chris Lattner37caf8c2007-04-09 23:33:39 +00003517 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003518 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003519 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003520
Chris Lattner37caf8c2007-04-09 23:33:39 +00003521 switch (AM.Scale) {
3522 case 0: // no scale reg, must be "r+i" or "r", or "i".
3523 break;
3524 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00003525 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00003526 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00003527 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00003528 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00003529 // ARM doesn't support any R+R*scale+imm addr modes.
3530 if (AM.BaseOffs)
3531 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003532
Bob Wilson2c7dab12009-04-08 17:55:28 +00003533 if (!VT.isSimple())
3534 return false;
3535
Evan Chenge6c835f2009-08-14 20:09:37 +00003536 if (Subtarget->isThumb2())
3537 return isLegalT2ScaledAddressingMode(AM, VT);
3538
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003539 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00003540 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00003541 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00003542 case MVT::i1:
3543 case MVT::i8:
3544 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003545 if (Scale < 0) Scale = -Scale;
3546 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003547 return true;
3548 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00003549 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003550 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00003551 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00003552 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003553 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003554 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00003555 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003556
Owen Anderson825b72b2009-08-11 20:47:22 +00003557 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00003558 // Note, we allow "void" uses (basically, uses that aren't loads or
3559 // stores), because arm allows folding a scale into many arithmetic
3560 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003561
Chris Lattner37caf8c2007-04-09 23:33:39 +00003562 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00003563 if (Scale & 1) return false;
3564 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00003565 }
3566 break;
Evan Chengb01fad62007-03-12 23:30:29 +00003567 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00003568 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00003569}
3570
Owen Andersone50ed302009-08-10 22:56:29 +00003571static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00003572 bool isSEXTLoad, SDValue &Base,
3573 SDValue &Offset, bool &isInc,
3574 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00003575 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
3576 return false;
3577
Owen Anderson825b72b2009-08-11 20:47:22 +00003578 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00003579 // AddressingMode 3
3580 Base = Ptr->getOperand(0);
3581 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003582 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003583 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003584 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00003585 isInc = false;
3586 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3587 return true;
3588 }
3589 }
3590 isInc = (Ptr->getOpcode() == ISD::ADD);
3591 Offset = Ptr->getOperand(1);
3592 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00003593 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00003594 // AddressingMode 2
3595 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003596 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003597 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003598 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00003599 isInc = false;
3600 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3601 Base = Ptr->getOperand(0);
3602 return true;
3603 }
3604 }
3605
3606 if (Ptr->getOpcode() == ISD::ADD) {
3607 isInc = true;
3608 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
3609 if (ShOpcVal != ARM_AM::no_shift) {
3610 Base = Ptr->getOperand(1);
3611 Offset = Ptr->getOperand(0);
3612 } else {
3613 Base = Ptr->getOperand(0);
3614 Offset = Ptr->getOperand(1);
3615 }
3616 return true;
3617 }
3618
3619 isInc = (Ptr->getOpcode() == ISD::ADD);
3620 Base = Ptr->getOperand(0);
3621 Offset = Ptr->getOperand(1);
3622 return true;
3623 }
3624
3625 // FIXME: Use FLDM / FSTM to emulate indexed FP load / store.
3626 return false;
3627}
3628
Owen Andersone50ed302009-08-10 22:56:29 +00003629static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00003630 bool isSEXTLoad, SDValue &Base,
3631 SDValue &Offset, bool &isInc,
3632 SelectionDAG &DAG) {
3633 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
3634 return false;
3635
3636 Base = Ptr->getOperand(0);
3637 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
3638 int RHSC = (int)RHS->getZExtValue();
3639 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
3640 assert(Ptr->getOpcode() == ISD::ADD);
3641 isInc = false;
3642 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3643 return true;
3644 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
3645 isInc = Ptr->getOpcode() == ISD::ADD;
3646 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
3647 return true;
3648 }
3649 }
3650
3651 return false;
3652}
3653
Evan Chenga8e29892007-01-19 07:51:42 +00003654/// getPreIndexedAddressParts - returns true by value, base pointer and
3655/// offset pointer and addressing mode by reference if the node's address
3656/// can be legally represented as pre-indexed load / store address.
3657bool
Dan Gohman475871a2008-07-27 21:46:04 +00003658ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
3659 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003660 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00003661 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003662 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00003663 return false;
3664
Owen Andersone50ed302009-08-10 22:56:29 +00003665 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00003666 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00003667 bool isSEXTLoad = false;
3668 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
3669 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003670 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003671 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
3672 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
3673 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003674 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003675 } else
3676 return false;
3677
3678 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00003679 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00003680 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00003681 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
3682 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00003683 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00003684 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00003685 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00003686 if (!isLegal)
3687 return false;
3688
3689 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
3690 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00003691}
3692
3693/// getPostIndexedAddressParts - returns true by value, base pointer and
3694/// offset pointer and addressing mode by reference if this node can be
3695/// combined with a load / store to form a post-indexed load / store.
3696bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00003697 SDValue &Base,
3698 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003699 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00003700 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003701 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00003702 return false;
3703
Owen Andersone50ed302009-08-10 22:56:29 +00003704 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00003705 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00003706 bool isSEXTLoad = false;
3707 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003708 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003709 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
3710 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003711 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003712 } else
3713 return false;
3714
3715 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00003716 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00003717 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00003718 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003719 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00003720 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00003721 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
3722 isInc, DAG);
3723 if (!isLegal)
3724 return false;
3725
3726 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
3727 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00003728}
3729
Dan Gohman475871a2008-07-27 21:46:04 +00003730void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00003731 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003732 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003733 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00003734 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00003735 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003736 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00003737 switch (Op.getOpcode()) {
3738 default: break;
3739 case ARMISD::CMOV: {
3740 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00003741 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00003742 if (KnownZero == 0 && KnownOne == 0) return;
3743
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003744 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00003745 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
3746 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00003747 KnownZero &= KnownZeroRHS;
3748 KnownOne &= KnownOneRHS;
3749 return;
3750 }
3751 }
3752}
3753
3754//===----------------------------------------------------------------------===//
3755// ARM Inline Assembly Support
3756//===----------------------------------------------------------------------===//
3757
3758/// getConstraintType - Given a constraint letter, return the type of
3759/// constraint it is for this target.
3760ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00003761ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
3762 if (Constraint.size() == 1) {
3763 switch (Constraint[0]) {
3764 default: break;
3765 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003766 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00003767 }
Evan Chenga8e29892007-01-19 07:51:42 +00003768 }
Chris Lattner4234f572007-03-25 02:14:49 +00003769 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00003770}
3771
Bob Wilson2dc4f542009-03-20 22:42:55 +00003772std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00003773ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00003774 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003775 if (Constraint.size() == 1) {
3776 // GCC RS6000 Constraint Letters
3777 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003778 case 'l':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003779 if (Subtarget->isThumb1Only())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00003780 return std::make_pair(0U, ARM::tGPRRegisterClass);
3781 else
3782 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003783 case 'r':
3784 return std::make_pair(0U, ARM::GPRRegisterClass);
3785 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00003786 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003787 return std::make_pair(0U, ARM::SPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00003788 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003789 return std::make_pair(0U, ARM::DPRRegisterClass);
3790 break;
Evan Chenga8e29892007-01-19 07:51:42 +00003791 }
3792 }
3793 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3794}
3795
3796std::vector<unsigned> ARMTargetLowering::
3797getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00003798 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003799 if (Constraint.size() != 1)
3800 return std::vector<unsigned>();
3801
3802 switch (Constraint[0]) { // GCC ARM Constraint Letters
3803 default: break;
3804 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00003805 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
3806 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
3807 0);
Evan Chenga8e29892007-01-19 07:51:42 +00003808 case 'r':
3809 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
3810 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
3811 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
3812 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003813 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00003814 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003815 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
3816 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
3817 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
3818 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
3819 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
3820 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
3821 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
3822 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00003823 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003824 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
3825 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
3826 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
3827 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
3828 break;
Evan Chenga8e29892007-01-19 07:51:42 +00003829 }
3830
3831 return std::vector<unsigned>();
3832}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003833
3834/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3835/// vector. If it is invalid, don't add anything to Ops.
3836void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3837 char Constraint,
3838 bool hasMemory,
3839 std::vector<SDValue>&Ops,
3840 SelectionDAG &DAG) const {
3841 SDValue Result(0, 0);
3842
3843 switch (Constraint) {
3844 default: break;
3845 case 'I': case 'J': case 'K': case 'L':
3846 case 'M': case 'N': case 'O':
3847 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
3848 if (!C)
3849 return;
3850
3851 int64_t CVal64 = C->getSExtValue();
3852 int CVal = (int) CVal64;
3853 // None of these constraints allow values larger than 32 bits. Check
3854 // that the value fits in an int.
3855 if (CVal != CVal64)
3856 return;
3857
3858 switch (Constraint) {
3859 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003860 if (Subtarget->isThumb1Only()) {
3861 // This must be a constant between 0 and 255, for ADD
3862 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003863 if (CVal >= 0 && CVal <= 255)
3864 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00003865 } else if (Subtarget->isThumb2()) {
3866 // A constant that can be used as an immediate value in a
3867 // data-processing instruction.
3868 if (ARM_AM::getT2SOImmVal(CVal) != -1)
3869 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003870 } else {
3871 // A constant that can be used as an immediate value in a
3872 // data-processing instruction.
3873 if (ARM_AM::getSOImmVal(CVal) != -1)
3874 break;
3875 }
3876 return;
3877
3878 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003879 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003880 // This must be a constant between -255 and -1, for negated ADD
3881 // immediates. This can be used in GCC with an "n" modifier that
3882 // prints the negated value, for use with SUB instructions. It is
3883 // not useful otherwise but is implemented for compatibility.
3884 if (CVal >= -255 && CVal <= -1)
3885 break;
3886 } else {
3887 // This must be a constant between -4095 and 4095. It is not clear
3888 // what this constraint is intended for. Implemented for
3889 // compatibility with GCC.
3890 if (CVal >= -4095 && CVal <= 4095)
3891 break;
3892 }
3893 return;
3894
3895 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003896 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003897 // A 32-bit value where only one byte has a nonzero value. Exclude
3898 // zero to match GCC. This constraint is used by GCC internally for
3899 // constants that can be loaded with a move/shift combination.
3900 // It is not useful otherwise but is implemented for compatibility.
3901 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
3902 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00003903 } else if (Subtarget->isThumb2()) {
3904 // A constant whose bitwise inverse can be used as an immediate
3905 // value in a data-processing instruction. This can be used in GCC
3906 // with a "B" modifier that prints the inverted value, for use with
3907 // BIC and MVN instructions. It is not useful otherwise but is
3908 // implemented for compatibility.
3909 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
3910 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003911 } else {
3912 // A constant whose bitwise inverse can be used as an immediate
3913 // value in a data-processing instruction. This can be used in GCC
3914 // with a "B" modifier that prints the inverted value, for use with
3915 // BIC and MVN instructions. It is not useful otherwise but is
3916 // implemented for compatibility.
3917 if (ARM_AM::getSOImmVal(~CVal) != -1)
3918 break;
3919 }
3920 return;
3921
3922 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003923 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003924 // This must be a constant between -7 and 7,
3925 // for 3-operand ADD/SUB immediate instructions.
3926 if (CVal >= -7 && CVal < 7)
3927 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00003928 } else if (Subtarget->isThumb2()) {
3929 // A constant whose negation can be used as an immediate value in a
3930 // data-processing instruction. This can be used in GCC with an "n"
3931 // modifier that prints the negated value, for use with SUB
3932 // instructions. It is not useful otherwise but is implemented for
3933 // compatibility.
3934 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
3935 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003936 } else {
3937 // A constant whose negation can be used as an immediate value in a
3938 // data-processing instruction. This can be used in GCC with an "n"
3939 // modifier that prints the negated value, for use with SUB
3940 // instructions. It is not useful otherwise but is implemented for
3941 // compatibility.
3942 if (ARM_AM::getSOImmVal(-CVal) != -1)
3943 break;
3944 }
3945 return;
3946
3947 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003948 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003949 // This must be a multiple of 4 between 0 and 1020, for
3950 // ADD sp + immediate.
3951 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
3952 break;
3953 } else {
3954 // A power of two or a constant between 0 and 32. This is used in
3955 // GCC for the shift amount on shifted register operands, but it is
3956 // useful in general for any shift amounts.
3957 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
3958 break;
3959 }
3960 return;
3961
3962 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003963 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003964 // This must be a constant between 0 and 31, for shift amounts.
3965 if (CVal >= 0 && CVal <= 31)
3966 break;
3967 }
3968 return;
3969
3970 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003971 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003972 // This must be a multiple of 4 between -508 and 508, for
3973 // ADD/SUB sp = sp + immediate.
3974 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
3975 break;
3976 }
3977 return;
3978 }
3979 Result = DAG.getTargetConstant(CVal, Op.getValueType());
3980 break;
3981 }
3982
3983 if (Result.getNode()) {
3984 Ops.push_back(Result);
3985 return;
3986 }
3987 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
3988 Ops, DAG);
3989}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00003990
3991bool
3992ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3993 // The ARM target isn't yet aware of offsets.
3994 return false;
3995}
Evan Cheng39382422009-10-28 01:44:26 +00003996
3997int ARM::getVFPf32Imm(const APFloat &FPImm) {
3998 APInt Imm = FPImm.bitcastToAPInt();
3999 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
4000 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
4001 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
4002
4003 // We can handle 4 bits of mantissa.
4004 // mantissa = (16+UInt(e:f:g:h))/16.
4005 if (Mantissa & 0x7ffff)
4006 return -1;
4007 Mantissa >>= 19;
4008 if ((Mantissa & 0xf) != Mantissa)
4009 return -1;
4010
4011 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4012 if (Exp < -3 || Exp > 4)
4013 return -1;
4014 Exp = ((Exp+3) & 0x7) ^ 4;
4015
4016 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4017}
4018
4019int ARM::getVFPf64Imm(const APFloat &FPImm) {
4020 APInt Imm = FPImm.bitcastToAPInt();
4021 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
4022 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
4023 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
4024
4025 // We can handle 4 bits of mantissa.
4026 // mantissa = (16+UInt(e:f:g:h))/16.
4027 if (Mantissa & 0xffffffffffffLL)
4028 return -1;
4029 Mantissa >>= 48;
4030 if ((Mantissa & 0xf) != Mantissa)
4031 return -1;
4032
4033 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4034 if (Exp < -3 || Exp > 4)
4035 return -1;
4036 Exp = ((Exp+3) & 0x7) ^ 4;
4037
4038 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4039}
4040
4041/// isFPImmLegal - Returns true if the target can instruction select the
4042/// specified FP immediate natively. If false, the legalizer will
4043/// materialize the FP immediate as a load from a constant pool.
4044bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
4045 if (!Subtarget->hasVFP3())
4046 return false;
4047 if (VT == MVT::f32)
4048 return ARM::getVFPf32Imm(Imm) != -1;
4049 if (VT == MVT::f64)
4050 return ARM::getVFPf64Imm(Imm) != -1;
4051 return false;
4052}