blob: cd89c070b728b6d1c70cfc0f247ab0688adcf49f [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"
Evan Cheng27707472007-03-16 08:43:56 +000028#include "llvm/Instruction.h"
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +000029#include "llvm/Intrinsics.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000030#include "llvm/GlobalValue.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000031#include "llvm/CodeGen/CallingConvLower.h"
Evan Chenga8e29892007-01-19 07:51:42 +000032#include "llvm/CodeGen/MachineBasicBlock.h"
33#include "llvm/CodeGen/MachineFrameInfo.h"
34#include "llvm/CodeGen/MachineFunction.h"
35#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000036#include "llvm/CodeGen/MachineRegisterInfo.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000037#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chenga8e29892007-01-19 07:51:42 +000038#include "llvm/CodeGen/SelectionDAG.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000039#include "llvm/Target/TargetOptions.h"
Evan Chenga8e29892007-01-19 07:51:42 +000040#include "llvm/ADT/VectorExtras.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000041#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000042#include "llvm/Support/MathExtras.h"
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000043#include <sstream>
Evan Chenga8e29892007-01-19 07:51:42 +000044using namespace llvm;
45
Owen Andersone50ed302009-08-10 22:56:29 +000046static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000047 CCValAssign::LocInfo &LocInfo,
48 ISD::ArgFlagsTy &ArgFlags,
49 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000050static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000051 CCValAssign::LocInfo &LocInfo,
52 ISD::ArgFlagsTy &ArgFlags,
53 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000054static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000055 CCValAssign::LocInfo &LocInfo,
56 ISD::ArgFlagsTy &ArgFlags,
57 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000058static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000059 CCValAssign::LocInfo &LocInfo,
60 ISD::ArgFlagsTy &ArgFlags,
61 CCState &State);
62
Owen Andersone50ed302009-08-10 22:56:29 +000063void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
64 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000065 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000066 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000067 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
68 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000069
Owen Anderson70671842009-08-10 20:18:46 +000070 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +000071 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +000072 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000073 }
74
Owen Andersone50ed302009-08-10 22:56:29 +000075 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +000076 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Owen Anderson70671842009-08-10 20:18:46 +000077 setOperationAction(ISD::VSETCC, VT.getSimpleVT(), Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +000078 if (ElemTy == MVT::i8 || ElemTy == MVT::i16)
Owen Anderson70671842009-08-10 20:18:46 +000079 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson0696fdf2009-09-16 20:20:44 +000080 if (ElemTy != MVT::i32) {
81 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
82 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
83 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
84 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
85 }
Owen Anderson70671842009-08-10 20:18:46 +000086 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
87 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Owen Anderson70671842009-08-10 20:18:46 +000088 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Custom);
Anton Korobeynikov8e6c2b92009-08-21 12:40:35 +000089 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +000090 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +000091 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
92 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
93 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +000094 }
95
96 // Promote all bit-wise operations.
97 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +000098 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000099 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
100 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000101 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000102 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000103 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000104 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000105 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000106 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000107 }
Bob Wilson16330762009-09-16 00:17:28 +0000108
109 // Neon does not support vector divide/remainder operations.
110 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
111 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
112 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
113 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
114 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
115 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000116}
117
Owen Andersone50ed302009-08-10 22:56:29 +0000118void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000119 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000120 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000121}
122
Owen Andersone50ed302009-08-10 22:56:29 +0000123void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000124 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000125 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000126}
127
Chris Lattnerf0144122009-07-28 03:13:23 +0000128static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
129 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000130 return new TargetLoweringObjectFileMachO();
Chris Lattner80ec2792009-08-02 00:34:36 +0000131 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000132}
133
Evan Chenga8e29892007-01-19 07:51:42 +0000134ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000135 : TargetLowering(TM, createTLOF(TM)), ARMPCLabelIndex(0) {
Evan Chenga8e29892007-01-19 07:51:42 +0000136 Subtarget = &TM.getSubtarget<ARMSubtarget>();
137
Evan Chengb1df8f22007-04-27 08:15:43 +0000138 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000139 // Uses VFP for Thumb libfuncs if available.
140 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
141 // Single-precision floating-point arithmetic.
142 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
143 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
144 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
145 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000146
Evan Chengb1df8f22007-04-27 08:15:43 +0000147 // Double-precision floating-point arithmetic.
148 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
149 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
150 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
151 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000152
Evan Chengb1df8f22007-04-27 08:15:43 +0000153 // Single-precision comparisons.
154 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
155 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
156 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
157 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
158 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
159 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
160 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
161 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000162
Evan Chengb1df8f22007-04-27 08:15:43 +0000163 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
164 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
165 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
166 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
167 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
168 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
169 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
170 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000171
Evan Chengb1df8f22007-04-27 08:15:43 +0000172 // Double-precision comparisons.
173 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
174 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
175 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
176 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
177 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
178 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
179 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
180 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000181
Evan Chengb1df8f22007-04-27 08:15:43 +0000182 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
183 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
184 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
185 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
186 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
187 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
188 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
189 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000190
Evan Chengb1df8f22007-04-27 08:15:43 +0000191 // Floating-point to integer conversions.
192 // i64 conversions are done via library routines even when generating VFP
193 // instructions, so use the same ones.
194 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
195 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
196 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
197 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000198
Evan Chengb1df8f22007-04-27 08:15:43 +0000199 // Conversions between floating types.
200 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
201 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
202
203 // Integer to floating-point conversions.
204 // i64 conversions are done via library routines even when generating VFP
205 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000206 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
207 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000208 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
209 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
210 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
211 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
212 }
Evan Chenga8e29892007-01-19 07:51:42 +0000213 }
214
Bob Wilson2f954612009-05-22 17:38:41 +0000215 // These libcalls are not available in 32-bit.
216 setLibcallName(RTLIB::SHL_I128, 0);
217 setLibcallName(RTLIB::SRL_I128, 0);
218 setLibcallName(RTLIB::SRA_I128, 0);
219
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000220 // Libcalls should use the AAPCS base standard ABI, even if hard float
221 // is in effect, as per the ARM RTABI specification, section 4.1.2.
222 if (Subtarget->isAAPCS_ABI()) {
223 for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i) {
224 setLibcallCallingConv(static_cast<RTLIB::Libcall>(i),
225 CallingConv::ARM_AAPCS);
226 }
227 }
228
David Goodwinf1daf7d2009-07-08 23:10:31 +0000229 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000230 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000231 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000232 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000233 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000234 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
235 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000236
Owen Anderson825b72b2009-08-11 20:47:22 +0000237 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000238 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000239
240 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000241 addDRTypeForNEON(MVT::v2f32);
242 addDRTypeForNEON(MVT::v8i8);
243 addDRTypeForNEON(MVT::v4i16);
244 addDRTypeForNEON(MVT::v2i32);
245 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000246
Owen Anderson825b72b2009-08-11 20:47:22 +0000247 addQRTypeForNEON(MVT::v4f32);
248 addQRTypeForNEON(MVT::v2f64);
249 addQRTypeForNEON(MVT::v16i8);
250 addQRTypeForNEON(MVT::v8i16);
251 addQRTypeForNEON(MVT::v4i32);
252 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000253
Bob Wilson74dc72e2009-09-15 23:55:57 +0000254 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
255 // neither Neon nor VFP support any arithmetic operations on it.
256 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
257 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
258 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
259 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
260 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
261 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
262 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
263 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
264 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
265 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
266 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
267 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
268 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
269 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
270 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
271 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
272 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
273 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
274 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
275 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
276 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
277 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
278 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
279 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
280
Bob Wilson642b3292009-09-16 00:32:15 +0000281 // Neon does not support some operations on v1i64 and v2i64 types.
282 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
283 setOperationAction(ISD::MUL, MVT::v2i64, Expand);
284 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
285 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
286
Bob Wilson5bafff32009-06-22 23:27:02 +0000287 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
288 setTargetDAGCombine(ISD::SHL);
289 setTargetDAGCombine(ISD::SRL);
290 setTargetDAGCombine(ISD::SRA);
291 setTargetDAGCombine(ISD::SIGN_EXTEND);
292 setTargetDAGCombine(ISD::ZERO_EXTEND);
293 setTargetDAGCombine(ISD::ANY_EXTEND);
294 }
295
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000296 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000297
298 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000299 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000300
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000301 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000302 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000303
Evan Chenga8e29892007-01-19 07:51:42 +0000304 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000305 if (!Subtarget->isThumb1Only()) {
306 for (unsigned im = (unsigned)ISD::PRE_INC;
307 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000308 setIndexedLoadAction(im, MVT::i1, Legal);
309 setIndexedLoadAction(im, MVT::i8, Legal);
310 setIndexedLoadAction(im, MVT::i16, Legal);
311 setIndexedLoadAction(im, MVT::i32, Legal);
312 setIndexedStoreAction(im, MVT::i1, Legal);
313 setIndexedStoreAction(im, MVT::i8, Legal);
314 setIndexedStoreAction(im, MVT::i16, Legal);
315 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000316 }
Evan Chenga8e29892007-01-19 07:51:42 +0000317 }
318
319 // i64 operation support.
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000320 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000321 setOperationAction(ISD::MUL, MVT::i64, Expand);
322 setOperationAction(ISD::MULHU, MVT::i32, Expand);
323 setOperationAction(ISD::MULHS, MVT::i32, Expand);
324 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
325 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000326 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000327 setOperationAction(ISD::MUL, MVT::i64, Expand);
328 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb6207242009-08-01 00:16:10 +0000329 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000330 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000331 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000332 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
333 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
334 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
335 setOperationAction(ISD::SRL, MVT::i64, Custom);
336 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000337
338 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000339 setOperationAction(ISD::ROTL, MVT::i32, Expand);
340 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
341 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000342 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000343 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000344
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000345 // Only ARMv6 has BSWAP.
346 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000347 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000348
Evan Chenga8e29892007-01-19 07:51:42 +0000349 // These are expanded into libcalls.
Owen Anderson825b72b2009-08-11 20:47:22 +0000350 setOperationAction(ISD::SDIV, MVT::i32, Expand);
351 setOperationAction(ISD::UDIV, MVT::i32, Expand);
352 setOperationAction(ISD::SREM, MVT::i32, Expand);
353 setOperationAction(ISD::UREM, MVT::i32, Expand);
354 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
355 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000356
Evan Chenga8e29892007-01-19 07:51:42 +0000357 // Support label based line numbers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000358 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
359 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000360
Owen Anderson825b72b2009-08-11 20:47:22 +0000361 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
362 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
363 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
364 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000365
Evan Chenga8e29892007-01-19 07:51:42 +0000366 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000367 setOperationAction(ISD::VASTART, MVT::Other, Custom);
368 setOperationAction(ISD::VAARG, MVT::Other, Expand);
369 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
370 setOperationAction(ISD::VAEND, MVT::Other, Expand);
371 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
372 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000373 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
374 // FIXME: Shouldn't need this, since no register is used, but the legalizer
375 // doesn't yet know how to not do that for SjLj.
376 setExceptionSelectorRegister(ARM::R0);
Evan Cheng86198642009-08-07 00:34:42 +0000377 if (Subtarget->isThumb())
Owen Anderson825b72b2009-08-11 20:47:22 +0000378 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Evan Cheng86198642009-08-07 00:34:42 +0000379 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000380 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
381 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000382
Evan Chengd27c9fc2009-07-03 01:43:10 +0000383 if (!Subtarget->hasV6Ops() && !Subtarget->isThumb2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000384 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
385 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000386 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000387 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000388
David Goodwinf1daf7d2009-07-08 23:10:31 +0000389 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only())
Evan Chengc7c77292008-11-04 19:57:48 +0000390 // Turn f64->i64 into FMRRD, i64 -> f64 to FMDRR iff target supports vfp2.
Owen Anderson825b72b2009-08-11 20:47:22 +0000391 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000392
393 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000394 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
395 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
396 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000397
Owen Anderson825b72b2009-08-11 20:47:22 +0000398 setOperationAction(ISD::SETCC, MVT::i32, Expand);
399 setOperationAction(ISD::SETCC, MVT::f32, Expand);
400 setOperationAction(ISD::SETCC, MVT::f64, Expand);
401 setOperationAction(ISD::SELECT, MVT::i32, Expand);
402 setOperationAction(ISD::SELECT, MVT::f32, Expand);
403 setOperationAction(ISD::SELECT, MVT::f64, Expand);
404 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
405 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
406 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000407
Owen Anderson825b72b2009-08-11 20:47:22 +0000408 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
409 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
410 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
411 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
412 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000413
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000414 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000415 setOperationAction(ISD::FSIN, MVT::f64, Expand);
416 setOperationAction(ISD::FSIN, MVT::f32, Expand);
417 setOperationAction(ISD::FCOS, MVT::f32, Expand);
418 setOperationAction(ISD::FCOS, MVT::f64, Expand);
419 setOperationAction(ISD::FREM, MVT::f64, Expand);
420 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000421 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000422 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
423 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000424 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000425 setOperationAction(ISD::FPOW, MVT::f64, Expand);
426 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000427
Evan Chenga8e29892007-01-19 07:51:42 +0000428 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
David Goodwinf1daf7d2009-07-08 23:10:31 +0000429 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000430 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
431 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
432 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
433 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000434 }
Evan Chenga8e29892007-01-19 07:51:42 +0000435
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000436 // We have target-specific dag combine patterns for the following nodes:
437 // ARMISD::FMRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000438 setTargetDAGCombine(ISD::ADD);
439 setTargetDAGCombine(ISD::SUB);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000440
Evan Chenga8e29892007-01-19 07:51:42 +0000441 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Chenga8e29892007-01-19 07:51:42 +0000442 setSchedulingPreference(SchedulingForRegPressure);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000443
Evan Chengbc9b7542009-08-15 07:59:10 +0000444 // FIXME: If-converter should use instruction latency to determine
445 // profitability rather than relying on fixed limits.
446 if (Subtarget->getCPUString() == "generic") {
447 // Generic (and overly aggressive) if-conversion limits.
448 setIfCvtBlockSizeLimit(10);
449 setIfCvtDupBlockSizeLimit(2);
450 } else if (Subtarget->hasV6Ops()) {
451 setIfCvtBlockSizeLimit(2);
452 setIfCvtDupBlockSizeLimit(1);
453 } else {
454 setIfCvtBlockSizeLimit(3);
455 setIfCvtDupBlockSizeLimit(2);
Evan Cheng8557c2b2009-06-19 01:51:50 +0000456 }
457
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000458 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Bob Wilsone6abdff2009-05-18 20:55:32 +0000459 // Do not enable CodePlacementOpt for now: it currently runs after the
460 // ARMConstantIslandPass and messes up branch relaxation and placement
461 // of constant islands.
462 // benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000463}
464
Evan Chenga8e29892007-01-19 07:51:42 +0000465const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
466 switch (Opcode) {
467 default: return 0;
468 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000469 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
470 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000471 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000472 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
473 case ARMISD::tCALL: return "ARMISD::tCALL";
474 case ARMISD::BRCOND: return "ARMISD::BRCOND";
475 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000476 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000477 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
478 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
479 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000480 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000481 case ARMISD::CMPFP: return "ARMISD::CMPFP";
482 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
483 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
484 case ARMISD::CMOV: return "ARMISD::CMOV";
485 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000486
Evan Chenga8e29892007-01-19 07:51:42 +0000487 case ARMISD::FTOSI: return "ARMISD::FTOSI";
488 case ARMISD::FTOUI: return "ARMISD::FTOUI";
489 case ARMISD::SITOF: return "ARMISD::SITOF";
490 case ARMISD::UITOF: return "ARMISD::UITOF";
Evan Chenga8e29892007-01-19 07:51:42 +0000491
492 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
493 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
494 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000495
Evan Chenga8e29892007-01-19 07:51:42 +0000496 case ARMISD::FMRRD: return "ARMISD::FMRRD";
497 case ARMISD::FMDRR: return "ARMISD::FMDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000498
499 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000500
Evan Cheng86198642009-08-07 00:34:42 +0000501 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
502
Bob Wilson5bafff32009-06-22 23:27:02 +0000503 case ARMISD::VCEQ: return "ARMISD::VCEQ";
504 case ARMISD::VCGE: return "ARMISD::VCGE";
505 case ARMISD::VCGEU: return "ARMISD::VCGEU";
506 case ARMISD::VCGT: return "ARMISD::VCGT";
507 case ARMISD::VCGTU: return "ARMISD::VCGTU";
508 case ARMISD::VTST: return "ARMISD::VTST";
509
510 case ARMISD::VSHL: return "ARMISD::VSHL";
511 case ARMISD::VSHRs: return "ARMISD::VSHRs";
512 case ARMISD::VSHRu: return "ARMISD::VSHRu";
513 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
514 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
515 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
516 case ARMISD::VSHRN: return "ARMISD::VSHRN";
517 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
518 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
519 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
520 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
521 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
522 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
523 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
524 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
525 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
526 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
527 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
528 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
529 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
530 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000531 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000532 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000533 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000534 case ARMISD::VREV64: return "ARMISD::VREV64";
535 case ARMISD::VREV32: return "ARMISD::VREV32";
536 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000537 case ARMISD::VZIP: return "ARMISD::VZIP";
538 case ARMISD::VUZP: return "ARMISD::VUZP";
539 case ARMISD::VTRN: return "ARMISD::VTRN";
Evan Chenga8e29892007-01-19 07:51:42 +0000540 }
541}
542
Bill Wendlingb4202b82009-07-01 18:50:55 +0000543/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000544unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Evan Cheng048e36f2009-10-02 06:57:25 +0000545 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 0 : 1;
Bill Wendling20c568f2009-06-30 22:38:32 +0000546}
547
Evan Chenga8e29892007-01-19 07:51:42 +0000548//===----------------------------------------------------------------------===//
549// Lowering Code
550//===----------------------------------------------------------------------===//
551
Evan Chenga8e29892007-01-19 07:51:42 +0000552/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
553static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
554 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000555 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000556 case ISD::SETNE: return ARMCC::NE;
557 case ISD::SETEQ: return ARMCC::EQ;
558 case ISD::SETGT: return ARMCC::GT;
559 case ISD::SETGE: return ARMCC::GE;
560 case ISD::SETLT: return ARMCC::LT;
561 case ISD::SETLE: return ARMCC::LE;
562 case ISD::SETUGT: return ARMCC::HI;
563 case ISD::SETUGE: return ARMCC::HS;
564 case ISD::SETULT: return ARMCC::LO;
565 case ISD::SETULE: return ARMCC::LS;
566 }
567}
568
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000569/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
570static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000571 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000572 CondCode2 = ARMCC::AL;
573 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000574 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000575 case ISD::SETEQ:
576 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
577 case ISD::SETGT:
578 case ISD::SETOGT: CondCode = ARMCC::GT; break;
579 case ISD::SETGE:
580 case ISD::SETOGE: CondCode = ARMCC::GE; break;
581 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000582 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000583 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
584 case ISD::SETO: CondCode = ARMCC::VC; break;
585 case ISD::SETUO: CondCode = ARMCC::VS; break;
586 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
587 case ISD::SETUGT: CondCode = ARMCC::HI; break;
588 case ISD::SETUGE: CondCode = ARMCC::PL; break;
589 case ISD::SETLT:
590 case ISD::SETULT: CondCode = ARMCC::LT; break;
591 case ISD::SETLE:
592 case ISD::SETULE: CondCode = ARMCC::LE; break;
593 case ISD::SETNE:
594 case ISD::SETUNE: CondCode = ARMCC::NE; break;
595 }
Evan Chenga8e29892007-01-19 07:51:42 +0000596}
597
Bob Wilson1f595bb2009-04-17 19:07:39 +0000598//===----------------------------------------------------------------------===//
599// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +0000600//===----------------------------------------------------------------------===//
601
602#include "ARMGenCallingConv.inc"
603
604// APCS f64 is in register pairs, possibly split to stack
Owen Andersone50ed302009-08-10 22:56:29 +0000605static bool f64AssignAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000606 CCValAssign::LocInfo &LocInfo,
607 CCState &State, bool CanFail) {
608 static const unsigned RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
609
610 // Try to get the first register.
611 if (unsigned Reg = State.AllocateReg(RegList, 4))
612 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
613 else {
614 // For the 2nd half of a v2f64, do not fail.
615 if (CanFail)
616 return false;
617
618 // Put the whole thing on the stack.
619 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
620 State.AllocateStack(8, 4),
621 LocVT, LocInfo));
622 return true;
623 }
624
625 // Try to get the second register.
626 if (unsigned Reg = State.AllocateReg(RegList, 4))
627 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
628 else
629 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
630 State.AllocateStack(4, 4),
631 LocVT, LocInfo));
632 return true;
633}
634
Owen Andersone50ed302009-08-10 22:56:29 +0000635static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000636 CCValAssign::LocInfo &LocInfo,
637 ISD::ArgFlagsTy &ArgFlags,
638 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000639 if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
640 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000641 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000642 !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
643 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000644 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000645}
646
647// AAPCS f64 is in aligned register pairs
Owen Andersone50ed302009-08-10 22:56:29 +0000648static bool f64AssignAAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000649 CCValAssign::LocInfo &LocInfo,
650 CCState &State, bool CanFail) {
651 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
652 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
653
654 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
655 if (Reg == 0) {
656 // For the 2nd half of a v2f64, do not just fail.
657 if (CanFail)
658 return false;
659
660 // Put the whole thing on the stack.
661 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
662 State.AllocateStack(8, 8),
663 LocVT, LocInfo));
664 return true;
665 }
666
667 unsigned i;
668 for (i = 0; i < 2; ++i)
669 if (HiRegList[i] == Reg)
670 break;
671
672 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
673 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
674 LocVT, LocInfo));
675 return true;
676}
677
Owen Andersone50ed302009-08-10 22:56:29 +0000678static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000679 CCValAssign::LocInfo &LocInfo,
680 ISD::ArgFlagsTy &ArgFlags,
681 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000682 if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
683 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000684 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000685 !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
686 return false;
687 return true; // we handled it
688}
689
Owen Andersone50ed302009-08-10 22:56:29 +0000690static bool f64RetAssign(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000691 CCValAssign::LocInfo &LocInfo, CCState &State) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000692 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
693 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
694
Bob Wilsone65586b2009-04-17 20:40:45 +0000695 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
696 if (Reg == 0)
697 return false; // we didn't handle it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000698
Bob Wilsone65586b2009-04-17 20:40:45 +0000699 unsigned i;
700 for (i = 0; i < 2; ++i)
701 if (HiRegList[i] == Reg)
702 break;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000703
Bob Wilson5bafff32009-06-22 23:27:02 +0000704 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bob Wilsone65586b2009-04-17 20:40:45 +0000705 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000706 LocVT, LocInfo));
707 return true;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000708}
709
Owen Andersone50ed302009-08-10 22:56:29 +0000710static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000711 CCValAssign::LocInfo &LocInfo,
712 ISD::ArgFlagsTy &ArgFlags,
713 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000714 if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
715 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000716 if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
Bob Wilson5bafff32009-06-22 23:27:02 +0000717 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000718 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000719}
720
Owen Andersone50ed302009-08-10 22:56:29 +0000721static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000722 CCValAssign::LocInfo &LocInfo,
723 ISD::ArgFlagsTy &ArgFlags,
724 CCState &State) {
725 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
726 State);
727}
728
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000729/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
730/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000731CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000732 bool Return,
733 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000734 switch (CC) {
735 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000736 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000737 case CallingConv::C:
738 case CallingConv::Fast:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000739 // Use target triple & subtarget features to do actual dispatch.
740 if (Subtarget->isAAPCS_ABI()) {
741 if (Subtarget->hasVFP2() &&
742 FloatABIType == FloatABI::Hard && !isVarArg)
743 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
744 else
745 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
746 } else
747 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000748 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000749 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000750 case CallingConv::ARM_AAPCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000751 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000752 case CallingConv::ARM_APCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000753 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000754 }
755}
756
Dan Gohman98ca4f22009-08-05 01:29:28 +0000757/// LowerCallResult - Lower the result values of a call into the
758/// appropriate copies out of appropriate physical registers.
759SDValue
760ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000761 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000762 const SmallVectorImpl<ISD::InputArg> &Ins,
763 DebugLoc dl, SelectionDAG &DAG,
764 SmallVectorImpl<SDValue> &InVals) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000765
Bob Wilson1f595bb2009-04-17 19:07:39 +0000766 // Assign locations to each value returned by this call.
767 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000768 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000769 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000770 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000771 CCAssignFnForNode(CallConv, /* Return*/ true,
772 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000773
774 // Copy all of the result registers out of their specified physreg.
775 for (unsigned i = 0; i != RVLocs.size(); ++i) {
776 CCValAssign VA = RVLocs[i];
777
Bob Wilson80915242009-04-25 00:33:20 +0000778 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000779 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000780 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000781 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000782 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000783 Chain = Lo.getValue(1);
784 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000785 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000786 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000787 InFlag);
788 Chain = Hi.getValue(1);
789 InFlag = Hi.getValue(2);
Owen Anderson825b72b2009-08-11 20:47:22 +0000790 Val = DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +0000791
Owen Anderson825b72b2009-08-11 20:47:22 +0000792 if (VA.getLocVT() == MVT::v2f64) {
793 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
794 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
795 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000796
797 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000798 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000799 Chain = Lo.getValue(1);
800 InFlag = Lo.getValue(2);
801 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000802 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000803 Chain = Hi.getValue(1);
804 InFlag = Hi.getValue(2);
Owen Anderson825b72b2009-08-11 20:47:22 +0000805 Val = DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
806 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
807 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000808 }
Bob Wilson1f595bb2009-04-17 19:07:39 +0000809 } else {
Bob Wilson80915242009-04-25 00:33:20 +0000810 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
811 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000812 Chain = Val.getValue(1);
813 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000814 }
Bob Wilson80915242009-04-25 00:33:20 +0000815
816 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000817 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +0000818 case CCValAssign::Full: break;
819 case CCValAssign::BCvt:
820 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
821 break;
822 }
823
Dan Gohman98ca4f22009-08-05 01:29:28 +0000824 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000825 }
826
Dan Gohman98ca4f22009-08-05 01:29:28 +0000827 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000828}
829
830/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
831/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +0000832/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +0000833/// a byval function parameter.
834/// Sometimes what we are copying is the end of a larger object, the part that
835/// does not fit in registers.
836static SDValue
837CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
838 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
839 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000840 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000841 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
842 /*AlwaysInline=*/false, NULL, 0, NULL, 0);
843}
844
Bob Wilsondee46d72009-04-17 20:35:10 +0000845/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000846SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +0000847ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
848 SDValue StackPtr, SDValue Arg,
849 DebugLoc dl, SelectionDAG &DAG,
850 const CCValAssign &VA,
851 ISD::ArgFlagsTy Flags) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000852 unsigned LocMemOffset = VA.getLocMemOffset();
853 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
854 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
855 if (Flags.isByVal()) {
856 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
857 }
858 return DAG.getStore(Chain, dl, Arg, PtrOff,
859 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chenga8e29892007-01-19 07:51:42 +0000860}
861
Dan Gohman98ca4f22009-08-05 01:29:28 +0000862void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +0000863 SDValue Chain, SDValue &Arg,
864 RegsToPassVector &RegsToPass,
865 CCValAssign &VA, CCValAssign &NextVA,
866 SDValue &StackPtr,
867 SmallVector<SDValue, 8> &MemOpChains,
868 ISD::ArgFlagsTy Flags) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000869
870 SDValue fmrrd = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +0000871 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +0000872 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
873
874 if (NextVA.isRegLoc())
875 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
876 else {
877 assert(NextVA.isMemLoc());
878 if (StackPtr.getNode() == 0)
879 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
880
Dan Gohman98ca4f22009-08-05 01:29:28 +0000881 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
882 dl, DAG, NextVA,
883 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000884 }
885}
886
Dan Gohman98ca4f22009-08-05 01:29:28 +0000887/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +0000888/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
889/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000890SDValue
891ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000892 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000893 bool isTailCall,
894 const SmallVectorImpl<ISD::OutputArg> &Outs,
895 const SmallVectorImpl<ISD::InputArg> &Ins,
896 DebugLoc dl, SelectionDAG &DAG,
897 SmallVectorImpl<SDValue> &InVals) {
Evan Chenga8e29892007-01-19 07:51:42 +0000898
Bob Wilson1f595bb2009-04-17 19:07:39 +0000899 // Analyze operands of the call, assigning locations to each operand.
900 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000901 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
902 *DAG.getContext());
903 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000904 CCAssignFnForNode(CallConv, /* Return*/ false,
905 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +0000906
Bob Wilson1f595bb2009-04-17 19:07:39 +0000907 // Get a count of how many bytes are to be pushed on the stack.
908 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +0000909
910 // Adjust the stack pointer for the new arguments...
911 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattnere563bbc2008-10-11 22:08:30 +0000912 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +0000913
Owen Anderson825b72b2009-08-11 20:47:22 +0000914 SDValue StackPtr = DAG.getRegister(ARM::SP, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000915
Bob Wilson5bafff32009-06-22 23:27:02 +0000916 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000917 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +0000918
Bob Wilson1f595bb2009-04-17 19:07:39 +0000919 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +0000920 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000921 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
922 i != e;
923 ++i, ++realArgIdx) {
924 CCValAssign &VA = ArgLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +0000925 SDValue Arg = Outs[realArgIdx].Val;
926 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +0000927
Bob Wilson1f595bb2009-04-17 19:07:39 +0000928 // Promote the value if needed.
929 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000930 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +0000931 case CCValAssign::Full: break;
932 case CCValAssign::SExt:
933 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
934 break;
935 case CCValAssign::ZExt:
936 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
937 break;
938 case CCValAssign::AExt:
939 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
940 break;
941 case CCValAssign::BCvt:
942 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
943 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000944 }
945
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000946 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +0000947 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000948 if (VA.getLocVT() == MVT::v2f64) {
949 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
950 DAG.getConstant(0, MVT::i32));
951 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
952 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000953
Dan Gohman98ca4f22009-08-05 01:29:28 +0000954 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +0000955 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
956
957 VA = ArgLocs[++i]; // skip ahead to next loc
958 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000959 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +0000960 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
961 } else {
962 assert(VA.isMemLoc());
963 if (StackPtr.getNode() == 0)
964 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
965
Dan Gohman98ca4f22009-08-05 01:29:28 +0000966 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
967 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000968 }
969 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000970 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000971 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000972 }
973 } else if (VA.isRegLoc()) {
974 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
975 } else {
976 assert(VA.isMemLoc());
977 if (StackPtr.getNode() == 0)
978 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
979
Dan Gohman98ca4f22009-08-05 01:29:28 +0000980 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
981 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000982 }
Evan Chenga8e29892007-01-19 07:51:42 +0000983 }
984
985 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000986 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +0000987 &MemOpChains[0], MemOpChains.size());
988
989 // Build a sequence of copy-to-reg nodes chained together with token chain
990 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +0000991 SDValue InFlag;
Evan Chenga8e29892007-01-19 07:51:42 +0000992 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Bob Wilson2dc4f542009-03-20 22:42:55 +0000993 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000994 RegsToPass[i].second, InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +0000995 InFlag = Chain.getValue(1);
996 }
997
Bill Wendling056292f2008-09-16 21:48:12 +0000998 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
999 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1000 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001001 bool isDirect = false;
1002 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001003 bool isLocalARMFunc = false;
Evan Chenga8e29892007-01-19 07:51:42 +00001004 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1005 GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001006 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001007 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001008 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001009 getTargetMachine().getRelocationModel() != Reloc::Static;
1010 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001011 // ARM call to a local ARM function is predicable.
1012 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
Evan Chengc60e76d2007-01-30 20:37:08 +00001013 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001014 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge4e4ed32009-08-28 23:18:09 +00001015 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001016 ARMPCLabelIndex,
1017 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001018 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001019 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001020 Callee = DAG.getLoad(getPointerTy(), dl,
1021 DAG.getEntryNode(), CPAddr, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001022 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001023 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001024 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001025 } else
1026 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +00001027 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001028 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001029 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001030 getTargetMachine().getRelocationModel() != Reloc::Static;
1031 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001032 // tBX takes a register source operand.
1033 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001034 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Owen Anderson1d0be152009-08-13 21:58:54 +00001035 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001036 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001037 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001038 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001039 Callee = DAG.getLoad(getPointerTy(), dl,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001040 DAG.getEntryNode(), CPAddr, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001041 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001042 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001043 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001044 } else
Bill Wendling056292f2008-09-16 21:48:12 +00001045 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001046 }
1047
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001048 // FIXME: handle tail calls differently.
1049 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001050 if (Subtarget->isThumb()) {
1051 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001052 CallOpc = ARMISD::CALL_NOLINK;
1053 else
1054 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1055 } else {
1056 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001057 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1058 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001059 }
David Goodwinf1daf7d2009-07-08 23:10:31 +00001060 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb1Only()) {
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +00001061 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Owen Anderson825b72b2009-08-11 20:47:22 +00001062 Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001063 InFlag = Chain.getValue(1);
1064 }
1065
Dan Gohman475871a2008-07-27 21:46:04 +00001066 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001067 Ops.push_back(Chain);
1068 Ops.push_back(Callee);
1069
1070 // Add argument registers to the end of the list so that they are known live
1071 // into the call.
1072 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1073 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1074 RegsToPass[i].second.getValueType()));
1075
Gabor Greifba36cb52008-08-28 21:40:38 +00001076 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001077 Ops.push_back(InFlag);
Duncan Sands4bdcb612008-07-02 17:40:58 +00001078 // Returns a chain and a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00001079 Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Flag),
Duncan Sands4bdcb612008-07-02 17:40:58 +00001080 &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001081 InFlag = Chain.getValue(1);
1082
Chris Lattnere563bbc2008-10-11 22:08:30 +00001083 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1084 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001085 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001086 InFlag = Chain.getValue(1);
1087
Bob Wilson1f595bb2009-04-17 19:07:39 +00001088 // Handle result values, copying them out of physregs into vregs that we
1089 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001090 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1091 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001092}
1093
Dan Gohman98ca4f22009-08-05 01:29:28 +00001094SDValue
1095ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001096 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001097 const SmallVectorImpl<ISD::OutputArg> &Outs,
1098 DebugLoc dl, SelectionDAG &DAG) {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001099
Bob Wilsondee46d72009-04-17 20:35:10 +00001100 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001101 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001102
Bob Wilsondee46d72009-04-17 20:35:10 +00001103 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001104 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1105 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001106
Dan Gohman98ca4f22009-08-05 01:29:28 +00001107 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001108 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1109 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001110
1111 // If this is the first return lowered for this function, add
1112 // the regs to the liveout set for the function.
1113 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1114 for (unsigned i = 0; i != RVLocs.size(); ++i)
1115 if (RVLocs[i].isRegLoc())
1116 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001117 }
1118
Bob Wilson1f595bb2009-04-17 19:07:39 +00001119 SDValue Flag;
1120
1121 // Copy the result values into the output registers.
1122 for (unsigned i = 0, realRVLocIdx = 0;
1123 i != RVLocs.size();
1124 ++i, ++realRVLocIdx) {
1125 CCValAssign &VA = RVLocs[i];
1126 assert(VA.isRegLoc() && "Can only return in registers!");
1127
Dan Gohman98ca4f22009-08-05 01:29:28 +00001128 SDValue Arg = Outs[realRVLocIdx].Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001129
1130 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001131 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001132 case CCValAssign::Full: break;
1133 case CCValAssign::BCvt:
1134 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1135 break;
1136 }
1137
Bob Wilson1f595bb2009-04-17 19:07:39 +00001138 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001139 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001140 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001141 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1142 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001143 SDValue HalfGPRs = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001144 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001145
1146 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1147 Flag = Chain.getValue(1);
1148 VA = RVLocs[++i]; // skip ahead to next loc
1149 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1150 HalfGPRs.getValue(1), Flag);
1151 Flag = Chain.getValue(1);
1152 VA = RVLocs[++i]; // skip ahead to next loc
1153
1154 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001155 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1156 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001157 }
1158 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1159 // available.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001160 SDValue fmrrd = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001161 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001162 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001163 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001164 VA = RVLocs[++i]; // skip ahead to next loc
1165 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1166 Flag);
1167 } else
1168 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1169
Bob Wilsondee46d72009-04-17 20:35:10 +00001170 // Guarantee that all emitted copies are
1171 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001172 Flag = Chain.getValue(1);
1173 }
1174
1175 SDValue result;
1176 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001177 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001178 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001179 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001180
1181 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001182}
1183
Bob Wilson2dc4f542009-03-20 22:42:55 +00001184// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
Bob Wilsond2559bf2009-07-13 18:11:36 +00001185// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
Bill Wendling056292f2008-09-16 21:48:12 +00001186// one of the above mentioned nodes. It has to be wrapped because otherwise
1187// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1188// be used to form addressing mode. These wrapped nodes will be selected
1189// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001190static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001191 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001192 // FIXME there is no actual debug info here
1193 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001194 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001195 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001196 if (CP->isMachineConstantPoolEntry())
1197 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1198 CP->getAlignment());
1199 else
1200 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1201 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001202 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001203}
1204
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001205// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001206SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001207ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
1208 SelectionDAG &DAG) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001209 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001210 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001211 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1212 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001213 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001214 ARMCP::CPValue, PCAdj, "tlsgd", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001215 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001216 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001217 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001218 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001219
Owen Anderson825b72b2009-08-11 20:47:22 +00001220 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001221 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001222
1223 // call __tls_get_addr.
1224 ArgListTy Args;
1225 ArgListEntry Entry;
1226 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001227 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001228 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001229 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001230 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001231 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1232 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001233 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001234 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001235 return CallResult.first;
1236}
1237
1238// Lower ISD::GlobalTLSAddress using the "initial exec" or
1239// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001240SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001241ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001242 SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001243 GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001244 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001245 SDValue Offset;
1246 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001247 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001248 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001249 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001250
Chris Lattner4fb63d02009-07-15 04:12:33 +00001251 if (GV->isDeclaration()) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001252 // initial exec model
1253 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1254 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001255 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001256 ARMCP::CPValue, PCAdj, "gottpoff", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001257 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001258 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001259 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001260 Chain = Offset.getValue(1);
1261
Owen Anderson825b72b2009-08-11 20:47:22 +00001262 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001263 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001264
Dale Johannesen33c960f2009-02-04 20:06:27 +00001265 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001266 } else {
1267 // local exec model
Evan Chenge4e4ed32009-08-28 23:18:09 +00001268 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, "tpoff");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001269 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001270 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001271 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001272 }
1273
1274 // The address of the thread local variable is the add of the thread
1275 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001276 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001277}
1278
Dan Gohman475871a2008-07-27 21:46:04 +00001279SDValue
1280ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001281 // TODO: implement the "local dynamic" model
1282 assert(Subtarget->isTargetELF() &&
1283 "TLS not implemented for non-ELF targets");
1284 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1285 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1286 // otherwise use the "Local Exec" TLS Model
1287 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1288 return LowerToTLSGeneralDynamicModel(GA, DAG);
1289 else
1290 return LowerToTLSExecModels(GA, DAG);
1291}
1292
Dan Gohman475871a2008-07-27 21:46:04 +00001293SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001294 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001295 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001296 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001297 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1298 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1299 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001300 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001301 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001302 new ARMConstantPoolValue(GV, UseGOTOFF ? "GOTOFF" : "GOT");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001303 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001304 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001305 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001306 CPAddr,
1307 PseudoSourceValue::getConstantPool(), 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001308 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001309 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001310 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001311 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001312 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
1313 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001314 return Result;
1315 } else {
Evan Cheng1606e8e2009-03-13 07:51:59 +00001316 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001317 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001318 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1319 PseudoSourceValue::getConstantPool(), 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001320 }
1321}
1322
Dan Gohman475871a2008-07-27 21:46:04 +00001323SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001324 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001325 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001326 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001327 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1328 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Dan Gohman475871a2008-07-27 21:46:04 +00001329 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +00001330 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001331 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001332 else {
Evan Chenge4e4ed32009-08-28 23:18:09 +00001333 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
1334 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001335 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001336 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001337 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001338 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001339
Dale Johannesen33c960f2009-02-04 20:06:27 +00001340 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001341 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001342
1343 if (RelocM == Reloc::PIC_) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001344 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001345 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001346 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00001347
Evan Cheng63476a82009-09-03 07:04:02 +00001348 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Dale Johannesen33c960f2009-02-04 20:06:27 +00001349 Result = DAG.getLoad(PtrVT, dl, Chain, Result, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001350
1351 return Result;
1352}
1353
Dan Gohman475871a2008-07-27 21:46:04 +00001354SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001355 SelectionDAG &DAG){
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001356 assert(Subtarget->isTargetELF() &&
1357 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Owen Andersone50ed302009-08-10 22:56:29 +00001358 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001359 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001360 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00001361 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1362 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00001363 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001364 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001365 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001366 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1367 PseudoSourceValue::getConstantPool(), 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001368 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001369 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001370}
1371
Bob Wilsona599bff2009-08-04 00:36:16 +00001372static SDValue LowerNeonVLDIntrinsic(SDValue Op, SelectionDAG &DAG,
Bob Wilson31fb12f2009-08-26 17:39:53 +00001373 unsigned NumVecs) {
Bob Wilsona599bff2009-08-04 00:36:16 +00001374 SDNode *Node = Op.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001375 EVT VT = Node->getValueType(0);
Bob Wilsona599bff2009-08-04 00:36:16 +00001376
Bob Wilson31fb12f2009-08-26 17:39:53 +00001377 // No expansion needed for 64-bit vectors.
1378 if (VT.is64BitVector())
1379 return SDValue();
Bob Wilsona599bff2009-08-04 00:36:16 +00001380
Bob Wilson31fb12f2009-08-26 17:39:53 +00001381 // FIXME: We need to expand VLD3 and VLD4 of 128-bit vectors into separate
1382 // operations to load the even and odd registers.
1383 return SDValue();
Bob Wilsona599bff2009-08-04 00:36:16 +00001384}
1385
Bob Wilsonb36ec862009-08-06 18:47:44 +00001386static SDValue LowerNeonVSTIntrinsic(SDValue Op, SelectionDAG &DAG,
Bob Wilson31fb12f2009-08-26 17:39:53 +00001387 unsigned NumVecs) {
Bob Wilsonb36ec862009-08-06 18:47:44 +00001388 SDNode *Node = Op.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001389 EVT VT = Node->getOperand(3).getValueType();
Bob Wilsonb36ec862009-08-06 18:47:44 +00001390
Bob Wilson31fb12f2009-08-26 17:39:53 +00001391 // No expansion needed for 64-bit vectors.
1392 if (VT.is64BitVector())
1393 return SDValue();
Bob Wilsonb36ec862009-08-06 18:47:44 +00001394
Bob Wilson31fb12f2009-08-26 17:39:53 +00001395 // FIXME: We need to expand VST3 and VST4 of 128-bit vectors into separate
1396 // operations to store the even and odd registers.
1397 return SDValue();
Bob Wilsonb36ec862009-08-06 18:47:44 +00001398}
1399
Bob Wilson243fcc52009-09-01 04:26:28 +00001400static SDValue LowerNeonVLDLaneIntrinsic(SDValue Op, SelectionDAG &DAG,
1401 unsigned NumVecs) {
1402 SDNode *Node = Op.getNode();
1403 EVT VT = Node->getValueType(0);
1404
1405 if (!VT.is64BitVector())
1406 return SDValue(); // unimplemented
1407
1408 // Change the lane number operand to be a TargetConstant; otherwise it
1409 // will be legalized into a register.
1410 ConstantSDNode *Lane = dyn_cast<ConstantSDNode>(Node->getOperand(NumVecs+3));
1411 if (!Lane) {
1412 assert(false && "vld lane number must be a constant");
1413 return SDValue();
1414 }
1415 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
1416 Ops[NumVecs+3] = DAG.getTargetConstant(Lane->getZExtValue(), MVT::i32);
1417 return DAG.UpdateNodeOperands(Op, &Ops[0], Ops.size());
1418}
1419
Bob Wilson8a3198b2009-09-01 18:51:56 +00001420static SDValue LowerNeonVSTLaneIntrinsic(SDValue Op, SelectionDAG &DAG,
1421 unsigned NumVecs) {
1422 SDNode *Node = Op.getNode();
1423 EVT VT = Node->getOperand(3).getValueType();
1424
1425 if (!VT.is64BitVector())
1426 return SDValue(); // unimplemented
1427
1428 // Change the lane number operand to be a TargetConstant; otherwise it
1429 // will be legalized into a register.
1430 ConstantSDNode *Lane = dyn_cast<ConstantSDNode>(Node->getOperand(NumVecs+3));
1431 if (!Lane) {
1432 assert(false && "vst lane number must be a constant");
1433 return SDValue();
1434 }
1435 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
1436 Ops[NumVecs+3] = DAG.getTargetConstant(Lane->getZExtValue(), MVT::i32);
1437 return DAG.UpdateNodeOperands(Op, &Ops[0], Ops.size());
1438}
1439
Bob Wilsona599bff2009-08-04 00:36:16 +00001440SDValue
1441ARMTargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) {
1442 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1443 switch (IntNo) {
Bob Wilsonb0abb4d2009-08-11 05:39:44 +00001444 case Intrinsic::arm_neon_vld3:
Bob Wilson31fb12f2009-08-26 17:39:53 +00001445 return LowerNeonVLDIntrinsic(Op, DAG, 3);
Bob Wilsonb0abb4d2009-08-11 05:39:44 +00001446 case Intrinsic::arm_neon_vld4:
Bob Wilson31fb12f2009-08-26 17:39:53 +00001447 return LowerNeonVLDIntrinsic(Op, DAG, 4);
Bob Wilson243fcc52009-09-01 04:26:28 +00001448 case Intrinsic::arm_neon_vld2lane:
1449 return LowerNeonVLDLaneIntrinsic(Op, DAG, 2);
1450 case Intrinsic::arm_neon_vld3lane:
1451 return LowerNeonVLDLaneIntrinsic(Op, DAG, 3);
1452 case Intrinsic::arm_neon_vld4lane:
1453 return LowerNeonVLDLaneIntrinsic(Op, DAG, 4);
Bob Wilsonb0abb4d2009-08-11 05:39:44 +00001454 case Intrinsic::arm_neon_vst3:
Bob Wilson31fb12f2009-08-26 17:39:53 +00001455 return LowerNeonVSTIntrinsic(Op, DAG, 3);
Bob Wilsonb0abb4d2009-08-11 05:39:44 +00001456 case Intrinsic::arm_neon_vst4:
Bob Wilson31fb12f2009-08-26 17:39:53 +00001457 return LowerNeonVSTIntrinsic(Op, DAG, 4);
Bob Wilson8a3198b2009-09-01 18:51:56 +00001458 case Intrinsic::arm_neon_vst2lane:
1459 return LowerNeonVSTLaneIntrinsic(Op, DAG, 2);
1460 case Intrinsic::arm_neon_vst3lane:
1461 return LowerNeonVSTLaneIntrinsic(Op, DAG, 3);
1462 case Intrinsic::arm_neon_vst4lane:
1463 return LowerNeonVSTLaneIntrinsic(Op, DAG, 4);
Bob Wilsona599bff2009-08-04 00:36:16 +00001464 default: return SDValue(); // Don't custom lower most intrinsics.
1465 }
1466}
1467
Jim Grosbach0e0da732009-05-12 23:59:14 +00001468SDValue
1469ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001470 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001471 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001472 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001473 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00001474 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00001475 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00001476 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1477 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001478 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001479 MachineFunction &MF = DAG.getMachineFunction();
1480 EVT PtrVT = getPointerTy();
1481 DebugLoc dl = Op.getDebugLoc();
1482 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1483 SDValue CPAddr;
1484 unsigned PCAdj = (RelocM != Reloc::PIC_)
1485 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001486 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001487 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
1488 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001489 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001490 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001491 SDValue Result =
1492 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
1493 SDValue Chain = Result.getValue(1);
1494
1495 if (RelocM == Reloc::PIC_) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001496 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001497 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1498 }
1499 return Result;
1500 }
Jim Grosbachf9570122009-05-14 00:46:35 +00001501 case Intrinsic::eh_sjlj_setjmp:
Owen Anderson825b72b2009-08-11 20:47:22 +00001502 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(1));
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001503 }
1504}
1505
Dan Gohman475871a2008-07-27 21:46:04 +00001506static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001507 unsigned VarArgsFrameIndex) {
Evan Chenga8e29892007-01-19 07:51:42 +00001508 // vastart just stores the address of the VarArgsFrameIndex slot into the
1509 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001510 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001511 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001512 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001513 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001514 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001515}
1516
Dan Gohman475871a2008-07-27 21:46:04 +00001517SDValue
Evan Cheng86198642009-08-07 00:34:42 +00001518ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) {
1519 SDNode *Node = Op.getNode();
1520 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001521 EVT VT = Node->getValueType(0);
Evan Cheng86198642009-08-07 00:34:42 +00001522 SDValue Chain = Op.getOperand(0);
1523 SDValue Size = Op.getOperand(1);
1524 SDValue Align = Op.getOperand(2);
1525
1526 // Chain the dynamic stack allocation so that it doesn't modify the stack
1527 // pointer when other instructions are using the stack.
1528 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1529
1530 unsigned AlignVal = cast<ConstantSDNode>(Align)->getZExtValue();
1531 unsigned StackAlign = getTargetMachine().getFrameInfo()->getStackAlignment();
1532 if (AlignVal > StackAlign)
1533 // Do this now since selection pass cannot introduce new target
1534 // independent node.
1535 Align = DAG.getConstant(-(uint64_t)AlignVal, VT);
1536
1537 // In Thumb1 mode, there isn't a "sub r, sp, r" instruction, we will end up
1538 // using a "add r, sp, r" instead. Negate the size now so we don't have to
1539 // do even more horrible hack later.
1540 MachineFunction &MF = DAG.getMachineFunction();
1541 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1542 if (AFI->isThumb1OnlyFunction()) {
1543 bool Negate = true;
1544 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Size);
1545 if (C) {
1546 uint32_t Val = C->getZExtValue();
1547 if (Val <= 508 && ((Val & 3) == 0))
1548 Negate = false;
1549 }
1550 if (Negate)
1551 Size = DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(0, VT), Size);
1552 }
1553
Owen Anderson825b72b2009-08-11 20:47:22 +00001554 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
Evan Cheng86198642009-08-07 00:34:42 +00001555 SDValue Ops1[] = { Chain, Size, Align };
1556 SDValue Res = DAG.getNode(ARMISD::DYN_ALLOC, dl, VTList, Ops1, 3);
1557 Chain = Res.getValue(1);
1558 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1559 DAG.getIntPtrConstant(0, true), SDValue());
1560 SDValue Ops2[] = { Res, Chain };
1561 return DAG.getMergeValues(Ops2, 2, dl);
1562}
1563
1564SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00001565ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
1566 SDValue &Root, SelectionDAG &DAG,
1567 DebugLoc dl) {
1568 MachineFunction &MF = DAG.getMachineFunction();
1569 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1570
1571 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001572 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00001573 RC = ARM::tGPRRegisterClass;
1574 else
1575 RC = ARM::GPRRegisterClass;
1576
1577 // Transform the arguments stored in physical registers into virtual ones.
1578 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001579 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001580
1581 SDValue ArgValue2;
1582 if (NextVA.isMemLoc()) {
1583 unsigned ArgSize = NextVA.getLocVT().getSizeInBits()/8;
1584 MachineFrameInfo *MFI = MF.getFrameInfo();
1585 int FI = MFI->CreateFixedObject(ArgSize, NextVA.getLocMemOffset());
1586
1587 // Create load node to retrieve arguments from the stack.
1588 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00001589 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN, NULL, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00001590 } else {
1591 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001592 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001593 }
1594
Owen Anderson825b72b2009-08-11 20:47:22 +00001595 return DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00001596}
1597
1598SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001599ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001600 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001601 const SmallVectorImpl<ISD::InputArg>
1602 &Ins,
1603 DebugLoc dl, SelectionDAG &DAG,
1604 SmallVectorImpl<SDValue> &InVals) {
1605
Bob Wilson1f595bb2009-04-17 19:07:39 +00001606 MachineFunction &MF = DAG.getMachineFunction();
1607 MachineFrameInfo *MFI = MF.getFrameInfo();
1608
Bob Wilson1f595bb2009-04-17 19:07:39 +00001609 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1610
1611 // Assign locations to all of the incoming arguments.
1612 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001613 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1614 *DAG.getContext());
1615 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001616 CCAssignFnForNode(CallConv, /* Return*/ false,
1617 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001618
1619 SmallVector<SDValue, 16> ArgValues;
1620
1621 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1622 CCValAssign &VA = ArgLocs[i];
1623
Bob Wilsondee46d72009-04-17 20:35:10 +00001624 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001625 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001626 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00001627
Bob Wilson5bafff32009-06-22 23:27:02 +00001628 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001629 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001630 // f64 and vector types are split up into multiple registers or
1631 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00001632 RegVT = MVT::i32;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001633
Owen Anderson825b72b2009-08-11 20:47:22 +00001634 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001635 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001636 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00001637 VA = ArgLocs[++i]; // skip ahead to next loc
1638 SDValue ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001639 Chain, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001640 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1641 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001642 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00001643 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001644 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
1645 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001646 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001647
Bob Wilson5bafff32009-06-22 23:27:02 +00001648 } else {
1649 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001650
Owen Anderson825b72b2009-08-11 20:47:22 +00001651 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00001652 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001653 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00001654 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001655 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001656 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001657 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001658 RC = (AFI->isThumb1OnlyFunction() ?
1659 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00001660 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001661 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00001662
1663 // Transform the arguments in physical registers into virtual ones.
1664 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001665 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001666 }
1667
1668 // If this is an 8 or 16-bit value, it is really passed promoted
1669 // to 32 bits. Insert an assert[sz]ext to capture this, then
1670 // truncate to the right size.
1671 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001672 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001673 case CCValAssign::Full: break;
1674 case CCValAssign::BCvt:
1675 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1676 break;
1677 case CCValAssign::SExt:
1678 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
1679 DAG.getValueType(VA.getValVT()));
1680 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1681 break;
1682 case CCValAssign::ZExt:
1683 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
1684 DAG.getValueType(VA.getValVT()));
1685 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1686 break;
1687 }
1688
Dan Gohman98ca4f22009-08-05 01:29:28 +00001689 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001690
1691 } else { // VA.isRegLoc()
1692
1693 // sanity check
1694 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00001695 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001696
1697 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
1698 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset());
1699
Bob Wilsondee46d72009-04-17 20:35:10 +00001700 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001701 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001702 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN, NULL, 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001703 }
1704 }
1705
1706 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00001707 if (isVarArg) {
1708 static const unsigned GPRArgRegs[] = {
1709 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1710 };
1711
Bob Wilsondee46d72009-04-17 20:35:10 +00001712 unsigned NumGPRs = CCInfo.getFirstUnallocated
1713 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001714
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001715 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1716 unsigned VARegSize = (4 - NumGPRs) * 4;
1717 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001718 unsigned ArgOffset = 0;
Evan Chenga8e29892007-01-19 07:51:42 +00001719 if (VARegSaveSize) {
1720 // If this function is vararg, store any remaining integer argument regs
1721 // to their spots on the stack so that they may be loaded by deferencing
1722 // the result of va_next.
1723 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001724 ArgOffset = CCInfo.getNextStackOffset();
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001725 VarArgsFrameIndex = MFI->CreateFixedObject(VARegSaveSize, ArgOffset +
1726 VARegSaveSize - VARegSize);
Dan Gohman475871a2008-07-27 21:46:04 +00001727 SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001728
Dan Gohman475871a2008-07-27 21:46:04 +00001729 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00001730 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001731 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001732 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00001733 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001734 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00001735 RC = ARM::GPRRegisterClass;
1736
Bob Wilson998e1252009-04-20 18:36:57 +00001737 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001738 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001739 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001740 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001741 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00001742 DAG.getConstant(4, getPointerTy()));
1743 }
1744 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001745 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001746 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001747 } else
1748 // This will point to the next argument passed via stack.
1749 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1750 }
1751
Dan Gohman98ca4f22009-08-05 01:29:28 +00001752 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00001753}
1754
1755/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00001756static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00001757 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001758 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00001759 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00001760 // Maybe this has already been legalized into the constant pool?
1761 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00001762 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001763 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
1764 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001765 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001766 }
1767 }
1768 return false;
1769}
1770
David Goodwinf1daf7d2009-07-08 23:10:31 +00001771static bool isLegalCmpImmediate(unsigned C, bool isThumb1Only) {
1772 return ( isThumb1Only && (C & ~255U) == 0) ||
1773 (!isThumb1Only && ARM_AM::getSOImmVal(C) != -1);
Evan Chenga8e29892007-01-19 07:51:42 +00001774}
1775
1776/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1777/// the given operands.
Dan Gohman475871a2008-07-27 21:46:04 +00001778static SDValue getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
David Goodwinf1daf7d2009-07-08 23:10:31 +00001779 SDValue &ARMCC, SelectionDAG &DAG, bool isThumb1Only,
Dale Johannesende064702009-02-06 21:50:26 +00001780 DebugLoc dl) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001781 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001782 unsigned C = RHSC->getZExtValue();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001783 if (!isLegalCmpImmediate(C, isThumb1Only)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001784 // Constant does not fit, try adjusting it by one?
1785 switch (CC) {
1786 default: break;
1787 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00001788 case ISD::SETGE:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001789 if (isLegalCmpImmediate(C-1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001790 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001791 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001792 }
1793 break;
1794 case ISD::SETULT:
1795 case ISD::SETUGE:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001796 if (C > 0 && isLegalCmpImmediate(C-1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001797 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001798 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001799 }
1800 break;
1801 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00001802 case ISD::SETGT:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001803 if (isLegalCmpImmediate(C+1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001804 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001805 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001806 }
1807 break;
1808 case ISD::SETULE:
1809 case ISD::SETUGT:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001810 if (C < 0xffffffff && isLegalCmpImmediate(C+1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001811 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001812 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001813 }
1814 break;
1815 }
1816 }
1817 }
1818
1819 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001820 ARMISD::NodeType CompareType;
1821 switch (CondCode) {
1822 default:
1823 CompareType = ARMISD::CMP;
1824 break;
1825 case ARMCC::EQ:
1826 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00001827 // Uses only Z Flag
1828 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001829 break;
1830 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001831 ARMCC = DAG.getConstant(CondCode, MVT::i32);
1832 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001833}
1834
1835/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001836static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Dale Johannesende064702009-02-06 21:50:26 +00001837 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001838 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00001839 if (!isFloatingPointZero(RHS))
Owen Anderson825b72b2009-08-11 20:47:22 +00001840 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001841 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001842 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
1843 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001844}
1845
Dan Gohman475871a2008-07-27 21:46:04 +00001846static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001847 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00001848 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001849 SDValue LHS = Op.getOperand(0);
1850 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001851 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001852 SDValue TrueVal = Op.getOperand(2);
1853 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00001854 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001855
Owen Anderson825b72b2009-08-11 20:47:22 +00001856 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001857 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001858 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
David Goodwinf1daf7d2009-07-08 23:10:31 +00001859 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb1Only(), dl);
Dale Johannesende064702009-02-06 21:50:26 +00001860 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001861 }
1862
1863 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001864 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00001865
Owen Anderson825b72b2009-08-11 20:47:22 +00001866 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1867 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001868 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
1869 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00001870 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001871 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001872 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001873 // FIXME: Needs another CMP because flag can have but one use.
Dale Johannesende064702009-02-06 21:50:26 +00001874 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001875 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Dale Johannesende064702009-02-06 21:50:26 +00001876 Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00001877 }
1878 return Result;
1879}
1880
Dan Gohman475871a2008-07-27 21:46:04 +00001881static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001882 const ARMSubtarget *ST) {
Dan Gohman475871a2008-07-27 21:46:04 +00001883 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001884 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001885 SDValue LHS = Op.getOperand(2);
1886 SDValue RHS = Op.getOperand(3);
1887 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00001888 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001889
Owen Anderson825b72b2009-08-11 20:47:22 +00001890 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001891 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001892 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
David Goodwinf1daf7d2009-07-08 23:10:31 +00001893 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb1Only(), dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001894 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Dale Johannesende064702009-02-06 21:50:26 +00001895 Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001896 }
1897
Owen Anderson825b72b2009-08-11 20:47:22 +00001898 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Chenga8e29892007-01-19 07:51:42 +00001899 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001900 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001901
Dale Johannesende064702009-02-06 21:50:26 +00001902 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001903 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1904 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1905 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00001906 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00001907 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001908 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001909 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00001910 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00001911 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001912 }
1913 return Res;
1914}
1915
Dan Gohman475871a2008-07-27 21:46:04 +00001916SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) {
1917 SDValue Chain = Op.getOperand(0);
1918 SDValue Table = Op.getOperand(1);
1919 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001920 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001921
Owen Andersone50ed302009-08-10 22:56:29 +00001922 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00001923 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
1924 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00001925 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00001926 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00001927 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00001928 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
1929 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00001930 if (Subtarget->isThumb2()) {
1931 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
1932 // which does another jump to the destination. This also makes it easier
1933 // to translate it to TBB / TBH later.
1934 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00001935 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00001936 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001937 }
Evan Cheng66ac5312009-07-25 00:33:29 +00001938 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001939 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr, NULL, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00001940 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001941 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00001942 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001943 } else {
1944 Addr = DAG.getLoad(PTy, dl, Chain, Addr, NULL, 0);
1945 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00001946 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001947 }
Evan Chenga8e29892007-01-19 07:51:42 +00001948}
1949
Dan Gohman475871a2008-07-27 21:46:04 +00001950static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Dale Johannesende064702009-02-06 21:50:26 +00001951 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001952 unsigned Opc =
1953 Op.getOpcode() == ISD::FP_TO_SINT ? ARMISD::FTOSI : ARMISD::FTOUI;
Owen Anderson825b72b2009-08-11 20:47:22 +00001954 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
1955 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001956}
1957
Dan Gohman475871a2008-07-27 21:46:04 +00001958static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001959 EVT VT = Op.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001960 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001961 unsigned Opc =
1962 Op.getOpcode() == ISD::SINT_TO_FP ? ARMISD::SITOF : ARMISD::UITOF;
1963
Owen Anderson825b72b2009-08-11 20:47:22 +00001964 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
Dale Johannesende064702009-02-06 21:50:26 +00001965 return DAG.getNode(Opc, dl, VT, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001966}
1967
Dan Gohman475871a2008-07-27 21:46:04 +00001968static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00001969 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00001970 SDValue Tmp0 = Op.getOperand(0);
1971 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00001972 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001973 EVT VT = Op.getValueType();
1974 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001975 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
1976 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001977 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
1978 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001979 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001980}
1981
Jim Grosbach0e0da732009-05-12 23:59:14 +00001982SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
1983 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1984 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00001985 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001986 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
1987 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00001988 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00001989 ? ARM::R7 : ARM::R11;
1990 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
1991 while (Depth--)
1992 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
1993 return FrameAddr;
1994}
1995
Dan Gohman475871a2008-07-27 21:46:04 +00001996SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00001997ARMTargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman475871a2008-07-27 21:46:04 +00001998 SDValue Chain,
1999 SDValue Dst, SDValue Src,
2000 SDValue Size, unsigned Align,
Dan Gohman707e0182008-04-12 04:36:06 +00002001 bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002002 const Value *DstSV, uint64_t DstSVOff,
2003 const Value *SrcSV, uint64_t SrcSVOff){
Evan Cheng4102eb52007-10-22 22:11:27 +00002004 // Do repeated 4-byte loads and stores. To be improved.
Dan Gohman707e0182008-04-12 04:36:06 +00002005 // This requires 4-byte alignment.
2006 if ((Align & 3) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00002007 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00002008 // This requires the copy size to be a constant, preferrably
2009 // within a subtarget-specific limit.
2010 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2011 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00002012 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002013 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00002014 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00002015 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00002016
2017 unsigned BytesLeft = SizeVal & 3;
2018 unsigned NumMemOps = SizeVal >> 2;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002019 unsigned EmittedNumMemOps = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00002020 EVT VT = MVT::i32;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002021 unsigned VTSize = 4;
Evan Cheng4102eb52007-10-22 22:11:27 +00002022 unsigned i = 0;
Evan Chenge5e7ce42007-05-18 01:19:57 +00002023 const unsigned MAX_LOADS_IN_LDM = 6;
Dan Gohman475871a2008-07-27 21:46:04 +00002024 SDValue TFOps[MAX_LOADS_IN_LDM];
2025 SDValue Loads[MAX_LOADS_IN_LDM];
Dan Gohman1f13c682008-04-28 17:15:20 +00002026 uint64_t SrcOff = 0, DstOff = 0;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002027
Evan Cheng4102eb52007-10-22 22:11:27 +00002028 // Emit up to MAX_LOADS_IN_LDM loads, then a TokenFactor barrier, then the
2029 // same number of stores. The loads and stores will get combined into
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002030 // ldm/stm later on.
Evan Cheng4102eb52007-10-22 22:11:27 +00002031 while (EmittedNumMemOps < NumMemOps) {
2032 for (i = 0;
2033 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00002034 Loads[i] = DAG.getLoad(VT, dl, Chain,
Owen Anderson825b72b2009-08-11 20:47:22 +00002035 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
2036 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00002037 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00002038 TFOps[i] = Loads[i].getValue(1);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002039 SrcOff += VTSize;
2040 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002041 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002042
Evan Cheng4102eb52007-10-22 22:11:27 +00002043 for (i = 0;
2044 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00002045 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Owen Anderson825b72b2009-08-11 20:47:22 +00002046 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
2047 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00002048 DstSV, DstSVOff + DstOff);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002049 DstOff += VTSize;
2050 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002051 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00002052
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002053 EmittedNumMemOps += i;
2054 }
2055
Bob Wilson2dc4f542009-03-20 22:42:55 +00002056 if (BytesLeft == 0)
Evan Cheng4102eb52007-10-22 22:11:27 +00002057 return Chain;
2058
2059 // Issue loads / stores for the trailing (1 - 3) bytes.
2060 unsigned BytesLeftSave = BytesLeft;
2061 i = 0;
2062 while (BytesLeft) {
2063 if (BytesLeft >= 2) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002064 VT = MVT::i16;
Evan Cheng4102eb52007-10-22 22:11:27 +00002065 VTSize = 2;
2066 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00002067 VT = MVT::i8;
Evan Cheng4102eb52007-10-22 22:11:27 +00002068 VTSize = 1;
2069 }
2070
Dale Johannesen0f502f62009-02-03 22:26:09 +00002071 Loads[i] = DAG.getLoad(VT, dl, Chain,
Owen Anderson825b72b2009-08-11 20:47:22 +00002072 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
2073 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00002074 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00002075 TFOps[i] = Loads[i].getValue(1);
2076 ++i;
2077 SrcOff += VTSize;
2078 BytesLeft -= VTSize;
2079 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002080 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00002081
2082 i = 0;
2083 BytesLeft = BytesLeftSave;
2084 while (BytesLeft) {
2085 if (BytesLeft >= 2) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002086 VT = MVT::i16;
Evan Cheng4102eb52007-10-22 22:11:27 +00002087 VTSize = 2;
2088 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00002089 VT = MVT::i8;
Evan Cheng4102eb52007-10-22 22:11:27 +00002090 VTSize = 1;
2091 }
2092
Dale Johannesen0f502f62009-02-03 22:26:09 +00002093 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Owen Anderson825b72b2009-08-11 20:47:22 +00002094 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
2095 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00002096 DstSV, DstSVOff + DstOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00002097 ++i;
2098 DstOff += VTSize;
2099 BytesLeft -= VTSize;
2100 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002101 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002102}
2103
Duncan Sands1607f052008-12-01 11:39:25 +00002104static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00002105 SDValue Op = N->getOperand(0);
Dale Johannesende064702009-02-06 21:50:26 +00002106 DebugLoc dl = N->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00002107 if (N->getValueType(0) == MVT::f64) {
Evan Chengc7c77292008-11-04 19:57:48 +00002108 // Turn i64->f64 into FMDRR.
Owen Anderson825b72b2009-08-11 20:47:22 +00002109 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2110 DAG.getConstant(0, MVT::i32));
2111 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2112 DAG.getConstant(1, MVT::i32));
2113 return DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
Evan Chengc7c77292008-11-04 19:57:48 +00002114 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002115
Evan Chengc7c77292008-11-04 19:57:48 +00002116 // Turn f64->i64 into FMRRD.
Bob Wilson2dc4f542009-03-20 22:42:55 +00002117 SDValue Cvt = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00002118 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002119
Chris Lattner27a6c732007-11-24 07:07:01 +00002120 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002121 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
Chris Lattner27a6c732007-11-24 07:07:01 +00002122}
2123
Bob Wilson5bafff32009-06-22 23:27:02 +00002124/// getZeroVector - Returns a vector of specified type with all zero elements.
2125///
Owen Andersone50ed302009-08-10 22:56:29 +00002126static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002127 assert(VT.isVector() && "Expected a vector type");
2128
2129 // Zero vectors are used to represent vector negation and in those cases
2130 // will be implemented with the NEON VNEG instruction. However, VNEG does
2131 // not support i64 elements, so sometimes the zero vectors will need to be
2132 // explicitly constructed. For those cases, and potentially other uses in
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002133 // the future, always build zero vectors as <16 x i8> or <8 x i8> bitcasted
Bob Wilson5bafff32009-06-22 23:27:02 +00002134 // to their dest type. This ensures they get CSE'd.
2135 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002136 SDValue Cst = DAG.getTargetConstant(0, MVT::i8);
2137 SmallVector<SDValue, 8> Ops;
2138 MVT TVT;
2139
2140 if (VT.getSizeInBits() == 64) {
2141 Ops.assign(8, Cst); TVT = MVT::v8i8;
2142 } else {
2143 Ops.assign(16, Cst); TVT = MVT::v16i8;
2144 }
2145 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002146
2147 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2148}
2149
2150/// getOnesVector - Returns a vector of specified type with all bits set.
2151///
Owen Andersone50ed302009-08-10 22:56:29 +00002152static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002153 assert(VT.isVector() && "Expected a vector type");
2154
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002155 // Always build ones vectors as <16 x i32> or <8 x i32> bitcasted to their
2156 // dest type. This ensures they get CSE'd.
Bob Wilson5bafff32009-06-22 23:27:02 +00002157 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002158 SDValue Cst = DAG.getTargetConstant(0xFF, MVT::i8);
2159 SmallVector<SDValue, 8> Ops;
2160 MVT TVT;
2161
2162 if (VT.getSizeInBits() == 64) {
2163 Ops.assign(8, Cst); TVT = MVT::v8i8;
2164 } else {
2165 Ops.assign(16, Cst); TVT = MVT::v16i8;
2166 }
2167 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002168
2169 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2170}
2171
2172static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2173 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00002174 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002175 DebugLoc dl = N->getDebugLoc();
2176
2177 // Lower vector shifts on NEON to use VSHL.
2178 if (VT.isVector()) {
2179 assert(ST->hasNEON() && "unexpected vector shift");
2180
2181 // Left shifts translate directly to the vshiftu intrinsic.
2182 if (N->getOpcode() == ISD::SHL)
2183 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002184 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002185 N->getOperand(0), N->getOperand(1));
2186
2187 assert((N->getOpcode() == ISD::SRA ||
2188 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2189
2190 // NEON uses the same intrinsics for both left and right shifts. For
2191 // right shifts, the shift amounts are negative, so negate the vector of
2192 // shift amounts.
Owen Andersone50ed302009-08-10 22:56:29 +00002193 EVT ShiftVT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002194 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2195 getZeroVector(ShiftVT, DAG, dl),
2196 N->getOperand(1));
2197 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2198 Intrinsic::arm_neon_vshifts :
2199 Intrinsic::arm_neon_vshiftu);
2200 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002201 DAG.getConstant(vshiftInt, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002202 N->getOperand(0), NegatedCount);
2203 }
2204
Eli Friedmance392eb2009-08-22 03:13:10 +00002205 // We can get here for a node like i32 = ISD::SHL i32, i64
2206 if (VT != MVT::i64)
2207 return SDValue();
2208
2209 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00002210 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00002211
Chris Lattner27a6c732007-11-24 07:07:01 +00002212 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2213 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002214 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00002215 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002216
Chris Lattner27a6c732007-11-24 07:07:01 +00002217 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00002218 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002219
Chris Lattner27a6c732007-11-24 07:07:01 +00002220 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00002221 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2222 DAG.getConstant(0, MVT::i32));
2223 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2224 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002225
Chris Lattner27a6c732007-11-24 07:07:01 +00002226 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2227 // captures the result into a carry flag.
2228 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Owen Anderson825b72b2009-08-11 20:47:22 +00002229 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002230
Chris Lattner27a6c732007-11-24 07:07:01 +00002231 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00002232 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002233
Chris Lattner27a6c732007-11-24 07:07:01 +00002234 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002235 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00002236}
2237
Bob Wilson5bafff32009-06-22 23:27:02 +00002238static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2239 SDValue TmpOp0, TmpOp1;
2240 bool Invert = false;
2241 bool Swap = false;
2242 unsigned Opc = 0;
2243
2244 SDValue Op0 = Op.getOperand(0);
2245 SDValue Op1 = Op.getOperand(1);
2246 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00002247 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002248 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2249 DebugLoc dl = Op.getDebugLoc();
2250
2251 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2252 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002253 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002254 case ISD::SETUNE:
2255 case ISD::SETNE: Invert = true; // Fallthrough
2256 case ISD::SETOEQ:
2257 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2258 case ISD::SETOLT:
2259 case ISD::SETLT: Swap = true; // Fallthrough
2260 case ISD::SETOGT:
2261 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2262 case ISD::SETOLE:
2263 case ISD::SETLE: Swap = true; // Fallthrough
2264 case ISD::SETOGE:
2265 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2266 case ISD::SETUGE: Swap = true; // Fallthrough
2267 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2268 case ISD::SETUGT: Swap = true; // Fallthrough
2269 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2270 case ISD::SETUEQ: Invert = true; // Fallthrough
2271 case ISD::SETONE:
2272 // Expand this to (OLT | OGT).
2273 TmpOp0 = Op0;
2274 TmpOp1 = Op1;
2275 Opc = ISD::OR;
2276 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2277 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2278 break;
2279 case ISD::SETUO: Invert = true; // Fallthrough
2280 case ISD::SETO:
2281 // Expand this to (OLT | OGE).
2282 TmpOp0 = Op0;
2283 TmpOp1 = Op1;
2284 Opc = ISD::OR;
2285 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2286 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2287 break;
2288 }
2289 } else {
2290 // Integer comparisons.
2291 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002292 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002293 case ISD::SETNE: Invert = true;
2294 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2295 case ISD::SETLT: Swap = true;
2296 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2297 case ISD::SETLE: Swap = true;
2298 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2299 case ISD::SETULT: Swap = true;
2300 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2301 case ISD::SETULE: Swap = true;
2302 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2303 }
2304
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002305 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00002306 if (Opc == ARMISD::VCEQ) {
2307
2308 SDValue AndOp;
2309 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2310 AndOp = Op0;
2311 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2312 AndOp = Op1;
2313
2314 // Ignore bitconvert.
2315 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2316 AndOp = AndOp.getOperand(0);
2317
2318 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2319 Opc = ARMISD::VTST;
2320 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2321 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2322 Invert = !Invert;
2323 }
2324 }
2325 }
2326
2327 if (Swap)
2328 std::swap(Op0, Op1);
2329
2330 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
2331
2332 if (Invert)
2333 Result = DAG.getNOT(dl, Result, VT);
2334
2335 return Result;
2336}
2337
2338/// isVMOVSplat - Check if the specified splat value corresponds to an immediate
2339/// VMOV instruction, and if so, return the constant being splatted.
2340static SDValue isVMOVSplat(uint64_t SplatBits, uint64_t SplatUndef,
2341 unsigned SplatBitSize, SelectionDAG &DAG) {
2342 switch (SplatBitSize) {
2343 case 8:
2344 // Any 1-byte value is OK.
2345 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Owen Anderson825b72b2009-08-11 20:47:22 +00002346 return DAG.getTargetConstant(SplatBits, MVT::i8);
Bob Wilson5bafff32009-06-22 23:27:02 +00002347
2348 case 16:
2349 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
2350 if ((SplatBits & ~0xff) == 0 ||
2351 (SplatBits & ~0xff00) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002352 return DAG.getTargetConstant(SplatBits, MVT::i16);
Bob Wilson5bafff32009-06-22 23:27:02 +00002353 break;
2354
2355 case 32:
2356 // NEON's 32-bit VMOV supports splat values where:
2357 // * only one byte is nonzero, or
2358 // * the least significant byte is 0xff and the second byte is nonzero, or
2359 // * the least significant 2 bytes are 0xff and the third is nonzero.
2360 if ((SplatBits & ~0xff) == 0 ||
2361 (SplatBits & ~0xff00) == 0 ||
2362 (SplatBits & ~0xff0000) == 0 ||
2363 (SplatBits & ~0xff000000) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002364 return DAG.getTargetConstant(SplatBits, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002365
2366 if ((SplatBits & ~0xffff) == 0 &&
2367 ((SplatBits | SplatUndef) & 0xff) == 0xff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002368 return DAG.getTargetConstant(SplatBits | 0xff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002369
2370 if ((SplatBits & ~0xffffff) == 0 &&
2371 ((SplatBits | SplatUndef) & 0xffff) == 0xffff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002372 return DAG.getTargetConstant(SplatBits | 0xffff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002373
2374 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
2375 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
2376 // VMOV.I32. A (very) minor optimization would be to replicate the value
2377 // and fall through here to test for a valid 64-bit splat. But, then the
2378 // caller would also need to check and handle the change in size.
2379 break;
2380
2381 case 64: {
2382 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
2383 uint64_t BitMask = 0xff;
2384 uint64_t Val = 0;
2385 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
2386 if (((SplatBits | SplatUndef) & BitMask) == BitMask)
2387 Val |= BitMask;
2388 else if ((SplatBits & BitMask) != 0)
2389 return SDValue();
2390 BitMask <<= 8;
2391 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002392 return DAG.getTargetConstant(Val, MVT::i64);
Bob Wilson5bafff32009-06-22 23:27:02 +00002393 }
2394
2395 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002396 llvm_unreachable("unexpected size for isVMOVSplat");
Bob Wilson5bafff32009-06-22 23:27:02 +00002397 break;
2398 }
2399
2400 return SDValue();
2401}
2402
2403/// getVMOVImm - If this is a build_vector of constants which can be
2404/// formed by using a VMOV instruction of the specified element size,
2405/// return the constant being splatted. The ByteSize field indicates the
2406/// number of bytes of each element [1248].
2407SDValue ARM::getVMOVImm(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
2408 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
2409 APInt SplatBits, SplatUndef;
2410 unsigned SplatBitSize;
2411 bool HasAnyUndefs;
2412 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
2413 HasAnyUndefs, ByteSize * 8))
2414 return SDValue();
2415
2416 if (SplatBitSize > ByteSize * 8)
2417 return SDValue();
2418
2419 return isVMOVSplat(SplatBits.getZExtValue(), SplatUndef.getZExtValue(),
2420 SplatBitSize, DAG);
2421}
2422
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002423static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
2424 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002425 unsigned NumElts = VT.getVectorNumElements();
2426 ReverseVEXT = false;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002427 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002428
2429 // If this is a VEXT shuffle, the immediate value is the index of the first
2430 // element. The other shuffle indices must be the successive elements after
2431 // the first one.
2432 unsigned ExpectedElt = Imm;
2433 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002434 // Increment the expected index. If it wraps around, it may still be
2435 // a VEXT but the source vectors must be swapped.
2436 ExpectedElt += 1;
2437 if (ExpectedElt == NumElts * 2) {
2438 ExpectedElt = 0;
2439 ReverseVEXT = true;
2440 }
2441
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002442 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002443 return false;
2444 }
2445
2446 // Adjust the index value if the source operands will be swapped.
2447 if (ReverseVEXT)
2448 Imm -= NumElts;
2449
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002450 return true;
2451}
2452
Bob Wilson8bb9e482009-07-26 00:39:34 +00002453/// isVREVMask - Check if a vector shuffle corresponds to a VREV
2454/// instruction with the specified blocksize. (The order of the elements
2455/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002456static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
2457 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00002458 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
2459 "Only possible block sizes for VREV are: 16, 32, 64");
2460
Bob Wilson8bb9e482009-07-26 00:39:34 +00002461 unsigned NumElts = VT.getVectorNumElements();
2462 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002463 unsigned BlockElts = M[0] + 1;
Bob Wilson8bb9e482009-07-26 00:39:34 +00002464
2465 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
2466 return false;
2467
2468 for (unsigned i = 0; i < NumElts; ++i) {
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002469 if ((unsigned) M[i] !=
Bob Wilson8bb9e482009-07-26 00:39:34 +00002470 (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
2471 return false;
2472 }
2473
2474 return true;
2475}
2476
Bob Wilsonc692cb72009-08-21 20:54:19 +00002477static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
2478 unsigned &WhichResult) {
2479 unsigned NumElts = VT.getVectorNumElements();
2480 WhichResult = (M[0] == 0 ? 0 : 1);
2481 for (unsigned i = 0; i < NumElts; i += 2) {
2482 if ((unsigned) M[i] != i + WhichResult ||
2483 (unsigned) M[i+1] != i + NumElts + WhichResult)
2484 return false;
2485 }
2486 return true;
2487}
2488
2489static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
2490 unsigned &WhichResult) {
2491 unsigned NumElts = VT.getVectorNumElements();
2492 WhichResult = (M[0] == 0 ? 0 : 1);
2493 for (unsigned i = 0; i != NumElts; ++i) {
2494 if ((unsigned) M[i] != 2 * i + WhichResult)
2495 return false;
2496 }
2497
2498 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2499 if (VT.is64BitVector() && VT.getVectorElementType().getSizeInBits() == 32)
2500 return false;
2501
2502 return true;
2503}
2504
2505static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
2506 unsigned &WhichResult) {
2507 unsigned NumElts = VT.getVectorNumElements();
2508 WhichResult = (M[0] == 0 ? 0 : 1);
2509 unsigned Idx = WhichResult * NumElts / 2;
2510 for (unsigned i = 0; i != NumElts; i += 2) {
2511 if ((unsigned) M[i] != Idx ||
2512 (unsigned) M[i+1] != Idx + NumElts)
2513 return false;
2514 Idx += 1;
2515 }
2516
2517 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2518 if (VT.is64BitVector() && VT.getVectorElementType().getSizeInBits() == 32)
2519 return false;
2520
2521 return true;
2522}
2523
Owen Andersone50ed302009-08-10 22:56:29 +00002524static SDValue BuildSplat(SDValue Val, EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002525 // Canonicalize all-zeros and all-ones vectors.
Bob Wilsond06791f2009-08-13 01:57:47 +00002526 ConstantSDNode *ConstVal = cast<ConstantSDNode>(Val.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002527 if (ConstVal->isNullValue())
2528 return getZeroVector(VT, DAG, dl);
2529 if (ConstVal->isAllOnesValue())
2530 return getOnesVector(VT, DAG, dl);
2531
Owen Andersone50ed302009-08-10 22:56:29 +00002532 EVT CanonicalVT;
Bob Wilson5bafff32009-06-22 23:27:02 +00002533 if (VT.is64BitVector()) {
2534 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002535 case 8: CanonicalVT = MVT::v8i8; break;
2536 case 16: CanonicalVT = MVT::v4i16; break;
2537 case 32: CanonicalVT = MVT::v2i32; break;
2538 case 64: CanonicalVT = MVT::v1i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002539 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002540 }
2541 } else {
2542 assert(VT.is128BitVector() && "unknown splat vector size");
2543 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002544 case 8: CanonicalVT = MVT::v16i8; break;
2545 case 16: CanonicalVT = MVT::v8i16; break;
2546 case 32: CanonicalVT = MVT::v4i32; break;
2547 case 64: CanonicalVT = MVT::v2i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002548 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002549 }
2550 }
2551
2552 // Build a canonical splat for this value.
2553 SmallVector<SDValue, 8> Ops;
2554 Ops.assign(CanonicalVT.getVectorNumElements(), Val);
2555 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, &Ops[0],
2556 Ops.size());
2557 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Res);
2558}
2559
2560// If this is a case we can't handle, return null and let the default
2561// expansion code take care of it.
2562static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bob Wilsond06791f2009-08-13 01:57:47 +00002563 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002564 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002565 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002566
2567 APInt SplatBits, SplatUndef;
2568 unsigned SplatBitSize;
2569 bool HasAnyUndefs;
2570 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00002571 if (SplatBitSize <= 64) {
2572 SDValue Val = isVMOVSplat(SplatBits.getZExtValue(),
2573 SplatUndef.getZExtValue(), SplatBitSize, DAG);
2574 if (Val.getNode())
2575 return BuildSplat(Val, VT, DAG, dl);
2576 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00002577 }
2578
2579 // If there are only 2 elements in a 128-bit vector, insert them into an
2580 // undef vector. This handles the common case for 128-bit vector argument
2581 // passing, where the insertions should be translated to subreg accesses
2582 // with no real instructions.
2583 if (VT.is128BitVector() && Op.getNumOperands() == 2) {
2584 SDValue Val = DAG.getUNDEF(VT);
2585 SDValue Op0 = Op.getOperand(0);
2586 SDValue Op1 = Op.getOperand(1);
2587 if (Op0.getOpcode() != ISD::UNDEF)
2588 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op0,
2589 DAG.getIntPtrConstant(0));
2590 if (Op1.getOpcode() != ISD::UNDEF)
2591 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op1,
2592 DAG.getIntPtrConstant(1));
2593 return Val;
Bob Wilson5bafff32009-06-22 23:27:02 +00002594 }
2595
2596 return SDValue();
2597}
2598
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002599/// isShuffleMaskLegal - Targets can use this to indicate that they only
2600/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
2601/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
2602/// are assumed to be legal.
2603bool
2604ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
2605 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002606 if (VT.getVectorNumElements() == 4 &&
2607 (VT.is128BitVector() || VT.is64BitVector())) {
2608 unsigned PFIndexes[4];
2609 for (unsigned i = 0; i != 4; ++i) {
2610 if (M[i] < 0)
2611 PFIndexes[i] = 8;
2612 else
2613 PFIndexes[i] = M[i];
2614 }
2615
2616 // Compute the index in the perfect shuffle table.
2617 unsigned PFTableIndex =
2618 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2619 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2620 unsigned Cost = (PFEntry >> 30);
2621
2622 if (Cost <= 4)
2623 return true;
2624 }
2625
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002626 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00002627 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002628
2629 return (ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
2630 isVREVMask(M, VT, 64) ||
2631 isVREVMask(M, VT, 32) ||
2632 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00002633 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
2634 isVTRNMask(M, VT, WhichResult) ||
2635 isVUZPMask(M, VT, WhichResult) ||
2636 isVZIPMask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002637}
2638
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002639/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
2640/// the specified operations to build the shuffle.
2641static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
2642 SDValue RHS, SelectionDAG &DAG,
2643 DebugLoc dl) {
2644 unsigned OpNum = (PFEntry >> 26) & 0x0F;
2645 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
2646 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
2647
2648 enum {
2649 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
2650 OP_VREV,
2651 OP_VDUP0,
2652 OP_VDUP1,
2653 OP_VDUP2,
2654 OP_VDUP3,
2655 OP_VEXT1,
2656 OP_VEXT2,
2657 OP_VEXT3,
2658 OP_VUZPL, // VUZP, left result
2659 OP_VUZPR, // VUZP, right result
2660 OP_VZIPL, // VZIP, left result
2661 OP_VZIPR, // VZIP, right result
2662 OP_VTRNL, // VTRN, left result
2663 OP_VTRNR // VTRN, right result
2664 };
2665
2666 if (OpNum == OP_COPY) {
2667 if (LHSID == (1*9+2)*9+3) return LHS;
2668 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
2669 return RHS;
2670 }
2671
2672 SDValue OpLHS, OpRHS;
2673 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
2674 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
2675 EVT VT = OpLHS.getValueType();
2676
2677 switch (OpNum) {
2678 default: llvm_unreachable("Unknown shuffle opcode!");
2679 case OP_VREV:
2680 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
2681 case OP_VDUP0:
2682 case OP_VDUP1:
2683 case OP_VDUP2:
2684 case OP_VDUP3:
2685 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002686 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002687 case OP_VEXT1:
2688 case OP_VEXT2:
2689 case OP_VEXT3:
2690 return DAG.getNode(ARMISD::VEXT, dl, VT,
2691 OpLHS, OpRHS,
2692 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
2693 case OP_VUZPL:
2694 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002695 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002696 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
2697 case OP_VZIPL:
2698 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002699 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002700 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
2701 case OP_VTRNL:
2702 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002703 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2704 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002705 }
2706}
2707
Bob Wilson5bafff32009-06-22 23:27:02 +00002708static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002709 SDValue V1 = Op.getOperand(0);
2710 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00002711 DebugLoc dl = Op.getDebugLoc();
2712 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002713 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002714 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00002715
Bob Wilson28865062009-08-13 02:13:04 +00002716 // Convert shuffles that are directly supported on NEON to target-specific
2717 // DAG nodes, instead of keeping them as shuffles and matching them again
2718 // during code selection. This is more efficient and avoids the possibility
2719 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00002720 // FIXME: floating-point vectors should be canonicalized to integer vectors
2721 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002722 SVN->getMask(ShuffleMask);
2723
2724 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
Bob Wilson0ce37102009-08-14 05:08:32 +00002725 int Lane = SVN->getSplatIndex();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002726 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
2727 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00002728 }
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002729 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002730 DAG.getConstant(Lane, MVT::i32));
Bob Wilson0ce37102009-08-14 05:08:32 +00002731 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002732
2733 bool ReverseVEXT;
2734 unsigned Imm;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002735 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002736 if (ReverseVEXT)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002737 std::swap(V1, V2);
2738 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002739 DAG.getConstant(Imm, MVT::i32));
2740 }
2741
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002742 if (isVREVMask(ShuffleMask, VT, 64))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002743 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002744 if (isVREVMask(ShuffleMask, VT, 32))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002745 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002746 if (isVREVMask(ShuffleMask, VT, 16))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002747 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
2748
Bob Wilsonc692cb72009-08-21 20:54:19 +00002749 // Check for Neon shuffles that modify both input vectors in place.
2750 // If both results are used, i.e., if there are two shuffles with the same
2751 // source operands and with masks corresponding to both results of one of
2752 // these operations, DAG memoization will ensure that a single node is
2753 // used for both shuffles.
2754 unsigned WhichResult;
2755 if (isVTRNMask(ShuffleMask, VT, WhichResult))
2756 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2757 V1, V2).getValue(WhichResult);
2758 if (isVUZPMask(ShuffleMask, VT, WhichResult))
2759 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
2760 V1, V2).getValue(WhichResult);
2761 if (isVZIPMask(ShuffleMask, VT, WhichResult))
2762 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
2763 V1, V2).getValue(WhichResult);
2764
2765 // If the shuffle is not directly supported and it has 4 elements, use
2766 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002767 if (VT.getVectorNumElements() == 4 &&
2768 (VT.is128BitVector() || VT.is64BitVector())) {
2769 unsigned PFIndexes[4];
2770 for (unsigned i = 0; i != 4; ++i) {
2771 if (ShuffleMask[i] < 0)
2772 PFIndexes[i] = 8;
2773 else
2774 PFIndexes[i] = ShuffleMask[i];
2775 }
2776
2777 // Compute the index in the perfect shuffle table.
2778 unsigned PFTableIndex =
2779 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2780
2781 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2782 unsigned Cost = (PFEntry >> 30);
2783
2784 if (Cost <= 4)
2785 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
2786 }
Bob Wilsond8e17572009-08-12 22:31:50 +00002787
Bob Wilson22cac0d2009-08-14 05:16:33 +00002788 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00002789}
2790
Bob Wilson5bafff32009-06-22 23:27:02 +00002791static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002792 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002793 DebugLoc dl = Op.getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00002794 SDValue Vec = Op.getOperand(0);
2795 SDValue Lane = Op.getOperand(1);
Anton Korobeynikovb00c03b2009-08-30 17:14:54 +00002796
2797 // FIXME: This is invalid for 8 and 16-bit elements - the information about
2798 // sign / zero extension is lost!
Owen Anderson825b72b2009-08-11 20:47:22 +00002799 Op = DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
2800 Op = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Op, DAG.getValueType(VT));
Anton Korobeynikovb00c03b2009-08-30 17:14:54 +00002801
2802 if (VT.bitsLT(MVT::i32))
2803 Op = DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
2804 else if (VT.bitsGT(MVT::i32))
2805 Op = DAG.getNode(ISD::ANY_EXTEND, dl, VT, Op);
2806
2807 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00002808}
2809
Bob Wilsona6d65862009-08-03 20:36:38 +00002810static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
2811 // The only time a CONCAT_VECTORS operation can have legal types is when
2812 // two 64-bit vectors are concatenated to a 128-bit vector.
2813 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
2814 "unexpected CONCAT_VECTORS");
2815 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00002816 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00002817 SDValue Op0 = Op.getOperand(0);
2818 SDValue Op1 = Op.getOperand(1);
2819 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00002820 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
2821 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00002822 DAG.getIntPtrConstant(0));
2823 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00002824 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
2825 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00002826 DAG.getIntPtrConstant(1));
2827 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00002828}
2829
Dan Gohman475871a2008-07-27 21:46:04 +00002830SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00002831 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002832 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00002833 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002834 case ISD::GlobalAddress:
2835 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
2836 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002837 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002838 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, Subtarget);
2839 case ISD::BR_CC: return LowerBR_CC(Op, DAG, Subtarget);
2840 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Evan Cheng86198642009-08-07 00:34:42 +00002841 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002842 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
2843 case ISD::SINT_TO_FP:
2844 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
2845 case ISD::FP_TO_SINT:
2846 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
2847 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00002848 case ISD::RETURNADDR: break;
Jim Grosbach0e0da732009-05-12 23:59:14 +00002849 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002850 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Bob Wilsonb36ec862009-08-06 18:47:44 +00002851 case ISD::INTRINSIC_VOID:
Bob Wilsona599bff2009-08-04 00:36:16 +00002852 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002853 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00002854 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00002855 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00002856 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00002857 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
2858 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
2859 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2860 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00002861 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00002862 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002863 }
Dan Gohman475871a2008-07-27 21:46:04 +00002864 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00002865}
2866
Duncan Sands1607f052008-12-01 11:39:25 +00002867/// ReplaceNodeResults - Replace the results of node with an illegal result
2868/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00002869void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
2870 SmallVectorImpl<SDValue>&Results,
2871 SelectionDAG &DAG) {
Chris Lattner27a6c732007-11-24 07:07:01 +00002872 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00002873 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002874 llvm_unreachable("Don't know how to custom expand this!");
Duncan Sands1607f052008-12-01 11:39:25 +00002875 return;
2876 case ISD::BIT_CONVERT:
2877 Results.push_back(ExpandBIT_CONVERT(N, DAG));
2878 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00002879 case ISD::SRL:
Duncan Sands1607f052008-12-01 11:39:25 +00002880 case ISD::SRA: {
Bob Wilson5bafff32009-06-22 23:27:02 +00002881 SDValue Res = LowerShift(N, DAG, Subtarget);
Duncan Sands1607f052008-12-01 11:39:25 +00002882 if (Res.getNode())
2883 Results.push_back(Res);
2884 return;
2885 }
Chris Lattner27a6c732007-11-24 07:07:01 +00002886 }
2887}
Chris Lattner27a6c732007-11-24 07:07:01 +00002888
Evan Chenga8e29892007-01-19 07:51:42 +00002889//===----------------------------------------------------------------------===//
2890// ARM Scheduler Hooks
2891//===----------------------------------------------------------------------===//
2892
2893MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00002894ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +00002895 MachineBasicBlock *BB,
2896 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002897 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00002898 DebugLoc dl = MI->getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002899 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00002900 default:
2901 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng007ea272009-08-12 05:17:19 +00002902 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00002903 // To "insert" a SELECT_CC instruction, we actually have to insert the
2904 // diamond control-flow pattern. The incoming instruction knows the
2905 // destination vreg to set, the condition code register to branch on, the
2906 // true/false values to select between, and a branch opcode to use.
2907 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002908 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00002909 ++It;
2910
2911 // thisMBB:
2912 // ...
2913 // TrueVal = ...
2914 // cmpTY ccX, r1, r2
2915 // bCC copy1MBB
2916 // fallthrough --> copy0MBB
2917 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002918 MachineFunction *F = BB->getParent();
2919 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
2920 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenb6728402009-02-13 02:25:56 +00002921 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00002922 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002923 F->insert(It, copy0MBB);
2924 F->insert(It, sinkMBB);
Evan Chenga8e29892007-01-19 07:51:42 +00002925 // Update machine-CFG edges by first adding all successors of the current
2926 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00002927 // Also inform sdisel of the edge changes.
2928 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
2929 E = BB->succ_end(); I != E; ++I) {
2930 EM->insert(std::make_pair(*I, sinkMBB));
2931 sinkMBB->addSuccessor(*I);
2932 }
Evan Chenga8e29892007-01-19 07:51:42 +00002933 // Next, remove all successors of the current block, and add the true
2934 // and fallthrough blocks as its successors.
Evan Chengce319102009-09-19 09:51:03 +00002935 while (!BB->succ_empty())
Evan Chenga8e29892007-01-19 07:51:42 +00002936 BB->removeSuccessor(BB->succ_begin());
2937 BB->addSuccessor(copy0MBB);
2938 BB->addSuccessor(sinkMBB);
2939
2940 // copy0MBB:
2941 // %FalseValue = ...
2942 // # fallthrough to sinkMBB
2943 BB = copy0MBB;
2944
2945 // Update machine-CFG edges
2946 BB->addSuccessor(sinkMBB);
2947
2948 // sinkMBB:
2949 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2950 // ...
2951 BB = sinkMBB;
Dale Johannesenb6728402009-02-13 02:25:56 +00002952 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00002953 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
2954 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
2955
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002956 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00002957 return BB;
2958 }
Evan Cheng86198642009-08-07 00:34:42 +00002959
2960 case ARM::tANDsp:
2961 case ARM::tADDspr_:
2962 case ARM::tSUBspi_:
2963 case ARM::t2SUBrSPi_:
2964 case ARM::t2SUBrSPi12_:
2965 case ARM::t2SUBrSPs_: {
2966 MachineFunction *MF = BB->getParent();
2967 unsigned DstReg = MI->getOperand(0).getReg();
2968 unsigned SrcReg = MI->getOperand(1).getReg();
2969 bool DstIsDead = MI->getOperand(0).isDead();
2970 bool SrcIsKill = MI->getOperand(1).isKill();
2971
2972 if (SrcReg != ARM::SP) {
2973 // Copy the source to SP from virtual register.
2974 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(SrcReg);
2975 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
2976 ? ARM::tMOVtgpr2gpr : ARM::tMOVgpr2gpr;
2977 BuildMI(BB, dl, TII->get(CopyOpc), ARM::SP)
2978 .addReg(SrcReg, getKillRegState(SrcIsKill));
2979 }
2980
2981 unsigned OpOpc = 0;
2982 bool NeedPred = false, NeedCC = false, NeedOp3 = false;
2983 switch (MI->getOpcode()) {
2984 default:
2985 llvm_unreachable("Unexpected pseudo instruction!");
2986 case ARM::tANDsp:
2987 OpOpc = ARM::tAND;
2988 NeedPred = true;
2989 break;
2990 case ARM::tADDspr_:
2991 OpOpc = ARM::tADDspr;
2992 break;
2993 case ARM::tSUBspi_:
2994 OpOpc = ARM::tSUBspi;
2995 break;
2996 case ARM::t2SUBrSPi_:
2997 OpOpc = ARM::t2SUBrSPi;
2998 NeedPred = true; NeedCC = true;
2999 break;
3000 case ARM::t2SUBrSPi12_:
3001 OpOpc = ARM::t2SUBrSPi12;
3002 NeedPred = true;
3003 break;
3004 case ARM::t2SUBrSPs_:
3005 OpOpc = ARM::t2SUBrSPs;
3006 NeedPred = true; NeedCC = true; NeedOp3 = true;
3007 break;
3008 }
3009 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(OpOpc), ARM::SP);
3010 if (OpOpc == ARM::tAND)
3011 AddDefaultT1CC(MIB);
3012 MIB.addReg(ARM::SP);
3013 MIB.addOperand(MI->getOperand(2));
3014 if (NeedOp3)
3015 MIB.addOperand(MI->getOperand(3));
3016 if (NeedPred)
3017 AddDefaultPred(MIB);
3018 if (NeedCC)
3019 AddDefaultCC(MIB);
3020
3021 // Copy the result from SP to virtual register.
3022 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(DstReg);
3023 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3024 ? ARM::tMOVgpr2tgpr : ARM::tMOVgpr2gpr;
3025 BuildMI(BB, dl, TII->get(CopyOpc))
3026 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
3027 .addReg(ARM::SP);
3028 MF->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
3029 return BB;
3030 }
Evan Chenga8e29892007-01-19 07:51:42 +00003031 }
3032}
3033
3034//===----------------------------------------------------------------------===//
3035// ARM Optimization Hooks
3036//===----------------------------------------------------------------------===//
3037
Chris Lattnerd1980a52009-03-12 06:52:53 +00003038static
3039SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
3040 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00003041 SelectionDAG &DAG = DCI.DAG;
3042 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003043 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00003044 unsigned Opc = N->getOpcode();
3045 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
3046 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
3047 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
3048 ISD::CondCode CC = ISD::SETCC_INVALID;
3049
3050 if (isSlctCC) {
3051 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
3052 } else {
3053 SDValue CCOp = Slct.getOperand(0);
3054 if (CCOp.getOpcode() == ISD::SETCC)
3055 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
3056 }
3057
3058 bool DoXform = false;
3059 bool InvCC = false;
3060 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
3061 "Bad input!");
3062
3063 if (LHS.getOpcode() == ISD::Constant &&
3064 cast<ConstantSDNode>(LHS)->isNullValue()) {
3065 DoXform = true;
3066 } else if (CC != ISD::SETCC_INVALID &&
3067 RHS.getOpcode() == ISD::Constant &&
3068 cast<ConstantSDNode>(RHS)->isNullValue()) {
3069 std::swap(LHS, RHS);
3070 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003071 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00003072 Op0.getOperand(0).getValueType();
3073 bool isInt = OpVT.isInteger();
3074 CC = ISD::getSetCCInverse(CC, isInt);
3075
3076 if (!TLI.isCondCodeLegal(CC, OpVT))
3077 return SDValue(); // Inverse operator isn't legal.
3078
3079 DoXform = true;
3080 InvCC = true;
3081 }
3082
3083 if (DoXform) {
3084 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
3085 if (isSlctCC)
3086 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
3087 Slct.getOperand(0), Slct.getOperand(1), CC);
3088 SDValue CCOp = Slct.getOperand(0);
3089 if (InvCC)
3090 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
3091 CCOp.getOperand(0), CCOp.getOperand(1), CC);
3092 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
3093 CCOp, OtherOp, Result);
3094 }
3095 return SDValue();
3096}
3097
3098/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
3099static SDValue PerformADDCombine(SDNode *N,
3100 TargetLowering::DAGCombinerInfo &DCI) {
3101 // added by evan in r37685 with no testcase.
3102 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003103
Chris Lattnerd1980a52009-03-12 06:52:53 +00003104 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
3105 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
3106 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
3107 if (Result.getNode()) return Result;
3108 }
3109 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3110 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3111 if (Result.getNode()) return Result;
3112 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003113
Chris Lattnerd1980a52009-03-12 06:52:53 +00003114 return SDValue();
3115}
3116
3117/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
3118static SDValue PerformSUBCombine(SDNode *N,
3119 TargetLowering::DAGCombinerInfo &DCI) {
3120 // added by evan in r37685 with no testcase.
3121 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003122
Chris Lattnerd1980a52009-03-12 06:52:53 +00003123 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
3124 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3125 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3126 if (Result.getNode()) return Result;
3127 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003128
Chris Lattnerd1980a52009-03-12 06:52:53 +00003129 return SDValue();
3130}
3131
3132
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003133/// PerformFMRRDCombine - Target-specific dag combine xforms for ARMISD::FMRRD.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003134static SDValue PerformFMRRDCombine(SDNode *N,
3135 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003136 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00003137 SDValue InDouble = N->getOperand(0);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003138 if (InDouble.getOpcode() == ARMISD::FMDRR)
3139 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003140 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003141}
3142
Bob Wilson5bafff32009-06-22 23:27:02 +00003143/// getVShiftImm - Check if this is a valid build_vector for the immediate
3144/// operand of a vector shift operation, where all the elements of the
3145/// build_vector must have the same constant integer value.
3146static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
3147 // Ignore bit_converts.
3148 while (Op.getOpcode() == ISD::BIT_CONVERT)
3149 Op = Op.getOperand(0);
3150 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
3151 APInt SplatBits, SplatUndef;
3152 unsigned SplatBitSize;
3153 bool HasAnyUndefs;
3154 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
3155 HasAnyUndefs, ElementBits) ||
3156 SplatBitSize > ElementBits)
3157 return false;
3158 Cnt = SplatBits.getSExtValue();
3159 return true;
3160}
3161
3162/// isVShiftLImm - Check if this is a valid build_vector for the immediate
3163/// operand of a vector shift left operation. That value must be in the range:
3164/// 0 <= Value < ElementBits for a left shift; or
3165/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003166static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003167 assert(VT.isVector() && "vector shift count is not a vector type");
3168 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3169 if (! getVShiftImm(Op, ElementBits, Cnt))
3170 return false;
3171 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
3172}
3173
3174/// isVShiftRImm - Check if this is a valid build_vector for the immediate
3175/// operand of a vector shift right operation. For a shift opcode, the value
3176/// is positive, but for an intrinsic the value count must be negative. The
3177/// absolute value must be in the range:
3178/// 1 <= |Value| <= ElementBits for a right shift; or
3179/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003180static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00003181 int64_t &Cnt) {
3182 assert(VT.isVector() && "vector shift count is not a vector type");
3183 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3184 if (! getVShiftImm(Op, ElementBits, Cnt))
3185 return false;
3186 if (isIntrinsic)
3187 Cnt = -Cnt;
3188 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
3189}
3190
3191/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
3192static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
3193 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
3194 switch (IntNo) {
3195 default:
3196 // Don't do anything for most intrinsics.
3197 break;
3198
3199 // Vector shifts: check for immediate versions and lower them.
3200 // Note: This is done during DAG combining instead of DAG legalizing because
3201 // the build_vectors for 64-bit vector element shift counts are generally
3202 // not legal, and it is hard to see their values after they get legalized to
3203 // loads from a constant pool.
3204 case Intrinsic::arm_neon_vshifts:
3205 case Intrinsic::arm_neon_vshiftu:
3206 case Intrinsic::arm_neon_vshiftls:
3207 case Intrinsic::arm_neon_vshiftlu:
3208 case Intrinsic::arm_neon_vshiftn:
3209 case Intrinsic::arm_neon_vrshifts:
3210 case Intrinsic::arm_neon_vrshiftu:
3211 case Intrinsic::arm_neon_vrshiftn:
3212 case Intrinsic::arm_neon_vqshifts:
3213 case Intrinsic::arm_neon_vqshiftu:
3214 case Intrinsic::arm_neon_vqshiftsu:
3215 case Intrinsic::arm_neon_vqshiftns:
3216 case Intrinsic::arm_neon_vqshiftnu:
3217 case Intrinsic::arm_neon_vqshiftnsu:
3218 case Intrinsic::arm_neon_vqrshiftns:
3219 case Intrinsic::arm_neon_vqrshiftnu:
3220 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00003221 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003222 int64_t Cnt;
3223 unsigned VShiftOpc = 0;
3224
3225 switch (IntNo) {
3226 case Intrinsic::arm_neon_vshifts:
3227 case Intrinsic::arm_neon_vshiftu:
3228 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
3229 VShiftOpc = ARMISD::VSHL;
3230 break;
3231 }
3232 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
3233 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
3234 ARMISD::VSHRs : ARMISD::VSHRu);
3235 break;
3236 }
3237 return SDValue();
3238
3239 case Intrinsic::arm_neon_vshiftls:
3240 case Intrinsic::arm_neon_vshiftlu:
3241 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
3242 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003243 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003244
3245 case Intrinsic::arm_neon_vrshifts:
3246 case Intrinsic::arm_neon_vrshiftu:
3247 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
3248 break;
3249 return SDValue();
3250
3251 case Intrinsic::arm_neon_vqshifts:
3252 case Intrinsic::arm_neon_vqshiftu:
3253 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3254 break;
3255 return SDValue();
3256
3257 case Intrinsic::arm_neon_vqshiftsu:
3258 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3259 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003260 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003261
3262 case Intrinsic::arm_neon_vshiftn:
3263 case Intrinsic::arm_neon_vrshiftn:
3264 case Intrinsic::arm_neon_vqshiftns:
3265 case Intrinsic::arm_neon_vqshiftnu:
3266 case Intrinsic::arm_neon_vqshiftnsu:
3267 case Intrinsic::arm_neon_vqrshiftns:
3268 case Intrinsic::arm_neon_vqrshiftnu:
3269 case Intrinsic::arm_neon_vqrshiftnsu:
3270 // Narrowing shifts require an immediate right shift.
3271 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
3272 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003273 llvm_unreachable("invalid shift count for narrowing vector shift intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003274
3275 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003276 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003277 }
3278
3279 switch (IntNo) {
3280 case Intrinsic::arm_neon_vshifts:
3281 case Intrinsic::arm_neon_vshiftu:
3282 // Opcode already set above.
3283 break;
3284 case Intrinsic::arm_neon_vshiftls:
3285 case Intrinsic::arm_neon_vshiftlu:
3286 if (Cnt == VT.getVectorElementType().getSizeInBits())
3287 VShiftOpc = ARMISD::VSHLLi;
3288 else
3289 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
3290 ARMISD::VSHLLs : ARMISD::VSHLLu);
3291 break;
3292 case Intrinsic::arm_neon_vshiftn:
3293 VShiftOpc = ARMISD::VSHRN; break;
3294 case Intrinsic::arm_neon_vrshifts:
3295 VShiftOpc = ARMISD::VRSHRs; break;
3296 case Intrinsic::arm_neon_vrshiftu:
3297 VShiftOpc = ARMISD::VRSHRu; break;
3298 case Intrinsic::arm_neon_vrshiftn:
3299 VShiftOpc = ARMISD::VRSHRN; break;
3300 case Intrinsic::arm_neon_vqshifts:
3301 VShiftOpc = ARMISD::VQSHLs; break;
3302 case Intrinsic::arm_neon_vqshiftu:
3303 VShiftOpc = ARMISD::VQSHLu; break;
3304 case Intrinsic::arm_neon_vqshiftsu:
3305 VShiftOpc = ARMISD::VQSHLsu; break;
3306 case Intrinsic::arm_neon_vqshiftns:
3307 VShiftOpc = ARMISD::VQSHRNs; break;
3308 case Intrinsic::arm_neon_vqshiftnu:
3309 VShiftOpc = ARMISD::VQSHRNu; break;
3310 case Intrinsic::arm_neon_vqshiftnsu:
3311 VShiftOpc = ARMISD::VQSHRNsu; break;
3312 case Intrinsic::arm_neon_vqrshiftns:
3313 VShiftOpc = ARMISD::VQRSHRNs; break;
3314 case Intrinsic::arm_neon_vqrshiftnu:
3315 VShiftOpc = ARMISD::VQRSHRNu; break;
3316 case Intrinsic::arm_neon_vqrshiftnsu:
3317 VShiftOpc = ARMISD::VQRSHRNsu; break;
3318 }
3319
3320 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003321 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003322 }
3323
3324 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00003325 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003326 int64_t Cnt;
3327 unsigned VShiftOpc = 0;
3328
3329 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
3330 VShiftOpc = ARMISD::VSLI;
3331 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
3332 VShiftOpc = ARMISD::VSRI;
3333 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00003334 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003335 }
3336
3337 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
3338 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00003339 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003340 }
3341
3342 case Intrinsic::arm_neon_vqrshifts:
3343 case Intrinsic::arm_neon_vqrshiftu:
3344 // No immediate versions of these to check for.
3345 break;
3346 }
3347
3348 return SDValue();
3349}
3350
3351/// PerformShiftCombine - Checks for immediate versions of vector shifts and
3352/// lowers them. As with the vector shift intrinsics, this is done during DAG
3353/// combining instead of DAG legalizing because the build_vectors for 64-bit
3354/// vector element shift counts are generally not legal, and it is hard to see
3355/// their values after they get legalized to loads from a constant pool.
3356static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
3357 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003358 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003359
3360 // Nothing to be done for scalar shifts.
3361 if (! VT.isVector())
3362 return SDValue();
3363
3364 assert(ST->hasNEON() && "unexpected vector shift");
3365 int64_t Cnt;
3366
3367 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003368 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003369
3370 case ISD::SHL:
3371 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
3372 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003373 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003374 break;
3375
3376 case ISD::SRA:
3377 case ISD::SRL:
3378 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
3379 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
3380 ARMISD::VSHRs : ARMISD::VSHRu);
3381 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003382 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003383 }
3384 }
3385 return SDValue();
3386}
3387
3388/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
3389/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
3390static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
3391 const ARMSubtarget *ST) {
3392 SDValue N0 = N->getOperand(0);
3393
3394 // Check for sign- and zero-extensions of vector extract operations of 8-
3395 // and 16-bit vector elements. NEON supports these directly. They are
3396 // handled during DAG combining because type legalization will promote them
3397 // to 32-bit types and it is messy to recognize the operations after that.
3398 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
3399 SDValue Vec = N0.getOperand(0);
3400 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00003401 EVT VT = N->getValueType(0);
3402 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003403 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3404
Owen Anderson825b72b2009-08-11 20:47:22 +00003405 if (VT == MVT::i32 &&
3406 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00003407 TLI.isTypeLegal(Vec.getValueType())) {
3408
3409 unsigned Opc = 0;
3410 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003411 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003412 case ISD::SIGN_EXTEND:
3413 Opc = ARMISD::VGETLANEs;
3414 break;
3415 case ISD::ZERO_EXTEND:
3416 case ISD::ANY_EXTEND:
3417 Opc = ARMISD::VGETLANEu;
3418 break;
3419 }
3420 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
3421 }
3422 }
3423
3424 return SDValue();
3425}
3426
Dan Gohman475871a2008-07-27 21:46:04 +00003427SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003428 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003429 switch (N->getOpcode()) {
3430 default: break;
Chris Lattnerd1980a52009-03-12 06:52:53 +00003431 case ISD::ADD: return PerformADDCombine(N, DCI);
3432 case ISD::SUB: return PerformSUBCombine(N, DCI);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003433 case ARMISD::FMRRD: return PerformFMRRDCombine(N, DCI);
Bob Wilson5bafff32009-06-22 23:27:02 +00003434 case ISD::INTRINSIC_WO_CHAIN:
3435 return PerformIntrinsicCombine(N, DCI.DAG);
3436 case ISD::SHL:
3437 case ISD::SRA:
3438 case ISD::SRL:
3439 return PerformShiftCombine(N, DCI.DAG, Subtarget);
3440 case ISD::SIGN_EXTEND:
3441 case ISD::ZERO_EXTEND:
3442 case ISD::ANY_EXTEND:
3443 return PerformExtendCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003444 }
Dan Gohman475871a2008-07-27 21:46:04 +00003445 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003446}
3447
Bill Wendlingaf566342009-08-15 21:21:19 +00003448bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
3449 if (!Subtarget->hasV6Ops())
3450 // Pre-v6 does not support unaligned mem access.
3451 return false;
3452 else if (!Subtarget->hasV6Ops()) {
3453 // v6 may or may not support unaligned mem access.
3454 if (!Subtarget->isTargetDarwin())
3455 return false;
3456 }
3457
3458 switch (VT.getSimpleVT().SimpleTy) {
3459 default:
3460 return false;
3461 case MVT::i8:
3462 case MVT::i16:
3463 case MVT::i32:
3464 return true;
3465 // FIXME: VLD1 etc with standard alignment is legal.
3466 }
3467}
3468
Evan Chenge6c835f2009-08-14 20:09:37 +00003469static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
3470 if (V < 0)
3471 return false;
3472
3473 unsigned Scale = 1;
3474 switch (VT.getSimpleVT().SimpleTy) {
3475 default: return false;
3476 case MVT::i1:
3477 case MVT::i8:
3478 // Scale == 1;
3479 break;
3480 case MVT::i16:
3481 // Scale == 2;
3482 Scale = 2;
3483 break;
3484 case MVT::i32:
3485 // Scale == 4;
3486 Scale = 4;
3487 break;
3488 }
3489
3490 if ((V & (Scale - 1)) != 0)
3491 return false;
3492 V /= Scale;
3493 return V == (V & ((1LL << 5) - 1));
3494}
3495
3496static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
3497 const ARMSubtarget *Subtarget) {
3498 bool isNeg = false;
3499 if (V < 0) {
3500 isNeg = true;
3501 V = - V;
3502 }
3503
3504 switch (VT.getSimpleVT().SimpleTy) {
3505 default: return false;
3506 case MVT::i1:
3507 case MVT::i8:
3508 case MVT::i16:
3509 case MVT::i32:
3510 // + imm12 or - imm8
3511 if (isNeg)
3512 return V == (V & ((1LL << 8) - 1));
3513 return V == (V & ((1LL << 12) - 1));
3514 case MVT::f32:
3515 case MVT::f64:
3516 // Same as ARM mode. FIXME: NEON?
3517 if (!Subtarget->hasVFP2())
3518 return false;
3519 if ((V & 3) != 0)
3520 return false;
3521 V >>= 2;
3522 return V == (V & ((1LL << 8) - 1));
3523 }
3524}
3525
Evan Chengb01fad62007-03-12 23:30:29 +00003526/// isLegalAddressImmediate - Return true if the integer value can be used
3527/// as the offset of the target addressing mode for load / store of the
3528/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00003529static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00003530 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00003531 if (V == 0)
3532 return true;
3533
Evan Cheng65011532009-03-09 19:15:00 +00003534 if (!VT.isSimple())
3535 return false;
3536
Evan Chenge6c835f2009-08-14 20:09:37 +00003537 if (Subtarget->isThumb1Only())
3538 return isLegalT1AddressImmediate(V, VT);
3539 else if (Subtarget->isThumb2())
3540 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00003541
Evan Chenge6c835f2009-08-14 20:09:37 +00003542 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00003543 if (V < 0)
3544 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00003545 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00003546 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00003547 case MVT::i1:
3548 case MVT::i8:
3549 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00003550 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003551 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003552 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00003553 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003554 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003555 case MVT::f32:
3556 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00003557 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00003558 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00003559 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00003560 return false;
3561 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003562 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00003563 }
Evan Chenga8e29892007-01-19 07:51:42 +00003564}
3565
Evan Chenge6c835f2009-08-14 20:09:37 +00003566bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
3567 EVT VT) const {
3568 int Scale = AM.Scale;
3569 if (Scale < 0)
3570 return false;
3571
3572 switch (VT.getSimpleVT().SimpleTy) {
3573 default: return false;
3574 case MVT::i1:
3575 case MVT::i8:
3576 case MVT::i16:
3577 case MVT::i32:
3578 if (Scale == 1)
3579 return true;
3580 // r + r << imm
3581 Scale = Scale & ~1;
3582 return Scale == 2 || Scale == 4 || Scale == 8;
3583 case MVT::i64:
3584 // r + r
3585 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
3586 return true;
3587 return false;
3588 case MVT::isVoid:
3589 // Note, we allow "void" uses (basically, uses that aren't loads or
3590 // stores), because arm allows folding a scale into many arithmetic
3591 // operations. This should be made more precise and revisited later.
3592
3593 // Allow r << imm, but the imm has to be a multiple of two.
3594 if (Scale & 1) return false;
3595 return isPowerOf2_32(Scale);
3596 }
3597}
3598
Chris Lattner37caf8c2007-04-09 23:33:39 +00003599/// isLegalAddressingMode - Return true if the addressing mode represented
3600/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003601bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00003602 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00003603 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00003604 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00003605 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003606
Chris Lattner37caf8c2007-04-09 23:33:39 +00003607 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003608 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003609 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003610
Chris Lattner37caf8c2007-04-09 23:33:39 +00003611 switch (AM.Scale) {
3612 case 0: // no scale reg, must be "r+i" or "r", or "i".
3613 break;
3614 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00003615 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00003616 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00003617 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00003618 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00003619 // ARM doesn't support any R+R*scale+imm addr modes.
3620 if (AM.BaseOffs)
3621 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003622
Bob Wilson2c7dab12009-04-08 17:55:28 +00003623 if (!VT.isSimple())
3624 return false;
3625
Evan Chenge6c835f2009-08-14 20:09:37 +00003626 if (Subtarget->isThumb2())
3627 return isLegalT2ScaledAddressingMode(AM, VT);
3628
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003629 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00003630 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00003631 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00003632 case MVT::i1:
3633 case MVT::i8:
3634 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003635 if (Scale < 0) Scale = -Scale;
3636 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003637 return true;
3638 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00003639 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003640 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00003641 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00003642 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003643 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003644 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00003645 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003646
Owen Anderson825b72b2009-08-11 20:47:22 +00003647 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00003648 // Note, we allow "void" uses (basically, uses that aren't loads or
3649 // stores), because arm allows folding a scale into many arithmetic
3650 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003651
Chris Lattner37caf8c2007-04-09 23:33:39 +00003652 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00003653 if (Scale & 1) return false;
3654 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00003655 }
3656 break;
Evan Chengb01fad62007-03-12 23:30:29 +00003657 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00003658 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00003659}
3660
Owen Andersone50ed302009-08-10 22:56:29 +00003661static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00003662 bool isSEXTLoad, SDValue &Base,
3663 SDValue &Offset, bool &isInc,
3664 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00003665 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
3666 return false;
3667
Owen Anderson825b72b2009-08-11 20:47:22 +00003668 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00003669 // AddressingMode 3
3670 Base = Ptr->getOperand(0);
3671 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003672 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003673 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003674 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00003675 isInc = false;
3676 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3677 return true;
3678 }
3679 }
3680 isInc = (Ptr->getOpcode() == ISD::ADD);
3681 Offset = Ptr->getOperand(1);
3682 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00003683 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00003684 // AddressingMode 2
3685 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003686 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003687 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003688 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00003689 isInc = false;
3690 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3691 Base = Ptr->getOperand(0);
3692 return true;
3693 }
3694 }
3695
3696 if (Ptr->getOpcode() == ISD::ADD) {
3697 isInc = true;
3698 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
3699 if (ShOpcVal != ARM_AM::no_shift) {
3700 Base = Ptr->getOperand(1);
3701 Offset = Ptr->getOperand(0);
3702 } else {
3703 Base = Ptr->getOperand(0);
3704 Offset = Ptr->getOperand(1);
3705 }
3706 return true;
3707 }
3708
3709 isInc = (Ptr->getOpcode() == ISD::ADD);
3710 Base = Ptr->getOperand(0);
3711 Offset = Ptr->getOperand(1);
3712 return true;
3713 }
3714
3715 // FIXME: Use FLDM / FSTM to emulate indexed FP load / store.
3716 return false;
3717}
3718
Owen Andersone50ed302009-08-10 22:56:29 +00003719static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00003720 bool isSEXTLoad, SDValue &Base,
3721 SDValue &Offset, bool &isInc,
3722 SelectionDAG &DAG) {
3723 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
3724 return false;
3725
3726 Base = Ptr->getOperand(0);
3727 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
3728 int RHSC = (int)RHS->getZExtValue();
3729 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
3730 assert(Ptr->getOpcode() == ISD::ADD);
3731 isInc = false;
3732 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3733 return true;
3734 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
3735 isInc = Ptr->getOpcode() == ISD::ADD;
3736 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
3737 return true;
3738 }
3739 }
3740
3741 return false;
3742}
3743
Evan Chenga8e29892007-01-19 07:51:42 +00003744/// getPreIndexedAddressParts - returns true by value, base pointer and
3745/// offset pointer and addressing mode by reference if the node's address
3746/// can be legally represented as pre-indexed load / store address.
3747bool
Dan Gohman475871a2008-07-27 21:46:04 +00003748ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
3749 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003750 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00003751 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003752 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00003753 return false;
3754
Owen Andersone50ed302009-08-10 22:56:29 +00003755 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00003756 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00003757 bool isSEXTLoad = false;
3758 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
3759 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003760 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003761 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
3762 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
3763 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003764 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003765 } else
3766 return false;
3767
3768 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00003769 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00003770 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00003771 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
3772 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00003773 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00003774 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00003775 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00003776 if (!isLegal)
3777 return false;
3778
3779 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
3780 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00003781}
3782
3783/// getPostIndexedAddressParts - returns true by value, base pointer and
3784/// offset pointer and addressing mode by reference if this node can be
3785/// combined with a load / store to form a post-indexed load / store.
3786bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00003787 SDValue &Base,
3788 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003789 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00003790 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003791 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00003792 return false;
3793
Owen Andersone50ed302009-08-10 22:56:29 +00003794 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00003795 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00003796 bool isSEXTLoad = false;
3797 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003798 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003799 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
3800 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003801 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003802 } else
3803 return false;
3804
3805 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00003806 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00003807 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00003808 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003809 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00003810 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00003811 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
3812 isInc, DAG);
3813 if (!isLegal)
3814 return false;
3815
3816 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
3817 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00003818}
3819
Dan Gohman475871a2008-07-27 21:46:04 +00003820void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00003821 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003822 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003823 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00003824 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00003825 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003826 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00003827 switch (Op.getOpcode()) {
3828 default: break;
3829 case ARMISD::CMOV: {
3830 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00003831 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00003832 if (KnownZero == 0 && KnownOne == 0) return;
3833
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003834 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00003835 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
3836 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00003837 KnownZero &= KnownZeroRHS;
3838 KnownOne &= KnownOneRHS;
3839 return;
3840 }
3841 }
3842}
3843
3844//===----------------------------------------------------------------------===//
3845// ARM Inline Assembly Support
3846//===----------------------------------------------------------------------===//
3847
3848/// getConstraintType - Given a constraint letter, return the type of
3849/// constraint it is for this target.
3850ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00003851ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
3852 if (Constraint.size() == 1) {
3853 switch (Constraint[0]) {
3854 default: break;
3855 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003856 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00003857 }
Evan Chenga8e29892007-01-19 07:51:42 +00003858 }
Chris Lattner4234f572007-03-25 02:14:49 +00003859 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00003860}
3861
Bob Wilson2dc4f542009-03-20 22:42:55 +00003862std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00003863ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00003864 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003865 if (Constraint.size() == 1) {
3866 // GCC RS6000 Constraint Letters
3867 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003868 case 'l':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003869 if (Subtarget->isThumb1Only())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00003870 return std::make_pair(0U, ARM::tGPRRegisterClass);
3871 else
3872 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003873 case 'r':
3874 return std::make_pair(0U, ARM::GPRRegisterClass);
3875 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00003876 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003877 return std::make_pair(0U, ARM::SPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00003878 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003879 return std::make_pair(0U, ARM::DPRRegisterClass);
3880 break;
Evan Chenga8e29892007-01-19 07:51:42 +00003881 }
3882 }
3883 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3884}
3885
3886std::vector<unsigned> ARMTargetLowering::
3887getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00003888 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003889 if (Constraint.size() != 1)
3890 return std::vector<unsigned>();
3891
3892 switch (Constraint[0]) { // GCC ARM Constraint Letters
3893 default: break;
3894 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00003895 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
3896 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
3897 0);
Evan Chenga8e29892007-01-19 07:51:42 +00003898 case 'r':
3899 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
3900 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
3901 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
3902 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003903 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00003904 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003905 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
3906 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
3907 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
3908 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
3909 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
3910 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
3911 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
3912 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00003913 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003914 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
3915 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
3916 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
3917 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
3918 break;
Evan Chenga8e29892007-01-19 07:51:42 +00003919 }
3920
3921 return std::vector<unsigned>();
3922}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003923
3924/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3925/// vector. If it is invalid, don't add anything to Ops.
3926void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3927 char Constraint,
3928 bool hasMemory,
3929 std::vector<SDValue>&Ops,
3930 SelectionDAG &DAG) const {
3931 SDValue Result(0, 0);
3932
3933 switch (Constraint) {
3934 default: break;
3935 case 'I': case 'J': case 'K': case 'L':
3936 case 'M': case 'N': case 'O':
3937 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
3938 if (!C)
3939 return;
3940
3941 int64_t CVal64 = C->getSExtValue();
3942 int CVal = (int) CVal64;
3943 // None of these constraints allow values larger than 32 bits. Check
3944 // that the value fits in an int.
3945 if (CVal != CVal64)
3946 return;
3947
3948 switch (Constraint) {
3949 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003950 if (Subtarget->isThumb1Only()) {
3951 // This must be a constant between 0 and 255, for ADD
3952 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003953 if (CVal >= 0 && CVal <= 255)
3954 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00003955 } else if (Subtarget->isThumb2()) {
3956 // A constant that can be used as an immediate value in a
3957 // data-processing instruction.
3958 if (ARM_AM::getT2SOImmVal(CVal) != -1)
3959 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003960 } else {
3961 // A constant that can be used as an immediate value in a
3962 // data-processing instruction.
3963 if (ARM_AM::getSOImmVal(CVal) != -1)
3964 break;
3965 }
3966 return;
3967
3968 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003969 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003970 // This must be a constant between -255 and -1, for negated ADD
3971 // immediates. This can be used in GCC with an "n" modifier that
3972 // prints the negated value, for use with SUB instructions. It is
3973 // not useful otherwise but is implemented for compatibility.
3974 if (CVal >= -255 && CVal <= -1)
3975 break;
3976 } else {
3977 // This must be a constant between -4095 and 4095. It is not clear
3978 // what this constraint is intended for. Implemented for
3979 // compatibility with GCC.
3980 if (CVal >= -4095 && CVal <= 4095)
3981 break;
3982 }
3983 return;
3984
3985 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003986 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003987 // A 32-bit value where only one byte has a nonzero value. Exclude
3988 // zero to match GCC. This constraint is used by GCC internally for
3989 // constants that can be loaded with a move/shift combination.
3990 // It is not useful otherwise but is implemented for compatibility.
3991 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
3992 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00003993 } else if (Subtarget->isThumb2()) {
3994 // A constant whose bitwise inverse can be used as an immediate
3995 // value in a data-processing instruction. This can be used in GCC
3996 // with a "B" modifier that prints the inverted value, for use with
3997 // BIC and MVN instructions. It is not useful otherwise but is
3998 // implemented for compatibility.
3999 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
4000 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004001 } else {
4002 // A constant whose bitwise inverse can be used as an immediate
4003 // value in a data-processing instruction. This can be used in GCC
4004 // with a "B" modifier that prints the inverted value, for use with
4005 // BIC and MVN instructions. It is not useful otherwise but is
4006 // implemented for compatibility.
4007 if (ARM_AM::getSOImmVal(~CVal) != -1)
4008 break;
4009 }
4010 return;
4011
4012 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004013 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004014 // This must be a constant between -7 and 7,
4015 // for 3-operand ADD/SUB immediate instructions.
4016 if (CVal >= -7 && CVal < 7)
4017 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004018 } else if (Subtarget->isThumb2()) {
4019 // A constant whose negation can be used as an immediate value in a
4020 // data-processing instruction. This can be used in GCC with an "n"
4021 // modifier that prints the negated value, for use with SUB
4022 // instructions. It is not useful otherwise but is implemented for
4023 // compatibility.
4024 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
4025 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004026 } else {
4027 // A constant whose negation can be used as an immediate value in a
4028 // data-processing instruction. This can be used in GCC with an "n"
4029 // modifier that prints the negated value, for use with SUB
4030 // instructions. It is not useful otherwise but is implemented for
4031 // compatibility.
4032 if (ARM_AM::getSOImmVal(-CVal) != -1)
4033 break;
4034 }
4035 return;
4036
4037 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004038 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004039 // This must be a multiple of 4 between 0 and 1020, for
4040 // ADD sp + immediate.
4041 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
4042 break;
4043 } else {
4044 // A power of two or a constant between 0 and 32. This is used in
4045 // GCC for the shift amount on shifted register operands, but it is
4046 // useful in general for any shift amounts.
4047 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
4048 break;
4049 }
4050 return;
4051
4052 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004053 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004054 // This must be a constant between 0 and 31, for shift amounts.
4055 if (CVal >= 0 && CVal <= 31)
4056 break;
4057 }
4058 return;
4059
4060 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004061 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004062 // This must be a multiple of 4 between -508 and 508, for
4063 // ADD/SUB sp = sp + immediate.
4064 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
4065 break;
4066 }
4067 return;
4068 }
4069 Result = DAG.getTargetConstant(CVal, Op.getValueType());
4070 break;
4071 }
4072
4073 if (Result.getNode()) {
4074 Ops.push_back(Result);
4075 return;
4076 }
4077 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
4078 Ops, DAG);
4079}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00004080
4081bool
4082ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
4083 // The ARM target isn't yet aware of offsets.
4084 return false;
4085}