blob: 1549b637f952c9004f3b9916dc06933d20e9b30c [file] [log] [blame]
Evan Chenga8e29892007-01-19 07:51:42 +00001//===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chenga8e29892007-01-19 07:51:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that ARM uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "ARM.h"
16#include "ARMAddressingModes.h"
17#include "ARMConstantPoolValue.h"
18#include "ARMISelLowering.h"
19#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +000020#include "ARMPerfectShuffle.h"
Evan Chenga8e29892007-01-19 07:51:42 +000021#include "ARMRegisterInfo.h"
22#include "ARMSubtarget.h"
23#include "ARMTargetMachine.h"
Chris Lattner80ec2792009-08-02 00:34:36 +000024#include "ARMTargetObjectFile.h"
Evan Chenga8e29892007-01-19 07:51:42 +000025#include "llvm/CallingConv.h"
26#include "llvm/Constants.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000027#include "llvm/Function.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000028#include "llvm/GlobalValue.h"
Evan Cheng27707472007-03-16 08:43:56 +000029#include "llvm/Instruction.h"
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +000030#include "llvm/Intrinsics.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000031#include "llvm/Type.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000032#include "llvm/CodeGen/CallingConvLower.h"
Evan Chenga8e29892007-01-19 07:51:42 +000033#include "llvm/CodeGen/MachineBasicBlock.h"
34#include "llvm/CodeGen/MachineFrameInfo.h"
35#include "llvm/CodeGen/MachineFunction.h"
36#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000038#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chenga8e29892007-01-19 07:51:42 +000039#include "llvm/CodeGen/SelectionDAG.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000040#include "llvm/Target/TargetOptions.h"
Evan Chenga8e29892007-01-19 07:51:42 +000041#include "llvm/ADT/VectorExtras.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000042#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000043#include "llvm/Support/MathExtras.h"
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000044#include <sstream>
Evan Chenga8e29892007-01-19 07:51:42 +000045using namespace llvm;
46
Owen Andersone50ed302009-08-10 22:56:29 +000047static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000048 CCValAssign::LocInfo &LocInfo,
49 ISD::ArgFlagsTy &ArgFlags,
50 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000051static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000052 CCValAssign::LocInfo &LocInfo,
53 ISD::ArgFlagsTy &ArgFlags,
54 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000055static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000056 CCValAssign::LocInfo &LocInfo,
57 ISD::ArgFlagsTy &ArgFlags,
58 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000059static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000060 CCValAssign::LocInfo &LocInfo,
61 ISD::ArgFlagsTy &ArgFlags,
62 CCState &State);
63
Owen Andersone50ed302009-08-10 22:56:29 +000064void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
65 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000066 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000067 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000068 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
69 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000070
Owen Anderson70671842009-08-10 20:18:46 +000071 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +000072 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +000073 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000074 }
75
Owen Andersone50ed302009-08-10 22:56:29 +000076 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +000077 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Owen Anderson70671842009-08-10 20:18:46 +000078 setOperationAction(ISD::VSETCC, VT.getSimpleVT(), Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +000079 if (ElemTy == MVT::i8 || ElemTy == MVT::i16)
Owen Anderson70671842009-08-10 20:18:46 +000080 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson0696fdf2009-09-16 20:20:44 +000081 if (ElemTy != MVT::i32) {
82 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
83 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
84 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
85 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
86 }
Owen Anderson70671842009-08-10 20:18:46 +000087 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
88 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Owen Anderson70671842009-08-10 20:18:46 +000089 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Custom);
Anton Korobeynikov8e6c2b92009-08-21 12:40:35 +000090 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +000091 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +000092 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
93 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
94 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +000095 }
96
97 // Promote all bit-wise operations.
98 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +000099 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000100 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
101 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000102 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000103 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000104 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000105 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000106 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000107 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000108 }
Bob Wilson16330762009-09-16 00:17:28 +0000109
110 // Neon does not support vector divide/remainder operations.
111 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
112 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
113 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
114 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
115 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
116 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000117}
118
Owen Andersone50ed302009-08-10 22:56:29 +0000119void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000120 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000121 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000122}
123
Owen Andersone50ed302009-08-10 22:56:29 +0000124void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000125 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000126 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000127}
128
Chris Lattnerf0144122009-07-28 03:13:23 +0000129static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
130 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000131 return new TargetLoweringObjectFileMachO();
Chris Lattner80ec2792009-08-02 00:34:36 +0000132 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000133}
134
Evan Chenga8e29892007-01-19 07:51:42 +0000135ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000136 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000137 Subtarget = &TM.getSubtarget<ARMSubtarget>();
138
Evan Chengb1df8f22007-04-27 08:15:43 +0000139 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000140 // Uses VFP for Thumb libfuncs if available.
141 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
142 // Single-precision floating-point arithmetic.
143 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
144 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
145 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
146 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000147
Evan Chengb1df8f22007-04-27 08:15:43 +0000148 // Double-precision floating-point arithmetic.
149 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
150 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
151 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
152 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000153
Evan Chengb1df8f22007-04-27 08:15:43 +0000154 // Single-precision comparisons.
155 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
156 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
157 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
158 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
159 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
160 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
161 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
162 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000163
Evan Chengb1df8f22007-04-27 08:15:43 +0000164 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
165 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
166 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
167 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
168 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
169 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
170 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
171 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000172
Evan Chengb1df8f22007-04-27 08:15:43 +0000173 // Double-precision comparisons.
174 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
175 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
176 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
177 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
178 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
179 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
180 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
181 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000182
Evan Chengb1df8f22007-04-27 08:15:43 +0000183 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
184 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
185 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
186 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
187 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
188 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
189 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
190 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000191
Evan Chengb1df8f22007-04-27 08:15:43 +0000192 // Floating-point to integer conversions.
193 // i64 conversions are done via library routines even when generating VFP
194 // instructions, so use the same ones.
195 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
196 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
197 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
198 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000199
Evan Chengb1df8f22007-04-27 08:15:43 +0000200 // Conversions between floating types.
201 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
202 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
203
204 // Integer to floating-point conversions.
205 // i64 conversions are done via library routines even when generating VFP
206 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000207 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
208 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000209 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
210 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
211 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
212 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
213 }
Evan Chenga8e29892007-01-19 07:51:42 +0000214 }
215
Bob Wilson2f954612009-05-22 17:38:41 +0000216 // These libcalls are not available in 32-bit.
217 setLibcallName(RTLIB::SHL_I128, 0);
218 setLibcallName(RTLIB::SRL_I128, 0);
219 setLibcallName(RTLIB::SRA_I128, 0);
220
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000221 // Libcalls should use the AAPCS base standard ABI, even if hard float
222 // is in effect, as per the ARM RTABI specification, section 4.1.2.
223 if (Subtarget->isAAPCS_ABI()) {
224 for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i) {
225 setLibcallCallingConv(static_cast<RTLIB::Libcall>(i),
226 CallingConv::ARM_AAPCS);
227 }
228 }
229
David Goodwinf1daf7d2009-07-08 23:10:31 +0000230 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000231 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000232 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000233 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000234 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000235 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
236 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000237
Owen Anderson825b72b2009-08-11 20:47:22 +0000238 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000239 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000240
241 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000242 addDRTypeForNEON(MVT::v2f32);
243 addDRTypeForNEON(MVT::v8i8);
244 addDRTypeForNEON(MVT::v4i16);
245 addDRTypeForNEON(MVT::v2i32);
246 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000247
Owen Anderson825b72b2009-08-11 20:47:22 +0000248 addQRTypeForNEON(MVT::v4f32);
249 addQRTypeForNEON(MVT::v2f64);
250 addQRTypeForNEON(MVT::v16i8);
251 addQRTypeForNEON(MVT::v8i16);
252 addQRTypeForNEON(MVT::v4i32);
253 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000254
Bob Wilson74dc72e2009-09-15 23:55:57 +0000255 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
256 // neither Neon nor VFP support any arithmetic operations on it.
257 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
258 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
259 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
260 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
261 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
262 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
263 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
264 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
265 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
266 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
267 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
268 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
269 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
270 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
271 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
272 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
273 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
274 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
275 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
276 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
277 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
278 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
279 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
280 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
281
Bob Wilson642b3292009-09-16 00:32:15 +0000282 // Neon does not support some operations on v1i64 and v2i64 types.
283 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
284 setOperationAction(ISD::MUL, MVT::v2i64, Expand);
285 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
286 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
287
Bob Wilson5bafff32009-06-22 23:27:02 +0000288 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
289 setTargetDAGCombine(ISD::SHL);
290 setTargetDAGCombine(ISD::SRL);
291 setTargetDAGCombine(ISD::SRA);
292 setTargetDAGCombine(ISD::SIGN_EXTEND);
293 setTargetDAGCombine(ISD::ZERO_EXTEND);
294 setTargetDAGCombine(ISD::ANY_EXTEND);
295 }
296
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000297 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000298
299 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000300 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000301
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000302 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000303 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000304
Evan Chenga8e29892007-01-19 07:51:42 +0000305 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000306 if (!Subtarget->isThumb1Only()) {
307 for (unsigned im = (unsigned)ISD::PRE_INC;
308 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000309 setIndexedLoadAction(im, MVT::i1, Legal);
310 setIndexedLoadAction(im, MVT::i8, Legal);
311 setIndexedLoadAction(im, MVT::i16, Legal);
312 setIndexedLoadAction(im, MVT::i32, Legal);
313 setIndexedStoreAction(im, MVT::i1, Legal);
314 setIndexedStoreAction(im, MVT::i8, Legal);
315 setIndexedStoreAction(im, MVT::i16, Legal);
316 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000317 }
Evan Chenga8e29892007-01-19 07:51:42 +0000318 }
319
320 // i64 operation support.
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000321 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000322 setOperationAction(ISD::MUL, MVT::i64, Expand);
323 setOperationAction(ISD::MULHU, MVT::i32, Expand);
324 setOperationAction(ISD::MULHS, MVT::i32, Expand);
325 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
326 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000327 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000328 setOperationAction(ISD::MUL, MVT::i64, Expand);
329 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb6207242009-08-01 00:16:10 +0000330 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000331 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000332 }
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000333 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000334 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000335 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000336 setOperationAction(ISD::SRL, MVT::i64, Custom);
337 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000338
339 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000340 setOperationAction(ISD::ROTL, MVT::i32, Expand);
341 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
342 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000343 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000344 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000345
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000346 // Only ARMv6 has BSWAP.
347 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000348 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000349
Evan Chenga8e29892007-01-19 07:51:42 +0000350 // These are expanded into libcalls.
Owen Anderson825b72b2009-08-11 20:47:22 +0000351 setOperationAction(ISD::SDIV, MVT::i32, Expand);
352 setOperationAction(ISD::UDIV, MVT::i32, Expand);
353 setOperationAction(ISD::SREM, MVT::i32, Expand);
354 setOperationAction(ISD::UREM, MVT::i32, Expand);
355 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
356 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000357
Owen Anderson825b72b2009-08-11 20:47:22 +0000358 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
359 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
360 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
361 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000362 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000363
Evan Chenga8e29892007-01-19 07:51:42 +0000364 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000365 setOperationAction(ISD::VASTART, MVT::Other, Custom);
366 setOperationAction(ISD::VAARG, MVT::Other, Expand);
367 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
368 setOperationAction(ISD::VAEND, MVT::Other, Expand);
369 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
370 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000371 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
372 // FIXME: Shouldn't need this, since no register is used, but the legalizer
373 // doesn't yet know how to not do that for SjLj.
374 setExceptionSelectorRegister(ARM::R0);
Evan Cheng86198642009-08-07 00:34:42 +0000375 if (Subtarget->isThumb())
Owen Anderson825b72b2009-08-11 20:47:22 +0000376 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Evan Cheng86198642009-08-07 00:34:42 +0000377 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000378 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
379 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000380
Evan Chengd27c9fc2009-07-03 01:43:10 +0000381 if (!Subtarget->hasV6Ops() && !Subtarget->isThumb2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000382 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
383 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000384 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000385 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000386
David Goodwinf1daf7d2009-07-08 23:10:31 +0000387 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only())
Jim Grosbache5165492009-11-09 00:11:35 +0000388 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR iff target supports vfp2.
Owen Anderson825b72b2009-08-11 20:47:22 +0000389 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000390
391 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000392 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000393
Owen Anderson825b72b2009-08-11 20:47:22 +0000394 setOperationAction(ISD::SETCC, MVT::i32, Expand);
395 setOperationAction(ISD::SETCC, MVT::f32, Expand);
396 setOperationAction(ISD::SETCC, MVT::f64, Expand);
397 setOperationAction(ISD::SELECT, MVT::i32, Expand);
398 setOperationAction(ISD::SELECT, MVT::f32, Expand);
399 setOperationAction(ISD::SELECT, MVT::f64, Expand);
400 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
401 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
402 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000403
Owen Anderson825b72b2009-08-11 20:47:22 +0000404 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
405 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
406 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
407 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
408 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000409
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000410 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000411 setOperationAction(ISD::FSIN, MVT::f64, Expand);
412 setOperationAction(ISD::FSIN, MVT::f32, Expand);
413 setOperationAction(ISD::FCOS, MVT::f32, Expand);
414 setOperationAction(ISD::FCOS, MVT::f64, Expand);
415 setOperationAction(ISD::FREM, MVT::f64, Expand);
416 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000417 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000418 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
419 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000420 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000421 setOperationAction(ISD::FPOW, MVT::f64, Expand);
422 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000423
Evan Chenga8e29892007-01-19 07:51:42 +0000424 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
David Goodwinf1daf7d2009-07-08 23:10:31 +0000425 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000426 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
427 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
428 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
429 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000430 }
Evan Chenga8e29892007-01-19 07:51:42 +0000431
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000432 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000433 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000434 setTargetDAGCombine(ISD::ADD);
435 setTargetDAGCombine(ISD::SUB);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000436
Evan Chenga8e29892007-01-19 07:51:42 +0000437 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Chenga8e29892007-01-19 07:51:42 +0000438 setSchedulingPreference(SchedulingForRegPressure);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000439
Evan Chengbc9b7542009-08-15 07:59:10 +0000440 // FIXME: If-converter should use instruction latency to determine
441 // profitability rather than relying on fixed limits.
442 if (Subtarget->getCPUString() == "generic") {
443 // Generic (and overly aggressive) if-conversion limits.
444 setIfCvtBlockSizeLimit(10);
445 setIfCvtDupBlockSizeLimit(2);
446 } else if (Subtarget->hasV6Ops()) {
447 setIfCvtBlockSizeLimit(2);
448 setIfCvtDupBlockSizeLimit(1);
449 } else {
450 setIfCvtBlockSizeLimit(3);
451 setIfCvtDupBlockSizeLimit(2);
Evan Cheng8557c2b2009-06-19 01:51:50 +0000452 }
453
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000454 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Bob Wilsone6abdff2009-05-18 20:55:32 +0000455 // Do not enable CodePlacementOpt for now: it currently runs after the
456 // ARMConstantIslandPass and messes up branch relaxation and placement
457 // of constant islands.
458 // benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000459}
460
Evan Chenga8e29892007-01-19 07:51:42 +0000461const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
462 switch (Opcode) {
463 default: return 0;
464 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000465 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
466 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000467 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000468 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
469 case ARMISD::tCALL: return "ARMISD::tCALL";
470 case ARMISD::BRCOND: return "ARMISD::BRCOND";
471 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000472 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000473 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
474 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
475 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000476 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000477 case ARMISD::CMPFP: return "ARMISD::CMPFP";
478 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
479 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
480 case ARMISD::CMOV: return "ARMISD::CMOV";
481 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000482
Evan Chenga8e29892007-01-19 07:51:42 +0000483 case ARMISD::FTOSI: return "ARMISD::FTOSI";
484 case ARMISD::FTOUI: return "ARMISD::FTOUI";
485 case ARMISD::SITOF: return "ARMISD::SITOF";
486 case ARMISD::UITOF: return "ARMISD::UITOF";
Evan Chenga8e29892007-01-19 07:51:42 +0000487
488 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
489 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
490 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000491
Jim Grosbache5165492009-11-09 00:11:35 +0000492 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
493 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000494
Evan Chengc5942082009-10-28 06:55:03 +0000495 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
496 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
497
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000498 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000499
Evan Cheng86198642009-08-07 00:34:42 +0000500 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
501
Bob Wilson5bafff32009-06-22 23:27:02 +0000502 case ARMISD::VCEQ: return "ARMISD::VCEQ";
503 case ARMISD::VCGE: return "ARMISD::VCGE";
504 case ARMISD::VCGEU: return "ARMISD::VCGEU";
505 case ARMISD::VCGT: return "ARMISD::VCGT";
506 case ARMISD::VCGTU: return "ARMISD::VCGTU";
507 case ARMISD::VTST: return "ARMISD::VTST";
508
509 case ARMISD::VSHL: return "ARMISD::VSHL";
510 case ARMISD::VSHRs: return "ARMISD::VSHRs";
511 case ARMISD::VSHRu: return "ARMISD::VSHRu";
512 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
513 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
514 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
515 case ARMISD::VSHRN: return "ARMISD::VSHRN";
516 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
517 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
518 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
519 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
520 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
521 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
522 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
523 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
524 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
525 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
526 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
527 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
528 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
529 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000530 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000531 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000532 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000533 case ARMISD::VREV64: return "ARMISD::VREV64";
534 case ARMISD::VREV32: return "ARMISD::VREV32";
535 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000536 case ARMISD::VZIP: return "ARMISD::VZIP";
537 case ARMISD::VUZP: return "ARMISD::VUZP";
538 case ARMISD::VTRN: return "ARMISD::VTRN";
Evan Chenga8e29892007-01-19 07:51:42 +0000539 }
540}
541
Bill Wendlingb4202b82009-07-01 18:50:55 +0000542/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000543unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Evan Cheng048e36f2009-10-02 06:57:25 +0000544 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 0 : 1;
Bill Wendling20c568f2009-06-30 22:38:32 +0000545}
546
Evan Chenga8e29892007-01-19 07:51:42 +0000547//===----------------------------------------------------------------------===//
548// Lowering Code
549//===----------------------------------------------------------------------===//
550
Evan Chenga8e29892007-01-19 07:51:42 +0000551/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
552static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
553 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000554 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000555 case ISD::SETNE: return ARMCC::NE;
556 case ISD::SETEQ: return ARMCC::EQ;
557 case ISD::SETGT: return ARMCC::GT;
558 case ISD::SETGE: return ARMCC::GE;
559 case ISD::SETLT: return ARMCC::LT;
560 case ISD::SETLE: return ARMCC::LE;
561 case ISD::SETUGT: return ARMCC::HI;
562 case ISD::SETUGE: return ARMCC::HS;
563 case ISD::SETULT: return ARMCC::LO;
564 case ISD::SETULE: return ARMCC::LS;
565 }
566}
567
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000568/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
569static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000570 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000571 CondCode2 = ARMCC::AL;
572 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000573 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000574 case ISD::SETEQ:
575 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
576 case ISD::SETGT:
577 case ISD::SETOGT: CondCode = ARMCC::GT; break;
578 case ISD::SETGE:
579 case ISD::SETOGE: CondCode = ARMCC::GE; break;
580 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000581 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000582 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
583 case ISD::SETO: CondCode = ARMCC::VC; break;
584 case ISD::SETUO: CondCode = ARMCC::VS; break;
585 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
586 case ISD::SETUGT: CondCode = ARMCC::HI; break;
587 case ISD::SETUGE: CondCode = ARMCC::PL; break;
588 case ISD::SETLT:
589 case ISD::SETULT: CondCode = ARMCC::LT; break;
590 case ISD::SETLE:
591 case ISD::SETULE: CondCode = ARMCC::LE; break;
592 case ISD::SETNE:
593 case ISD::SETUNE: CondCode = ARMCC::NE; break;
594 }
Evan Chenga8e29892007-01-19 07:51:42 +0000595}
596
Bob Wilson1f595bb2009-04-17 19:07:39 +0000597//===----------------------------------------------------------------------===//
598// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +0000599//===----------------------------------------------------------------------===//
600
601#include "ARMGenCallingConv.inc"
602
603// APCS f64 is in register pairs, possibly split to stack
Owen Andersone50ed302009-08-10 22:56:29 +0000604static bool f64AssignAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000605 CCValAssign::LocInfo &LocInfo,
606 CCState &State, bool CanFail) {
607 static const unsigned RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
608
609 // Try to get the first register.
610 if (unsigned Reg = State.AllocateReg(RegList, 4))
611 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
612 else {
613 // For the 2nd half of a v2f64, do not fail.
614 if (CanFail)
615 return false;
616
617 // Put the whole thing on the stack.
618 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
619 State.AllocateStack(8, 4),
620 LocVT, LocInfo));
621 return true;
622 }
623
624 // Try to get the second register.
625 if (unsigned Reg = State.AllocateReg(RegList, 4))
626 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
627 else
628 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
629 State.AllocateStack(4, 4),
630 LocVT, LocInfo));
631 return true;
632}
633
Owen Andersone50ed302009-08-10 22:56:29 +0000634static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000635 CCValAssign::LocInfo &LocInfo,
636 ISD::ArgFlagsTy &ArgFlags,
637 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000638 if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
639 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000640 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000641 !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
642 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000643 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000644}
645
646// AAPCS f64 is in aligned register pairs
Owen Andersone50ed302009-08-10 22:56:29 +0000647static bool f64AssignAAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000648 CCValAssign::LocInfo &LocInfo,
649 CCState &State, bool CanFail) {
650 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
651 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
652
653 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
654 if (Reg == 0) {
655 // For the 2nd half of a v2f64, do not just fail.
656 if (CanFail)
657 return false;
658
659 // Put the whole thing on the stack.
660 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
661 State.AllocateStack(8, 8),
662 LocVT, LocInfo));
663 return true;
664 }
665
666 unsigned i;
667 for (i = 0; i < 2; ++i)
668 if (HiRegList[i] == Reg)
669 break;
670
671 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
672 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
673 LocVT, LocInfo));
674 return true;
675}
676
Owen Andersone50ed302009-08-10 22:56:29 +0000677static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000678 CCValAssign::LocInfo &LocInfo,
679 ISD::ArgFlagsTy &ArgFlags,
680 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000681 if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
682 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000683 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000684 !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
685 return false;
686 return true; // we handled it
687}
688
Owen Andersone50ed302009-08-10 22:56:29 +0000689static bool f64RetAssign(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000690 CCValAssign::LocInfo &LocInfo, CCState &State) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000691 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
692 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
693
Bob Wilsone65586b2009-04-17 20:40:45 +0000694 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
695 if (Reg == 0)
696 return false; // we didn't handle it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000697
Bob Wilsone65586b2009-04-17 20:40:45 +0000698 unsigned i;
699 for (i = 0; i < 2; ++i)
700 if (HiRegList[i] == Reg)
701 break;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000702
Bob Wilson5bafff32009-06-22 23:27:02 +0000703 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bob Wilsone65586b2009-04-17 20:40:45 +0000704 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000705 LocVT, LocInfo));
706 return true;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000707}
708
Owen Andersone50ed302009-08-10 22:56:29 +0000709static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000710 CCValAssign::LocInfo &LocInfo,
711 ISD::ArgFlagsTy &ArgFlags,
712 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000713 if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
714 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000715 if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
Bob Wilson5bafff32009-06-22 23:27:02 +0000716 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000717 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000718}
719
Owen Andersone50ed302009-08-10 22:56:29 +0000720static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000721 CCValAssign::LocInfo &LocInfo,
722 ISD::ArgFlagsTy &ArgFlags,
723 CCState &State) {
724 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
725 State);
726}
727
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000728/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
729/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000730CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000731 bool Return,
732 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000733 switch (CC) {
734 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000735 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000736 case CallingConv::C:
737 case CallingConv::Fast:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000738 // Use target triple & subtarget features to do actual dispatch.
739 if (Subtarget->isAAPCS_ABI()) {
740 if (Subtarget->hasVFP2() &&
741 FloatABIType == FloatABI::Hard && !isVarArg)
742 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
743 else
744 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
745 } else
746 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000747 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000748 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000749 case CallingConv::ARM_AAPCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000750 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000751 case CallingConv::ARM_APCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000752 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000753 }
754}
755
Dan Gohman98ca4f22009-08-05 01:29:28 +0000756/// LowerCallResult - Lower the result values of a call into the
757/// appropriate copies out of appropriate physical registers.
758SDValue
759ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000760 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000761 const SmallVectorImpl<ISD::InputArg> &Ins,
762 DebugLoc dl, SelectionDAG &DAG,
763 SmallVectorImpl<SDValue> &InVals) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000764
Bob Wilson1f595bb2009-04-17 19:07:39 +0000765 // Assign locations to each value returned by this call.
766 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000767 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000768 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000769 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000770 CCAssignFnForNode(CallConv, /* Return*/ true,
771 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000772
773 // Copy all of the result registers out of their specified physreg.
774 for (unsigned i = 0; i != RVLocs.size(); ++i) {
775 CCValAssign VA = RVLocs[i];
776
Bob Wilson80915242009-04-25 00:33:20 +0000777 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000778 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000779 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000780 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000781 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000782 Chain = Lo.getValue(1);
783 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000784 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000785 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000786 InFlag);
787 Chain = Hi.getValue(1);
788 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +0000789 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +0000790
Owen Anderson825b72b2009-08-11 20:47:22 +0000791 if (VA.getLocVT() == MVT::v2f64) {
792 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
793 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
794 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000795
796 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000797 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000798 Chain = Lo.getValue(1);
799 InFlag = Lo.getValue(2);
800 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000801 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000802 Chain = Hi.getValue(1);
803 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +0000804 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +0000805 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
806 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000807 }
Bob Wilson1f595bb2009-04-17 19:07:39 +0000808 } else {
Bob Wilson80915242009-04-25 00:33:20 +0000809 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
810 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000811 Chain = Val.getValue(1);
812 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000813 }
Bob Wilson80915242009-04-25 00:33:20 +0000814
815 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000816 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +0000817 case CCValAssign::Full: break;
818 case CCValAssign::BCvt:
819 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
820 break;
821 }
822
Dan Gohman98ca4f22009-08-05 01:29:28 +0000823 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000824 }
825
Dan Gohman98ca4f22009-08-05 01:29:28 +0000826 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000827}
828
829/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
830/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +0000831/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +0000832/// a byval function parameter.
833/// Sometimes what we are copying is the end of a larger object, the part that
834/// does not fit in registers.
835static SDValue
836CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
837 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
838 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000839 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000840 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
841 /*AlwaysInline=*/false, NULL, 0, NULL, 0);
842}
843
Bob Wilsondee46d72009-04-17 20:35:10 +0000844/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000845SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +0000846ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
847 SDValue StackPtr, SDValue Arg,
848 DebugLoc dl, SelectionDAG &DAG,
849 const CCValAssign &VA,
850 ISD::ArgFlagsTy Flags) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000851 unsigned LocMemOffset = VA.getLocMemOffset();
852 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
853 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
854 if (Flags.isByVal()) {
855 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
856 }
857 return DAG.getStore(Chain, dl, Arg, PtrOff,
858 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chenga8e29892007-01-19 07:51:42 +0000859}
860
Dan Gohman98ca4f22009-08-05 01:29:28 +0000861void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +0000862 SDValue Chain, SDValue &Arg,
863 RegsToPassVector &RegsToPass,
864 CCValAssign &VA, CCValAssign &NextVA,
865 SDValue &StackPtr,
866 SmallVector<SDValue, 8> &MemOpChains,
867 ISD::ArgFlagsTy Flags) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000868
Jim Grosbache5165492009-11-09 00:11:35 +0000869 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +0000870 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +0000871 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
872
873 if (NextVA.isRegLoc())
874 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
875 else {
876 assert(NextVA.isMemLoc());
877 if (StackPtr.getNode() == 0)
878 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
879
Dan Gohman98ca4f22009-08-05 01:29:28 +0000880 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
881 dl, DAG, NextVA,
882 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000883 }
884}
885
Dan Gohman98ca4f22009-08-05 01:29:28 +0000886/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +0000887/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
888/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000889SDValue
890ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000891 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000892 bool isTailCall,
893 const SmallVectorImpl<ISD::OutputArg> &Outs,
894 const SmallVectorImpl<ISD::InputArg> &Ins,
895 DebugLoc dl, SelectionDAG &DAG,
896 SmallVectorImpl<SDValue> &InVals) {
Evan Chenga8e29892007-01-19 07:51:42 +0000897
Bob Wilson1f595bb2009-04-17 19:07:39 +0000898 // Analyze operands of the call, assigning locations to each operand.
899 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000900 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
901 *DAG.getContext());
902 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000903 CCAssignFnForNode(CallConv, /* Return*/ false,
904 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +0000905
Bob Wilson1f595bb2009-04-17 19:07:39 +0000906 // Get a count of how many bytes are to be pushed on the stack.
907 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +0000908
909 // Adjust the stack pointer for the new arguments...
910 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattnere563bbc2008-10-11 22:08:30 +0000911 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +0000912
Owen Anderson825b72b2009-08-11 20:47:22 +0000913 SDValue StackPtr = DAG.getRegister(ARM::SP, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000914
Bob Wilson5bafff32009-06-22 23:27:02 +0000915 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000916 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +0000917
Bob Wilson1f595bb2009-04-17 19:07:39 +0000918 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +0000919 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000920 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
921 i != e;
922 ++i, ++realArgIdx) {
923 CCValAssign &VA = ArgLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +0000924 SDValue Arg = Outs[realArgIdx].Val;
925 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +0000926
Bob Wilson1f595bb2009-04-17 19:07:39 +0000927 // Promote the value if needed.
928 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000929 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +0000930 case CCValAssign::Full: break;
931 case CCValAssign::SExt:
932 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
933 break;
934 case CCValAssign::ZExt:
935 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
936 break;
937 case CCValAssign::AExt:
938 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
939 break;
940 case CCValAssign::BCvt:
941 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
942 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000943 }
944
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000945 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +0000946 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000947 if (VA.getLocVT() == MVT::v2f64) {
948 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
949 DAG.getConstant(0, MVT::i32));
950 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
951 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000952
Dan Gohman98ca4f22009-08-05 01:29:28 +0000953 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +0000954 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
955
956 VA = ArgLocs[++i]; // skip ahead to next loc
957 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000958 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +0000959 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
960 } else {
961 assert(VA.isMemLoc());
962 if (StackPtr.getNode() == 0)
963 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
964
Dan Gohman98ca4f22009-08-05 01:29:28 +0000965 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
966 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000967 }
968 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000969 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000970 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000971 }
972 } else if (VA.isRegLoc()) {
973 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
974 } else {
975 assert(VA.isMemLoc());
976 if (StackPtr.getNode() == 0)
977 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
978
Dan Gohman98ca4f22009-08-05 01:29:28 +0000979 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
980 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000981 }
Evan Chenga8e29892007-01-19 07:51:42 +0000982 }
983
984 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000985 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +0000986 &MemOpChains[0], MemOpChains.size());
987
988 // Build a sequence of copy-to-reg nodes chained together with token chain
989 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +0000990 SDValue InFlag;
Evan Chenga8e29892007-01-19 07:51:42 +0000991 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Bob Wilson2dc4f542009-03-20 22:42:55 +0000992 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000993 RegsToPass[i].second, InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +0000994 InFlag = Chain.getValue(1);
995 }
996
Bill Wendling056292f2008-09-16 21:48:12 +0000997 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
998 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
999 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001000 bool isDirect = false;
1001 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001002 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001003 MachineFunction &MF = DAG.getMachineFunction();
1004 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chenga8e29892007-01-19 07:51:42 +00001005 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1006 GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001007 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001008 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001009 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001010 getTargetMachine().getRelocationModel() != Reloc::Static;
1011 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001012 // ARM call to a local ARM function is predicable.
1013 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
Evan Chengc60e76d2007-01-30 20:37:08 +00001014 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001015 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001016 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
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,
Evan Cheng9eda6892009-10-31 03:39:36 +00001023 DAG.getEntryNode(), CPAddr,
1024 PseudoSourceValue::getConstantPool(), 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001025 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001026 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001027 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001028 } else
1029 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +00001030 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001031 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001032 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001033 getTargetMachine().getRelocationModel() != Reloc::Static;
1034 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001035 // tBX takes a register source operand.
1036 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001037 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001038 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001039 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001040 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001041 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001042 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001043 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001044 DAG.getEntryNode(), CPAddr,
1045 PseudoSourceValue::getConstantPool(), 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001046 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001047 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001048 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001049 } else
Bill Wendling056292f2008-09-16 21:48:12 +00001050 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001051 }
1052
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001053 // FIXME: handle tail calls differently.
1054 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001055 if (Subtarget->isThumb()) {
1056 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001057 CallOpc = ARMISD::CALL_NOLINK;
1058 else
1059 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1060 } else {
1061 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001062 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1063 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001064 }
David Goodwinf1daf7d2009-07-08 23:10:31 +00001065 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb1Only()) {
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +00001066 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Owen Anderson825b72b2009-08-11 20:47:22 +00001067 Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001068 InFlag = Chain.getValue(1);
1069 }
1070
Dan Gohman475871a2008-07-27 21:46:04 +00001071 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001072 Ops.push_back(Chain);
1073 Ops.push_back(Callee);
1074
1075 // Add argument registers to the end of the list so that they are known live
1076 // into the call.
1077 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1078 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1079 RegsToPass[i].second.getValueType()));
1080
Gabor Greifba36cb52008-08-28 21:40:38 +00001081 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001082 Ops.push_back(InFlag);
Duncan Sands4bdcb612008-07-02 17:40:58 +00001083 // Returns a chain and a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00001084 Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Flag),
Duncan Sands4bdcb612008-07-02 17:40:58 +00001085 &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001086 InFlag = Chain.getValue(1);
1087
Chris Lattnere563bbc2008-10-11 22:08:30 +00001088 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1089 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001090 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001091 InFlag = Chain.getValue(1);
1092
Bob Wilson1f595bb2009-04-17 19:07:39 +00001093 // Handle result values, copying them out of physregs into vregs that we
1094 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001095 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1096 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001097}
1098
Dan Gohman98ca4f22009-08-05 01:29:28 +00001099SDValue
1100ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001101 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001102 const SmallVectorImpl<ISD::OutputArg> &Outs,
1103 DebugLoc dl, SelectionDAG &DAG) {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001104
Bob Wilsondee46d72009-04-17 20:35:10 +00001105 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001106 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001107
Bob Wilsondee46d72009-04-17 20:35:10 +00001108 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001109 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1110 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001111
Dan Gohman98ca4f22009-08-05 01:29:28 +00001112 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001113 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1114 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001115
1116 // If this is the first return lowered for this function, add
1117 // the regs to the liveout set for the function.
1118 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1119 for (unsigned i = 0; i != RVLocs.size(); ++i)
1120 if (RVLocs[i].isRegLoc())
1121 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001122 }
1123
Bob Wilson1f595bb2009-04-17 19:07:39 +00001124 SDValue Flag;
1125
1126 // Copy the result values into the output registers.
1127 for (unsigned i = 0, realRVLocIdx = 0;
1128 i != RVLocs.size();
1129 ++i, ++realRVLocIdx) {
1130 CCValAssign &VA = RVLocs[i];
1131 assert(VA.isRegLoc() && "Can only return in registers!");
1132
Dan Gohman98ca4f22009-08-05 01:29:28 +00001133 SDValue Arg = Outs[realRVLocIdx].Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001134
1135 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001136 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001137 case CCValAssign::Full: break;
1138 case CCValAssign::BCvt:
1139 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1140 break;
1141 }
1142
Bob Wilson1f595bb2009-04-17 19:07:39 +00001143 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001144 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001145 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001146 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1147 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001148 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001149 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001150
1151 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1152 Flag = Chain.getValue(1);
1153 VA = RVLocs[++i]; // skip ahead to next loc
1154 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1155 HalfGPRs.getValue(1), Flag);
1156 Flag = Chain.getValue(1);
1157 VA = RVLocs[++i]; // skip ahead to next loc
1158
1159 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001160 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1161 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001162 }
1163 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1164 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001165 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001166 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001167 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001168 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001169 VA = RVLocs[++i]; // skip ahead to next loc
1170 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1171 Flag);
1172 } else
1173 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1174
Bob Wilsondee46d72009-04-17 20:35:10 +00001175 // Guarantee that all emitted copies are
1176 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001177 Flag = Chain.getValue(1);
1178 }
1179
1180 SDValue result;
1181 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001182 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001183 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001184 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001185
1186 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001187}
1188
Bob Wilsonb62d2572009-11-03 00:02:05 +00001189// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1190// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1191// one of the above mentioned nodes. It has to be wrapped because otherwise
1192// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1193// be used to form addressing mode. These wrapped nodes will be selected
1194// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001195static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001196 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001197 // FIXME there is no actual debug info here
1198 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001199 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001200 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001201 if (CP->isMachineConstantPoolEntry())
1202 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1203 CP->getAlignment());
1204 else
1205 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1206 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001207 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001208}
1209
Bob Wilsonddb16df2009-10-30 05:45:42 +00001210SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001211 MachineFunction &MF = DAG.getMachineFunction();
1212 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1213 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001214 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001215 EVT PtrVT = getPointerTy();
Bob Wilsonddb16df2009-10-30 05:45:42 +00001216 BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001217 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1218 SDValue CPAddr;
1219 if (RelocM == Reloc::Static) {
1220 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1221 } else {
1222 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001223 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001224 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1225 ARMCP::CPBlockAddress,
1226 PCAdj);
1227 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1228 }
1229 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1230 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
1231 PseudoSourceValue::getConstantPool(), 0);
1232 if (RelocM == Reloc::Static)
1233 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001234 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001235 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001236}
1237
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001238// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001239SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001240ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
1241 SelectionDAG &DAG) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001242 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001243 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001244 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001245 MachineFunction &MF = DAG.getMachineFunction();
1246 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1247 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001248 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001249 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001250 ARMCP::CPValue, PCAdj, "tlsgd", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001251 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001252 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001253 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
1254 PseudoSourceValue::getConstantPool(), 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001255 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001256
Evan Chenge7e0d622009-11-06 22:24:13 +00001257 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001258 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001259
1260 // call __tls_get_addr.
1261 ArgListTy Args;
1262 ArgListEntry Entry;
1263 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001264 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001265 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001266 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001267 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001268 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1269 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001270 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001271 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001272 return CallResult.first;
1273}
1274
1275// Lower ISD::GlobalTLSAddress using the "initial exec" or
1276// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001277SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001278ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001279 SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001280 GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001281 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001282 SDValue Offset;
1283 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001284 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001285 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001286 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001287
Chris Lattner4fb63d02009-07-15 04:12:33 +00001288 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001289 MachineFunction &MF = DAG.getMachineFunction();
1290 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1291 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1292 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001293 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1294 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001295 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001296 ARMCP::CPValue, PCAdj, "gottpoff", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001297 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001298 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001299 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
1300 PseudoSourceValue::getConstantPool(), 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001301 Chain = Offset.getValue(1);
1302
Evan Chenge7e0d622009-11-06 22:24:13 +00001303 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001304 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001305
Evan Cheng9eda6892009-10-31 03:39:36 +00001306 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
1307 PseudoSourceValue::getConstantPool(), 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001308 } else {
1309 // local exec model
Evan Chenge4e4ed32009-08-28 23:18:09 +00001310 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, "tpoff");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001311 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001312 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001313 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
1314 PseudoSourceValue::getConstantPool(), 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001315 }
1316
1317 // The address of the thread local variable is the add of the thread
1318 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001319 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001320}
1321
Dan Gohman475871a2008-07-27 21:46:04 +00001322SDValue
1323ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001324 // TODO: implement the "local dynamic" model
1325 assert(Subtarget->isTargetELF() &&
1326 "TLS not implemented for non-ELF targets");
1327 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1328 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1329 // otherwise use the "Local Exec" TLS Model
1330 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1331 return LowerToTLSGeneralDynamicModel(GA, DAG);
1332 else
1333 return LowerToTLSExecModels(GA, DAG);
1334}
1335
Dan Gohman475871a2008-07-27 21:46:04 +00001336SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001337 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001338 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001339 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001340 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1341 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1342 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001343 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001344 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001345 new ARMConstantPoolValue(GV, UseGOTOFF ? "GOTOFF" : "GOT");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001346 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001347 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001348 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001349 CPAddr,
1350 PseudoSourceValue::getConstantPool(), 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001351 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001352 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001353 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001354 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001355 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
1356 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001357 return Result;
1358 } else {
Evan Cheng1606e8e2009-03-13 07:51:59 +00001359 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001360 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001361 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1362 PseudoSourceValue::getConstantPool(), 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001363 }
1364}
1365
Dan Gohman475871a2008-07-27 21:46:04 +00001366SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001367 SelectionDAG &DAG) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001368 MachineFunction &MF = DAG.getMachineFunction();
1369 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1370 unsigned ARMPCLabelIndex = 0;
Owen Andersone50ed302009-08-10 22:56:29 +00001371 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001372 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001373 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1374 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Dan Gohman475871a2008-07-27 21:46:04 +00001375 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +00001376 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001377 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001378 else {
Evan Chenge7e0d622009-11-06 22:24:13 +00001379 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001380 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
1381 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001382 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001383 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001384 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001385 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001386
Evan Cheng9eda6892009-10-31 03:39:36 +00001387 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1388 PseudoSourceValue::getConstantPool(), 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001389 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001390
1391 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001392 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001393 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001394 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00001395
Evan Cheng63476a82009-09-03 07:04:02 +00001396 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Evan Cheng9eda6892009-10-31 03:39:36 +00001397 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
1398 PseudoSourceValue::getGOT(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001399
1400 return Result;
1401}
1402
Dan Gohman475871a2008-07-27 21:46:04 +00001403SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001404 SelectionDAG &DAG){
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001405 assert(Subtarget->isTargetELF() &&
1406 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00001407 MachineFunction &MF = DAG.getMachineFunction();
1408 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1409 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Andersone50ed302009-08-10 22:56:29 +00001410 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001411 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001412 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00001413 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1414 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00001415 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001416 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001417 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001418 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1419 PseudoSourceValue::getConstantPool(), 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001420 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001421 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001422}
1423
Jim Grosbach0e0da732009-05-12 23:59:14 +00001424SDValue
1425ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001426 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001427 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001428 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001429 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00001430 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00001431 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00001432 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1433 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001434 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001435 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00001436 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1437 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001438 EVT PtrVT = getPointerTy();
1439 DebugLoc dl = Op.getDebugLoc();
1440 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1441 SDValue CPAddr;
1442 unsigned PCAdj = (RelocM != Reloc::PIC_)
1443 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001444 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001445 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
1446 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001447 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001448 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001449 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00001450 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1451 PseudoSourceValue::getConstantPool(), 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001452 SDValue Chain = Result.getValue(1);
1453
1454 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001455 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001456 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1457 }
1458 return Result;
1459 }
Jim Grosbachf9570122009-05-14 00:46:35 +00001460 case Intrinsic::eh_sjlj_setjmp:
Owen Anderson825b72b2009-08-11 20:47:22 +00001461 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(1));
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001462 }
1463}
1464
Dan Gohman475871a2008-07-27 21:46:04 +00001465static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001466 unsigned VarArgsFrameIndex) {
Evan Chenga8e29892007-01-19 07:51:42 +00001467 // vastart just stores the address of the VarArgsFrameIndex slot into the
1468 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001469 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001470 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001471 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001472 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001473 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001474}
1475
Dan Gohman475871a2008-07-27 21:46:04 +00001476SDValue
Evan Cheng86198642009-08-07 00:34:42 +00001477ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) {
1478 SDNode *Node = Op.getNode();
1479 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001480 EVT VT = Node->getValueType(0);
Evan Cheng86198642009-08-07 00:34:42 +00001481 SDValue Chain = Op.getOperand(0);
1482 SDValue Size = Op.getOperand(1);
1483 SDValue Align = Op.getOperand(2);
1484
1485 // Chain the dynamic stack allocation so that it doesn't modify the stack
1486 // pointer when other instructions are using the stack.
1487 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1488
1489 unsigned AlignVal = cast<ConstantSDNode>(Align)->getZExtValue();
1490 unsigned StackAlign = getTargetMachine().getFrameInfo()->getStackAlignment();
1491 if (AlignVal > StackAlign)
1492 // Do this now since selection pass cannot introduce new target
1493 // independent node.
1494 Align = DAG.getConstant(-(uint64_t)AlignVal, VT);
1495
1496 // In Thumb1 mode, there isn't a "sub r, sp, r" instruction, we will end up
1497 // using a "add r, sp, r" instead. Negate the size now so we don't have to
1498 // do even more horrible hack later.
1499 MachineFunction &MF = DAG.getMachineFunction();
1500 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1501 if (AFI->isThumb1OnlyFunction()) {
1502 bool Negate = true;
1503 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Size);
1504 if (C) {
1505 uint32_t Val = C->getZExtValue();
1506 if (Val <= 508 && ((Val & 3) == 0))
1507 Negate = false;
1508 }
1509 if (Negate)
1510 Size = DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(0, VT), Size);
1511 }
1512
Owen Anderson825b72b2009-08-11 20:47:22 +00001513 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
Evan Cheng86198642009-08-07 00:34:42 +00001514 SDValue Ops1[] = { Chain, Size, Align };
1515 SDValue Res = DAG.getNode(ARMISD::DYN_ALLOC, dl, VTList, Ops1, 3);
1516 Chain = Res.getValue(1);
1517 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1518 DAG.getIntPtrConstant(0, true), SDValue());
1519 SDValue Ops2[] = { Res, Chain };
1520 return DAG.getMergeValues(Ops2, 2, dl);
1521}
1522
1523SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00001524ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
1525 SDValue &Root, SelectionDAG &DAG,
1526 DebugLoc dl) {
1527 MachineFunction &MF = DAG.getMachineFunction();
1528 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1529
1530 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001531 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00001532 RC = ARM::tGPRRegisterClass;
1533 else
1534 RC = ARM::GPRRegisterClass;
1535
1536 // Transform the arguments stored in physical registers into virtual ones.
1537 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001538 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001539
1540 SDValue ArgValue2;
1541 if (NextVA.isMemLoc()) {
1542 unsigned ArgSize = NextVA.getLocVT().getSizeInBits()/8;
1543 MachineFrameInfo *MFI = MF.getFrameInfo();
David Greene3f2bf852009-11-12 20:49:22 +00001544 int FI = MFI->CreateFixedObject(ArgSize, NextVA.getLocMemOffset(),
1545 true, false);
Bob Wilson5bafff32009-06-22 23:27:02 +00001546
1547 // Create load node to retrieve arguments from the stack.
1548 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00001549 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
1550 PseudoSourceValue::getFixedStack(FI), 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00001551 } else {
1552 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001553 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001554 }
1555
Jim Grosbache5165492009-11-09 00:11:35 +00001556 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00001557}
1558
1559SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001560ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001561 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001562 const SmallVectorImpl<ISD::InputArg>
1563 &Ins,
1564 DebugLoc dl, SelectionDAG &DAG,
1565 SmallVectorImpl<SDValue> &InVals) {
1566
Bob Wilson1f595bb2009-04-17 19:07:39 +00001567 MachineFunction &MF = DAG.getMachineFunction();
1568 MachineFrameInfo *MFI = MF.getFrameInfo();
1569
Bob Wilson1f595bb2009-04-17 19:07:39 +00001570 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1571
1572 // Assign locations to all of the incoming arguments.
1573 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001574 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1575 *DAG.getContext());
1576 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001577 CCAssignFnForNode(CallConv, /* Return*/ false,
1578 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001579
1580 SmallVector<SDValue, 16> ArgValues;
1581
1582 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1583 CCValAssign &VA = ArgLocs[i];
1584
Bob Wilsondee46d72009-04-17 20:35:10 +00001585 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001586 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001587 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00001588
Bob Wilson5bafff32009-06-22 23:27:02 +00001589 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001590 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001591 // f64 and vector types are split up into multiple registers or
1592 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00001593 RegVT = MVT::i32;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001594
Owen Anderson825b72b2009-08-11 20:47:22 +00001595 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001596 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001597 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00001598 VA = ArgLocs[++i]; // skip ahead to next loc
1599 SDValue ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001600 Chain, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001601 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1602 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001603 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00001604 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001605 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
1606 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001607 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001608
Bob Wilson5bafff32009-06-22 23:27:02 +00001609 } else {
1610 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001611
Owen Anderson825b72b2009-08-11 20:47:22 +00001612 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00001613 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001614 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00001615 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001616 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001617 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001618 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001619 RC = (AFI->isThumb1OnlyFunction() ?
1620 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00001621 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001622 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00001623
1624 // Transform the arguments in physical registers into virtual ones.
1625 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001626 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001627 }
1628
1629 // If this is an 8 or 16-bit value, it is really passed promoted
1630 // to 32 bits. Insert an assert[sz]ext to capture this, then
1631 // truncate to the right size.
1632 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001633 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001634 case CCValAssign::Full: break;
1635 case CCValAssign::BCvt:
1636 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1637 break;
1638 case CCValAssign::SExt:
1639 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
1640 DAG.getValueType(VA.getValVT()));
1641 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1642 break;
1643 case CCValAssign::ZExt:
1644 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
1645 DAG.getValueType(VA.getValVT()));
1646 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1647 break;
1648 }
1649
Dan Gohman98ca4f22009-08-05 01:29:28 +00001650 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001651
1652 } else { // VA.isRegLoc()
1653
1654 // sanity check
1655 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00001656 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001657
1658 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001659 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
1660 true, false);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001661
Bob Wilsondee46d72009-04-17 20:35:10 +00001662 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001663 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00001664 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
1665 PseudoSourceValue::getFixedStack(FI), 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001666 }
1667 }
1668
1669 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00001670 if (isVarArg) {
1671 static const unsigned GPRArgRegs[] = {
1672 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1673 };
1674
Bob Wilsondee46d72009-04-17 20:35:10 +00001675 unsigned NumGPRs = CCInfo.getFirstUnallocated
1676 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001677
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001678 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1679 unsigned VARegSize = (4 - NumGPRs) * 4;
1680 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Rafael Espindolac1382b72009-10-30 14:33:14 +00001681 unsigned ArgOffset = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001682 if (VARegSaveSize) {
1683 // If this function is vararg, store any remaining integer argument regs
1684 // to their spots on the stack so that they may be loaded by deferencing
1685 // the result of va_next.
1686 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001687 VarArgsFrameIndex = MFI->CreateFixedObject(VARegSaveSize, ArgOffset +
David Greene3f2bf852009-11-12 20:49:22 +00001688 VARegSaveSize - VARegSize,
1689 true, false);
Dan Gohman475871a2008-07-27 21:46:04 +00001690 SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001691
Dan Gohman475871a2008-07-27 21:46:04 +00001692 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00001693 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001694 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001695 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00001696 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001697 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00001698 RC = ARM::GPRRegisterClass;
1699
Bob Wilson998e1252009-04-20 18:36:57 +00001700 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001701 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Evan Cheng9eda6892009-10-31 03:39:36 +00001702 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
1703 PseudoSourceValue::getFixedStack(VarArgsFrameIndex), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001704 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001705 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00001706 DAG.getConstant(4, getPointerTy()));
1707 }
1708 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001709 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001710 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001711 } else
1712 // This will point to the next argument passed via stack.
David Greene3f2bf852009-11-12 20:49:22 +00001713 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset, true, false);
Evan Chenga8e29892007-01-19 07:51:42 +00001714 }
1715
Dan Gohman98ca4f22009-08-05 01:29:28 +00001716 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00001717}
1718
1719/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00001720static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00001721 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001722 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00001723 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00001724 // Maybe this has already been legalized into the constant pool?
1725 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00001726 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001727 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
1728 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001729 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001730 }
1731 }
1732 return false;
1733}
1734
Evan Chenga8e29892007-01-19 07:51:42 +00001735/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1736/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00001737SDValue
1738ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
1739 SDValue &ARMCC, SelectionDAG &DAG, DebugLoc dl) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001740 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001741 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00001742 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001743 // Constant does not fit, try adjusting it by one?
1744 switch (CC) {
1745 default: break;
1746 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00001747 case ISD::SETGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00001748 if (isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001749 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001750 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001751 }
1752 break;
1753 case ISD::SETULT:
1754 case ISD::SETUGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00001755 if (C > 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001756 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001757 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001758 }
1759 break;
1760 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00001761 case ISD::SETGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00001762 if (isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001763 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001764 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001765 }
1766 break;
1767 case ISD::SETULE:
1768 case ISD::SETUGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00001769 if (C < 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001770 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001771 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001772 }
1773 break;
1774 }
1775 }
1776 }
1777
1778 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001779 ARMISD::NodeType CompareType;
1780 switch (CondCode) {
1781 default:
1782 CompareType = ARMISD::CMP;
1783 break;
1784 case ARMCC::EQ:
1785 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00001786 // Uses only Z Flag
1787 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001788 break;
1789 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001790 ARMCC = DAG.getConstant(CondCode, MVT::i32);
1791 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001792}
1793
1794/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001795static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Dale Johannesende064702009-02-06 21:50:26 +00001796 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001797 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00001798 if (!isFloatingPointZero(RHS))
Owen Anderson825b72b2009-08-11 20:47:22 +00001799 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001800 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001801 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
1802 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001803}
1804
Evan Cheng06b53c02009-11-12 07:13:11 +00001805SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001806 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001807 SDValue LHS = Op.getOperand(0);
1808 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001809 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001810 SDValue TrueVal = Op.getOperand(2);
1811 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00001812 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001813
Owen Anderson825b72b2009-08-11 20:47:22 +00001814 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001815 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001816 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng06b53c02009-11-12 07:13:11 +00001817 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
Dale Johannesende064702009-02-06 21:50:26 +00001818 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001819 }
1820
1821 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001822 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00001823
Owen Anderson825b72b2009-08-11 20:47:22 +00001824 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1825 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001826 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
1827 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00001828 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001829 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001830 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001831 // FIXME: Needs another CMP because flag can have but one use.
Dale Johannesende064702009-02-06 21:50:26 +00001832 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001833 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Dale Johannesende064702009-02-06 21:50:26 +00001834 Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00001835 }
1836 return Result;
1837}
1838
Evan Cheng06b53c02009-11-12 07:13:11 +00001839SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00001840 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001841 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001842 SDValue LHS = Op.getOperand(2);
1843 SDValue RHS = Op.getOperand(3);
1844 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00001845 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001846
Owen Anderson825b72b2009-08-11 20:47:22 +00001847 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001848 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001849 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng06b53c02009-11-12 07:13:11 +00001850 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001851 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Dale Johannesende064702009-02-06 21:50:26 +00001852 Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001853 }
1854
Owen Anderson825b72b2009-08-11 20:47:22 +00001855 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Chenga8e29892007-01-19 07:51:42 +00001856 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001857 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001858
Dale Johannesende064702009-02-06 21:50:26 +00001859 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001860 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1861 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1862 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00001863 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00001864 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001865 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001866 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00001867 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00001868 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001869 }
1870 return Res;
1871}
1872
Dan Gohman475871a2008-07-27 21:46:04 +00001873SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) {
1874 SDValue Chain = Op.getOperand(0);
1875 SDValue Table = Op.getOperand(1);
1876 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001877 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001878
Owen Andersone50ed302009-08-10 22:56:29 +00001879 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00001880 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
1881 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00001882 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00001883 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00001884 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00001885 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
1886 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00001887 if (Subtarget->isThumb2()) {
1888 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
1889 // which does another jump to the destination. This also makes it easier
1890 // to translate it to TBB / TBH later.
1891 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00001892 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00001893 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001894 }
Evan Cheng66ac5312009-07-25 00:33:29 +00001895 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00001896 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
1897 PseudoSourceValue::getJumpTable(), 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00001898 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001899 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00001900 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001901 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00001902 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
1903 PseudoSourceValue::getJumpTable(), 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00001904 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00001905 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001906 }
Evan Chenga8e29892007-01-19 07:51:42 +00001907}
1908
Dan Gohman475871a2008-07-27 21:46:04 +00001909static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Dale Johannesende064702009-02-06 21:50:26 +00001910 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001911 unsigned Opc =
1912 Op.getOpcode() == ISD::FP_TO_SINT ? ARMISD::FTOSI : ARMISD::FTOUI;
Owen Anderson825b72b2009-08-11 20:47:22 +00001913 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
1914 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001915}
1916
Dan Gohman475871a2008-07-27 21:46:04 +00001917static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001918 EVT VT = Op.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001919 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001920 unsigned Opc =
1921 Op.getOpcode() == ISD::SINT_TO_FP ? ARMISD::SITOF : ARMISD::UITOF;
1922
Owen Anderson825b72b2009-08-11 20:47:22 +00001923 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
Dale Johannesende064702009-02-06 21:50:26 +00001924 return DAG.getNode(Opc, dl, VT, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001925}
1926
Dan Gohman475871a2008-07-27 21:46:04 +00001927static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00001928 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00001929 SDValue Tmp0 = Op.getOperand(0);
1930 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00001931 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001932 EVT VT = Op.getValueType();
1933 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001934 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
1935 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001936 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
1937 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001938 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001939}
1940
Jim Grosbach0e0da732009-05-12 23:59:14 +00001941SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
1942 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1943 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00001944 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001945 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
1946 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00001947 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00001948 ? ARM::R7 : ARM::R11;
1949 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
1950 while (Depth--)
1951 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
1952 return FrameAddr;
1953}
1954
Dan Gohman475871a2008-07-27 21:46:04 +00001955SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00001956ARMTargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman475871a2008-07-27 21:46:04 +00001957 SDValue Chain,
1958 SDValue Dst, SDValue Src,
1959 SDValue Size, unsigned Align,
Dan Gohman707e0182008-04-12 04:36:06 +00001960 bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00001961 const Value *DstSV, uint64_t DstSVOff,
1962 const Value *SrcSV, uint64_t SrcSVOff){
Evan Cheng4102eb52007-10-22 22:11:27 +00001963 // Do repeated 4-byte loads and stores. To be improved.
Dan Gohman707e0182008-04-12 04:36:06 +00001964 // This requires 4-byte alignment.
1965 if ((Align & 3) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00001966 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001967 // This requires the copy size to be a constant, preferrably
1968 // within a subtarget-specific limit.
1969 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
1970 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00001971 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001972 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001973 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00001974 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001975
1976 unsigned BytesLeft = SizeVal & 3;
1977 unsigned NumMemOps = SizeVal >> 2;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001978 unsigned EmittedNumMemOps = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001979 EVT VT = MVT::i32;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001980 unsigned VTSize = 4;
Evan Cheng4102eb52007-10-22 22:11:27 +00001981 unsigned i = 0;
Evan Chenge5e7ce42007-05-18 01:19:57 +00001982 const unsigned MAX_LOADS_IN_LDM = 6;
Dan Gohman475871a2008-07-27 21:46:04 +00001983 SDValue TFOps[MAX_LOADS_IN_LDM];
1984 SDValue Loads[MAX_LOADS_IN_LDM];
Dan Gohman1f13c682008-04-28 17:15:20 +00001985 uint64_t SrcOff = 0, DstOff = 0;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001986
Evan Cheng4102eb52007-10-22 22:11:27 +00001987 // Emit up to MAX_LOADS_IN_LDM loads, then a TokenFactor barrier, then the
1988 // same number of stores. The loads and stores will get combined into
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001989 // ldm/stm later on.
Evan Cheng4102eb52007-10-22 22:11:27 +00001990 while (EmittedNumMemOps < NumMemOps) {
1991 for (i = 0;
1992 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00001993 Loads[i] = DAG.getLoad(VT, dl, Chain,
Owen Anderson825b72b2009-08-11 20:47:22 +00001994 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
1995 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001996 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00001997 TFOps[i] = Loads[i].getValue(1);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001998 SrcOff += VTSize;
1999 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002000 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002001
Evan Cheng4102eb52007-10-22 22:11:27 +00002002 for (i = 0;
2003 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00002004 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Owen Anderson825b72b2009-08-11 20:47:22 +00002005 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
2006 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00002007 DstSV, DstSVOff + DstOff);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002008 DstOff += VTSize;
2009 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002010 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00002011
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002012 EmittedNumMemOps += i;
2013 }
2014
Bob Wilson2dc4f542009-03-20 22:42:55 +00002015 if (BytesLeft == 0)
Evan Cheng4102eb52007-10-22 22:11:27 +00002016 return Chain;
2017
2018 // Issue loads / stores for the trailing (1 - 3) bytes.
2019 unsigned BytesLeftSave = BytesLeft;
2020 i = 0;
2021 while (BytesLeft) {
2022 if (BytesLeft >= 2) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002023 VT = MVT::i16;
Evan Cheng4102eb52007-10-22 22:11:27 +00002024 VTSize = 2;
2025 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00002026 VT = MVT::i8;
Evan Cheng4102eb52007-10-22 22:11:27 +00002027 VTSize = 1;
2028 }
2029
Dale Johannesen0f502f62009-02-03 22:26:09 +00002030 Loads[i] = DAG.getLoad(VT, dl, Chain,
Owen Anderson825b72b2009-08-11 20:47:22 +00002031 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
2032 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00002033 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00002034 TFOps[i] = Loads[i].getValue(1);
2035 ++i;
2036 SrcOff += VTSize;
2037 BytesLeft -= VTSize;
2038 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002039 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00002040
2041 i = 0;
2042 BytesLeft = BytesLeftSave;
2043 while (BytesLeft) {
2044 if (BytesLeft >= 2) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002045 VT = MVT::i16;
Evan Cheng4102eb52007-10-22 22:11:27 +00002046 VTSize = 2;
2047 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00002048 VT = MVT::i8;
Evan Cheng4102eb52007-10-22 22:11:27 +00002049 VTSize = 1;
2050 }
2051
Dale Johannesen0f502f62009-02-03 22:26:09 +00002052 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Owen Anderson825b72b2009-08-11 20:47:22 +00002053 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
2054 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00002055 DstSV, DstSVOff + DstOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00002056 ++i;
2057 DstOff += VTSize;
2058 BytesLeft -= VTSize;
2059 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002060 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002061}
2062
Duncan Sands1607f052008-12-01 11:39:25 +00002063static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00002064 SDValue Op = N->getOperand(0);
Dale Johannesende064702009-02-06 21:50:26 +00002065 DebugLoc dl = N->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00002066 if (N->getValueType(0) == MVT::f64) {
Jim Grosbache5165492009-11-09 00:11:35 +00002067 // Turn i64->f64 into VMOVDRR.
Owen Anderson825b72b2009-08-11 20:47:22 +00002068 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2069 DAG.getConstant(0, MVT::i32));
2070 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2071 DAG.getConstant(1, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00002072 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chengc7c77292008-11-04 19:57:48 +00002073 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002074
Jim Grosbache5165492009-11-09 00:11:35 +00002075 // Turn f64->i64 into VMOVRRD.
2076 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00002077 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002078
Chris Lattner27a6c732007-11-24 07:07:01 +00002079 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002080 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
Chris Lattner27a6c732007-11-24 07:07:01 +00002081}
2082
Bob Wilson5bafff32009-06-22 23:27:02 +00002083/// getZeroVector - Returns a vector of specified type with all zero elements.
2084///
Owen Andersone50ed302009-08-10 22:56:29 +00002085static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002086 assert(VT.isVector() && "Expected a vector type");
2087
2088 // Zero vectors are used to represent vector negation and in those cases
2089 // will be implemented with the NEON VNEG instruction. However, VNEG does
2090 // not support i64 elements, so sometimes the zero vectors will need to be
2091 // explicitly constructed. For those cases, and potentially other uses in
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002092 // the future, always build zero vectors as <16 x i8> or <8 x i8> bitcasted
Bob Wilson5bafff32009-06-22 23:27:02 +00002093 // to their dest type. This ensures they get CSE'd.
2094 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002095 SDValue Cst = DAG.getTargetConstant(0, MVT::i8);
2096 SmallVector<SDValue, 8> Ops;
2097 MVT TVT;
2098
2099 if (VT.getSizeInBits() == 64) {
2100 Ops.assign(8, Cst); TVT = MVT::v8i8;
2101 } else {
2102 Ops.assign(16, Cst); TVT = MVT::v16i8;
2103 }
2104 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002105
2106 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2107}
2108
2109/// getOnesVector - Returns a vector of specified type with all bits set.
2110///
Owen Andersone50ed302009-08-10 22:56:29 +00002111static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002112 assert(VT.isVector() && "Expected a vector type");
2113
Bob Wilson929ffa22009-10-30 20:13:25 +00002114 // Always build ones vectors as <16 x i8> or <8 x i8> bitcasted to their
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002115 // dest type. This ensures they get CSE'd.
Bob Wilson5bafff32009-06-22 23:27:02 +00002116 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002117 SDValue Cst = DAG.getTargetConstant(0xFF, MVT::i8);
2118 SmallVector<SDValue, 8> Ops;
2119 MVT TVT;
2120
2121 if (VT.getSizeInBits() == 64) {
2122 Ops.assign(8, Cst); TVT = MVT::v8i8;
2123 } else {
2124 Ops.assign(16, Cst); TVT = MVT::v16i8;
2125 }
2126 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002127
2128 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2129}
2130
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002131/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
2132/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Evan Cheng06b53c02009-11-12 07:13:11 +00002133SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op, SelectionDAG &DAG) {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002134 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2135 EVT VT = Op.getValueType();
2136 unsigned VTBits = VT.getSizeInBits();
2137 DebugLoc dl = Op.getDebugLoc();
2138 SDValue ShOpLo = Op.getOperand(0);
2139 SDValue ShOpHi = Op.getOperand(1);
2140 SDValue ShAmt = Op.getOperand(2);
2141 SDValue ARMCC;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002142 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002143
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002144 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
2145
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002146 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2147 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2148 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
2149 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2150 DAG.getConstant(VTBits, MVT::i32));
2151 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
2152 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002153 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002154
2155 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2156 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng06b53c02009-11-12 07:13:11 +00002157 ARMCC, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002158 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002159 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC,
2160 CCR, Cmp);
2161
2162 SDValue Ops[2] = { Lo, Hi };
2163 return DAG.getMergeValues(Ops, 2, dl);
2164}
2165
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002166/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
2167/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Evan Cheng06b53c02009-11-12 07:13:11 +00002168SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op, SelectionDAG &DAG) {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002169 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2170 EVT VT = Op.getValueType();
2171 unsigned VTBits = VT.getSizeInBits();
2172 DebugLoc dl = Op.getDebugLoc();
2173 SDValue ShOpLo = Op.getOperand(0);
2174 SDValue ShOpHi = Op.getOperand(1);
2175 SDValue ShAmt = Op.getOperand(2);
2176 SDValue ARMCC;
2177
2178 assert(Op.getOpcode() == ISD::SHL_PARTS);
2179 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2180 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2181 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
2182 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2183 DAG.getConstant(VTBits, MVT::i32));
2184 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
2185 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
2186
2187 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2188 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2189 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng06b53c02009-11-12 07:13:11 +00002190 ARMCC, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002191 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
2192 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMCC,
2193 CCR, Cmp);
2194
2195 SDValue Ops[2] = { Lo, Hi };
2196 return DAG.getMergeValues(Ops, 2, dl);
2197}
2198
Bob Wilson5bafff32009-06-22 23:27:02 +00002199static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2200 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00002201 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002202 DebugLoc dl = N->getDebugLoc();
2203
2204 // Lower vector shifts on NEON to use VSHL.
2205 if (VT.isVector()) {
2206 assert(ST->hasNEON() && "unexpected vector shift");
2207
2208 // Left shifts translate directly to the vshiftu intrinsic.
2209 if (N->getOpcode() == ISD::SHL)
2210 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002211 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002212 N->getOperand(0), N->getOperand(1));
2213
2214 assert((N->getOpcode() == ISD::SRA ||
2215 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2216
2217 // NEON uses the same intrinsics for both left and right shifts. For
2218 // right shifts, the shift amounts are negative, so negate the vector of
2219 // shift amounts.
Owen Andersone50ed302009-08-10 22:56:29 +00002220 EVT ShiftVT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002221 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2222 getZeroVector(ShiftVT, DAG, dl),
2223 N->getOperand(1));
2224 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2225 Intrinsic::arm_neon_vshifts :
2226 Intrinsic::arm_neon_vshiftu);
2227 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002228 DAG.getConstant(vshiftInt, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002229 N->getOperand(0), NegatedCount);
2230 }
2231
Eli Friedmance392eb2009-08-22 03:13:10 +00002232 // We can get here for a node like i32 = ISD::SHL i32, i64
2233 if (VT != MVT::i64)
2234 return SDValue();
2235
2236 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00002237 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00002238
Chris Lattner27a6c732007-11-24 07:07:01 +00002239 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2240 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002241 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00002242 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002243
Chris Lattner27a6c732007-11-24 07:07:01 +00002244 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00002245 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002246
Chris Lattner27a6c732007-11-24 07:07:01 +00002247 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00002248 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2249 DAG.getConstant(0, MVT::i32));
2250 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2251 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002252
Chris Lattner27a6c732007-11-24 07:07:01 +00002253 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2254 // captures the result into a carry flag.
2255 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Owen Anderson825b72b2009-08-11 20:47:22 +00002256 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002257
Chris Lattner27a6c732007-11-24 07:07:01 +00002258 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00002259 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002260
Chris Lattner27a6c732007-11-24 07:07:01 +00002261 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002262 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00002263}
2264
Bob Wilson5bafff32009-06-22 23:27:02 +00002265static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2266 SDValue TmpOp0, TmpOp1;
2267 bool Invert = false;
2268 bool Swap = false;
2269 unsigned Opc = 0;
2270
2271 SDValue Op0 = Op.getOperand(0);
2272 SDValue Op1 = Op.getOperand(1);
2273 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00002274 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002275 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2276 DebugLoc dl = Op.getDebugLoc();
2277
2278 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2279 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002280 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002281 case ISD::SETUNE:
2282 case ISD::SETNE: Invert = true; // Fallthrough
2283 case ISD::SETOEQ:
2284 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2285 case ISD::SETOLT:
2286 case ISD::SETLT: Swap = true; // Fallthrough
2287 case ISD::SETOGT:
2288 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2289 case ISD::SETOLE:
2290 case ISD::SETLE: Swap = true; // Fallthrough
2291 case ISD::SETOGE:
2292 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2293 case ISD::SETUGE: Swap = true; // Fallthrough
2294 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2295 case ISD::SETUGT: Swap = true; // Fallthrough
2296 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2297 case ISD::SETUEQ: Invert = true; // Fallthrough
2298 case ISD::SETONE:
2299 // Expand this to (OLT | OGT).
2300 TmpOp0 = Op0;
2301 TmpOp1 = Op1;
2302 Opc = ISD::OR;
2303 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2304 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2305 break;
2306 case ISD::SETUO: Invert = true; // Fallthrough
2307 case ISD::SETO:
2308 // Expand this to (OLT | OGE).
2309 TmpOp0 = Op0;
2310 TmpOp1 = Op1;
2311 Opc = ISD::OR;
2312 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2313 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2314 break;
2315 }
2316 } else {
2317 // Integer comparisons.
2318 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002319 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002320 case ISD::SETNE: Invert = true;
2321 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2322 case ISD::SETLT: Swap = true;
2323 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2324 case ISD::SETLE: Swap = true;
2325 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2326 case ISD::SETULT: Swap = true;
2327 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2328 case ISD::SETULE: Swap = true;
2329 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2330 }
2331
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002332 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00002333 if (Opc == ARMISD::VCEQ) {
2334
2335 SDValue AndOp;
2336 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2337 AndOp = Op0;
2338 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2339 AndOp = Op1;
2340
2341 // Ignore bitconvert.
2342 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2343 AndOp = AndOp.getOperand(0);
2344
2345 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2346 Opc = ARMISD::VTST;
2347 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2348 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2349 Invert = !Invert;
2350 }
2351 }
2352 }
2353
2354 if (Swap)
2355 std::swap(Op0, Op1);
2356
2357 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
2358
2359 if (Invert)
2360 Result = DAG.getNOT(dl, Result, VT);
2361
2362 return Result;
2363}
2364
2365/// isVMOVSplat - Check if the specified splat value corresponds to an immediate
2366/// VMOV instruction, and if so, return the constant being splatted.
2367static SDValue isVMOVSplat(uint64_t SplatBits, uint64_t SplatUndef,
2368 unsigned SplatBitSize, SelectionDAG &DAG) {
2369 switch (SplatBitSize) {
2370 case 8:
2371 // Any 1-byte value is OK.
2372 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Owen Anderson825b72b2009-08-11 20:47:22 +00002373 return DAG.getTargetConstant(SplatBits, MVT::i8);
Bob Wilson5bafff32009-06-22 23:27:02 +00002374
2375 case 16:
2376 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
2377 if ((SplatBits & ~0xff) == 0 ||
2378 (SplatBits & ~0xff00) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002379 return DAG.getTargetConstant(SplatBits, MVT::i16);
Bob Wilson5bafff32009-06-22 23:27:02 +00002380 break;
2381
2382 case 32:
2383 // NEON's 32-bit VMOV supports splat values where:
2384 // * only one byte is nonzero, or
2385 // * the least significant byte is 0xff and the second byte is nonzero, or
2386 // * the least significant 2 bytes are 0xff and the third is nonzero.
2387 if ((SplatBits & ~0xff) == 0 ||
2388 (SplatBits & ~0xff00) == 0 ||
2389 (SplatBits & ~0xff0000) == 0 ||
2390 (SplatBits & ~0xff000000) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002391 return DAG.getTargetConstant(SplatBits, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002392
2393 if ((SplatBits & ~0xffff) == 0 &&
2394 ((SplatBits | SplatUndef) & 0xff) == 0xff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002395 return DAG.getTargetConstant(SplatBits | 0xff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002396
2397 if ((SplatBits & ~0xffffff) == 0 &&
2398 ((SplatBits | SplatUndef) & 0xffff) == 0xffff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002399 return DAG.getTargetConstant(SplatBits | 0xffff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002400
2401 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
2402 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
2403 // VMOV.I32. A (very) minor optimization would be to replicate the value
2404 // and fall through here to test for a valid 64-bit splat. But, then the
2405 // caller would also need to check and handle the change in size.
2406 break;
2407
2408 case 64: {
2409 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
2410 uint64_t BitMask = 0xff;
2411 uint64_t Val = 0;
2412 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
2413 if (((SplatBits | SplatUndef) & BitMask) == BitMask)
2414 Val |= BitMask;
2415 else if ((SplatBits & BitMask) != 0)
2416 return SDValue();
2417 BitMask <<= 8;
2418 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002419 return DAG.getTargetConstant(Val, MVT::i64);
Bob Wilson5bafff32009-06-22 23:27:02 +00002420 }
2421
2422 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002423 llvm_unreachable("unexpected size for isVMOVSplat");
Bob Wilson5bafff32009-06-22 23:27:02 +00002424 break;
2425 }
2426
2427 return SDValue();
2428}
2429
2430/// getVMOVImm - If this is a build_vector of constants which can be
2431/// formed by using a VMOV instruction of the specified element size,
2432/// return the constant being splatted. The ByteSize field indicates the
2433/// number of bytes of each element [1248].
2434SDValue ARM::getVMOVImm(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
2435 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
2436 APInt SplatBits, SplatUndef;
2437 unsigned SplatBitSize;
2438 bool HasAnyUndefs;
2439 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
2440 HasAnyUndefs, ByteSize * 8))
2441 return SDValue();
2442
2443 if (SplatBitSize > ByteSize * 8)
2444 return SDValue();
2445
2446 return isVMOVSplat(SplatBits.getZExtValue(), SplatUndef.getZExtValue(),
2447 SplatBitSize, DAG);
2448}
2449
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002450static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
2451 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002452 unsigned NumElts = VT.getVectorNumElements();
2453 ReverseVEXT = false;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002454 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002455
2456 // If this is a VEXT shuffle, the immediate value is the index of the first
2457 // element. The other shuffle indices must be the successive elements after
2458 // the first one.
2459 unsigned ExpectedElt = Imm;
2460 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002461 // Increment the expected index. If it wraps around, it may still be
2462 // a VEXT but the source vectors must be swapped.
2463 ExpectedElt += 1;
2464 if (ExpectedElt == NumElts * 2) {
2465 ExpectedElt = 0;
2466 ReverseVEXT = true;
2467 }
2468
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002469 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002470 return false;
2471 }
2472
2473 // Adjust the index value if the source operands will be swapped.
2474 if (ReverseVEXT)
2475 Imm -= NumElts;
2476
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002477 return true;
2478}
2479
Bob Wilson8bb9e482009-07-26 00:39:34 +00002480/// isVREVMask - Check if a vector shuffle corresponds to a VREV
2481/// instruction with the specified blocksize. (The order of the elements
2482/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002483static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
2484 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00002485 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
2486 "Only possible block sizes for VREV are: 16, 32, 64");
2487
Bob Wilson8bb9e482009-07-26 00:39:34 +00002488 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00002489 if (EltSz == 64)
2490 return false;
2491
2492 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002493 unsigned BlockElts = M[0] + 1;
Bob Wilson8bb9e482009-07-26 00:39:34 +00002494
2495 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
2496 return false;
2497
2498 for (unsigned i = 0; i < NumElts; ++i) {
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002499 if ((unsigned) M[i] !=
Bob Wilson8bb9e482009-07-26 00:39:34 +00002500 (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
2501 return false;
2502 }
2503
2504 return true;
2505}
2506
Bob Wilsonc692cb72009-08-21 20:54:19 +00002507static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
2508 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002509 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2510 if (EltSz == 64)
2511 return false;
2512
Bob Wilsonc692cb72009-08-21 20:54:19 +00002513 unsigned NumElts = VT.getVectorNumElements();
2514 WhichResult = (M[0] == 0 ? 0 : 1);
2515 for (unsigned i = 0; i < NumElts; i += 2) {
2516 if ((unsigned) M[i] != i + WhichResult ||
2517 (unsigned) M[i+1] != i + NumElts + WhichResult)
2518 return false;
2519 }
2520 return true;
2521}
2522
2523static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
2524 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002525 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2526 if (EltSz == 64)
2527 return false;
2528
Bob Wilsonc692cb72009-08-21 20:54:19 +00002529 unsigned NumElts = VT.getVectorNumElements();
2530 WhichResult = (M[0] == 0 ? 0 : 1);
2531 for (unsigned i = 0; i != NumElts; ++i) {
2532 if ((unsigned) M[i] != 2 * i + WhichResult)
2533 return false;
2534 }
2535
2536 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00002537 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002538 return false;
2539
2540 return true;
2541}
2542
2543static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
2544 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002545 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2546 if (EltSz == 64)
2547 return false;
2548
Bob Wilsonc692cb72009-08-21 20:54:19 +00002549 unsigned NumElts = VT.getVectorNumElements();
2550 WhichResult = (M[0] == 0 ? 0 : 1);
2551 unsigned Idx = WhichResult * NumElts / 2;
2552 for (unsigned i = 0; i != NumElts; i += 2) {
2553 if ((unsigned) M[i] != Idx ||
2554 (unsigned) M[i+1] != Idx + NumElts)
2555 return false;
2556 Idx += 1;
2557 }
2558
2559 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00002560 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002561 return false;
2562
2563 return true;
2564}
2565
Owen Andersone50ed302009-08-10 22:56:29 +00002566static SDValue BuildSplat(SDValue Val, EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002567 // Canonicalize all-zeros and all-ones vectors.
Bob Wilsond06791f2009-08-13 01:57:47 +00002568 ConstantSDNode *ConstVal = cast<ConstantSDNode>(Val.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002569 if (ConstVal->isNullValue())
2570 return getZeroVector(VT, DAG, dl);
2571 if (ConstVal->isAllOnesValue())
2572 return getOnesVector(VT, DAG, dl);
2573
Owen Andersone50ed302009-08-10 22:56:29 +00002574 EVT CanonicalVT;
Bob Wilson5bafff32009-06-22 23:27:02 +00002575 if (VT.is64BitVector()) {
2576 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002577 case 8: CanonicalVT = MVT::v8i8; break;
2578 case 16: CanonicalVT = MVT::v4i16; break;
2579 case 32: CanonicalVT = MVT::v2i32; break;
2580 case 64: CanonicalVT = MVT::v1i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002581 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002582 }
2583 } else {
2584 assert(VT.is128BitVector() && "unknown splat vector size");
2585 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002586 case 8: CanonicalVT = MVT::v16i8; break;
2587 case 16: CanonicalVT = MVT::v8i16; break;
2588 case 32: CanonicalVT = MVT::v4i32; break;
2589 case 64: CanonicalVT = MVT::v2i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002590 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002591 }
2592 }
2593
2594 // Build a canonical splat for this value.
2595 SmallVector<SDValue, 8> Ops;
2596 Ops.assign(CanonicalVT.getVectorNumElements(), Val);
2597 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, &Ops[0],
2598 Ops.size());
2599 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Res);
2600}
2601
2602// If this is a case we can't handle, return null and let the default
2603// expansion code take care of it.
2604static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bob Wilsond06791f2009-08-13 01:57:47 +00002605 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002606 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002607 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002608
2609 APInt SplatBits, SplatUndef;
2610 unsigned SplatBitSize;
2611 bool HasAnyUndefs;
2612 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00002613 if (SplatBitSize <= 64) {
2614 SDValue Val = isVMOVSplat(SplatBits.getZExtValue(),
2615 SplatUndef.getZExtValue(), SplatBitSize, DAG);
2616 if (Val.getNode())
2617 return BuildSplat(Val, VT, DAG, dl);
2618 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00002619 }
2620
2621 // If there are only 2 elements in a 128-bit vector, insert them into an
2622 // undef vector. This handles the common case for 128-bit vector argument
2623 // passing, where the insertions should be translated to subreg accesses
2624 // with no real instructions.
2625 if (VT.is128BitVector() && Op.getNumOperands() == 2) {
2626 SDValue Val = DAG.getUNDEF(VT);
2627 SDValue Op0 = Op.getOperand(0);
2628 SDValue Op1 = Op.getOperand(1);
2629 if (Op0.getOpcode() != ISD::UNDEF)
2630 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op0,
2631 DAG.getIntPtrConstant(0));
2632 if (Op1.getOpcode() != ISD::UNDEF)
2633 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op1,
2634 DAG.getIntPtrConstant(1));
2635 return Val;
Bob Wilson5bafff32009-06-22 23:27:02 +00002636 }
2637
2638 return SDValue();
2639}
2640
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002641/// isShuffleMaskLegal - Targets can use this to indicate that they only
2642/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
2643/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
2644/// are assumed to be legal.
2645bool
2646ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
2647 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002648 if (VT.getVectorNumElements() == 4 &&
2649 (VT.is128BitVector() || VT.is64BitVector())) {
2650 unsigned PFIndexes[4];
2651 for (unsigned i = 0; i != 4; ++i) {
2652 if (M[i] < 0)
2653 PFIndexes[i] = 8;
2654 else
2655 PFIndexes[i] = M[i];
2656 }
2657
2658 // Compute the index in the perfect shuffle table.
2659 unsigned PFTableIndex =
2660 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2661 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2662 unsigned Cost = (PFEntry >> 30);
2663
2664 if (Cost <= 4)
2665 return true;
2666 }
2667
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002668 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00002669 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002670
2671 return (ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
2672 isVREVMask(M, VT, 64) ||
2673 isVREVMask(M, VT, 32) ||
2674 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00002675 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
2676 isVTRNMask(M, VT, WhichResult) ||
2677 isVUZPMask(M, VT, WhichResult) ||
2678 isVZIPMask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002679}
2680
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002681/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
2682/// the specified operations to build the shuffle.
2683static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
2684 SDValue RHS, SelectionDAG &DAG,
2685 DebugLoc dl) {
2686 unsigned OpNum = (PFEntry >> 26) & 0x0F;
2687 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
2688 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
2689
2690 enum {
2691 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
2692 OP_VREV,
2693 OP_VDUP0,
2694 OP_VDUP1,
2695 OP_VDUP2,
2696 OP_VDUP3,
2697 OP_VEXT1,
2698 OP_VEXT2,
2699 OP_VEXT3,
2700 OP_VUZPL, // VUZP, left result
2701 OP_VUZPR, // VUZP, right result
2702 OP_VZIPL, // VZIP, left result
2703 OP_VZIPR, // VZIP, right result
2704 OP_VTRNL, // VTRN, left result
2705 OP_VTRNR // VTRN, right result
2706 };
2707
2708 if (OpNum == OP_COPY) {
2709 if (LHSID == (1*9+2)*9+3) return LHS;
2710 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
2711 return RHS;
2712 }
2713
2714 SDValue OpLHS, OpRHS;
2715 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
2716 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
2717 EVT VT = OpLHS.getValueType();
2718
2719 switch (OpNum) {
2720 default: llvm_unreachable("Unknown shuffle opcode!");
2721 case OP_VREV:
2722 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
2723 case OP_VDUP0:
2724 case OP_VDUP1:
2725 case OP_VDUP2:
2726 case OP_VDUP3:
2727 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002728 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002729 case OP_VEXT1:
2730 case OP_VEXT2:
2731 case OP_VEXT3:
2732 return DAG.getNode(ARMISD::VEXT, dl, VT,
2733 OpLHS, OpRHS,
2734 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
2735 case OP_VUZPL:
2736 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002737 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002738 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
2739 case OP_VZIPL:
2740 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002741 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002742 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
2743 case OP_VTRNL:
2744 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002745 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2746 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002747 }
2748}
2749
Bob Wilson5bafff32009-06-22 23:27:02 +00002750static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002751 SDValue V1 = Op.getOperand(0);
2752 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00002753 DebugLoc dl = Op.getDebugLoc();
2754 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002755 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002756 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00002757
Bob Wilson28865062009-08-13 02:13:04 +00002758 // Convert shuffles that are directly supported on NEON to target-specific
2759 // DAG nodes, instead of keeping them as shuffles and matching them again
2760 // during code selection. This is more efficient and avoids the possibility
2761 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00002762 // FIXME: floating-point vectors should be canonicalized to integer vectors
2763 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002764 SVN->getMask(ShuffleMask);
2765
2766 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
Bob Wilson0ce37102009-08-14 05:08:32 +00002767 int Lane = SVN->getSplatIndex();
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00002768 // If this is undef splat, generate it via "just" vdup, if possible.
2769 if (Lane == -1) Lane = 0;
2770
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002771 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
2772 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00002773 }
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002774 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002775 DAG.getConstant(Lane, MVT::i32));
Bob Wilson0ce37102009-08-14 05:08:32 +00002776 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002777
2778 bool ReverseVEXT;
2779 unsigned Imm;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002780 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002781 if (ReverseVEXT)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002782 std::swap(V1, V2);
2783 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002784 DAG.getConstant(Imm, MVT::i32));
2785 }
2786
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002787 if (isVREVMask(ShuffleMask, VT, 64))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002788 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002789 if (isVREVMask(ShuffleMask, VT, 32))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002790 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002791 if (isVREVMask(ShuffleMask, VT, 16))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002792 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
2793
Bob Wilsonc692cb72009-08-21 20:54:19 +00002794 // Check for Neon shuffles that modify both input vectors in place.
2795 // If both results are used, i.e., if there are two shuffles with the same
2796 // source operands and with masks corresponding to both results of one of
2797 // these operations, DAG memoization will ensure that a single node is
2798 // used for both shuffles.
2799 unsigned WhichResult;
2800 if (isVTRNMask(ShuffleMask, VT, WhichResult))
2801 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2802 V1, V2).getValue(WhichResult);
2803 if (isVUZPMask(ShuffleMask, VT, WhichResult))
2804 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
2805 V1, V2).getValue(WhichResult);
2806 if (isVZIPMask(ShuffleMask, VT, WhichResult))
2807 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
2808 V1, V2).getValue(WhichResult);
2809
2810 // If the shuffle is not directly supported and it has 4 elements, use
2811 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002812 if (VT.getVectorNumElements() == 4 &&
2813 (VT.is128BitVector() || VT.is64BitVector())) {
2814 unsigned PFIndexes[4];
2815 for (unsigned i = 0; i != 4; ++i) {
2816 if (ShuffleMask[i] < 0)
2817 PFIndexes[i] = 8;
2818 else
2819 PFIndexes[i] = ShuffleMask[i];
2820 }
2821
2822 // Compute the index in the perfect shuffle table.
2823 unsigned PFTableIndex =
2824 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2825
2826 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2827 unsigned Cost = (PFEntry >> 30);
2828
2829 if (Cost <= 4)
2830 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
2831 }
Bob Wilsond8e17572009-08-12 22:31:50 +00002832
Bob Wilson22cac0d2009-08-14 05:16:33 +00002833 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00002834}
2835
Bob Wilson5bafff32009-06-22 23:27:02 +00002836static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002837 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002838 DebugLoc dl = Op.getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00002839 SDValue Vec = Op.getOperand(0);
2840 SDValue Lane = Op.getOperand(1);
Bob Wilson934f98b2009-10-15 23:12:05 +00002841 assert(VT == MVT::i32 &&
2842 Vec.getValueType().getVectorElementType().getSizeInBits() < 32 &&
2843 "unexpected type for custom-lowering vector extract");
2844 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
Bob Wilson5bafff32009-06-22 23:27:02 +00002845}
2846
Bob Wilsona6d65862009-08-03 20:36:38 +00002847static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
2848 // The only time a CONCAT_VECTORS operation can have legal types is when
2849 // two 64-bit vectors are concatenated to a 128-bit vector.
2850 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
2851 "unexpected CONCAT_VECTORS");
2852 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00002853 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00002854 SDValue Op0 = Op.getOperand(0);
2855 SDValue Op1 = Op.getOperand(1);
2856 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00002857 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
2858 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00002859 DAG.getIntPtrConstant(0));
2860 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00002861 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
2862 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00002863 DAG.getIntPtrConstant(1));
2864 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00002865}
2866
Dan Gohman475871a2008-07-27 21:46:04 +00002867SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00002868 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002869 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00002870 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00002871 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002872 case ISD::GlobalAddress:
2873 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
2874 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002875 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00002876 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
2877 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002878 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Evan Cheng86198642009-08-07 00:34:42 +00002879 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002880 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
2881 case ISD::SINT_TO_FP:
2882 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
2883 case ISD::FP_TO_SINT:
2884 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
2885 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00002886 case ISD::RETURNADDR: break;
Jim Grosbach0e0da732009-05-12 23:59:14 +00002887 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002888 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002889 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00002890 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00002891 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00002892 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00002893 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00002894 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002895 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00002896 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00002897 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
2898 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2899 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00002900 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00002901 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002902 }
Dan Gohman475871a2008-07-27 21:46:04 +00002903 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00002904}
2905
Duncan Sands1607f052008-12-01 11:39:25 +00002906/// ReplaceNodeResults - Replace the results of node with an illegal result
2907/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00002908void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
2909 SmallVectorImpl<SDValue>&Results,
2910 SelectionDAG &DAG) {
Chris Lattner27a6c732007-11-24 07:07:01 +00002911 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00002912 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002913 llvm_unreachable("Don't know how to custom expand this!");
Duncan Sands1607f052008-12-01 11:39:25 +00002914 return;
2915 case ISD::BIT_CONVERT:
2916 Results.push_back(ExpandBIT_CONVERT(N, DAG));
2917 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00002918 case ISD::SRL:
Duncan Sands1607f052008-12-01 11:39:25 +00002919 case ISD::SRA: {
Bob Wilson5bafff32009-06-22 23:27:02 +00002920 SDValue Res = LowerShift(N, DAG, Subtarget);
Duncan Sands1607f052008-12-01 11:39:25 +00002921 if (Res.getNode())
2922 Results.push_back(Res);
2923 return;
2924 }
Chris Lattner27a6c732007-11-24 07:07:01 +00002925 }
2926}
Chris Lattner27a6c732007-11-24 07:07:01 +00002927
Evan Chenga8e29892007-01-19 07:51:42 +00002928//===----------------------------------------------------------------------===//
2929// ARM Scheduler Hooks
2930//===----------------------------------------------------------------------===//
2931
2932MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00002933ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +00002934 MachineBasicBlock *BB,
2935 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002936 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00002937 DebugLoc dl = MI->getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002938 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00002939 default:
2940 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng007ea272009-08-12 05:17:19 +00002941 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00002942 // To "insert" a SELECT_CC instruction, we actually have to insert the
2943 // diamond control-flow pattern. The incoming instruction knows the
2944 // destination vreg to set, the condition code register to branch on, the
2945 // true/false values to select between, and a branch opcode to use.
2946 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002947 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00002948 ++It;
2949
2950 // thisMBB:
2951 // ...
2952 // TrueVal = ...
2953 // cmpTY ccX, r1, r2
2954 // bCC copy1MBB
2955 // fallthrough --> copy0MBB
2956 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002957 MachineFunction *F = BB->getParent();
2958 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
2959 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenb6728402009-02-13 02:25:56 +00002960 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00002961 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002962 F->insert(It, copy0MBB);
2963 F->insert(It, sinkMBB);
Evan Chenga8e29892007-01-19 07:51:42 +00002964 // Update machine-CFG edges by first adding all successors of the current
2965 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00002966 // Also inform sdisel of the edge changes.
2967 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
2968 E = BB->succ_end(); I != E; ++I) {
2969 EM->insert(std::make_pair(*I, sinkMBB));
2970 sinkMBB->addSuccessor(*I);
2971 }
Evan Chenga8e29892007-01-19 07:51:42 +00002972 // Next, remove all successors of the current block, and add the true
2973 // and fallthrough blocks as its successors.
Evan Chengce319102009-09-19 09:51:03 +00002974 while (!BB->succ_empty())
Evan Chenga8e29892007-01-19 07:51:42 +00002975 BB->removeSuccessor(BB->succ_begin());
2976 BB->addSuccessor(copy0MBB);
2977 BB->addSuccessor(sinkMBB);
2978
2979 // copy0MBB:
2980 // %FalseValue = ...
2981 // # fallthrough to sinkMBB
2982 BB = copy0MBB;
2983
2984 // Update machine-CFG edges
2985 BB->addSuccessor(sinkMBB);
2986
2987 // sinkMBB:
2988 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2989 // ...
2990 BB = sinkMBB;
Dale Johannesenb6728402009-02-13 02:25:56 +00002991 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00002992 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
2993 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
2994
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002995 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00002996 return BB;
2997 }
Evan Cheng86198642009-08-07 00:34:42 +00002998
2999 case ARM::tANDsp:
3000 case ARM::tADDspr_:
3001 case ARM::tSUBspi_:
3002 case ARM::t2SUBrSPi_:
3003 case ARM::t2SUBrSPi12_:
3004 case ARM::t2SUBrSPs_: {
3005 MachineFunction *MF = BB->getParent();
3006 unsigned DstReg = MI->getOperand(0).getReg();
3007 unsigned SrcReg = MI->getOperand(1).getReg();
3008 bool DstIsDead = MI->getOperand(0).isDead();
3009 bool SrcIsKill = MI->getOperand(1).isKill();
3010
3011 if (SrcReg != ARM::SP) {
3012 // Copy the source to SP from virtual register.
3013 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(SrcReg);
3014 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3015 ? ARM::tMOVtgpr2gpr : ARM::tMOVgpr2gpr;
3016 BuildMI(BB, dl, TII->get(CopyOpc), ARM::SP)
3017 .addReg(SrcReg, getKillRegState(SrcIsKill));
3018 }
3019
3020 unsigned OpOpc = 0;
3021 bool NeedPred = false, NeedCC = false, NeedOp3 = false;
3022 switch (MI->getOpcode()) {
3023 default:
3024 llvm_unreachable("Unexpected pseudo instruction!");
3025 case ARM::tANDsp:
3026 OpOpc = ARM::tAND;
3027 NeedPred = true;
3028 break;
3029 case ARM::tADDspr_:
3030 OpOpc = ARM::tADDspr;
3031 break;
3032 case ARM::tSUBspi_:
3033 OpOpc = ARM::tSUBspi;
3034 break;
3035 case ARM::t2SUBrSPi_:
3036 OpOpc = ARM::t2SUBrSPi;
3037 NeedPred = true; NeedCC = true;
3038 break;
3039 case ARM::t2SUBrSPi12_:
3040 OpOpc = ARM::t2SUBrSPi12;
3041 NeedPred = true;
3042 break;
3043 case ARM::t2SUBrSPs_:
3044 OpOpc = ARM::t2SUBrSPs;
3045 NeedPred = true; NeedCC = true; NeedOp3 = true;
3046 break;
3047 }
3048 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(OpOpc), ARM::SP);
3049 if (OpOpc == ARM::tAND)
3050 AddDefaultT1CC(MIB);
3051 MIB.addReg(ARM::SP);
3052 MIB.addOperand(MI->getOperand(2));
3053 if (NeedOp3)
3054 MIB.addOperand(MI->getOperand(3));
3055 if (NeedPred)
3056 AddDefaultPred(MIB);
3057 if (NeedCC)
3058 AddDefaultCC(MIB);
3059
3060 // Copy the result from SP to virtual register.
3061 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(DstReg);
3062 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3063 ? ARM::tMOVgpr2tgpr : ARM::tMOVgpr2gpr;
3064 BuildMI(BB, dl, TII->get(CopyOpc))
3065 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
3066 .addReg(ARM::SP);
3067 MF->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
3068 return BB;
3069 }
Evan Chenga8e29892007-01-19 07:51:42 +00003070 }
3071}
3072
3073//===----------------------------------------------------------------------===//
3074// ARM Optimization Hooks
3075//===----------------------------------------------------------------------===//
3076
Chris Lattnerd1980a52009-03-12 06:52:53 +00003077static
3078SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
3079 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00003080 SelectionDAG &DAG = DCI.DAG;
3081 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003082 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00003083 unsigned Opc = N->getOpcode();
3084 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
3085 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
3086 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
3087 ISD::CondCode CC = ISD::SETCC_INVALID;
3088
3089 if (isSlctCC) {
3090 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
3091 } else {
3092 SDValue CCOp = Slct.getOperand(0);
3093 if (CCOp.getOpcode() == ISD::SETCC)
3094 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
3095 }
3096
3097 bool DoXform = false;
3098 bool InvCC = false;
3099 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
3100 "Bad input!");
3101
3102 if (LHS.getOpcode() == ISD::Constant &&
3103 cast<ConstantSDNode>(LHS)->isNullValue()) {
3104 DoXform = true;
3105 } else if (CC != ISD::SETCC_INVALID &&
3106 RHS.getOpcode() == ISD::Constant &&
3107 cast<ConstantSDNode>(RHS)->isNullValue()) {
3108 std::swap(LHS, RHS);
3109 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003110 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00003111 Op0.getOperand(0).getValueType();
3112 bool isInt = OpVT.isInteger();
3113 CC = ISD::getSetCCInverse(CC, isInt);
3114
3115 if (!TLI.isCondCodeLegal(CC, OpVT))
3116 return SDValue(); // Inverse operator isn't legal.
3117
3118 DoXform = true;
3119 InvCC = true;
3120 }
3121
3122 if (DoXform) {
3123 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
3124 if (isSlctCC)
3125 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
3126 Slct.getOperand(0), Slct.getOperand(1), CC);
3127 SDValue CCOp = Slct.getOperand(0);
3128 if (InvCC)
3129 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
3130 CCOp.getOperand(0), CCOp.getOperand(1), CC);
3131 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
3132 CCOp, OtherOp, Result);
3133 }
3134 return SDValue();
3135}
3136
3137/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
3138static SDValue PerformADDCombine(SDNode *N,
3139 TargetLowering::DAGCombinerInfo &DCI) {
3140 // added by evan in r37685 with no testcase.
3141 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003142
Chris Lattnerd1980a52009-03-12 06:52:53 +00003143 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
3144 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
3145 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
3146 if (Result.getNode()) return Result;
3147 }
3148 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3149 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3150 if (Result.getNode()) return Result;
3151 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003152
Chris Lattnerd1980a52009-03-12 06:52:53 +00003153 return SDValue();
3154}
3155
3156/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
3157static SDValue PerformSUBCombine(SDNode *N,
3158 TargetLowering::DAGCombinerInfo &DCI) {
3159 // added by evan in r37685 with no testcase.
3160 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003161
Chris Lattnerd1980a52009-03-12 06:52:53 +00003162 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
3163 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3164 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3165 if (Result.getNode()) return Result;
3166 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003167
Chris Lattnerd1980a52009-03-12 06:52:53 +00003168 return SDValue();
3169}
3170
Jim Grosbache5165492009-11-09 00:11:35 +00003171/// PerformVMOVRRDCombine - Target-specific dag combine xforms for ARMISD::VMOVRRD.
3172static SDValue PerformVMOVRRDCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003173 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003174 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00003175 SDValue InDouble = N->getOperand(0);
Jim Grosbache5165492009-11-09 00:11:35 +00003176 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003177 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003178 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003179}
3180
Bob Wilson5bafff32009-06-22 23:27:02 +00003181/// getVShiftImm - Check if this is a valid build_vector for the immediate
3182/// operand of a vector shift operation, where all the elements of the
3183/// build_vector must have the same constant integer value.
3184static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
3185 // Ignore bit_converts.
3186 while (Op.getOpcode() == ISD::BIT_CONVERT)
3187 Op = Op.getOperand(0);
3188 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
3189 APInt SplatBits, SplatUndef;
3190 unsigned SplatBitSize;
3191 bool HasAnyUndefs;
3192 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
3193 HasAnyUndefs, ElementBits) ||
3194 SplatBitSize > ElementBits)
3195 return false;
3196 Cnt = SplatBits.getSExtValue();
3197 return true;
3198}
3199
3200/// isVShiftLImm - Check if this is a valid build_vector for the immediate
3201/// operand of a vector shift left operation. That value must be in the range:
3202/// 0 <= Value < ElementBits for a left shift; or
3203/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003204static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003205 assert(VT.isVector() && "vector shift count is not a vector type");
3206 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3207 if (! getVShiftImm(Op, ElementBits, Cnt))
3208 return false;
3209 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
3210}
3211
3212/// isVShiftRImm - Check if this is a valid build_vector for the immediate
3213/// operand of a vector shift right operation. For a shift opcode, the value
3214/// is positive, but for an intrinsic the value count must be negative. The
3215/// absolute value must be in the range:
3216/// 1 <= |Value| <= ElementBits for a right shift; or
3217/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003218static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00003219 int64_t &Cnt) {
3220 assert(VT.isVector() && "vector shift count is not a vector type");
3221 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3222 if (! getVShiftImm(Op, ElementBits, Cnt))
3223 return false;
3224 if (isIntrinsic)
3225 Cnt = -Cnt;
3226 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
3227}
3228
3229/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
3230static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
3231 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
3232 switch (IntNo) {
3233 default:
3234 // Don't do anything for most intrinsics.
3235 break;
3236
3237 // Vector shifts: check for immediate versions and lower them.
3238 // Note: This is done during DAG combining instead of DAG legalizing because
3239 // the build_vectors for 64-bit vector element shift counts are generally
3240 // not legal, and it is hard to see their values after they get legalized to
3241 // loads from a constant pool.
3242 case Intrinsic::arm_neon_vshifts:
3243 case Intrinsic::arm_neon_vshiftu:
3244 case Intrinsic::arm_neon_vshiftls:
3245 case Intrinsic::arm_neon_vshiftlu:
3246 case Intrinsic::arm_neon_vshiftn:
3247 case Intrinsic::arm_neon_vrshifts:
3248 case Intrinsic::arm_neon_vrshiftu:
3249 case Intrinsic::arm_neon_vrshiftn:
3250 case Intrinsic::arm_neon_vqshifts:
3251 case Intrinsic::arm_neon_vqshiftu:
3252 case Intrinsic::arm_neon_vqshiftsu:
3253 case Intrinsic::arm_neon_vqshiftns:
3254 case Intrinsic::arm_neon_vqshiftnu:
3255 case Intrinsic::arm_neon_vqshiftnsu:
3256 case Intrinsic::arm_neon_vqrshiftns:
3257 case Intrinsic::arm_neon_vqrshiftnu:
3258 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00003259 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003260 int64_t Cnt;
3261 unsigned VShiftOpc = 0;
3262
3263 switch (IntNo) {
3264 case Intrinsic::arm_neon_vshifts:
3265 case Intrinsic::arm_neon_vshiftu:
3266 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
3267 VShiftOpc = ARMISD::VSHL;
3268 break;
3269 }
3270 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
3271 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
3272 ARMISD::VSHRs : ARMISD::VSHRu);
3273 break;
3274 }
3275 return SDValue();
3276
3277 case Intrinsic::arm_neon_vshiftls:
3278 case Intrinsic::arm_neon_vshiftlu:
3279 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
3280 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003281 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003282
3283 case Intrinsic::arm_neon_vrshifts:
3284 case Intrinsic::arm_neon_vrshiftu:
3285 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
3286 break;
3287 return SDValue();
3288
3289 case Intrinsic::arm_neon_vqshifts:
3290 case Intrinsic::arm_neon_vqshiftu:
3291 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3292 break;
3293 return SDValue();
3294
3295 case Intrinsic::arm_neon_vqshiftsu:
3296 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3297 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003298 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003299
3300 case Intrinsic::arm_neon_vshiftn:
3301 case Intrinsic::arm_neon_vrshiftn:
3302 case Intrinsic::arm_neon_vqshiftns:
3303 case Intrinsic::arm_neon_vqshiftnu:
3304 case Intrinsic::arm_neon_vqshiftnsu:
3305 case Intrinsic::arm_neon_vqrshiftns:
3306 case Intrinsic::arm_neon_vqrshiftnu:
3307 case Intrinsic::arm_neon_vqrshiftnsu:
3308 // Narrowing shifts require an immediate right shift.
3309 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
3310 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003311 llvm_unreachable("invalid shift count for narrowing vector shift intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003312
3313 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003314 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003315 }
3316
3317 switch (IntNo) {
3318 case Intrinsic::arm_neon_vshifts:
3319 case Intrinsic::arm_neon_vshiftu:
3320 // Opcode already set above.
3321 break;
3322 case Intrinsic::arm_neon_vshiftls:
3323 case Intrinsic::arm_neon_vshiftlu:
3324 if (Cnt == VT.getVectorElementType().getSizeInBits())
3325 VShiftOpc = ARMISD::VSHLLi;
3326 else
3327 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
3328 ARMISD::VSHLLs : ARMISD::VSHLLu);
3329 break;
3330 case Intrinsic::arm_neon_vshiftn:
3331 VShiftOpc = ARMISD::VSHRN; break;
3332 case Intrinsic::arm_neon_vrshifts:
3333 VShiftOpc = ARMISD::VRSHRs; break;
3334 case Intrinsic::arm_neon_vrshiftu:
3335 VShiftOpc = ARMISD::VRSHRu; break;
3336 case Intrinsic::arm_neon_vrshiftn:
3337 VShiftOpc = ARMISD::VRSHRN; break;
3338 case Intrinsic::arm_neon_vqshifts:
3339 VShiftOpc = ARMISD::VQSHLs; break;
3340 case Intrinsic::arm_neon_vqshiftu:
3341 VShiftOpc = ARMISD::VQSHLu; break;
3342 case Intrinsic::arm_neon_vqshiftsu:
3343 VShiftOpc = ARMISD::VQSHLsu; break;
3344 case Intrinsic::arm_neon_vqshiftns:
3345 VShiftOpc = ARMISD::VQSHRNs; break;
3346 case Intrinsic::arm_neon_vqshiftnu:
3347 VShiftOpc = ARMISD::VQSHRNu; break;
3348 case Intrinsic::arm_neon_vqshiftnsu:
3349 VShiftOpc = ARMISD::VQSHRNsu; break;
3350 case Intrinsic::arm_neon_vqrshiftns:
3351 VShiftOpc = ARMISD::VQRSHRNs; break;
3352 case Intrinsic::arm_neon_vqrshiftnu:
3353 VShiftOpc = ARMISD::VQRSHRNu; break;
3354 case Intrinsic::arm_neon_vqrshiftnsu:
3355 VShiftOpc = ARMISD::VQRSHRNsu; break;
3356 }
3357
3358 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003359 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003360 }
3361
3362 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00003363 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003364 int64_t Cnt;
3365 unsigned VShiftOpc = 0;
3366
3367 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
3368 VShiftOpc = ARMISD::VSLI;
3369 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
3370 VShiftOpc = ARMISD::VSRI;
3371 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00003372 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003373 }
3374
3375 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
3376 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00003377 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003378 }
3379
3380 case Intrinsic::arm_neon_vqrshifts:
3381 case Intrinsic::arm_neon_vqrshiftu:
3382 // No immediate versions of these to check for.
3383 break;
3384 }
3385
3386 return SDValue();
3387}
3388
3389/// PerformShiftCombine - Checks for immediate versions of vector shifts and
3390/// lowers them. As with the vector shift intrinsics, this is done during DAG
3391/// combining instead of DAG legalizing because the build_vectors for 64-bit
3392/// vector element shift counts are generally not legal, and it is hard to see
3393/// their values after they get legalized to loads from a constant pool.
3394static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
3395 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003396 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003397
3398 // Nothing to be done for scalar shifts.
3399 if (! VT.isVector())
3400 return SDValue();
3401
3402 assert(ST->hasNEON() && "unexpected vector shift");
3403 int64_t Cnt;
3404
3405 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003406 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003407
3408 case ISD::SHL:
3409 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
3410 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003411 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003412 break;
3413
3414 case ISD::SRA:
3415 case ISD::SRL:
3416 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
3417 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
3418 ARMISD::VSHRs : ARMISD::VSHRu);
3419 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003420 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003421 }
3422 }
3423 return SDValue();
3424}
3425
3426/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
3427/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
3428static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
3429 const ARMSubtarget *ST) {
3430 SDValue N0 = N->getOperand(0);
3431
3432 // Check for sign- and zero-extensions of vector extract operations of 8-
3433 // and 16-bit vector elements. NEON supports these directly. They are
3434 // handled during DAG combining because type legalization will promote them
3435 // to 32-bit types and it is messy to recognize the operations after that.
3436 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
3437 SDValue Vec = N0.getOperand(0);
3438 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00003439 EVT VT = N->getValueType(0);
3440 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003441 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3442
Owen Anderson825b72b2009-08-11 20:47:22 +00003443 if (VT == MVT::i32 &&
3444 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00003445 TLI.isTypeLegal(Vec.getValueType())) {
3446
3447 unsigned Opc = 0;
3448 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003449 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003450 case ISD::SIGN_EXTEND:
3451 Opc = ARMISD::VGETLANEs;
3452 break;
3453 case ISD::ZERO_EXTEND:
3454 case ISD::ANY_EXTEND:
3455 Opc = ARMISD::VGETLANEu;
3456 break;
3457 }
3458 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
3459 }
3460 }
3461
3462 return SDValue();
3463}
3464
Dan Gohman475871a2008-07-27 21:46:04 +00003465SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003466 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003467 switch (N->getOpcode()) {
3468 default: break;
Chris Lattnerd1980a52009-03-12 06:52:53 +00003469 case ISD::ADD: return PerformADDCombine(N, DCI);
3470 case ISD::SUB: return PerformSUBCombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00003471 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson5bafff32009-06-22 23:27:02 +00003472 case ISD::INTRINSIC_WO_CHAIN:
3473 return PerformIntrinsicCombine(N, DCI.DAG);
3474 case ISD::SHL:
3475 case ISD::SRA:
3476 case ISD::SRL:
3477 return PerformShiftCombine(N, DCI.DAG, Subtarget);
3478 case ISD::SIGN_EXTEND:
3479 case ISD::ZERO_EXTEND:
3480 case ISD::ANY_EXTEND:
3481 return PerformExtendCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003482 }
Dan Gohman475871a2008-07-27 21:46:04 +00003483 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003484}
3485
Bill Wendlingaf566342009-08-15 21:21:19 +00003486bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
3487 if (!Subtarget->hasV6Ops())
3488 // Pre-v6 does not support unaligned mem access.
3489 return false;
3490 else if (!Subtarget->hasV6Ops()) {
3491 // v6 may or may not support unaligned mem access.
3492 if (!Subtarget->isTargetDarwin())
3493 return false;
3494 }
3495
3496 switch (VT.getSimpleVT().SimpleTy) {
3497 default:
3498 return false;
3499 case MVT::i8:
3500 case MVT::i16:
3501 case MVT::i32:
3502 return true;
3503 // FIXME: VLD1 etc with standard alignment is legal.
3504 }
3505}
3506
Evan Chenge6c835f2009-08-14 20:09:37 +00003507static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
3508 if (V < 0)
3509 return false;
3510
3511 unsigned Scale = 1;
3512 switch (VT.getSimpleVT().SimpleTy) {
3513 default: return false;
3514 case MVT::i1:
3515 case MVT::i8:
3516 // Scale == 1;
3517 break;
3518 case MVT::i16:
3519 // Scale == 2;
3520 Scale = 2;
3521 break;
3522 case MVT::i32:
3523 // Scale == 4;
3524 Scale = 4;
3525 break;
3526 }
3527
3528 if ((V & (Scale - 1)) != 0)
3529 return false;
3530 V /= Scale;
3531 return V == (V & ((1LL << 5) - 1));
3532}
3533
3534static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
3535 const ARMSubtarget *Subtarget) {
3536 bool isNeg = false;
3537 if (V < 0) {
3538 isNeg = true;
3539 V = - V;
3540 }
3541
3542 switch (VT.getSimpleVT().SimpleTy) {
3543 default: return false;
3544 case MVT::i1:
3545 case MVT::i8:
3546 case MVT::i16:
3547 case MVT::i32:
3548 // + imm12 or - imm8
3549 if (isNeg)
3550 return V == (V & ((1LL << 8) - 1));
3551 return V == (V & ((1LL << 12) - 1));
3552 case MVT::f32:
3553 case MVT::f64:
3554 // Same as ARM mode. FIXME: NEON?
3555 if (!Subtarget->hasVFP2())
3556 return false;
3557 if ((V & 3) != 0)
3558 return false;
3559 V >>= 2;
3560 return V == (V & ((1LL << 8) - 1));
3561 }
3562}
3563
Evan Chengb01fad62007-03-12 23:30:29 +00003564/// isLegalAddressImmediate - Return true if the integer value can be used
3565/// as the offset of the target addressing mode for load / store of the
3566/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00003567static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00003568 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00003569 if (V == 0)
3570 return true;
3571
Evan Cheng65011532009-03-09 19:15:00 +00003572 if (!VT.isSimple())
3573 return false;
3574
Evan Chenge6c835f2009-08-14 20:09:37 +00003575 if (Subtarget->isThumb1Only())
3576 return isLegalT1AddressImmediate(V, VT);
3577 else if (Subtarget->isThumb2())
3578 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00003579
Evan Chenge6c835f2009-08-14 20:09:37 +00003580 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00003581 if (V < 0)
3582 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00003583 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00003584 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00003585 case MVT::i1:
3586 case MVT::i8:
3587 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00003588 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003589 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003590 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00003591 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003592 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003593 case MVT::f32:
3594 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00003595 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00003596 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00003597 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00003598 return false;
3599 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003600 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00003601 }
Evan Chenga8e29892007-01-19 07:51:42 +00003602}
3603
Evan Chenge6c835f2009-08-14 20:09:37 +00003604bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
3605 EVT VT) const {
3606 int Scale = AM.Scale;
3607 if (Scale < 0)
3608 return false;
3609
3610 switch (VT.getSimpleVT().SimpleTy) {
3611 default: return false;
3612 case MVT::i1:
3613 case MVT::i8:
3614 case MVT::i16:
3615 case MVT::i32:
3616 if (Scale == 1)
3617 return true;
3618 // r + r << imm
3619 Scale = Scale & ~1;
3620 return Scale == 2 || Scale == 4 || Scale == 8;
3621 case MVT::i64:
3622 // r + r
3623 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
3624 return true;
3625 return false;
3626 case MVT::isVoid:
3627 // Note, we allow "void" uses (basically, uses that aren't loads or
3628 // stores), because arm allows folding a scale into many arithmetic
3629 // operations. This should be made more precise and revisited later.
3630
3631 // Allow r << imm, but the imm has to be a multiple of two.
3632 if (Scale & 1) return false;
3633 return isPowerOf2_32(Scale);
3634 }
3635}
3636
Chris Lattner37caf8c2007-04-09 23:33:39 +00003637/// isLegalAddressingMode - Return true if the addressing mode represented
3638/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003639bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00003640 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00003641 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00003642 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00003643 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003644
Chris Lattner37caf8c2007-04-09 23:33:39 +00003645 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003646 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003647 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003648
Chris Lattner37caf8c2007-04-09 23:33:39 +00003649 switch (AM.Scale) {
3650 case 0: // no scale reg, must be "r+i" or "r", or "i".
3651 break;
3652 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00003653 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00003654 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00003655 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00003656 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00003657 // ARM doesn't support any R+R*scale+imm addr modes.
3658 if (AM.BaseOffs)
3659 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003660
Bob Wilson2c7dab12009-04-08 17:55:28 +00003661 if (!VT.isSimple())
3662 return false;
3663
Evan Chenge6c835f2009-08-14 20:09:37 +00003664 if (Subtarget->isThumb2())
3665 return isLegalT2ScaledAddressingMode(AM, VT);
3666
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003667 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00003668 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00003669 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00003670 case MVT::i1:
3671 case MVT::i8:
3672 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003673 if (Scale < 0) Scale = -Scale;
3674 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003675 return true;
3676 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00003677 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003678 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00003679 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00003680 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003681 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003682 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00003683 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003684
Owen Anderson825b72b2009-08-11 20:47:22 +00003685 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00003686 // Note, we allow "void" uses (basically, uses that aren't loads or
3687 // stores), because arm allows folding a scale into many arithmetic
3688 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003689
Chris Lattner37caf8c2007-04-09 23:33:39 +00003690 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00003691 if (Scale & 1) return false;
3692 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00003693 }
3694 break;
Evan Chengb01fad62007-03-12 23:30:29 +00003695 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00003696 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00003697}
3698
Evan Cheng77e47512009-11-11 19:05:52 +00003699/// isLegalICmpImmediate - Return true if the specified immediate is legal
3700/// icmp immediate, that is the target has icmp instructions which can compare
3701/// a register against the immediate without having to materialize the
3702/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00003703bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00003704 if (!Subtarget->isThumb())
3705 return ARM_AM::getSOImmVal(Imm) != -1;
3706 if (Subtarget->isThumb2())
3707 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00003708 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00003709}
3710
Owen Andersone50ed302009-08-10 22:56:29 +00003711static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00003712 bool isSEXTLoad, SDValue &Base,
3713 SDValue &Offset, bool &isInc,
3714 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00003715 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
3716 return false;
3717
Owen Anderson825b72b2009-08-11 20:47:22 +00003718 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00003719 // AddressingMode 3
3720 Base = Ptr->getOperand(0);
3721 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003722 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003723 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003724 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00003725 isInc = false;
3726 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3727 return true;
3728 }
3729 }
3730 isInc = (Ptr->getOpcode() == ISD::ADD);
3731 Offset = Ptr->getOperand(1);
3732 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00003733 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00003734 // AddressingMode 2
3735 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003736 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003737 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003738 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00003739 isInc = false;
3740 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3741 Base = Ptr->getOperand(0);
3742 return true;
3743 }
3744 }
3745
3746 if (Ptr->getOpcode() == ISD::ADD) {
3747 isInc = true;
3748 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
3749 if (ShOpcVal != ARM_AM::no_shift) {
3750 Base = Ptr->getOperand(1);
3751 Offset = Ptr->getOperand(0);
3752 } else {
3753 Base = Ptr->getOperand(0);
3754 Offset = Ptr->getOperand(1);
3755 }
3756 return true;
3757 }
3758
3759 isInc = (Ptr->getOpcode() == ISD::ADD);
3760 Base = Ptr->getOperand(0);
3761 Offset = Ptr->getOperand(1);
3762 return true;
3763 }
3764
Jim Grosbache5165492009-11-09 00:11:35 +00003765 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00003766 return false;
3767}
3768
Owen Andersone50ed302009-08-10 22:56:29 +00003769static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00003770 bool isSEXTLoad, SDValue &Base,
3771 SDValue &Offset, bool &isInc,
3772 SelectionDAG &DAG) {
3773 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
3774 return false;
3775
3776 Base = Ptr->getOperand(0);
3777 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
3778 int RHSC = (int)RHS->getZExtValue();
3779 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
3780 assert(Ptr->getOpcode() == ISD::ADD);
3781 isInc = false;
3782 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3783 return true;
3784 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
3785 isInc = Ptr->getOpcode() == ISD::ADD;
3786 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
3787 return true;
3788 }
3789 }
3790
3791 return false;
3792}
3793
Evan Chenga8e29892007-01-19 07:51:42 +00003794/// getPreIndexedAddressParts - returns true by value, base pointer and
3795/// offset pointer and addressing mode by reference if the node's address
3796/// can be legally represented as pre-indexed load / store address.
3797bool
Dan Gohman475871a2008-07-27 21:46:04 +00003798ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
3799 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003800 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00003801 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003802 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00003803 return false;
3804
Owen Andersone50ed302009-08-10 22:56:29 +00003805 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00003806 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00003807 bool isSEXTLoad = false;
3808 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
3809 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003810 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003811 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
3812 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
3813 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003814 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003815 } else
3816 return false;
3817
3818 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00003819 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00003820 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00003821 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
3822 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00003823 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00003824 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00003825 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00003826 if (!isLegal)
3827 return false;
3828
3829 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
3830 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00003831}
3832
3833/// getPostIndexedAddressParts - returns true by value, base pointer and
3834/// offset pointer and addressing mode by reference if this node can be
3835/// combined with a load / store to form a post-indexed load / store.
3836bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00003837 SDValue &Base,
3838 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003839 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00003840 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003841 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00003842 return false;
3843
Owen Andersone50ed302009-08-10 22:56:29 +00003844 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00003845 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00003846 bool isSEXTLoad = false;
3847 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003848 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003849 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
3850 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003851 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003852 } else
3853 return false;
3854
3855 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00003856 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00003857 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00003858 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003859 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00003860 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00003861 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
3862 isInc, DAG);
3863 if (!isLegal)
3864 return false;
3865
3866 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
3867 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00003868}
3869
Dan Gohman475871a2008-07-27 21:46:04 +00003870void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00003871 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003872 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003873 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00003874 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00003875 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003876 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00003877 switch (Op.getOpcode()) {
3878 default: break;
3879 case ARMISD::CMOV: {
3880 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00003881 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00003882 if (KnownZero == 0 && KnownOne == 0) return;
3883
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003884 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00003885 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
3886 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00003887 KnownZero &= KnownZeroRHS;
3888 KnownOne &= KnownOneRHS;
3889 return;
3890 }
3891 }
3892}
3893
3894//===----------------------------------------------------------------------===//
3895// ARM Inline Assembly Support
3896//===----------------------------------------------------------------------===//
3897
3898/// getConstraintType - Given a constraint letter, return the type of
3899/// constraint it is for this target.
3900ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00003901ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
3902 if (Constraint.size() == 1) {
3903 switch (Constraint[0]) {
3904 default: break;
3905 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003906 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00003907 }
Evan Chenga8e29892007-01-19 07:51:42 +00003908 }
Chris Lattner4234f572007-03-25 02:14:49 +00003909 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00003910}
3911
Bob Wilson2dc4f542009-03-20 22:42:55 +00003912std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00003913ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00003914 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003915 if (Constraint.size() == 1) {
3916 // GCC RS6000 Constraint Letters
3917 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003918 case 'l':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003919 if (Subtarget->isThumb1Only())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00003920 return std::make_pair(0U, ARM::tGPRRegisterClass);
3921 else
3922 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003923 case 'r':
3924 return std::make_pair(0U, ARM::GPRRegisterClass);
3925 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00003926 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003927 return std::make_pair(0U, ARM::SPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00003928 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003929 return std::make_pair(0U, ARM::DPRRegisterClass);
3930 break;
Evan Chenga8e29892007-01-19 07:51:42 +00003931 }
3932 }
3933 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3934}
3935
3936std::vector<unsigned> ARMTargetLowering::
3937getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00003938 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003939 if (Constraint.size() != 1)
3940 return std::vector<unsigned>();
3941
3942 switch (Constraint[0]) { // GCC ARM Constraint Letters
3943 default: break;
3944 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00003945 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
3946 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
3947 0);
Evan Chenga8e29892007-01-19 07:51:42 +00003948 case 'r':
3949 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
3950 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
3951 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
3952 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003953 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00003954 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003955 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
3956 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
3957 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
3958 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
3959 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
3960 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
3961 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
3962 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00003963 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003964 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
3965 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
3966 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
3967 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
3968 break;
Evan Chenga8e29892007-01-19 07:51:42 +00003969 }
3970
3971 return std::vector<unsigned>();
3972}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003973
3974/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3975/// vector. If it is invalid, don't add anything to Ops.
3976void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3977 char Constraint,
3978 bool hasMemory,
3979 std::vector<SDValue>&Ops,
3980 SelectionDAG &DAG) const {
3981 SDValue Result(0, 0);
3982
3983 switch (Constraint) {
3984 default: break;
3985 case 'I': case 'J': case 'K': case 'L':
3986 case 'M': case 'N': case 'O':
3987 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
3988 if (!C)
3989 return;
3990
3991 int64_t CVal64 = C->getSExtValue();
3992 int CVal = (int) CVal64;
3993 // None of these constraints allow values larger than 32 bits. Check
3994 // that the value fits in an int.
3995 if (CVal != CVal64)
3996 return;
3997
3998 switch (Constraint) {
3999 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004000 if (Subtarget->isThumb1Only()) {
4001 // This must be a constant between 0 and 255, for ADD
4002 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004003 if (CVal >= 0 && CVal <= 255)
4004 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004005 } else if (Subtarget->isThumb2()) {
4006 // A constant that can be used as an immediate value in a
4007 // data-processing instruction.
4008 if (ARM_AM::getT2SOImmVal(CVal) != -1)
4009 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004010 } else {
4011 // A constant that can be used as an immediate value in a
4012 // data-processing instruction.
4013 if (ARM_AM::getSOImmVal(CVal) != -1)
4014 break;
4015 }
4016 return;
4017
4018 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004019 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004020 // This must be a constant between -255 and -1, for negated ADD
4021 // immediates. This can be used in GCC with an "n" modifier that
4022 // prints the negated value, for use with SUB instructions. It is
4023 // not useful otherwise but is implemented for compatibility.
4024 if (CVal >= -255 && CVal <= -1)
4025 break;
4026 } else {
4027 // This must be a constant between -4095 and 4095. It is not clear
4028 // what this constraint is intended for. Implemented for
4029 // compatibility with GCC.
4030 if (CVal >= -4095 && CVal <= 4095)
4031 break;
4032 }
4033 return;
4034
4035 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004036 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004037 // A 32-bit value where only one byte has a nonzero value. Exclude
4038 // zero to match GCC. This constraint is used by GCC internally for
4039 // constants that can be loaded with a move/shift combination.
4040 // It is not useful otherwise but is implemented for compatibility.
4041 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
4042 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004043 } else if (Subtarget->isThumb2()) {
4044 // A constant whose bitwise inverse can be used as an immediate
4045 // value in a data-processing instruction. This can be used in GCC
4046 // with a "B" modifier that prints the inverted value, for use with
4047 // BIC and MVN instructions. It is not useful otherwise but is
4048 // implemented for compatibility.
4049 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
4050 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004051 } else {
4052 // A constant whose bitwise inverse can be used as an immediate
4053 // value in a data-processing instruction. This can be used in GCC
4054 // with a "B" modifier that prints the inverted value, for use with
4055 // BIC and MVN instructions. It is not useful otherwise but is
4056 // implemented for compatibility.
4057 if (ARM_AM::getSOImmVal(~CVal) != -1)
4058 break;
4059 }
4060 return;
4061
4062 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004063 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004064 // This must be a constant between -7 and 7,
4065 // for 3-operand ADD/SUB immediate instructions.
4066 if (CVal >= -7 && CVal < 7)
4067 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004068 } else if (Subtarget->isThumb2()) {
4069 // A constant whose negation can be used as an immediate value in a
4070 // data-processing instruction. This can be used in GCC with an "n"
4071 // modifier that prints the negated value, for use with SUB
4072 // instructions. It is not useful otherwise but is implemented for
4073 // compatibility.
4074 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
4075 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004076 } else {
4077 // A constant whose negation can be used as an immediate value in a
4078 // data-processing instruction. This can be used in GCC with an "n"
4079 // modifier that prints the negated value, for use with SUB
4080 // instructions. It is not useful otherwise but is implemented for
4081 // compatibility.
4082 if (ARM_AM::getSOImmVal(-CVal) != -1)
4083 break;
4084 }
4085 return;
4086
4087 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004088 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004089 // This must be a multiple of 4 between 0 and 1020, for
4090 // ADD sp + immediate.
4091 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
4092 break;
4093 } else {
4094 // A power of two or a constant between 0 and 32. This is used in
4095 // GCC for the shift amount on shifted register operands, but it is
4096 // useful in general for any shift amounts.
4097 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
4098 break;
4099 }
4100 return;
4101
4102 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004103 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004104 // This must be a constant between 0 and 31, for shift amounts.
4105 if (CVal >= 0 && CVal <= 31)
4106 break;
4107 }
4108 return;
4109
4110 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004111 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004112 // This must be a multiple of 4 between -508 and 508, for
4113 // ADD/SUB sp = sp + immediate.
4114 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
4115 break;
4116 }
4117 return;
4118 }
4119 Result = DAG.getTargetConstant(CVal, Op.getValueType());
4120 break;
4121 }
4122
4123 if (Result.getNode()) {
4124 Ops.push_back(Result);
4125 return;
4126 }
4127 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
4128 Ops, DAG);
4129}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00004130
4131bool
4132ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
4133 // The ARM target isn't yet aware of offsets.
4134 return false;
4135}
Evan Cheng39382422009-10-28 01:44:26 +00004136
4137int ARM::getVFPf32Imm(const APFloat &FPImm) {
4138 APInt Imm = FPImm.bitcastToAPInt();
4139 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
4140 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
4141 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
4142
4143 // We can handle 4 bits of mantissa.
4144 // mantissa = (16+UInt(e:f:g:h))/16.
4145 if (Mantissa & 0x7ffff)
4146 return -1;
4147 Mantissa >>= 19;
4148 if ((Mantissa & 0xf) != Mantissa)
4149 return -1;
4150
4151 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4152 if (Exp < -3 || Exp > 4)
4153 return -1;
4154 Exp = ((Exp+3) & 0x7) ^ 4;
4155
4156 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4157}
4158
4159int ARM::getVFPf64Imm(const APFloat &FPImm) {
4160 APInt Imm = FPImm.bitcastToAPInt();
4161 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
4162 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
4163 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
4164
4165 // We can handle 4 bits of mantissa.
4166 // mantissa = (16+UInt(e:f:g:h))/16.
4167 if (Mantissa & 0xffffffffffffLL)
4168 return -1;
4169 Mantissa >>= 48;
4170 if ((Mantissa & 0xf) != Mantissa)
4171 return -1;
4172
4173 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4174 if (Exp < -3 || Exp > 4)
4175 return -1;
4176 Exp = ((Exp+3) & 0x7) ^ 4;
4177
4178 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4179}
4180
4181/// isFPImmLegal - Returns true if the target can instruction select the
4182/// specified FP immediate natively. If false, the legalizer will
4183/// materialize the FP immediate as a load from a constant pool.
4184bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
4185 if (!Subtarget->hasVFP3())
4186 return false;
4187 if (VT == MVT::f32)
4188 return ARM::getVFPf32Imm(Imm) != -1;
4189 if (VT == MVT::f64)
4190 return ARM::getVFPf64Imm(Imm) != -1;
4191 return false;
4192}