blob: 1ac594e192c33b5c95c4282e936349d606d04391 [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);
80 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
81 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Owen Anderson70671842009-08-10 20:18:46 +000082 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Custom);
Anton Korobeynikov8e6c2b92009-08-21 12:40:35 +000083 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +000084 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +000085 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
86 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
87 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +000088 }
89
90 // Promote all bit-wise operations.
91 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +000092 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000093 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
94 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +000095 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +000096 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +000097 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +000098 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +000099 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000100 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000101 }
102}
103
Owen Andersone50ed302009-08-10 22:56:29 +0000104void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000105 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000106 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000107}
108
Owen Andersone50ed302009-08-10 22:56:29 +0000109void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000110 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000111 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000112}
113
Chris Lattnerf0144122009-07-28 03:13:23 +0000114static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
115 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000116 return new TargetLoweringObjectFileMachO();
Chris Lattner80ec2792009-08-02 00:34:36 +0000117 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000118}
119
Evan Chenga8e29892007-01-19 07:51:42 +0000120ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000121 : TargetLowering(TM, createTLOF(TM)), ARMPCLabelIndex(0) {
Evan Chenga8e29892007-01-19 07:51:42 +0000122 Subtarget = &TM.getSubtarget<ARMSubtarget>();
123
Evan Chengb1df8f22007-04-27 08:15:43 +0000124 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000125 // Uses VFP for Thumb libfuncs if available.
126 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
127 // Single-precision floating-point arithmetic.
128 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
129 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
130 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
131 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000132
Evan Chengb1df8f22007-04-27 08:15:43 +0000133 // Double-precision floating-point arithmetic.
134 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
135 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
136 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
137 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000138
Evan Chengb1df8f22007-04-27 08:15:43 +0000139 // Single-precision comparisons.
140 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
141 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
142 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
143 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
144 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
145 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
146 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
147 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000148
Evan Chengb1df8f22007-04-27 08:15:43 +0000149 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
150 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
151 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
152 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
153 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
154 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
155 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
156 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000157
Evan Chengb1df8f22007-04-27 08:15:43 +0000158 // Double-precision comparisons.
159 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
160 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
161 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
162 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
163 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
164 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
165 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
166 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000167
Evan Chengb1df8f22007-04-27 08:15:43 +0000168 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
169 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
170 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
171 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
172 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
173 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
174 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
175 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000176
Evan Chengb1df8f22007-04-27 08:15:43 +0000177 // Floating-point to integer conversions.
178 // i64 conversions are done via library routines even when generating VFP
179 // instructions, so use the same ones.
180 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
181 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
182 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
183 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000184
Evan Chengb1df8f22007-04-27 08:15:43 +0000185 // Conversions between floating types.
186 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
187 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
188
189 // Integer to floating-point conversions.
190 // i64 conversions are done via library routines even when generating VFP
191 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000192 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
193 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000194 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
195 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
196 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
197 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
198 }
Evan Chenga8e29892007-01-19 07:51:42 +0000199 }
200
Bob Wilson2f954612009-05-22 17:38:41 +0000201 // These libcalls are not available in 32-bit.
202 setLibcallName(RTLIB::SHL_I128, 0);
203 setLibcallName(RTLIB::SRL_I128, 0);
204 setLibcallName(RTLIB::SRA_I128, 0);
205
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000206 // Libcalls should use the AAPCS base standard ABI, even if hard float
207 // is in effect, as per the ARM RTABI specification, section 4.1.2.
208 if (Subtarget->isAAPCS_ABI()) {
209 for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i) {
210 setLibcallCallingConv(static_cast<RTLIB::Libcall>(i),
211 CallingConv::ARM_AAPCS);
212 }
213 }
214
David Goodwinf1daf7d2009-07-08 23:10:31 +0000215 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000216 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000217 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000218 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000219 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000220 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
221 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000222
Owen Anderson825b72b2009-08-11 20:47:22 +0000223 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000224 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000225
226 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000227 addDRTypeForNEON(MVT::v2f32);
228 addDRTypeForNEON(MVT::v8i8);
229 addDRTypeForNEON(MVT::v4i16);
230 addDRTypeForNEON(MVT::v2i32);
231 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000232
Owen Anderson825b72b2009-08-11 20:47:22 +0000233 addQRTypeForNEON(MVT::v4f32);
234 addQRTypeForNEON(MVT::v2f64);
235 addQRTypeForNEON(MVT::v16i8);
236 addQRTypeForNEON(MVT::v8i16);
237 addQRTypeForNEON(MVT::v4i32);
238 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000239
Bob Wilson74dc72e2009-09-15 23:55:57 +0000240 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
241 // neither Neon nor VFP support any arithmetic operations on it.
242 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
243 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
244 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
245 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
246 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
247 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
248 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
249 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
250 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
251 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
252 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
253 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
254 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
255 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
256 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
257 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
258 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
259 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
260 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
261 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
262 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
263 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
264 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
265 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
266
Bob Wilson5bafff32009-06-22 23:27:02 +0000267 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
268 setTargetDAGCombine(ISD::SHL);
269 setTargetDAGCombine(ISD::SRL);
270 setTargetDAGCombine(ISD::SRA);
271 setTargetDAGCombine(ISD::SIGN_EXTEND);
272 setTargetDAGCombine(ISD::ZERO_EXTEND);
273 setTargetDAGCombine(ISD::ANY_EXTEND);
274 }
275
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000276 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000277
278 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000279 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000280
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000281 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000282 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000283
Evan Chenga8e29892007-01-19 07:51:42 +0000284 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000285 if (!Subtarget->isThumb1Only()) {
286 for (unsigned im = (unsigned)ISD::PRE_INC;
287 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000288 setIndexedLoadAction(im, MVT::i1, Legal);
289 setIndexedLoadAction(im, MVT::i8, Legal);
290 setIndexedLoadAction(im, MVT::i16, Legal);
291 setIndexedLoadAction(im, MVT::i32, Legal);
292 setIndexedStoreAction(im, MVT::i1, Legal);
293 setIndexedStoreAction(im, MVT::i8, Legal);
294 setIndexedStoreAction(im, MVT::i16, Legal);
295 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000296 }
Evan Chenga8e29892007-01-19 07:51:42 +0000297 }
298
299 // i64 operation support.
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000300 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000301 setOperationAction(ISD::MUL, MVT::i64, Expand);
302 setOperationAction(ISD::MULHU, MVT::i32, Expand);
303 setOperationAction(ISD::MULHS, MVT::i32, Expand);
304 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
305 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000306 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000307 setOperationAction(ISD::MUL, MVT::i64, Expand);
308 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb6207242009-08-01 00:16:10 +0000309 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000310 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000311 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000312 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
313 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
314 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
315 setOperationAction(ISD::SRL, MVT::i64, Custom);
316 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000317
318 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000319 setOperationAction(ISD::ROTL, MVT::i32, Expand);
320 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
321 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000322 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000323 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000324
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000325 // Only ARMv6 has BSWAP.
326 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000327 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000328
Evan Chenga8e29892007-01-19 07:51:42 +0000329 // These are expanded into libcalls.
Owen Anderson825b72b2009-08-11 20:47:22 +0000330 setOperationAction(ISD::SDIV, MVT::i32, Expand);
331 setOperationAction(ISD::UDIV, MVT::i32, Expand);
332 setOperationAction(ISD::SREM, MVT::i32, Expand);
333 setOperationAction(ISD::UREM, MVT::i32, Expand);
334 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
335 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000336
Evan Chenga8e29892007-01-19 07:51:42 +0000337 // Support label based line numbers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000338 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
339 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000340
Owen Anderson825b72b2009-08-11 20:47:22 +0000341 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
342 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
343 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
344 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000345
Evan Chenga8e29892007-01-19 07:51:42 +0000346 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000347 setOperationAction(ISD::VASTART, MVT::Other, Custom);
348 setOperationAction(ISD::VAARG, MVT::Other, Expand);
349 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
350 setOperationAction(ISD::VAEND, MVT::Other, Expand);
351 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
352 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000353 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
354 // FIXME: Shouldn't need this, since no register is used, but the legalizer
355 // doesn't yet know how to not do that for SjLj.
356 setExceptionSelectorRegister(ARM::R0);
Evan Cheng86198642009-08-07 00:34:42 +0000357 if (Subtarget->isThumb())
Owen Anderson825b72b2009-08-11 20:47:22 +0000358 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Evan Cheng86198642009-08-07 00:34:42 +0000359 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000360 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
361 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000362
Evan Chengd27c9fc2009-07-03 01:43:10 +0000363 if (!Subtarget->hasV6Ops() && !Subtarget->isThumb2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000364 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
365 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000366 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000367 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000368
David Goodwinf1daf7d2009-07-08 23:10:31 +0000369 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only())
Evan Chengc7c77292008-11-04 19:57:48 +0000370 // Turn f64->i64 into FMRRD, i64 -> f64 to FMDRR iff target supports vfp2.
Owen Anderson825b72b2009-08-11 20:47:22 +0000371 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000372
373 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000374 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
375 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
376 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000377
Owen Anderson825b72b2009-08-11 20:47:22 +0000378 setOperationAction(ISD::SETCC, MVT::i32, Expand);
379 setOperationAction(ISD::SETCC, MVT::f32, Expand);
380 setOperationAction(ISD::SETCC, MVT::f64, Expand);
381 setOperationAction(ISD::SELECT, MVT::i32, Expand);
382 setOperationAction(ISD::SELECT, MVT::f32, Expand);
383 setOperationAction(ISD::SELECT, MVT::f64, Expand);
384 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
385 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
386 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000387
Owen Anderson825b72b2009-08-11 20:47:22 +0000388 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
389 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
390 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
391 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
392 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000393
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000394 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000395 setOperationAction(ISD::FSIN, MVT::f64, Expand);
396 setOperationAction(ISD::FSIN, MVT::f32, Expand);
397 setOperationAction(ISD::FCOS, MVT::f32, Expand);
398 setOperationAction(ISD::FCOS, MVT::f64, Expand);
399 setOperationAction(ISD::FREM, MVT::f64, Expand);
400 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000401 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000402 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
403 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000404 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000405 setOperationAction(ISD::FPOW, MVT::f64, Expand);
406 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000407
Evan Chenga8e29892007-01-19 07:51:42 +0000408 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
David Goodwinf1daf7d2009-07-08 23:10:31 +0000409 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000410 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
411 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
412 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
413 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000414 }
Evan Chenga8e29892007-01-19 07:51:42 +0000415
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000416 // We have target-specific dag combine patterns for the following nodes:
417 // ARMISD::FMRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000418 setTargetDAGCombine(ISD::ADD);
419 setTargetDAGCombine(ISD::SUB);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000420
Evan Chenga8e29892007-01-19 07:51:42 +0000421 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Chenga8e29892007-01-19 07:51:42 +0000422 setSchedulingPreference(SchedulingForRegPressure);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000423
Evan Chengbc9b7542009-08-15 07:59:10 +0000424 // FIXME: If-converter should use instruction latency to determine
425 // profitability rather than relying on fixed limits.
426 if (Subtarget->getCPUString() == "generic") {
427 // Generic (and overly aggressive) if-conversion limits.
428 setIfCvtBlockSizeLimit(10);
429 setIfCvtDupBlockSizeLimit(2);
430 } else if (Subtarget->hasV6Ops()) {
431 setIfCvtBlockSizeLimit(2);
432 setIfCvtDupBlockSizeLimit(1);
433 } else {
434 setIfCvtBlockSizeLimit(3);
435 setIfCvtDupBlockSizeLimit(2);
Evan Cheng8557c2b2009-06-19 01:51:50 +0000436 }
437
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000438 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Bob Wilsone6abdff2009-05-18 20:55:32 +0000439 // Do not enable CodePlacementOpt for now: it currently runs after the
440 // ARMConstantIslandPass and messes up branch relaxation and placement
441 // of constant islands.
442 // benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000443}
444
Evan Chenga8e29892007-01-19 07:51:42 +0000445const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
446 switch (Opcode) {
447 default: return 0;
448 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000449 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
450 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000451 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000452 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
453 case ARMISD::tCALL: return "ARMISD::tCALL";
454 case ARMISD::BRCOND: return "ARMISD::BRCOND";
455 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000456 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000457 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
458 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
459 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000460 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000461 case ARMISD::CMPFP: return "ARMISD::CMPFP";
462 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
463 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
464 case ARMISD::CMOV: return "ARMISD::CMOV";
465 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000466
Evan Chenga8e29892007-01-19 07:51:42 +0000467 case ARMISD::FTOSI: return "ARMISD::FTOSI";
468 case ARMISD::FTOUI: return "ARMISD::FTOUI";
469 case ARMISD::SITOF: return "ARMISD::SITOF";
470 case ARMISD::UITOF: return "ARMISD::UITOF";
Evan Chenga8e29892007-01-19 07:51:42 +0000471
472 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
473 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
474 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000475
Evan Chenga8e29892007-01-19 07:51:42 +0000476 case ARMISD::FMRRD: return "ARMISD::FMRRD";
477 case ARMISD::FMDRR: return "ARMISD::FMDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000478
479 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000480
Evan Cheng86198642009-08-07 00:34:42 +0000481 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
482
Bob Wilson5bafff32009-06-22 23:27:02 +0000483 case ARMISD::VCEQ: return "ARMISD::VCEQ";
484 case ARMISD::VCGE: return "ARMISD::VCGE";
485 case ARMISD::VCGEU: return "ARMISD::VCGEU";
486 case ARMISD::VCGT: return "ARMISD::VCGT";
487 case ARMISD::VCGTU: return "ARMISD::VCGTU";
488 case ARMISD::VTST: return "ARMISD::VTST";
489
490 case ARMISD::VSHL: return "ARMISD::VSHL";
491 case ARMISD::VSHRs: return "ARMISD::VSHRs";
492 case ARMISD::VSHRu: return "ARMISD::VSHRu";
493 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
494 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
495 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
496 case ARMISD::VSHRN: return "ARMISD::VSHRN";
497 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
498 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
499 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
500 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
501 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
502 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
503 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
504 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
505 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
506 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
507 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
508 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
509 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
510 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000511 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000512 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000513 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000514 case ARMISD::VREV64: return "ARMISD::VREV64";
515 case ARMISD::VREV32: return "ARMISD::VREV32";
516 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000517 case ARMISD::VZIP: return "ARMISD::VZIP";
518 case ARMISD::VUZP: return "ARMISD::VUZP";
519 case ARMISD::VTRN: return "ARMISD::VTRN";
Evan Chenga8e29892007-01-19 07:51:42 +0000520 }
521}
522
Bill Wendlingb4202b82009-07-01 18:50:55 +0000523/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000524unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
525 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 1 : 2;
526}
527
Evan Chenga8e29892007-01-19 07:51:42 +0000528//===----------------------------------------------------------------------===//
529// Lowering Code
530//===----------------------------------------------------------------------===//
531
Evan Chenga8e29892007-01-19 07:51:42 +0000532/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
533static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
534 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000535 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000536 case ISD::SETNE: return ARMCC::NE;
537 case ISD::SETEQ: return ARMCC::EQ;
538 case ISD::SETGT: return ARMCC::GT;
539 case ISD::SETGE: return ARMCC::GE;
540 case ISD::SETLT: return ARMCC::LT;
541 case ISD::SETLE: return ARMCC::LE;
542 case ISD::SETUGT: return ARMCC::HI;
543 case ISD::SETUGE: return ARMCC::HS;
544 case ISD::SETULT: return ARMCC::LO;
545 case ISD::SETULE: return ARMCC::LS;
546 }
547}
548
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000549/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
550static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000551 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000552 CondCode2 = ARMCC::AL;
553 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000554 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000555 case ISD::SETEQ:
556 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
557 case ISD::SETGT:
558 case ISD::SETOGT: CondCode = ARMCC::GT; break;
559 case ISD::SETGE:
560 case ISD::SETOGE: CondCode = ARMCC::GE; break;
561 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000562 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000563 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
564 case ISD::SETO: CondCode = ARMCC::VC; break;
565 case ISD::SETUO: CondCode = ARMCC::VS; break;
566 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
567 case ISD::SETUGT: CondCode = ARMCC::HI; break;
568 case ISD::SETUGE: CondCode = ARMCC::PL; break;
569 case ISD::SETLT:
570 case ISD::SETULT: CondCode = ARMCC::LT; break;
571 case ISD::SETLE:
572 case ISD::SETULE: CondCode = ARMCC::LE; break;
573 case ISD::SETNE:
574 case ISD::SETUNE: CondCode = ARMCC::NE; break;
575 }
Evan Chenga8e29892007-01-19 07:51:42 +0000576}
577
Bob Wilson1f595bb2009-04-17 19:07:39 +0000578//===----------------------------------------------------------------------===//
579// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +0000580//===----------------------------------------------------------------------===//
581
582#include "ARMGenCallingConv.inc"
583
584// APCS f64 is in register pairs, possibly split to stack
Owen Andersone50ed302009-08-10 22:56:29 +0000585static bool f64AssignAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000586 CCValAssign::LocInfo &LocInfo,
587 CCState &State, bool CanFail) {
588 static const unsigned RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
589
590 // Try to get the first register.
591 if (unsigned Reg = State.AllocateReg(RegList, 4))
592 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
593 else {
594 // For the 2nd half of a v2f64, do not fail.
595 if (CanFail)
596 return false;
597
598 // Put the whole thing on the stack.
599 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
600 State.AllocateStack(8, 4),
601 LocVT, LocInfo));
602 return true;
603 }
604
605 // Try to get the second register.
606 if (unsigned Reg = State.AllocateReg(RegList, 4))
607 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
608 else
609 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
610 State.AllocateStack(4, 4),
611 LocVT, LocInfo));
612 return true;
613}
614
Owen Andersone50ed302009-08-10 22:56:29 +0000615static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000616 CCValAssign::LocInfo &LocInfo,
617 ISD::ArgFlagsTy &ArgFlags,
618 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000619 if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
620 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000621 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000622 !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
623 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000624 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000625}
626
627// AAPCS f64 is in aligned register pairs
Owen Andersone50ed302009-08-10 22:56:29 +0000628static bool f64AssignAAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000629 CCValAssign::LocInfo &LocInfo,
630 CCState &State, bool CanFail) {
631 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
632 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
633
634 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
635 if (Reg == 0) {
636 // For the 2nd half of a v2f64, do not just fail.
637 if (CanFail)
638 return false;
639
640 // Put the whole thing on the stack.
641 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
642 State.AllocateStack(8, 8),
643 LocVT, LocInfo));
644 return true;
645 }
646
647 unsigned i;
648 for (i = 0; i < 2; ++i)
649 if (HiRegList[i] == Reg)
650 break;
651
652 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
653 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
654 LocVT, LocInfo));
655 return true;
656}
657
Owen Andersone50ed302009-08-10 22:56:29 +0000658static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000659 CCValAssign::LocInfo &LocInfo,
660 ISD::ArgFlagsTy &ArgFlags,
661 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000662 if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
663 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000664 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000665 !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
666 return false;
667 return true; // we handled it
668}
669
Owen Andersone50ed302009-08-10 22:56:29 +0000670static bool f64RetAssign(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000671 CCValAssign::LocInfo &LocInfo, CCState &State) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000672 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
673 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
674
Bob Wilsone65586b2009-04-17 20:40:45 +0000675 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
676 if (Reg == 0)
677 return false; // we didn't handle it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000678
Bob Wilsone65586b2009-04-17 20:40:45 +0000679 unsigned i;
680 for (i = 0; i < 2; ++i)
681 if (HiRegList[i] == Reg)
682 break;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000683
Bob Wilson5bafff32009-06-22 23:27:02 +0000684 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bob Wilsone65586b2009-04-17 20:40:45 +0000685 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000686 LocVT, LocInfo));
687 return true;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000688}
689
Owen Andersone50ed302009-08-10 22:56:29 +0000690static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000691 CCValAssign::LocInfo &LocInfo,
692 ISD::ArgFlagsTy &ArgFlags,
693 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000694 if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
695 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000696 if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
Bob Wilson5bafff32009-06-22 23:27:02 +0000697 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000698 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000699}
700
Owen Andersone50ed302009-08-10 22:56:29 +0000701static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000702 CCValAssign::LocInfo &LocInfo,
703 ISD::ArgFlagsTy &ArgFlags,
704 CCState &State) {
705 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
706 State);
707}
708
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000709/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
710/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000711CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000712 bool Return,
713 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000714 switch (CC) {
715 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000716 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000717 case CallingConv::C:
718 case CallingConv::Fast:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000719 // Use target triple & subtarget features to do actual dispatch.
720 if (Subtarget->isAAPCS_ABI()) {
721 if (Subtarget->hasVFP2() &&
722 FloatABIType == FloatABI::Hard && !isVarArg)
723 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
724 else
725 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
726 } else
727 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000728 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000729 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000730 case CallingConv::ARM_AAPCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000731 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000732 case CallingConv::ARM_APCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000733 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000734 }
735}
736
Dan Gohman98ca4f22009-08-05 01:29:28 +0000737/// LowerCallResult - Lower the result values of a call into the
738/// appropriate copies out of appropriate physical registers.
739SDValue
740ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000741 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000742 const SmallVectorImpl<ISD::InputArg> &Ins,
743 DebugLoc dl, SelectionDAG &DAG,
744 SmallVectorImpl<SDValue> &InVals) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000745
Bob Wilson1f595bb2009-04-17 19:07:39 +0000746 // Assign locations to each value returned by this call.
747 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000748 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000749 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000750 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000751 CCAssignFnForNode(CallConv, /* Return*/ true,
752 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000753
754 // Copy all of the result registers out of their specified physreg.
755 for (unsigned i = 0; i != RVLocs.size(); ++i) {
756 CCValAssign VA = RVLocs[i];
757
Bob Wilson80915242009-04-25 00:33:20 +0000758 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000759 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000760 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000761 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000762 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000763 Chain = Lo.getValue(1);
764 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000765 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000766 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000767 InFlag);
768 Chain = Hi.getValue(1);
769 InFlag = Hi.getValue(2);
Owen Anderson825b72b2009-08-11 20:47:22 +0000770 Val = DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +0000771
Owen Anderson825b72b2009-08-11 20:47:22 +0000772 if (VA.getLocVT() == MVT::v2f64) {
773 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
774 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
775 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000776
777 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000778 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000779 Chain = Lo.getValue(1);
780 InFlag = Lo.getValue(2);
781 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000782 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000783 Chain = Hi.getValue(1);
784 InFlag = Hi.getValue(2);
Owen Anderson825b72b2009-08-11 20:47:22 +0000785 Val = DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
786 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
787 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000788 }
Bob Wilson1f595bb2009-04-17 19:07:39 +0000789 } else {
Bob Wilson80915242009-04-25 00:33:20 +0000790 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
791 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000792 Chain = Val.getValue(1);
793 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000794 }
Bob Wilson80915242009-04-25 00:33:20 +0000795
796 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000797 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +0000798 case CCValAssign::Full: break;
799 case CCValAssign::BCvt:
800 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
801 break;
802 }
803
Dan Gohman98ca4f22009-08-05 01:29:28 +0000804 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000805 }
806
Dan Gohman98ca4f22009-08-05 01:29:28 +0000807 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000808}
809
810/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
811/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +0000812/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +0000813/// a byval function parameter.
814/// Sometimes what we are copying is the end of a larger object, the part that
815/// does not fit in registers.
816static SDValue
817CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
818 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
819 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000820 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000821 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
822 /*AlwaysInline=*/false, NULL, 0, NULL, 0);
823}
824
Bob Wilsondee46d72009-04-17 20:35:10 +0000825/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000826SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +0000827ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
828 SDValue StackPtr, SDValue Arg,
829 DebugLoc dl, SelectionDAG &DAG,
830 const CCValAssign &VA,
831 ISD::ArgFlagsTy Flags) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000832 unsigned LocMemOffset = VA.getLocMemOffset();
833 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
834 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
835 if (Flags.isByVal()) {
836 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
837 }
838 return DAG.getStore(Chain, dl, Arg, PtrOff,
839 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chenga8e29892007-01-19 07:51:42 +0000840}
841
Dan Gohman98ca4f22009-08-05 01:29:28 +0000842void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +0000843 SDValue Chain, SDValue &Arg,
844 RegsToPassVector &RegsToPass,
845 CCValAssign &VA, CCValAssign &NextVA,
846 SDValue &StackPtr,
847 SmallVector<SDValue, 8> &MemOpChains,
848 ISD::ArgFlagsTy Flags) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000849
850 SDValue fmrrd = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +0000851 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +0000852 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
853
854 if (NextVA.isRegLoc())
855 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
856 else {
857 assert(NextVA.isMemLoc());
858 if (StackPtr.getNode() == 0)
859 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
860
Dan Gohman98ca4f22009-08-05 01:29:28 +0000861 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
862 dl, DAG, NextVA,
863 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000864 }
865}
866
Dan Gohman98ca4f22009-08-05 01:29:28 +0000867/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +0000868/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
869/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000870SDValue
871ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000872 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000873 bool isTailCall,
874 const SmallVectorImpl<ISD::OutputArg> &Outs,
875 const SmallVectorImpl<ISD::InputArg> &Ins,
876 DebugLoc dl, SelectionDAG &DAG,
877 SmallVectorImpl<SDValue> &InVals) {
Evan Chenga8e29892007-01-19 07:51:42 +0000878
Bob Wilson1f595bb2009-04-17 19:07:39 +0000879 // Analyze operands of the call, assigning locations to each operand.
880 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000881 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
882 *DAG.getContext());
883 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000884 CCAssignFnForNode(CallConv, /* Return*/ false,
885 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +0000886
Bob Wilson1f595bb2009-04-17 19:07:39 +0000887 // Get a count of how many bytes are to be pushed on the stack.
888 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +0000889
890 // Adjust the stack pointer for the new arguments...
891 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattnere563bbc2008-10-11 22:08:30 +0000892 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +0000893
Owen Anderson825b72b2009-08-11 20:47:22 +0000894 SDValue StackPtr = DAG.getRegister(ARM::SP, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000895
Bob Wilson5bafff32009-06-22 23:27:02 +0000896 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000897 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +0000898
Bob Wilson1f595bb2009-04-17 19:07:39 +0000899 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +0000900 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000901 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
902 i != e;
903 ++i, ++realArgIdx) {
904 CCValAssign &VA = ArgLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +0000905 SDValue Arg = Outs[realArgIdx].Val;
906 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +0000907
Bob Wilson1f595bb2009-04-17 19:07:39 +0000908 // Promote the value if needed.
909 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000910 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +0000911 case CCValAssign::Full: break;
912 case CCValAssign::SExt:
913 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
914 break;
915 case CCValAssign::ZExt:
916 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
917 break;
918 case CCValAssign::AExt:
919 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
920 break;
921 case CCValAssign::BCvt:
922 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
923 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000924 }
925
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000926 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +0000927 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000928 if (VA.getLocVT() == MVT::v2f64) {
929 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
930 DAG.getConstant(0, MVT::i32));
931 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
932 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000933
Dan Gohman98ca4f22009-08-05 01:29:28 +0000934 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +0000935 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
936
937 VA = ArgLocs[++i]; // skip ahead to next loc
938 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000939 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +0000940 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
941 } else {
942 assert(VA.isMemLoc());
943 if (StackPtr.getNode() == 0)
944 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
945
Dan Gohman98ca4f22009-08-05 01:29:28 +0000946 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
947 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000948 }
949 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000950 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000951 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000952 }
953 } else if (VA.isRegLoc()) {
954 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
955 } else {
956 assert(VA.isMemLoc());
957 if (StackPtr.getNode() == 0)
958 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
959
Dan Gohman98ca4f22009-08-05 01:29:28 +0000960 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
961 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000962 }
Evan Chenga8e29892007-01-19 07:51:42 +0000963 }
964
965 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000966 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +0000967 &MemOpChains[0], MemOpChains.size());
968
969 // Build a sequence of copy-to-reg nodes chained together with token chain
970 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +0000971 SDValue InFlag;
Evan Chenga8e29892007-01-19 07:51:42 +0000972 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Bob Wilson2dc4f542009-03-20 22:42:55 +0000973 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000974 RegsToPass[i].second, InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +0000975 InFlag = Chain.getValue(1);
976 }
977
Bill Wendling056292f2008-09-16 21:48:12 +0000978 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
979 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
980 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +0000981 bool isDirect = false;
982 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +0000983 bool isLocalARMFunc = false;
Evan Chenga8e29892007-01-19 07:51:42 +0000984 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
985 GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +0000986 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +0000987 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +0000988 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +0000989 getTargetMachine().getRelocationModel() != Reloc::Static;
990 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +0000991 // ARM call to a local ARM function is predicable.
992 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
Evan Chengc60e76d2007-01-30 20:37:08 +0000993 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +0000994 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge4e4ed32009-08-28 23:18:09 +0000995 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +0000996 ARMPCLabelIndex,
997 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000998 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +0000999 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001000 Callee = DAG.getLoad(getPointerTy(), dl,
1001 DAG.getEntryNode(), CPAddr, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001002 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001003 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001004 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001005 } else
1006 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +00001007 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001008 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001009 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001010 getTargetMachine().getRelocationModel() != Reloc::Static;
1011 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001012 // tBX takes a register source operand.
1013 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001014 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Owen Anderson1d0be152009-08-13 21:58:54 +00001015 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001016 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001017 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001018 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001019 Callee = DAG.getLoad(getPointerTy(), dl,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001020 DAG.getEntryNode(), CPAddr, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001021 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001022 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001023 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001024 } else
Bill Wendling056292f2008-09-16 21:48:12 +00001025 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001026 }
1027
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001028 // FIXME: handle tail calls differently.
1029 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001030 if (Subtarget->isThumb()) {
1031 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001032 CallOpc = ARMISD::CALL_NOLINK;
1033 else
1034 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1035 } else {
1036 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001037 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1038 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001039 }
David Goodwinf1daf7d2009-07-08 23:10:31 +00001040 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb1Only()) {
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +00001041 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Owen Anderson825b72b2009-08-11 20:47:22 +00001042 Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001043 InFlag = Chain.getValue(1);
1044 }
1045
Dan Gohman475871a2008-07-27 21:46:04 +00001046 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001047 Ops.push_back(Chain);
1048 Ops.push_back(Callee);
1049
1050 // Add argument registers to the end of the list so that they are known live
1051 // into the call.
1052 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1053 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1054 RegsToPass[i].second.getValueType()));
1055
Gabor Greifba36cb52008-08-28 21:40:38 +00001056 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001057 Ops.push_back(InFlag);
Duncan Sands4bdcb612008-07-02 17:40:58 +00001058 // Returns a chain and a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00001059 Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Flag),
Duncan Sands4bdcb612008-07-02 17:40:58 +00001060 &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001061 InFlag = Chain.getValue(1);
1062
Chris Lattnere563bbc2008-10-11 22:08:30 +00001063 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1064 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001065 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001066 InFlag = Chain.getValue(1);
1067
Bob Wilson1f595bb2009-04-17 19:07:39 +00001068 // Handle result values, copying them out of physregs into vregs that we
1069 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001070 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1071 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001072}
1073
Dan Gohman98ca4f22009-08-05 01:29:28 +00001074SDValue
1075ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001076 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001077 const SmallVectorImpl<ISD::OutputArg> &Outs,
1078 DebugLoc dl, SelectionDAG &DAG) {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001079
Bob Wilsondee46d72009-04-17 20:35:10 +00001080 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001081 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001082
Bob Wilsondee46d72009-04-17 20:35:10 +00001083 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001084 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1085 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001086
Dan Gohman98ca4f22009-08-05 01:29:28 +00001087 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001088 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1089 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001090
1091 // If this is the first return lowered for this function, add
1092 // the regs to the liveout set for the function.
1093 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1094 for (unsigned i = 0; i != RVLocs.size(); ++i)
1095 if (RVLocs[i].isRegLoc())
1096 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001097 }
1098
Bob Wilson1f595bb2009-04-17 19:07:39 +00001099 SDValue Flag;
1100
1101 // Copy the result values into the output registers.
1102 for (unsigned i = 0, realRVLocIdx = 0;
1103 i != RVLocs.size();
1104 ++i, ++realRVLocIdx) {
1105 CCValAssign &VA = RVLocs[i];
1106 assert(VA.isRegLoc() && "Can only return in registers!");
1107
Dan Gohman98ca4f22009-08-05 01:29:28 +00001108 SDValue Arg = Outs[realRVLocIdx].Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001109
1110 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001111 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001112 case CCValAssign::Full: break;
1113 case CCValAssign::BCvt:
1114 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1115 break;
1116 }
1117
Bob Wilson1f595bb2009-04-17 19:07:39 +00001118 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001119 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001120 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001121 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1122 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001123 SDValue HalfGPRs = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001124 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001125
1126 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1127 Flag = Chain.getValue(1);
1128 VA = RVLocs[++i]; // skip ahead to next loc
1129 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1130 HalfGPRs.getValue(1), Flag);
1131 Flag = Chain.getValue(1);
1132 VA = RVLocs[++i]; // skip ahead to next loc
1133
1134 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001135 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1136 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001137 }
1138 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1139 // available.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001140 SDValue fmrrd = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001141 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001142 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001143 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001144 VA = RVLocs[++i]; // skip ahead to next loc
1145 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1146 Flag);
1147 } else
1148 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1149
Bob Wilsondee46d72009-04-17 20:35:10 +00001150 // Guarantee that all emitted copies are
1151 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001152 Flag = Chain.getValue(1);
1153 }
1154
1155 SDValue result;
1156 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001157 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001158 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001159 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001160
1161 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001162}
1163
Bob Wilson2dc4f542009-03-20 22:42:55 +00001164// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
Bob Wilsond2559bf2009-07-13 18:11:36 +00001165// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
Bill Wendling056292f2008-09-16 21:48:12 +00001166// one of the above mentioned nodes. It has to be wrapped because otherwise
1167// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1168// be used to form addressing mode. These wrapped nodes will be selected
1169// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001170static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001171 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001172 // FIXME there is no actual debug info here
1173 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001174 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001175 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001176 if (CP->isMachineConstantPoolEntry())
1177 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1178 CP->getAlignment());
1179 else
1180 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1181 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001182 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001183}
1184
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001185// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001186SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001187ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
1188 SelectionDAG &DAG) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001189 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001190 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001191 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1192 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001193 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001194 ARMCP::CPValue, PCAdj, "tlsgd", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001195 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001196 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001197 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001198 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001199
Owen Anderson825b72b2009-08-11 20:47:22 +00001200 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001201 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001202
1203 // call __tls_get_addr.
1204 ArgListTy Args;
1205 ArgListEntry Entry;
1206 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001207 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001208 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001209 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001210 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001211 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1212 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001213 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001214 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001215 return CallResult.first;
1216}
1217
1218// Lower ISD::GlobalTLSAddress using the "initial exec" or
1219// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001220SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001221ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001222 SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001223 GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001224 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001225 SDValue Offset;
1226 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001227 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001228 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001229 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001230
Chris Lattner4fb63d02009-07-15 04:12:33 +00001231 if (GV->isDeclaration()) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001232 // initial exec model
1233 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1234 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001235 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001236 ARMCP::CPValue, PCAdj, "gottpoff", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001237 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001238 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001239 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001240 Chain = Offset.getValue(1);
1241
Owen Anderson825b72b2009-08-11 20:47:22 +00001242 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001243 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001244
Dale Johannesen33c960f2009-02-04 20:06:27 +00001245 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001246 } else {
1247 // local exec model
Evan Chenge4e4ed32009-08-28 23:18:09 +00001248 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, "tpoff");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001249 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001250 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001251 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001252 }
1253
1254 // The address of the thread local variable is the add of the thread
1255 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001256 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001257}
1258
Dan Gohman475871a2008-07-27 21:46:04 +00001259SDValue
1260ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001261 // TODO: implement the "local dynamic" model
1262 assert(Subtarget->isTargetELF() &&
1263 "TLS not implemented for non-ELF targets");
1264 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1265 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1266 // otherwise use the "Local Exec" TLS Model
1267 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1268 return LowerToTLSGeneralDynamicModel(GA, DAG);
1269 else
1270 return LowerToTLSExecModels(GA, DAG);
1271}
1272
Dan Gohman475871a2008-07-27 21:46:04 +00001273SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001274 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001275 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001276 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001277 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1278 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1279 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001280 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001281 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001282 new ARMConstantPoolValue(GV, UseGOTOFF ? "GOTOFF" : "GOT");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001283 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001284 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001285 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Dale Johannesen33c960f2009-02-04 20:06:27 +00001286 CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001287 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001288 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001289 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001290 if (!UseGOTOFF)
Dale Johannesen33c960f2009-02-04 20:06:27 +00001291 Result = DAG.getLoad(PtrVT, dl, Chain, Result, NULL, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001292 return Result;
1293 } else {
Evan Cheng1606e8e2009-03-13 07:51:59 +00001294 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001295 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001296 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001297 }
1298}
1299
Dan Gohman475871a2008-07-27 21:46:04 +00001300SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001301 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001302 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001303 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001304 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1305 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Dan Gohman475871a2008-07-27 21:46:04 +00001306 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +00001307 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001308 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001309 else {
Evan Chenge4e4ed32009-08-28 23:18:09 +00001310 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
1311 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001312 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001313 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001314 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001315 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001316
Dale Johannesen33c960f2009-02-04 20:06:27 +00001317 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001318 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001319
1320 if (RelocM == Reloc::PIC_) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001321 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001322 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001323 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00001324
Evan Cheng63476a82009-09-03 07:04:02 +00001325 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Dale Johannesen33c960f2009-02-04 20:06:27 +00001326 Result = DAG.getLoad(PtrVT, dl, Chain, Result, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001327
1328 return Result;
1329}
1330
Dan Gohman475871a2008-07-27 21:46:04 +00001331SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001332 SelectionDAG &DAG){
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001333 assert(Subtarget->isTargetELF() &&
1334 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Owen Andersone50ed302009-08-10 22:56:29 +00001335 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001336 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001337 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00001338 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1339 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00001340 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001341 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001342 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001343 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001344 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001345 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001346}
1347
Bob Wilsona599bff2009-08-04 00:36:16 +00001348static SDValue LowerNeonVLDIntrinsic(SDValue Op, SelectionDAG &DAG,
Bob Wilson31fb12f2009-08-26 17:39:53 +00001349 unsigned NumVecs) {
Bob Wilsona599bff2009-08-04 00:36:16 +00001350 SDNode *Node = Op.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001351 EVT VT = Node->getValueType(0);
Bob Wilsona599bff2009-08-04 00:36:16 +00001352
Bob Wilson31fb12f2009-08-26 17:39:53 +00001353 // No expansion needed for 64-bit vectors.
1354 if (VT.is64BitVector())
1355 return SDValue();
Bob Wilsona599bff2009-08-04 00:36:16 +00001356
Bob Wilson31fb12f2009-08-26 17:39:53 +00001357 // FIXME: We need to expand VLD3 and VLD4 of 128-bit vectors into separate
1358 // operations to load the even and odd registers.
1359 return SDValue();
Bob Wilsona599bff2009-08-04 00:36:16 +00001360}
1361
Bob Wilsonb36ec862009-08-06 18:47:44 +00001362static SDValue LowerNeonVSTIntrinsic(SDValue Op, SelectionDAG &DAG,
Bob Wilson31fb12f2009-08-26 17:39:53 +00001363 unsigned NumVecs) {
Bob Wilsonb36ec862009-08-06 18:47:44 +00001364 SDNode *Node = Op.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001365 EVT VT = Node->getOperand(3).getValueType();
Bob Wilsonb36ec862009-08-06 18:47:44 +00001366
Bob Wilson31fb12f2009-08-26 17:39:53 +00001367 // No expansion needed for 64-bit vectors.
1368 if (VT.is64BitVector())
1369 return SDValue();
Bob Wilsonb36ec862009-08-06 18:47:44 +00001370
Bob Wilson31fb12f2009-08-26 17:39:53 +00001371 // FIXME: We need to expand VST3 and VST4 of 128-bit vectors into separate
1372 // operations to store the even and odd registers.
1373 return SDValue();
Bob Wilsonb36ec862009-08-06 18:47:44 +00001374}
1375
Bob Wilson243fcc52009-09-01 04:26:28 +00001376static SDValue LowerNeonVLDLaneIntrinsic(SDValue Op, SelectionDAG &DAG,
1377 unsigned NumVecs) {
1378 SDNode *Node = Op.getNode();
1379 EVT VT = Node->getValueType(0);
1380
1381 if (!VT.is64BitVector())
1382 return SDValue(); // unimplemented
1383
1384 // Change the lane number operand to be a TargetConstant; otherwise it
1385 // will be legalized into a register.
1386 ConstantSDNode *Lane = dyn_cast<ConstantSDNode>(Node->getOperand(NumVecs+3));
1387 if (!Lane) {
1388 assert(false && "vld lane number must be a constant");
1389 return SDValue();
1390 }
1391 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
1392 Ops[NumVecs+3] = DAG.getTargetConstant(Lane->getZExtValue(), MVT::i32);
1393 return DAG.UpdateNodeOperands(Op, &Ops[0], Ops.size());
1394}
1395
Bob Wilson8a3198b2009-09-01 18:51:56 +00001396static SDValue LowerNeonVSTLaneIntrinsic(SDValue Op, SelectionDAG &DAG,
1397 unsigned NumVecs) {
1398 SDNode *Node = Op.getNode();
1399 EVT VT = Node->getOperand(3).getValueType();
1400
1401 if (!VT.is64BitVector())
1402 return SDValue(); // unimplemented
1403
1404 // Change the lane number operand to be a TargetConstant; otherwise it
1405 // will be legalized into a register.
1406 ConstantSDNode *Lane = dyn_cast<ConstantSDNode>(Node->getOperand(NumVecs+3));
1407 if (!Lane) {
1408 assert(false && "vst lane number must be a constant");
1409 return SDValue();
1410 }
1411 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
1412 Ops[NumVecs+3] = DAG.getTargetConstant(Lane->getZExtValue(), MVT::i32);
1413 return DAG.UpdateNodeOperands(Op, &Ops[0], Ops.size());
1414}
1415
Bob Wilsona599bff2009-08-04 00:36:16 +00001416SDValue
1417ARMTargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) {
1418 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1419 switch (IntNo) {
Bob Wilsonb0abb4d2009-08-11 05:39:44 +00001420 case Intrinsic::arm_neon_vld3:
Bob Wilson31fb12f2009-08-26 17:39:53 +00001421 return LowerNeonVLDIntrinsic(Op, DAG, 3);
Bob Wilsonb0abb4d2009-08-11 05:39:44 +00001422 case Intrinsic::arm_neon_vld4:
Bob Wilson31fb12f2009-08-26 17:39:53 +00001423 return LowerNeonVLDIntrinsic(Op, DAG, 4);
Bob Wilson243fcc52009-09-01 04:26:28 +00001424 case Intrinsic::arm_neon_vld2lane:
1425 return LowerNeonVLDLaneIntrinsic(Op, DAG, 2);
1426 case Intrinsic::arm_neon_vld3lane:
1427 return LowerNeonVLDLaneIntrinsic(Op, DAG, 3);
1428 case Intrinsic::arm_neon_vld4lane:
1429 return LowerNeonVLDLaneIntrinsic(Op, DAG, 4);
Bob Wilsonb0abb4d2009-08-11 05:39:44 +00001430 case Intrinsic::arm_neon_vst3:
Bob Wilson31fb12f2009-08-26 17:39:53 +00001431 return LowerNeonVSTIntrinsic(Op, DAG, 3);
Bob Wilsonb0abb4d2009-08-11 05:39:44 +00001432 case Intrinsic::arm_neon_vst4:
Bob Wilson31fb12f2009-08-26 17:39:53 +00001433 return LowerNeonVSTIntrinsic(Op, DAG, 4);
Bob Wilson8a3198b2009-09-01 18:51:56 +00001434 case Intrinsic::arm_neon_vst2lane:
1435 return LowerNeonVSTLaneIntrinsic(Op, DAG, 2);
1436 case Intrinsic::arm_neon_vst3lane:
1437 return LowerNeonVSTLaneIntrinsic(Op, DAG, 3);
1438 case Intrinsic::arm_neon_vst4lane:
1439 return LowerNeonVSTLaneIntrinsic(Op, DAG, 4);
Bob Wilsona599bff2009-08-04 00:36:16 +00001440 default: return SDValue(); // Don't custom lower most intrinsics.
1441 }
1442}
1443
Jim Grosbach0e0da732009-05-12 23:59:14 +00001444SDValue
1445ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001446 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001447 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001448 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001449 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00001450 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00001451 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00001452 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1453 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001454 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001455 MachineFunction &MF = DAG.getMachineFunction();
1456 EVT PtrVT = getPointerTy();
1457 DebugLoc dl = Op.getDebugLoc();
1458 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1459 SDValue CPAddr;
1460 unsigned PCAdj = (RelocM != Reloc::PIC_)
1461 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001462 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001463 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
1464 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001465 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001466 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001467 SDValue Result =
1468 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
1469 SDValue Chain = Result.getValue(1);
1470
1471 if (RelocM == Reloc::PIC_) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001472 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001473 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1474 }
1475 return Result;
1476 }
Jim Grosbachf9570122009-05-14 00:46:35 +00001477 case Intrinsic::eh_sjlj_setjmp:
Owen Anderson825b72b2009-08-11 20:47:22 +00001478 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(1));
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001479 }
1480}
1481
Dan Gohman475871a2008-07-27 21:46:04 +00001482static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001483 unsigned VarArgsFrameIndex) {
Evan Chenga8e29892007-01-19 07:51:42 +00001484 // vastart just stores the address of the VarArgsFrameIndex slot into the
1485 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001486 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001487 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001488 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001489 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001490 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001491}
1492
Dan Gohman475871a2008-07-27 21:46:04 +00001493SDValue
Evan Cheng86198642009-08-07 00:34:42 +00001494ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) {
1495 SDNode *Node = Op.getNode();
1496 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001497 EVT VT = Node->getValueType(0);
Evan Cheng86198642009-08-07 00:34:42 +00001498 SDValue Chain = Op.getOperand(0);
1499 SDValue Size = Op.getOperand(1);
1500 SDValue Align = Op.getOperand(2);
1501
1502 // Chain the dynamic stack allocation so that it doesn't modify the stack
1503 // pointer when other instructions are using the stack.
1504 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1505
1506 unsigned AlignVal = cast<ConstantSDNode>(Align)->getZExtValue();
1507 unsigned StackAlign = getTargetMachine().getFrameInfo()->getStackAlignment();
1508 if (AlignVal > StackAlign)
1509 // Do this now since selection pass cannot introduce new target
1510 // independent node.
1511 Align = DAG.getConstant(-(uint64_t)AlignVal, VT);
1512
1513 // In Thumb1 mode, there isn't a "sub r, sp, r" instruction, we will end up
1514 // using a "add r, sp, r" instead. Negate the size now so we don't have to
1515 // do even more horrible hack later.
1516 MachineFunction &MF = DAG.getMachineFunction();
1517 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1518 if (AFI->isThumb1OnlyFunction()) {
1519 bool Negate = true;
1520 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Size);
1521 if (C) {
1522 uint32_t Val = C->getZExtValue();
1523 if (Val <= 508 && ((Val & 3) == 0))
1524 Negate = false;
1525 }
1526 if (Negate)
1527 Size = DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(0, VT), Size);
1528 }
1529
Owen Anderson825b72b2009-08-11 20:47:22 +00001530 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
Evan Cheng86198642009-08-07 00:34:42 +00001531 SDValue Ops1[] = { Chain, Size, Align };
1532 SDValue Res = DAG.getNode(ARMISD::DYN_ALLOC, dl, VTList, Ops1, 3);
1533 Chain = Res.getValue(1);
1534 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1535 DAG.getIntPtrConstant(0, true), SDValue());
1536 SDValue Ops2[] = { Res, Chain };
1537 return DAG.getMergeValues(Ops2, 2, dl);
1538}
1539
1540SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00001541ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
1542 SDValue &Root, SelectionDAG &DAG,
1543 DebugLoc dl) {
1544 MachineFunction &MF = DAG.getMachineFunction();
1545 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1546
1547 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001548 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00001549 RC = ARM::tGPRRegisterClass;
1550 else
1551 RC = ARM::GPRRegisterClass;
1552
1553 // Transform the arguments stored in physical registers into virtual ones.
1554 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001555 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001556
1557 SDValue ArgValue2;
1558 if (NextVA.isMemLoc()) {
1559 unsigned ArgSize = NextVA.getLocVT().getSizeInBits()/8;
1560 MachineFrameInfo *MFI = MF.getFrameInfo();
1561 int FI = MFI->CreateFixedObject(ArgSize, NextVA.getLocMemOffset());
1562
1563 // Create load node to retrieve arguments from the stack.
1564 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00001565 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN, NULL, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00001566 } else {
1567 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001568 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001569 }
1570
Owen Anderson825b72b2009-08-11 20:47:22 +00001571 return DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00001572}
1573
1574SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001575ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001576 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001577 const SmallVectorImpl<ISD::InputArg>
1578 &Ins,
1579 DebugLoc dl, SelectionDAG &DAG,
1580 SmallVectorImpl<SDValue> &InVals) {
1581
Bob Wilson1f595bb2009-04-17 19:07:39 +00001582 MachineFunction &MF = DAG.getMachineFunction();
1583 MachineFrameInfo *MFI = MF.getFrameInfo();
1584
Bob Wilson1f595bb2009-04-17 19:07:39 +00001585 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1586
1587 // Assign locations to all of the incoming arguments.
1588 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001589 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1590 *DAG.getContext());
1591 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001592 CCAssignFnForNode(CallConv, /* Return*/ false,
1593 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001594
1595 SmallVector<SDValue, 16> ArgValues;
1596
1597 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1598 CCValAssign &VA = ArgLocs[i];
1599
Bob Wilsondee46d72009-04-17 20:35:10 +00001600 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001601 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001602 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00001603
Bob Wilson5bafff32009-06-22 23:27:02 +00001604 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001605 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001606 // f64 and vector types are split up into multiple registers or
1607 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00001608 RegVT = MVT::i32;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001609
Owen Anderson825b72b2009-08-11 20:47:22 +00001610 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001611 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001612 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00001613 VA = ArgLocs[++i]; // skip ahead to next loc
1614 SDValue ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001615 Chain, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001616 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1617 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001618 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00001619 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001620 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
1621 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001622 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001623
Bob Wilson5bafff32009-06-22 23:27:02 +00001624 } else {
1625 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001626
Owen Anderson825b72b2009-08-11 20:47:22 +00001627 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00001628 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001629 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00001630 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001631 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001632 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001633 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001634 RC = (AFI->isThumb1OnlyFunction() ?
1635 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00001636 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001637 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00001638
1639 // Transform the arguments in physical registers into virtual ones.
1640 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001641 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001642 }
1643
1644 // If this is an 8 or 16-bit value, it is really passed promoted
1645 // to 32 bits. Insert an assert[sz]ext to capture this, then
1646 // truncate to the right size.
1647 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001648 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001649 case CCValAssign::Full: break;
1650 case CCValAssign::BCvt:
1651 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1652 break;
1653 case CCValAssign::SExt:
1654 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
1655 DAG.getValueType(VA.getValVT()));
1656 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1657 break;
1658 case CCValAssign::ZExt:
1659 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
1660 DAG.getValueType(VA.getValVT()));
1661 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1662 break;
1663 }
1664
Dan Gohman98ca4f22009-08-05 01:29:28 +00001665 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001666
1667 } else { // VA.isRegLoc()
1668
1669 // sanity check
1670 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00001671 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001672
1673 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
1674 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset());
1675
Bob Wilsondee46d72009-04-17 20:35:10 +00001676 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001677 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001678 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN, NULL, 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001679 }
1680 }
1681
1682 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00001683 if (isVarArg) {
1684 static const unsigned GPRArgRegs[] = {
1685 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1686 };
1687
Bob Wilsondee46d72009-04-17 20:35:10 +00001688 unsigned NumGPRs = CCInfo.getFirstUnallocated
1689 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001690
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001691 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1692 unsigned VARegSize = (4 - NumGPRs) * 4;
1693 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001694 unsigned ArgOffset = 0;
Evan Chenga8e29892007-01-19 07:51:42 +00001695 if (VARegSaveSize) {
1696 // If this function is vararg, store any remaining integer argument regs
1697 // to their spots on the stack so that they may be loaded by deferencing
1698 // the result of va_next.
1699 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001700 ArgOffset = CCInfo.getNextStackOffset();
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001701 VarArgsFrameIndex = MFI->CreateFixedObject(VARegSaveSize, ArgOffset +
1702 VARegSaveSize - VARegSize);
Dan Gohman475871a2008-07-27 21:46:04 +00001703 SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001704
Dan Gohman475871a2008-07-27 21:46:04 +00001705 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00001706 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001707 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001708 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00001709 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001710 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00001711 RC = ARM::GPRRegisterClass;
1712
Bob Wilson998e1252009-04-20 18:36:57 +00001713 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001714 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001715 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001716 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001717 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00001718 DAG.getConstant(4, getPointerTy()));
1719 }
1720 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001721 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001722 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001723 } else
1724 // This will point to the next argument passed via stack.
1725 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1726 }
1727
Dan Gohman98ca4f22009-08-05 01:29:28 +00001728 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00001729}
1730
1731/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00001732static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00001733 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001734 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00001735 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00001736 // Maybe this has already been legalized into the constant pool?
1737 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00001738 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001739 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
1740 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001741 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001742 }
1743 }
1744 return false;
1745}
1746
David Goodwinf1daf7d2009-07-08 23:10:31 +00001747static bool isLegalCmpImmediate(unsigned C, bool isThumb1Only) {
1748 return ( isThumb1Only && (C & ~255U) == 0) ||
1749 (!isThumb1Only && ARM_AM::getSOImmVal(C) != -1);
Evan Chenga8e29892007-01-19 07:51:42 +00001750}
1751
1752/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1753/// the given operands.
Dan Gohman475871a2008-07-27 21:46:04 +00001754static SDValue getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
David Goodwinf1daf7d2009-07-08 23:10:31 +00001755 SDValue &ARMCC, SelectionDAG &DAG, bool isThumb1Only,
Dale Johannesende064702009-02-06 21:50:26 +00001756 DebugLoc dl) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001757 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001758 unsigned C = RHSC->getZExtValue();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001759 if (!isLegalCmpImmediate(C, isThumb1Only)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001760 // Constant does not fit, try adjusting it by one?
1761 switch (CC) {
1762 default: break;
1763 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00001764 case ISD::SETGE:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001765 if (isLegalCmpImmediate(C-1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001766 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001767 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001768 }
1769 break;
1770 case ISD::SETULT:
1771 case ISD::SETUGE:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001772 if (C > 0 && isLegalCmpImmediate(C-1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001773 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001774 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001775 }
1776 break;
1777 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00001778 case ISD::SETGT:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001779 if (isLegalCmpImmediate(C+1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001780 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001781 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001782 }
1783 break;
1784 case ISD::SETULE:
1785 case ISD::SETUGT:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001786 if (C < 0xffffffff && isLegalCmpImmediate(C+1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001787 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001788 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001789 }
1790 break;
1791 }
1792 }
1793 }
1794
1795 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001796 ARMISD::NodeType CompareType;
1797 switch (CondCode) {
1798 default:
1799 CompareType = ARMISD::CMP;
1800 break;
1801 case ARMCC::EQ:
1802 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00001803 // Uses only Z Flag
1804 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001805 break;
1806 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001807 ARMCC = DAG.getConstant(CondCode, MVT::i32);
1808 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001809}
1810
1811/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001812static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Dale Johannesende064702009-02-06 21:50:26 +00001813 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001814 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00001815 if (!isFloatingPointZero(RHS))
Owen Anderson825b72b2009-08-11 20:47:22 +00001816 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001817 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001818 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
1819 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001820}
1821
Dan Gohman475871a2008-07-27 21:46:04 +00001822static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001823 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00001824 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001825 SDValue LHS = Op.getOperand(0);
1826 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001827 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001828 SDValue TrueVal = Op.getOperand(2);
1829 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00001830 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001831
Owen Anderson825b72b2009-08-11 20:47:22 +00001832 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001833 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001834 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
David Goodwinf1daf7d2009-07-08 23:10:31 +00001835 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb1Only(), dl);
Dale Johannesende064702009-02-06 21:50:26 +00001836 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001837 }
1838
1839 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001840 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00001841
Owen Anderson825b72b2009-08-11 20:47:22 +00001842 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1843 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001844 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
1845 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00001846 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001847 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001848 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001849 // FIXME: Needs another CMP because flag can have but one use.
Dale Johannesende064702009-02-06 21:50:26 +00001850 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001851 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Dale Johannesende064702009-02-06 21:50:26 +00001852 Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00001853 }
1854 return Result;
1855}
1856
Dan Gohman475871a2008-07-27 21:46:04 +00001857static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001858 const ARMSubtarget *ST) {
Dan Gohman475871a2008-07-27 21:46:04 +00001859 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001860 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001861 SDValue LHS = Op.getOperand(2);
1862 SDValue RHS = Op.getOperand(3);
1863 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00001864 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001865
Owen Anderson825b72b2009-08-11 20:47:22 +00001866 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001867 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001868 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
David Goodwinf1daf7d2009-07-08 23:10:31 +00001869 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb1Only(), dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001870 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Dale Johannesende064702009-02-06 21:50:26 +00001871 Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001872 }
1873
Owen Anderson825b72b2009-08-11 20:47:22 +00001874 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Chenga8e29892007-01-19 07:51:42 +00001875 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001876 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001877
Dale Johannesende064702009-02-06 21:50:26 +00001878 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001879 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1880 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1881 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00001882 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00001883 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001884 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001885 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00001886 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00001887 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001888 }
1889 return Res;
1890}
1891
Dan Gohman475871a2008-07-27 21:46:04 +00001892SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) {
1893 SDValue Chain = Op.getOperand(0);
1894 SDValue Table = Op.getOperand(1);
1895 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001896 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001897
Owen Andersone50ed302009-08-10 22:56:29 +00001898 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00001899 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
1900 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00001901 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00001902 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00001903 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00001904 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
1905 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00001906 if (Subtarget->isThumb2()) {
1907 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
1908 // which does another jump to the destination. This also makes it easier
1909 // to translate it to TBB / TBH later.
1910 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00001911 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00001912 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001913 }
Evan Cheng66ac5312009-07-25 00:33:29 +00001914 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001915 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr, NULL, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00001916 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001917 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00001918 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001919 } else {
1920 Addr = DAG.getLoad(PTy, dl, Chain, Addr, NULL, 0);
1921 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00001922 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001923 }
Evan Chenga8e29892007-01-19 07:51:42 +00001924}
1925
Dan Gohman475871a2008-07-27 21:46:04 +00001926static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Dale Johannesende064702009-02-06 21:50:26 +00001927 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001928 unsigned Opc =
1929 Op.getOpcode() == ISD::FP_TO_SINT ? ARMISD::FTOSI : ARMISD::FTOUI;
Owen Anderson825b72b2009-08-11 20:47:22 +00001930 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
1931 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001932}
1933
Dan Gohman475871a2008-07-27 21:46:04 +00001934static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001935 EVT VT = Op.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001936 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001937 unsigned Opc =
1938 Op.getOpcode() == ISD::SINT_TO_FP ? ARMISD::SITOF : ARMISD::UITOF;
1939
Owen Anderson825b72b2009-08-11 20:47:22 +00001940 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
Dale Johannesende064702009-02-06 21:50:26 +00001941 return DAG.getNode(Opc, dl, VT, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001942}
1943
Dan Gohman475871a2008-07-27 21:46:04 +00001944static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00001945 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00001946 SDValue Tmp0 = Op.getOperand(0);
1947 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00001948 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001949 EVT VT = Op.getValueType();
1950 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001951 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
1952 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001953 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
1954 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001955 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001956}
1957
Jim Grosbach0e0da732009-05-12 23:59:14 +00001958SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
1959 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1960 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00001961 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001962 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
1963 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00001964 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00001965 ? ARM::R7 : ARM::R11;
1966 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
1967 while (Depth--)
1968 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
1969 return FrameAddr;
1970}
1971
Dan Gohman475871a2008-07-27 21:46:04 +00001972SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00001973ARMTargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman475871a2008-07-27 21:46:04 +00001974 SDValue Chain,
1975 SDValue Dst, SDValue Src,
1976 SDValue Size, unsigned Align,
Dan Gohman707e0182008-04-12 04:36:06 +00001977 bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00001978 const Value *DstSV, uint64_t DstSVOff,
1979 const Value *SrcSV, uint64_t SrcSVOff){
Evan Cheng4102eb52007-10-22 22:11:27 +00001980 // Do repeated 4-byte loads and stores. To be improved.
Dan Gohman707e0182008-04-12 04:36:06 +00001981 // This requires 4-byte alignment.
1982 if ((Align & 3) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00001983 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001984 // This requires the copy size to be a constant, preferrably
1985 // within a subtarget-specific limit.
1986 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
1987 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00001988 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001989 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001990 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00001991 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001992
1993 unsigned BytesLeft = SizeVal & 3;
1994 unsigned NumMemOps = SizeVal >> 2;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001995 unsigned EmittedNumMemOps = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001996 EVT VT = MVT::i32;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001997 unsigned VTSize = 4;
Evan Cheng4102eb52007-10-22 22:11:27 +00001998 unsigned i = 0;
Evan Chenge5e7ce42007-05-18 01:19:57 +00001999 const unsigned MAX_LOADS_IN_LDM = 6;
Dan Gohman475871a2008-07-27 21:46:04 +00002000 SDValue TFOps[MAX_LOADS_IN_LDM];
2001 SDValue Loads[MAX_LOADS_IN_LDM];
Dan Gohman1f13c682008-04-28 17:15:20 +00002002 uint64_t SrcOff = 0, DstOff = 0;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002003
Evan Cheng4102eb52007-10-22 22:11:27 +00002004 // Emit up to MAX_LOADS_IN_LDM loads, then a TokenFactor barrier, then the
2005 // same number of stores. The loads and stores will get combined into
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002006 // ldm/stm later on.
Evan Cheng4102eb52007-10-22 22:11:27 +00002007 while (EmittedNumMemOps < NumMemOps) {
2008 for (i = 0;
2009 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00002010 Loads[i] = DAG.getLoad(VT, dl, Chain,
Owen Anderson825b72b2009-08-11 20:47:22 +00002011 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
2012 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00002013 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00002014 TFOps[i] = Loads[i].getValue(1);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002015 SrcOff += VTSize;
2016 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002017 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002018
Evan Cheng4102eb52007-10-22 22:11:27 +00002019 for (i = 0;
2020 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00002021 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Owen Anderson825b72b2009-08-11 20:47:22 +00002022 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
2023 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00002024 DstSV, DstSVOff + DstOff);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002025 DstOff += VTSize;
2026 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002027 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00002028
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002029 EmittedNumMemOps += i;
2030 }
2031
Bob Wilson2dc4f542009-03-20 22:42:55 +00002032 if (BytesLeft == 0)
Evan Cheng4102eb52007-10-22 22:11:27 +00002033 return Chain;
2034
2035 // Issue loads / stores for the trailing (1 - 3) bytes.
2036 unsigned BytesLeftSave = BytesLeft;
2037 i = 0;
2038 while (BytesLeft) {
2039 if (BytesLeft >= 2) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002040 VT = MVT::i16;
Evan Cheng4102eb52007-10-22 22:11:27 +00002041 VTSize = 2;
2042 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00002043 VT = MVT::i8;
Evan Cheng4102eb52007-10-22 22:11:27 +00002044 VTSize = 1;
2045 }
2046
Dale Johannesen0f502f62009-02-03 22:26:09 +00002047 Loads[i] = DAG.getLoad(VT, dl, Chain,
Owen Anderson825b72b2009-08-11 20:47:22 +00002048 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
2049 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00002050 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00002051 TFOps[i] = Loads[i].getValue(1);
2052 ++i;
2053 SrcOff += VTSize;
2054 BytesLeft -= VTSize;
2055 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002056 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00002057
2058 i = 0;
2059 BytesLeft = BytesLeftSave;
2060 while (BytesLeft) {
2061 if (BytesLeft >= 2) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002062 VT = MVT::i16;
Evan Cheng4102eb52007-10-22 22:11:27 +00002063 VTSize = 2;
2064 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00002065 VT = MVT::i8;
Evan Cheng4102eb52007-10-22 22:11:27 +00002066 VTSize = 1;
2067 }
2068
Dale Johannesen0f502f62009-02-03 22:26:09 +00002069 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Owen Anderson825b72b2009-08-11 20:47:22 +00002070 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
2071 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00002072 DstSV, DstSVOff + DstOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00002073 ++i;
2074 DstOff += VTSize;
2075 BytesLeft -= VTSize;
2076 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002077 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002078}
2079
Duncan Sands1607f052008-12-01 11:39:25 +00002080static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00002081 SDValue Op = N->getOperand(0);
Dale Johannesende064702009-02-06 21:50:26 +00002082 DebugLoc dl = N->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00002083 if (N->getValueType(0) == MVT::f64) {
Evan Chengc7c77292008-11-04 19:57:48 +00002084 // Turn i64->f64 into FMDRR.
Owen Anderson825b72b2009-08-11 20:47:22 +00002085 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2086 DAG.getConstant(0, MVT::i32));
2087 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2088 DAG.getConstant(1, MVT::i32));
2089 return DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
Evan Chengc7c77292008-11-04 19:57:48 +00002090 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002091
Evan Chengc7c77292008-11-04 19:57:48 +00002092 // Turn f64->i64 into FMRRD.
Bob Wilson2dc4f542009-03-20 22:42:55 +00002093 SDValue Cvt = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00002094 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002095
Chris Lattner27a6c732007-11-24 07:07:01 +00002096 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002097 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
Chris Lattner27a6c732007-11-24 07:07:01 +00002098}
2099
Bob Wilson5bafff32009-06-22 23:27:02 +00002100/// getZeroVector - Returns a vector of specified type with all zero elements.
2101///
Owen Andersone50ed302009-08-10 22:56:29 +00002102static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002103 assert(VT.isVector() && "Expected a vector type");
2104
2105 // Zero vectors are used to represent vector negation and in those cases
2106 // will be implemented with the NEON VNEG instruction. However, VNEG does
2107 // not support i64 elements, so sometimes the zero vectors will need to be
2108 // explicitly constructed. For those cases, and potentially other uses in
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002109 // the future, always build zero vectors as <16 x i8> or <8 x i8> bitcasted
Bob Wilson5bafff32009-06-22 23:27:02 +00002110 // to their dest type. This ensures they get CSE'd.
2111 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002112 SDValue Cst = DAG.getTargetConstant(0, MVT::i8);
2113 SmallVector<SDValue, 8> Ops;
2114 MVT TVT;
2115
2116 if (VT.getSizeInBits() == 64) {
2117 Ops.assign(8, Cst); TVT = MVT::v8i8;
2118 } else {
2119 Ops.assign(16, Cst); TVT = MVT::v16i8;
2120 }
2121 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002122
2123 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2124}
2125
2126/// getOnesVector - Returns a vector of specified type with all bits set.
2127///
Owen Andersone50ed302009-08-10 22:56:29 +00002128static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002129 assert(VT.isVector() && "Expected a vector type");
2130
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002131 // Always build ones vectors as <16 x i32> or <8 x i32> bitcasted to their
2132 // dest type. This ensures they get CSE'd.
Bob Wilson5bafff32009-06-22 23:27:02 +00002133 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002134 SDValue Cst = DAG.getTargetConstant(0xFF, MVT::i8);
2135 SmallVector<SDValue, 8> Ops;
2136 MVT TVT;
2137
2138 if (VT.getSizeInBits() == 64) {
2139 Ops.assign(8, Cst); TVT = MVT::v8i8;
2140 } else {
2141 Ops.assign(16, Cst); TVT = MVT::v16i8;
2142 }
2143 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002144
2145 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2146}
2147
2148static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2149 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00002150 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002151 DebugLoc dl = N->getDebugLoc();
2152
2153 // Lower vector shifts on NEON to use VSHL.
2154 if (VT.isVector()) {
2155 assert(ST->hasNEON() && "unexpected vector shift");
2156
2157 // Left shifts translate directly to the vshiftu intrinsic.
2158 if (N->getOpcode() == ISD::SHL)
2159 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002160 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002161 N->getOperand(0), N->getOperand(1));
2162
2163 assert((N->getOpcode() == ISD::SRA ||
2164 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2165
2166 // NEON uses the same intrinsics for both left and right shifts. For
2167 // right shifts, the shift amounts are negative, so negate the vector of
2168 // shift amounts.
Owen Andersone50ed302009-08-10 22:56:29 +00002169 EVT ShiftVT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002170 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2171 getZeroVector(ShiftVT, DAG, dl),
2172 N->getOperand(1));
2173 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2174 Intrinsic::arm_neon_vshifts :
2175 Intrinsic::arm_neon_vshiftu);
2176 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002177 DAG.getConstant(vshiftInt, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002178 N->getOperand(0), NegatedCount);
2179 }
2180
Eli Friedmance392eb2009-08-22 03:13:10 +00002181 // We can get here for a node like i32 = ISD::SHL i32, i64
2182 if (VT != MVT::i64)
2183 return SDValue();
2184
2185 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00002186 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00002187
Chris Lattner27a6c732007-11-24 07:07:01 +00002188 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2189 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002190 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00002191 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002192
Chris Lattner27a6c732007-11-24 07:07:01 +00002193 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00002194 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002195
Chris Lattner27a6c732007-11-24 07:07:01 +00002196 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00002197 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2198 DAG.getConstant(0, MVT::i32));
2199 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2200 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002201
Chris Lattner27a6c732007-11-24 07:07:01 +00002202 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2203 // captures the result into a carry flag.
2204 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Owen Anderson825b72b2009-08-11 20:47:22 +00002205 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002206
Chris Lattner27a6c732007-11-24 07:07:01 +00002207 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00002208 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002209
Chris Lattner27a6c732007-11-24 07:07:01 +00002210 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002211 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00002212}
2213
Bob Wilson5bafff32009-06-22 23:27:02 +00002214static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2215 SDValue TmpOp0, TmpOp1;
2216 bool Invert = false;
2217 bool Swap = false;
2218 unsigned Opc = 0;
2219
2220 SDValue Op0 = Op.getOperand(0);
2221 SDValue Op1 = Op.getOperand(1);
2222 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00002223 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002224 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2225 DebugLoc dl = Op.getDebugLoc();
2226
2227 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2228 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002229 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002230 case ISD::SETUNE:
2231 case ISD::SETNE: Invert = true; // Fallthrough
2232 case ISD::SETOEQ:
2233 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2234 case ISD::SETOLT:
2235 case ISD::SETLT: Swap = true; // Fallthrough
2236 case ISD::SETOGT:
2237 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2238 case ISD::SETOLE:
2239 case ISD::SETLE: Swap = true; // Fallthrough
2240 case ISD::SETOGE:
2241 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2242 case ISD::SETUGE: Swap = true; // Fallthrough
2243 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2244 case ISD::SETUGT: Swap = true; // Fallthrough
2245 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2246 case ISD::SETUEQ: Invert = true; // Fallthrough
2247 case ISD::SETONE:
2248 // Expand this to (OLT | OGT).
2249 TmpOp0 = Op0;
2250 TmpOp1 = Op1;
2251 Opc = ISD::OR;
2252 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2253 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2254 break;
2255 case ISD::SETUO: Invert = true; // Fallthrough
2256 case ISD::SETO:
2257 // Expand this to (OLT | OGE).
2258 TmpOp0 = Op0;
2259 TmpOp1 = Op1;
2260 Opc = ISD::OR;
2261 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2262 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2263 break;
2264 }
2265 } else {
2266 // Integer comparisons.
2267 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002268 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002269 case ISD::SETNE: Invert = true;
2270 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2271 case ISD::SETLT: Swap = true;
2272 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2273 case ISD::SETLE: Swap = true;
2274 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2275 case ISD::SETULT: Swap = true;
2276 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2277 case ISD::SETULE: Swap = true;
2278 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2279 }
2280
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002281 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00002282 if (Opc == ARMISD::VCEQ) {
2283
2284 SDValue AndOp;
2285 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2286 AndOp = Op0;
2287 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2288 AndOp = Op1;
2289
2290 // Ignore bitconvert.
2291 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2292 AndOp = AndOp.getOperand(0);
2293
2294 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2295 Opc = ARMISD::VTST;
2296 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2297 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2298 Invert = !Invert;
2299 }
2300 }
2301 }
2302
2303 if (Swap)
2304 std::swap(Op0, Op1);
2305
2306 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
2307
2308 if (Invert)
2309 Result = DAG.getNOT(dl, Result, VT);
2310
2311 return Result;
2312}
2313
2314/// isVMOVSplat - Check if the specified splat value corresponds to an immediate
2315/// VMOV instruction, and if so, return the constant being splatted.
2316static SDValue isVMOVSplat(uint64_t SplatBits, uint64_t SplatUndef,
2317 unsigned SplatBitSize, SelectionDAG &DAG) {
2318 switch (SplatBitSize) {
2319 case 8:
2320 // Any 1-byte value is OK.
2321 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Owen Anderson825b72b2009-08-11 20:47:22 +00002322 return DAG.getTargetConstant(SplatBits, MVT::i8);
Bob Wilson5bafff32009-06-22 23:27:02 +00002323
2324 case 16:
2325 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
2326 if ((SplatBits & ~0xff) == 0 ||
2327 (SplatBits & ~0xff00) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002328 return DAG.getTargetConstant(SplatBits, MVT::i16);
Bob Wilson5bafff32009-06-22 23:27:02 +00002329 break;
2330
2331 case 32:
2332 // NEON's 32-bit VMOV supports splat values where:
2333 // * only one byte is nonzero, or
2334 // * the least significant byte is 0xff and the second byte is nonzero, or
2335 // * the least significant 2 bytes are 0xff and the third is nonzero.
2336 if ((SplatBits & ~0xff) == 0 ||
2337 (SplatBits & ~0xff00) == 0 ||
2338 (SplatBits & ~0xff0000) == 0 ||
2339 (SplatBits & ~0xff000000) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002340 return DAG.getTargetConstant(SplatBits, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002341
2342 if ((SplatBits & ~0xffff) == 0 &&
2343 ((SplatBits | SplatUndef) & 0xff) == 0xff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002344 return DAG.getTargetConstant(SplatBits | 0xff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002345
2346 if ((SplatBits & ~0xffffff) == 0 &&
2347 ((SplatBits | SplatUndef) & 0xffff) == 0xffff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002348 return DAG.getTargetConstant(SplatBits | 0xffff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002349
2350 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
2351 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
2352 // VMOV.I32. A (very) minor optimization would be to replicate the value
2353 // and fall through here to test for a valid 64-bit splat. But, then the
2354 // caller would also need to check and handle the change in size.
2355 break;
2356
2357 case 64: {
2358 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
2359 uint64_t BitMask = 0xff;
2360 uint64_t Val = 0;
2361 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
2362 if (((SplatBits | SplatUndef) & BitMask) == BitMask)
2363 Val |= BitMask;
2364 else if ((SplatBits & BitMask) != 0)
2365 return SDValue();
2366 BitMask <<= 8;
2367 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002368 return DAG.getTargetConstant(Val, MVT::i64);
Bob Wilson5bafff32009-06-22 23:27:02 +00002369 }
2370
2371 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002372 llvm_unreachable("unexpected size for isVMOVSplat");
Bob Wilson5bafff32009-06-22 23:27:02 +00002373 break;
2374 }
2375
2376 return SDValue();
2377}
2378
2379/// getVMOVImm - If this is a build_vector of constants which can be
2380/// formed by using a VMOV instruction of the specified element size,
2381/// return the constant being splatted. The ByteSize field indicates the
2382/// number of bytes of each element [1248].
2383SDValue ARM::getVMOVImm(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
2384 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
2385 APInt SplatBits, SplatUndef;
2386 unsigned SplatBitSize;
2387 bool HasAnyUndefs;
2388 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
2389 HasAnyUndefs, ByteSize * 8))
2390 return SDValue();
2391
2392 if (SplatBitSize > ByteSize * 8)
2393 return SDValue();
2394
2395 return isVMOVSplat(SplatBits.getZExtValue(), SplatUndef.getZExtValue(),
2396 SplatBitSize, DAG);
2397}
2398
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002399static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
2400 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002401 unsigned NumElts = VT.getVectorNumElements();
2402 ReverseVEXT = false;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002403 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002404
2405 // If this is a VEXT shuffle, the immediate value is the index of the first
2406 // element. The other shuffle indices must be the successive elements after
2407 // the first one.
2408 unsigned ExpectedElt = Imm;
2409 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002410 // Increment the expected index. If it wraps around, it may still be
2411 // a VEXT but the source vectors must be swapped.
2412 ExpectedElt += 1;
2413 if (ExpectedElt == NumElts * 2) {
2414 ExpectedElt = 0;
2415 ReverseVEXT = true;
2416 }
2417
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002418 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002419 return false;
2420 }
2421
2422 // Adjust the index value if the source operands will be swapped.
2423 if (ReverseVEXT)
2424 Imm -= NumElts;
2425
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002426 return true;
2427}
2428
Bob Wilson8bb9e482009-07-26 00:39:34 +00002429/// isVREVMask - Check if a vector shuffle corresponds to a VREV
2430/// instruction with the specified blocksize. (The order of the elements
2431/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002432static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
2433 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00002434 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
2435 "Only possible block sizes for VREV are: 16, 32, 64");
2436
Bob Wilson8bb9e482009-07-26 00:39:34 +00002437 unsigned NumElts = VT.getVectorNumElements();
2438 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002439 unsigned BlockElts = M[0] + 1;
Bob Wilson8bb9e482009-07-26 00:39:34 +00002440
2441 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
2442 return false;
2443
2444 for (unsigned i = 0; i < NumElts; ++i) {
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002445 if ((unsigned) M[i] !=
Bob Wilson8bb9e482009-07-26 00:39:34 +00002446 (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
2447 return false;
2448 }
2449
2450 return true;
2451}
2452
Bob Wilsonc692cb72009-08-21 20:54:19 +00002453static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
2454 unsigned &WhichResult) {
2455 unsigned NumElts = VT.getVectorNumElements();
2456 WhichResult = (M[0] == 0 ? 0 : 1);
2457 for (unsigned i = 0; i < NumElts; i += 2) {
2458 if ((unsigned) M[i] != i + WhichResult ||
2459 (unsigned) M[i+1] != i + NumElts + WhichResult)
2460 return false;
2461 }
2462 return true;
2463}
2464
2465static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
2466 unsigned &WhichResult) {
2467 unsigned NumElts = VT.getVectorNumElements();
2468 WhichResult = (M[0] == 0 ? 0 : 1);
2469 for (unsigned i = 0; i != NumElts; ++i) {
2470 if ((unsigned) M[i] != 2 * i + WhichResult)
2471 return false;
2472 }
2473
2474 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2475 if (VT.is64BitVector() && VT.getVectorElementType().getSizeInBits() == 32)
2476 return false;
2477
2478 return true;
2479}
2480
2481static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
2482 unsigned &WhichResult) {
2483 unsigned NumElts = VT.getVectorNumElements();
2484 WhichResult = (M[0] == 0 ? 0 : 1);
2485 unsigned Idx = WhichResult * NumElts / 2;
2486 for (unsigned i = 0; i != NumElts; i += 2) {
2487 if ((unsigned) M[i] != Idx ||
2488 (unsigned) M[i+1] != Idx + NumElts)
2489 return false;
2490 Idx += 1;
2491 }
2492
2493 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2494 if (VT.is64BitVector() && VT.getVectorElementType().getSizeInBits() == 32)
2495 return false;
2496
2497 return true;
2498}
2499
Owen Andersone50ed302009-08-10 22:56:29 +00002500static SDValue BuildSplat(SDValue Val, EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002501 // Canonicalize all-zeros and all-ones vectors.
Bob Wilsond06791f2009-08-13 01:57:47 +00002502 ConstantSDNode *ConstVal = cast<ConstantSDNode>(Val.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002503 if (ConstVal->isNullValue())
2504 return getZeroVector(VT, DAG, dl);
2505 if (ConstVal->isAllOnesValue())
2506 return getOnesVector(VT, DAG, dl);
2507
Owen Andersone50ed302009-08-10 22:56:29 +00002508 EVT CanonicalVT;
Bob Wilson5bafff32009-06-22 23:27:02 +00002509 if (VT.is64BitVector()) {
2510 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002511 case 8: CanonicalVT = MVT::v8i8; break;
2512 case 16: CanonicalVT = MVT::v4i16; break;
2513 case 32: CanonicalVT = MVT::v2i32; break;
2514 case 64: CanonicalVT = MVT::v1i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002515 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002516 }
2517 } else {
2518 assert(VT.is128BitVector() && "unknown splat vector size");
2519 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002520 case 8: CanonicalVT = MVT::v16i8; break;
2521 case 16: CanonicalVT = MVT::v8i16; break;
2522 case 32: CanonicalVT = MVT::v4i32; break;
2523 case 64: CanonicalVT = MVT::v2i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002524 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002525 }
2526 }
2527
2528 // Build a canonical splat for this value.
2529 SmallVector<SDValue, 8> Ops;
2530 Ops.assign(CanonicalVT.getVectorNumElements(), Val);
2531 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, &Ops[0],
2532 Ops.size());
2533 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Res);
2534}
2535
2536// If this is a case we can't handle, return null and let the default
2537// expansion code take care of it.
2538static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bob Wilsond06791f2009-08-13 01:57:47 +00002539 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002540 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002541 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002542
2543 APInt SplatBits, SplatUndef;
2544 unsigned SplatBitSize;
2545 bool HasAnyUndefs;
2546 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00002547 if (SplatBitSize <= 64) {
2548 SDValue Val = isVMOVSplat(SplatBits.getZExtValue(),
2549 SplatUndef.getZExtValue(), SplatBitSize, DAG);
2550 if (Val.getNode())
2551 return BuildSplat(Val, VT, DAG, dl);
2552 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00002553 }
2554
2555 // If there are only 2 elements in a 128-bit vector, insert them into an
2556 // undef vector. This handles the common case for 128-bit vector argument
2557 // passing, where the insertions should be translated to subreg accesses
2558 // with no real instructions.
2559 if (VT.is128BitVector() && Op.getNumOperands() == 2) {
2560 SDValue Val = DAG.getUNDEF(VT);
2561 SDValue Op0 = Op.getOperand(0);
2562 SDValue Op1 = Op.getOperand(1);
2563 if (Op0.getOpcode() != ISD::UNDEF)
2564 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op0,
2565 DAG.getIntPtrConstant(0));
2566 if (Op1.getOpcode() != ISD::UNDEF)
2567 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op1,
2568 DAG.getIntPtrConstant(1));
2569 return Val;
Bob Wilson5bafff32009-06-22 23:27:02 +00002570 }
2571
2572 return SDValue();
2573}
2574
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002575/// isShuffleMaskLegal - Targets can use this to indicate that they only
2576/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
2577/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
2578/// are assumed to be legal.
2579bool
2580ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
2581 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002582 if (VT.getVectorNumElements() == 4 &&
2583 (VT.is128BitVector() || VT.is64BitVector())) {
2584 unsigned PFIndexes[4];
2585 for (unsigned i = 0; i != 4; ++i) {
2586 if (M[i] < 0)
2587 PFIndexes[i] = 8;
2588 else
2589 PFIndexes[i] = M[i];
2590 }
2591
2592 // Compute the index in the perfect shuffle table.
2593 unsigned PFTableIndex =
2594 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2595 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2596 unsigned Cost = (PFEntry >> 30);
2597
2598 if (Cost <= 4)
2599 return true;
2600 }
2601
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002602 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00002603 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002604
2605 return (ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
2606 isVREVMask(M, VT, 64) ||
2607 isVREVMask(M, VT, 32) ||
2608 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00002609 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
2610 isVTRNMask(M, VT, WhichResult) ||
2611 isVUZPMask(M, VT, WhichResult) ||
2612 isVZIPMask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002613}
2614
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002615/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
2616/// the specified operations to build the shuffle.
2617static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
2618 SDValue RHS, SelectionDAG &DAG,
2619 DebugLoc dl) {
2620 unsigned OpNum = (PFEntry >> 26) & 0x0F;
2621 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
2622 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
2623
2624 enum {
2625 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
2626 OP_VREV,
2627 OP_VDUP0,
2628 OP_VDUP1,
2629 OP_VDUP2,
2630 OP_VDUP3,
2631 OP_VEXT1,
2632 OP_VEXT2,
2633 OP_VEXT3,
2634 OP_VUZPL, // VUZP, left result
2635 OP_VUZPR, // VUZP, right result
2636 OP_VZIPL, // VZIP, left result
2637 OP_VZIPR, // VZIP, right result
2638 OP_VTRNL, // VTRN, left result
2639 OP_VTRNR // VTRN, right result
2640 };
2641
2642 if (OpNum == OP_COPY) {
2643 if (LHSID == (1*9+2)*9+3) return LHS;
2644 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
2645 return RHS;
2646 }
2647
2648 SDValue OpLHS, OpRHS;
2649 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
2650 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
2651 EVT VT = OpLHS.getValueType();
2652
2653 switch (OpNum) {
2654 default: llvm_unreachable("Unknown shuffle opcode!");
2655 case OP_VREV:
2656 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
2657 case OP_VDUP0:
2658 case OP_VDUP1:
2659 case OP_VDUP2:
2660 case OP_VDUP3:
2661 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002662 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002663 case OP_VEXT1:
2664 case OP_VEXT2:
2665 case OP_VEXT3:
2666 return DAG.getNode(ARMISD::VEXT, dl, VT,
2667 OpLHS, OpRHS,
2668 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
2669 case OP_VUZPL:
2670 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002671 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002672 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
2673 case OP_VZIPL:
2674 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002675 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002676 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
2677 case OP_VTRNL:
2678 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002679 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2680 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002681 }
2682}
2683
Bob Wilson5bafff32009-06-22 23:27:02 +00002684static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002685 SDValue V1 = Op.getOperand(0);
2686 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00002687 DebugLoc dl = Op.getDebugLoc();
2688 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002689 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002690 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00002691
Bob Wilson28865062009-08-13 02:13:04 +00002692 // Convert shuffles that are directly supported on NEON to target-specific
2693 // DAG nodes, instead of keeping them as shuffles and matching them again
2694 // during code selection. This is more efficient and avoids the possibility
2695 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00002696 // FIXME: floating-point vectors should be canonicalized to integer vectors
2697 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002698 SVN->getMask(ShuffleMask);
2699
2700 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
Bob Wilson0ce37102009-08-14 05:08:32 +00002701 int Lane = SVN->getSplatIndex();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002702 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
2703 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00002704 }
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002705 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002706 DAG.getConstant(Lane, MVT::i32));
Bob Wilson0ce37102009-08-14 05:08:32 +00002707 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002708
2709 bool ReverseVEXT;
2710 unsigned Imm;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002711 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002712 if (ReverseVEXT)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002713 std::swap(V1, V2);
2714 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002715 DAG.getConstant(Imm, MVT::i32));
2716 }
2717
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002718 if (isVREVMask(ShuffleMask, VT, 64))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002719 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002720 if (isVREVMask(ShuffleMask, VT, 32))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002721 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002722 if (isVREVMask(ShuffleMask, VT, 16))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002723 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
2724
Bob Wilsonc692cb72009-08-21 20:54:19 +00002725 // Check for Neon shuffles that modify both input vectors in place.
2726 // If both results are used, i.e., if there are two shuffles with the same
2727 // source operands and with masks corresponding to both results of one of
2728 // these operations, DAG memoization will ensure that a single node is
2729 // used for both shuffles.
2730 unsigned WhichResult;
2731 if (isVTRNMask(ShuffleMask, VT, WhichResult))
2732 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2733 V1, V2).getValue(WhichResult);
2734 if (isVUZPMask(ShuffleMask, VT, WhichResult))
2735 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
2736 V1, V2).getValue(WhichResult);
2737 if (isVZIPMask(ShuffleMask, VT, WhichResult))
2738 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
2739 V1, V2).getValue(WhichResult);
2740
2741 // If the shuffle is not directly supported and it has 4 elements, use
2742 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002743 if (VT.getVectorNumElements() == 4 &&
2744 (VT.is128BitVector() || VT.is64BitVector())) {
2745 unsigned PFIndexes[4];
2746 for (unsigned i = 0; i != 4; ++i) {
2747 if (ShuffleMask[i] < 0)
2748 PFIndexes[i] = 8;
2749 else
2750 PFIndexes[i] = ShuffleMask[i];
2751 }
2752
2753 // Compute the index in the perfect shuffle table.
2754 unsigned PFTableIndex =
2755 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2756
2757 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2758 unsigned Cost = (PFEntry >> 30);
2759
2760 if (Cost <= 4)
2761 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
2762 }
Bob Wilsond8e17572009-08-12 22:31:50 +00002763
Bob Wilson22cac0d2009-08-14 05:16:33 +00002764 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00002765}
2766
Bob Wilson5bafff32009-06-22 23:27:02 +00002767static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002768 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002769 DebugLoc dl = Op.getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00002770 SDValue Vec = Op.getOperand(0);
2771 SDValue Lane = Op.getOperand(1);
Anton Korobeynikovb00c03b2009-08-30 17:14:54 +00002772
2773 // FIXME: This is invalid for 8 and 16-bit elements - the information about
2774 // sign / zero extension is lost!
Owen Anderson825b72b2009-08-11 20:47:22 +00002775 Op = DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
2776 Op = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Op, DAG.getValueType(VT));
Anton Korobeynikovb00c03b2009-08-30 17:14:54 +00002777
2778 if (VT.bitsLT(MVT::i32))
2779 Op = DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
2780 else if (VT.bitsGT(MVT::i32))
2781 Op = DAG.getNode(ISD::ANY_EXTEND, dl, VT, Op);
2782
2783 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00002784}
2785
Bob Wilsona6d65862009-08-03 20:36:38 +00002786static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
2787 // The only time a CONCAT_VECTORS operation can have legal types is when
2788 // two 64-bit vectors are concatenated to a 128-bit vector.
2789 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
2790 "unexpected CONCAT_VECTORS");
2791 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00002792 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00002793 SDValue Op0 = Op.getOperand(0);
2794 SDValue Op1 = Op.getOperand(1);
2795 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00002796 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
2797 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00002798 DAG.getIntPtrConstant(0));
2799 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00002800 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
2801 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00002802 DAG.getIntPtrConstant(1));
2803 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00002804}
2805
Dan Gohman475871a2008-07-27 21:46:04 +00002806SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00002807 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002808 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00002809 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002810 case ISD::GlobalAddress:
2811 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
2812 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002813 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002814 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, Subtarget);
2815 case ISD::BR_CC: return LowerBR_CC(Op, DAG, Subtarget);
2816 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Evan Cheng86198642009-08-07 00:34:42 +00002817 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002818 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
2819 case ISD::SINT_TO_FP:
2820 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
2821 case ISD::FP_TO_SINT:
2822 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
2823 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00002824 case ISD::RETURNADDR: break;
Jim Grosbach0e0da732009-05-12 23:59:14 +00002825 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002826 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Bob Wilsonb36ec862009-08-06 18:47:44 +00002827 case ISD::INTRINSIC_VOID:
Bob Wilsona599bff2009-08-04 00:36:16 +00002828 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002829 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00002830 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00002831 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00002832 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00002833 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
2834 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
2835 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2836 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00002837 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00002838 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002839 }
Dan Gohman475871a2008-07-27 21:46:04 +00002840 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00002841}
2842
Duncan Sands1607f052008-12-01 11:39:25 +00002843/// ReplaceNodeResults - Replace the results of node with an illegal result
2844/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00002845void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
2846 SmallVectorImpl<SDValue>&Results,
2847 SelectionDAG &DAG) {
Chris Lattner27a6c732007-11-24 07:07:01 +00002848 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00002849 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002850 llvm_unreachable("Don't know how to custom expand this!");
Duncan Sands1607f052008-12-01 11:39:25 +00002851 return;
2852 case ISD::BIT_CONVERT:
2853 Results.push_back(ExpandBIT_CONVERT(N, DAG));
2854 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00002855 case ISD::SRL:
Duncan Sands1607f052008-12-01 11:39:25 +00002856 case ISD::SRA: {
Bob Wilson5bafff32009-06-22 23:27:02 +00002857 SDValue Res = LowerShift(N, DAG, Subtarget);
Duncan Sands1607f052008-12-01 11:39:25 +00002858 if (Res.getNode())
2859 Results.push_back(Res);
2860 return;
2861 }
Chris Lattner27a6c732007-11-24 07:07:01 +00002862 }
2863}
Chris Lattner27a6c732007-11-24 07:07:01 +00002864
Evan Chenga8e29892007-01-19 07:51:42 +00002865//===----------------------------------------------------------------------===//
2866// ARM Scheduler Hooks
2867//===----------------------------------------------------------------------===//
2868
2869MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00002870ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00002871 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002872 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00002873 DebugLoc dl = MI->getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002874 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00002875 default:
2876 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng007ea272009-08-12 05:17:19 +00002877 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00002878 // To "insert" a SELECT_CC instruction, we actually have to insert the
2879 // diamond control-flow pattern. The incoming instruction knows the
2880 // destination vreg to set, the condition code register to branch on, the
2881 // true/false values to select between, and a branch opcode to use.
2882 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002883 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00002884 ++It;
2885
2886 // thisMBB:
2887 // ...
2888 // TrueVal = ...
2889 // cmpTY ccX, r1, r2
2890 // bCC copy1MBB
2891 // fallthrough --> copy0MBB
2892 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002893 MachineFunction *F = BB->getParent();
2894 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
2895 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenb6728402009-02-13 02:25:56 +00002896 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00002897 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002898 F->insert(It, copy0MBB);
2899 F->insert(It, sinkMBB);
Evan Chenga8e29892007-01-19 07:51:42 +00002900 // Update machine-CFG edges by first adding all successors of the current
2901 // block to the new block which will contain the Phi node for the select.
2902 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
2903 e = BB->succ_end(); i != e; ++i)
2904 sinkMBB->addSuccessor(*i);
2905 // Next, remove all successors of the current block, and add the true
2906 // and fallthrough blocks as its successors.
2907 while(!BB->succ_empty())
2908 BB->removeSuccessor(BB->succ_begin());
2909 BB->addSuccessor(copy0MBB);
2910 BB->addSuccessor(sinkMBB);
2911
2912 // copy0MBB:
2913 // %FalseValue = ...
2914 // # fallthrough to sinkMBB
2915 BB = copy0MBB;
2916
2917 // Update machine-CFG edges
2918 BB->addSuccessor(sinkMBB);
2919
2920 // sinkMBB:
2921 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2922 // ...
2923 BB = sinkMBB;
Dale Johannesenb6728402009-02-13 02:25:56 +00002924 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00002925 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
2926 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
2927
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002928 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00002929 return BB;
2930 }
Evan Cheng86198642009-08-07 00:34:42 +00002931
2932 case ARM::tANDsp:
2933 case ARM::tADDspr_:
2934 case ARM::tSUBspi_:
2935 case ARM::t2SUBrSPi_:
2936 case ARM::t2SUBrSPi12_:
2937 case ARM::t2SUBrSPs_: {
2938 MachineFunction *MF = BB->getParent();
2939 unsigned DstReg = MI->getOperand(0).getReg();
2940 unsigned SrcReg = MI->getOperand(1).getReg();
2941 bool DstIsDead = MI->getOperand(0).isDead();
2942 bool SrcIsKill = MI->getOperand(1).isKill();
2943
2944 if (SrcReg != ARM::SP) {
2945 // Copy the source to SP from virtual register.
2946 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(SrcReg);
2947 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
2948 ? ARM::tMOVtgpr2gpr : ARM::tMOVgpr2gpr;
2949 BuildMI(BB, dl, TII->get(CopyOpc), ARM::SP)
2950 .addReg(SrcReg, getKillRegState(SrcIsKill));
2951 }
2952
2953 unsigned OpOpc = 0;
2954 bool NeedPred = false, NeedCC = false, NeedOp3 = false;
2955 switch (MI->getOpcode()) {
2956 default:
2957 llvm_unreachable("Unexpected pseudo instruction!");
2958 case ARM::tANDsp:
2959 OpOpc = ARM::tAND;
2960 NeedPred = true;
2961 break;
2962 case ARM::tADDspr_:
2963 OpOpc = ARM::tADDspr;
2964 break;
2965 case ARM::tSUBspi_:
2966 OpOpc = ARM::tSUBspi;
2967 break;
2968 case ARM::t2SUBrSPi_:
2969 OpOpc = ARM::t2SUBrSPi;
2970 NeedPred = true; NeedCC = true;
2971 break;
2972 case ARM::t2SUBrSPi12_:
2973 OpOpc = ARM::t2SUBrSPi12;
2974 NeedPred = true;
2975 break;
2976 case ARM::t2SUBrSPs_:
2977 OpOpc = ARM::t2SUBrSPs;
2978 NeedPred = true; NeedCC = true; NeedOp3 = true;
2979 break;
2980 }
2981 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(OpOpc), ARM::SP);
2982 if (OpOpc == ARM::tAND)
2983 AddDefaultT1CC(MIB);
2984 MIB.addReg(ARM::SP);
2985 MIB.addOperand(MI->getOperand(2));
2986 if (NeedOp3)
2987 MIB.addOperand(MI->getOperand(3));
2988 if (NeedPred)
2989 AddDefaultPred(MIB);
2990 if (NeedCC)
2991 AddDefaultCC(MIB);
2992
2993 // Copy the result from SP to virtual register.
2994 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(DstReg);
2995 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
2996 ? ARM::tMOVgpr2tgpr : ARM::tMOVgpr2gpr;
2997 BuildMI(BB, dl, TII->get(CopyOpc))
2998 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
2999 .addReg(ARM::SP);
3000 MF->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
3001 return BB;
3002 }
Evan Chenga8e29892007-01-19 07:51:42 +00003003 }
3004}
3005
3006//===----------------------------------------------------------------------===//
3007// ARM Optimization Hooks
3008//===----------------------------------------------------------------------===//
3009
Chris Lattnerd1980a52009-03-12 06:52:53 +00003010static
3011SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
3012 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00003013 SelectionDAG &DAG = DCI.DAG;
3014 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003015 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00003016 unsigned Opc = N->getOpcode();
3017 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
3018 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
3019 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
3020 ISD::CondCode CC = ISD::SETCC_INVALID;
3021
3022 if (isSlctCC) {
3023 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
3024 } else {
3025 SDValue CCOp = Slct.getOperand(0);
3026 if (CCOp.getOpcode() == ISD::SETCC)
3027 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
3028 }
3029
3030 bool DoXform = false;
3031 bool InvCC = false;
3032 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
3033 "Bad input!");
3034
3035 if (LHS.getOpcode() == ISD::Constant &&
3036 cast<ConstantSDNode>(LHS)->isNullValue()) {
3037 DoXform = true;
3038 } else if (CC != ISD::SETCC_INVALID &&
3039 RHS.getOpcode() == ISD::Constant &&
3040 cast<ConstantSDNode>(RHS)->isNullValue()) {
3041 std::swap(LHS, RHS);
3042 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003043 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00003044 Op0.getOperand(0).getValueType();
3045 bool isInt = OpVT.isInteger();
3046 CC = ISD::getSetCCInverse(CC, isInt);
3047
3048 if (!TLI.isCondCodeLegal(CC, OpVT))
3049 return SDValue(); // Inverse operator isn't legal.
3050
3051 DoXform = true;
3052 InvCC = true;
3053 }
3054
3055 if (DoXform) {
3056 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
3057 if (isSlctCC)
3058 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
3059 Slct.getOperand(0), Slct.getOperand(1), CC);
3060 SDValue CCOp = Slct.getOperand(0);
3061 if (InvCC)
3062 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
3063 CCOp.getOperand(0), CCOp.getOperand(1), CC);
3064 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
3065 CCOp, OtherOp, Result);
3066 }
3067 return SDValue();
3068}
3069
3070/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
3071static SDValue PerformADDCombine(SDNode *N,
3072 TargetLowering::DAGCombinerInfo &DCI) {
3073 // added by evan in r37685 with no testcase.
3074 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003075
Chris Lattnerd1980a52009-03-12 06:52:53 +00003076 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
3077 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
3078 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
3079 if (Result.getNode()) return Result;
3080 }
3081 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3082 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3083 if (Result.getNode()) return Result;
3084 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003085
Chris Lattnerd1980a52009-03-12 06:52:53 +00003086 return SDValue();
3087}
3088
3089/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
3090static SDValue PerformSUBCombine(SDNode *N,
3091 TargetLowering::DAGCombinerInfo &DCI) {
3092 // added by evan in r37685 with no testcase.
3093 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003094
Chris Lattnerd1980a52009-03-12 06:52:53 +00003095 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
3096 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3097 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3098 if (Result.getNode()) return Result;
3099 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003100
Chris Lattnerd1980a52009-03-12 06:52:53 +00003101 return SDValue();
3102}
3103
3104
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003105/// PerformFMRRDCombine - Target-specific dag combine xforms for ARMISD::FMRRD.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003106static SDValue PerformFMRRDCombine(SDNode *N,
3107 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003108 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00003109 SDValue InDouble = N->getOperand(0);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003110 if (InDouble.getOpcode() == ARMISD::FMDRR)
3111 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003112 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003113}
3114
Bob Wilson5bafff32009-06-22 23:27:02 +00003115/// getVShiftImm - Check if this is a valid build_vector for the immediate
3116/// operand of a vector shift operation, where all the elements of the
3117/// build_vector must have the same constant integer value.
3118static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
3119 // Ignore bit_converts.
3120 while (Op.getOpcode() == ISD::BIT_CONVERT)
3121 Op = Op.getOperand(0);
3122 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
3123 APInt SplatBits, SplatUndef;
3124 unsigned SplatBitSize;
3125 bool HasAnyUndefs;
3126 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
3127 HasAnyUndefs, ElementBits) ||
3128 SplatBitSize > ElementBits)
3129 return false;
3130 Cnt = SplatBits.getSExtValue();
3131 return true;
3132}
3133
3134/// isVShiftLImm - Check if this is a valid build_vector for the immediate
3135/// operand of a vector shift left operation. That value must be in the range:
3136/// 0 <= Value < ElementBits for a left shift; or
3137/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003138static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003139 assert(VT.isVector() && "vector shift count is not a vector type");
3140 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3141 if (! getVShiftImm(Op, ElementBits, Cnt))
3142 return false;
3143 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
3144}
3145
3146/// isVShiftRImm - Check if this is a valid build_vector for the immediate
3147/// operand of a vector shift right operation. For a shift opcode, the value
3148/// is positive, but for an intrinsic the value count must be negative. The
3149/// absolute value must be in the range:
3150/// 1 <= |Value| <= ElementBits for a right shift; or
3151/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003152static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00003153 int64_t &Cnt) {
3154 assert(VT.isVector() && "vector shift count is not a vector type");
3155 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3156 if (! getVShiftImm(Op, ElementBits, Cnt))
3157 return false;
3158 if (isIntrinsic)
3159 Cnt = -Cnt;
3160 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
3161}
3162
3163/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
3164static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
3165 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
3166 switch (IntNo) {
3167 default:
3168 // Don't do anything for most intrinsics.
3169 break;
3170
3171 // Vector shifts: check for immediate versions and lower them.
3172 // Note: This is done during DAG combining instead of DAG legalizing because
3173 // the build_vectors for 64-bit vector element shift counts are generally
3174 // not legal, and it is hard to see their values after they get legalized to
3175 // loads from a constant pool.
3176 case Intrinsic::arm_neon_vshifts:
3177 case Intrinsic::arm_neon_vshiftu:
3178 case Intrinsic::arm_neon_vshiftls:
3179 case Intrinsic::arm_neon_vshiftlu:
3180 case Intrinsic::arm_neon_vshiftn:
3181 case Intrinsic::arm_neon_vrshifts:
3182 case Intrinsic::arm_neon_vrshiftu:
3183 case Intrinsic::arm_neon_vrshiftn:
3184 case Intrinsic::arm_neon_vqshifts:
3185 case Intrinsic::arm_neon_vqshiftu:
3186 case Intrinsic::arm_neon_vqshiftsu:
3187 case Intrinsic::arm_neon_vqshiftns:
3188 case Intrinsic::arm_neon_vqshiftnu:
3189 case Intrinsic::arm_neon_vqshiftnsu:
3190 case Intrinsic::arm_neon_vqrshiftns:
3191 case Intrinsic::arm_neon_vqrshiftnu:
3192 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00003193 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003194 int64_t Cnt;
3195 unsigned VShiftOpc = 0;
3196
3197 switch (IntNo) {
3198 case Intrinsic::arm_neon_vshifts:
3199 case Intrinsic::arm_neon_vshiftu:
3200 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
3201 VShiftOpc = ARMISD::VSHL;
3202 break;
3203 }
3204 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
3205 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
3206 ARMISD::VSHRs : ARMISD::VSHRu);
3207 break;
3208 }
3209 return SDValue();
3210
3211 case Intrinsic::arm_neon_vshiftls:
3212 case Intrinsic::arm_neon_vshiftlu:
3213 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
3214 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003215 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003216
3217 case Intrinsic::arm_neon_vrshifts:
3218 case Intrinsic::arm_neon_vrshiftu:
3219 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
3220 break;
3221 return SDValue();
3222
3223 case Intrinsic::arm_neon_vqshifts:
3224 case Intrinsic::arm_neon_vqshiftu:
3225 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3226 break;
3227 return SDValue();
3228
3229 case Intrinsic::arm_neon_vqshiftsu:
3230 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3231 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003232 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003233
3234 case Intrinsic::arm_neon_vshiftn:
3235 case Intrinsic::arm_neon_vrshiftn:
3236 case Intrinsic::arm_neon_vqshiftns:
3237 case Intrinsic::arm_neon_vqshiftnu:
3238 case Intrinsic::arm_neon_vqshiftnsu:
3239 case Intrinsic::arm_neon_vqrshiftns:
3240 case Intrinsic::arm_neon_vqrshiftnu:
3241 case Intrinsic::arm_neon_vqrshiftnsu:
3242 // Narrowing shifts require an immediate right shift.
3243 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
3244 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003245 llvm_unreachable("invalid shift count for narrowing vector shift intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003246
3247 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003248 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003249 }
3250
3251 switch (IntNo) {
3252 case Intrinsic::arm_neon_vshifts:
3253 case Intrinsic::arm_neon_vshiftu:
3254 // Opcode already set above.
3255 break;
3256 case Intrinsic::arm_neon_vshiftls:
3257 case Intrinsic::arm_neon_vshiftlu:
3258 if (Cnt == VT.getVectorElementType().getSizeInBits())
3259 VShiftOpc = ARMISD::VSHLLi;
3260 else
3261 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
3262 ARMISD::VSHLLs : ARMISD::VSHLLu);
3263 break;
3264 case Intrinsic::arm_neon_vshiftn:
3265 VShiftOpc = ARMISD::VSHRN; break;
3266 case Intrinsic::arm_neon_vrshifts:
3267 VShiftOpc = ARMISD::VRSHRs; break;
3268 case Intrinsic::arm_neon_vrshiftu:
3269 VShiftOpc = ARMISD::VRSHRu; break;
3270 case Intrinsic::arm_neon_vrshiftn:
3271 VShiftOpc = ARMISD::VRSHRN; break;
3272 case Intrinsic::arm_neon_vqshifts:
3273 VShiftOpc = ARMISD::VQSHLs; break;
3274 case Intrinsic::arm_neon_vqshiftu:
3275 VShiftOpc = ARMISD::VQSHLu; break;
3276 case Intrinsic::arm_neon_vqshiftsu:
3277 VShiftOpc = ARMISD::VQSHLsu; break;
3278 case Intrinsic::arm_neon_vqshiftns:
3279 VShiftOpc = ARMISD::VQSHRNs; break;
3280 case Intrinsic::arm_neon_vqshiftnu:
3281 VShiftOpc = ARMISD::VQSHRNu; break;
3282 case Intrinsic::arm_neon_vqshiftnsu:
3283 VShiftOpc = ARMISD::VQSHRNsu; break;
3284 case Intrinsic::arm_neon_vqrshiftns:
3285 VShiftOpc = ARMISD::VQRSHRNs; break;
3286 case Intrinsic::arm_neon_vqrshiftnu:
3287 VShiftOpc = ARMISD::VQRSHRNu; break;
3288 case Intrinsic::arm_neon_vqrshiftnsu:
3289 VShiftOpc = ARMISD::VQRSHRNsu; break;
3290 }
3291
3292 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003293 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003294 }
3295
3296 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00003297 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003298 int64_t Cnt;
3299 unsigned VShiftOpc = 0;
3300
3301 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
3302 VShiftOpc = ARMISD::VSLI;
3303 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
3304 VShiftOpc = ARMISD::VSRI;
3305 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00003306 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003307 }
3308
3309 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
3310 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00003311 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003312 }
3313
3314 case Intrinsic::arm_neon_vqrshifts:
3315 case Intrinsic::arm_neon_vqrshiftu:
3316 // No immediate versions of these to check for.
3317 break;
3318 }
3319
3320 return SDValue();
3321}
3322
3323/// PerformShiftCombine - Checks for immediate versions of vector shifts and
3324/// lowers them. As with the vector shift intrinsics, this is done during DAG
3325/// combining instead of DAG legalizing because the build_vectors for 64-bit
3326/// vector element shift counts are generally not legal, and it is hard to see
3327/// their values after they get legalized to loads from a constant pool.
3328static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
3329 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003330 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003331
3332 // Nothing to be done for scalar shifts.
3333 if (! VT.isVector())
3334 return SDValue();
3335
3336 assert(ST->hasNEON() && "unexpected vector shift");
3337 int64_t Cnt;
3338
3339 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003340 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003341
3342 case ISD::SHL:
3343 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
3344 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003345 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003346 break;
3347
3348 case ISD::SRA:
3349 case ISD::SRL:
3350 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
3351 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
3352 ARMISD::VSHRs : ARMISD::VSHRu);
3353 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003354 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003355 }
3356 }
3357 return SDValue();
3358}
3359
3360/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
3361/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
3362static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
3363 const ARMSubtarget *ST) {
3364 SDValue N0 = N->getOperand(0);
3365
3366 // Check for sign- and zero-extensions of vector extract operations of 8-
3367 // and 16-bit vector elements. NEON supports these directly. They are
3368 // handled during DAG combining because type legalization will promote them
3369 // to 32-bit types and it is messy to recognize the operations after that.
3370 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
3371 SDValue Vec = N0.getOperand(0);
3372 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00003373 EVT VT = N->getValueType(0);
3374 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003375 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3376
Owen Anderson825b72b2009-08-11 20:47:22 +00003377 if (VT == MVT::i32 &&
3378 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00003379 TLI.isTypeLegal(Vec.getValueType())) {
3380
3381 unsigned Opc = 0;
3382 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003383 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003384 case ISD::SIGN_EXTEND:
3385 Opc = ARMISD::VGETLANEs;
3386 break;
3387 case ISD::ZERO_EXTEND:
3388 case ISD::ANY_EXTEND:
3389 Opc = ARMISD::VGETLANEu;
3390 break;
3391 }
3392 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
3393 }
3394 }
3395
3396 return SDValue();
3397}
3398
Dan Gohman475871a2008-07-27 21:46:04 +00003399SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003400 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003401 switch (N->getOpcode()) {
3402 default: break;
Chris Lattnerd1980a52009-03-12 06:52:53 +00003403 case ISD::ADD: return PerformADDCombine(N, DCI);
3404 case ISD::SUB: return PerformSUBCombine(N, DCI);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003405 case ARMISD::FMRRD: return PerformFMRRDCombine(N, DCI);
Bob Wilson5bafff32009-06-22 23:27:02 +00003406 case ISD::INTRINSIC_WO_CHAIN:
3407 return PerformIntrinsicCombine(N, DCI.DAG);
3408 case ISD::SHL:
3409 case ISD::SRA:
3410 case ISD::SRL:
3411 return PerformShiftCombine(N, DCI.DAG, Subtarget);
3412 case ISD::SIGN_EXTEND:
3413 case ISD::ZERO_EXTEND:
3414 case ISD::ANY_EXTEND:
3415 return PerformExtendCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003416 }
Dan Gohman475871a2008-07-27 21:46:04 +00003417 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003418}
3419
Bill Wendlingaf566342009-08-15 21:21:19 +00003420bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
3421 if (!Subtarget->hasV6Ops())
3422 // Pre-v6 does not support unaligned mem access.
3423 return false;
3424 else if (!Subtarget->hasV6Ops()) {
3425 // v6 may or may not support unaligned mem access.
3426 if (!Subtarget->isTargetDarwin())
3427 return false;
3428 }
3429
3430 switch (VT.getSimpleVT().SimpleTy) {
3431 default:
3432 return false;
3433 case MVT::i8:
3434 case MVT::i16:
3435 case MVT::i32:
3436 return true;
3437 // FIXME: VLD1 etc with standard alignment is legal.
3438 }
3439}
3440
Evan Chenge6c835f2009-08-14 20:09:37 +00003441static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
3442 if (V < 0)
3443 return false;
3444
3445 unsigned Scale = 1;
3446 switch (VT.getSimpleVT().SimpleTy) {
3447 default: return false;
3448 case MVT::i1:
3449 case MVT::i8:
3450 // Scale == 1;
3451 break;
3452 case MVT::i16:
3453 // Scale == 2;
3454 Scale = 2;
3455 break;
3456 case MVT::i32:
3457 // Scale == 4;
3458 Scale = 4;
3459 break;
3460 }
3461
3462 if ((V & (Scale - 1)) != 0)
3463 return false;
3464 V /= Scale;
3465 return V == (V & ((1LL << 5) - 1));
3466}
3467
3468static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
3469 const ARMSubtarget *Subtarget) {
3470 bool isNeg = false;
3471 if (V < 0) {
3472 isNeg = true;
3473 V = - V;
3474 }
3475
3476 switch (VT.getSimpleVT().SimpleTy) {
3477 default: return false;
3478 case MVT::i1:
3479 case MVT::i8:
3480 case MVT::i16:
3481 case MVT::i32:
3482 // + imm12 or - imm8
3483 if (isNeg)
3484 return V == (V & ((1LL << 8) - 1));
3485 return V == (V & ((1LL << 12) - 1));
3486 case MVT::f32:
3487 case MVT::f64:
3488 // Same as ARM mode. FIXME: NEON?
3489 if (!Subtarget->hasVFP2())
3490 return false;
3491 if ((V & 3) != 0)
3492 return false;
3493 V >>= 2;
3494 return V == (V & ((1LL << 8) - 1));
3495 }
3496}
3497
Evan Chengb01fad62007-03-12 23:30:29 +00003498/// isLegalAddressImmediate - Return true if the integer value can be used
3499/// as the offset of the target addressing mode for load / store of the
3500/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00003501static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00003502 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00003503 if (V == 0)
3504 return true;
3505
Evan Cheng65011532009-03-09 19:15:00 +00003506 if (!VT.isSimple())
3507 return false;
3508
Evan Chenge6c835f2009-08-14 20:09:37 +00003509 if (Subtarget->isThumb1Only())
3510 return isLegalT1AddressImmediate(V, VT);
3511 else if (Subtarget->isThumb2())
3512 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00003513
Evan Chenge6c835f2009-08-14 20:09:37 +00003514 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00003515 if (V < 0)
3516 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00003517 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00003518 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00003519 case MVT::i1:
3520 case MVT::i8:
3521 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00003522 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003523 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003524 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00003525 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003526 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003527 case MVT::f32:
3528 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00003529 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00003530 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00003531 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00003532 return false;
3533 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003534 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00003535 }
Evan Chenga8e29892007-01-19 07:51:42 +00003536}
3537
Evan Chenge6c835f2009-08-14 20:09:37 +00003538bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
3539 EVT VT) const {
3540 int Scale = AM.Scale;
3541 if (Scale < 0)
3542 return false;
3543
3544 switch (VT.getSimpleVT().SimpleTy) {
3545 default: return false;
3546 case MVT::i1:
3547 case MVT::i8:
3548 case MVT::i16:
3549 case MVT::i32:
3550 if (Scale == 1)
3551 return true;
3552 // r + r << imm
3553 Scale = Scale & ~1;
3554 return Scale == 2 || Scale == 4 || Scale == 8;
3555 case MVT::i64:
3556 // r + r
3557 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
3558 return true;
3559 return false;
3560 case MVT::isVoid:
3561 // Note, we allow "void" uses (basically, uses that aren't loads or
3562 // stores), because arm allows folding a scale into many arithmetic
3563 // operations. This should be made more precise and revisited later.
3564
3565 // Allow r << imm, but the imm has to be a multiple of two.
3566 if (Scale & 1) return false;
3567 return isPowerOf2_32(Scale);
3568 }
3569}
3570
Chris Lattner37caf8c2007-04-09 23:33:39 +00003571/// isLegalAddressingMode - Return true if the addressing mode represented
3572/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003573bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00003574 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00003575 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00003576 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00003577 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003578
Chris Lattner37caf8c2007-04-09 23:33:39 +00003579 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003580 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003581 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003582
Chris Lattner37caf8c2007-04-09 23:33:39 +00003583 switch (AM.Scale) {
3584 case 0: // no scale reg, must be "r+i" or "r", or "i".
3585 break;
3586 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00003587 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00003588 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00003589 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00003590 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00003591 // ARM doesn't support any R+R*scale+imm addr modes.
3592 if (AM.BaseOffs)
3593 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003594
Bob Wilson2c7dab12009-04-08 17:55:28 +00003595 if (!VT.isSimple())
3596 return false;
3597
Evan Chenge6c835f2009-08-14 20:09:37 +00003598 if (Subtarget->isThumb2())
3599 return isLegalT2ScaledAddressingMode(AM, VT);
3600
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003601 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00003602 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00003603 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00003604 case MVT::i1:
3605 case MVT::i8:
3606 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003607 if (Scale < 0) Scale = -Scale;
3608 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003609 return true;
3610 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00003611 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003612 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00003613 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00003614 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003615 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003616 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00003617 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003618
Owen Anderson825b72b2009-08-11 20:47:22 +00003619 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00003620 // Note, we allow "void" uses (basically, uses that aren't loads or
3621 // stores), because arm allows folding a scale into many arithmetic
3622 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003623
Chris Lattner37caf8c2007-04-09 23:33:39 +00003624 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00003625 if (Scale & 1) return false;
3626 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00003627 }
3628 break;
Evan Chengb01fad62007-03-12 23:30:29 +00003629 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00003630 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00003631}
3632
Owen Andersone50ed302009-08-10 22:56:29 +00003633static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00003634 bool isSEXTLoad, SDValue &Base,
3635 SDValue &Offset, bool &isInc,
3636 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00003637 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
3638 return false;
3639
Owen Anderson825b72b2009-08-11 20:47:22 +00003640 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00003641 // AddressingMode 3
3642 Base = Ptr->getOperand(0);
3643 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003644 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003645 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003646 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00003647 isInc = false;
3648 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3649 return true;
3650 }
3651 }
3652 isInc = (Ptr->getOpcode() == ISD::ADD);
3653 Offset = Ptr->getOperand(1);
3654 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00003655 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00003656 // AddressingMode 2
3657 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003658 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003659 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003660 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00003661 isInc = false;
3662 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3663 Base = Ptr->getOperand(0);
3664 return true;
3665 }
3666 }
3667
3668 if (Ptr->getOpcode() == ISD::ADD) {
3669 isInc = true;
3670 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
3671 if (ShOpcVal != ARM_AM::no_shift) {
3672 Base = Ptr->getOperand(1);
3673 Offset = Ptr->getOperand(0);
3674 } else {
3675 Base = Ptr->getOperand(0);
3676 Offset = Ptr->getOperand(1);
3677 }
3678 return true;
3679 }
3680
3681 isInc = (Ptr->getOpcode() == ISD::ADD);
3682 Base = Ptr->getOperand(0);
3683 Offset = Ptr->getOperand(1);
3684 return true;
3685 }
3686
3687 // FIXME: Use FLDM / FSTM to emulate indexed FP load / store.
3688 return false;
3689}
3690
Owen Andersone50ed302009-08-10 22:56:29 +00003691static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00003692 bool isSEXTLoad, SDValue &Base,
3693 SDValue &Offset, bool &isInc,
3694 SelectionDAG &DAG) {
3695 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
3696 return false;
3697
3698 Base = Ptr->getOperand(0);
3699 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
3700 int RHSC = (int)RHS->getZExtValue();
3701 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
3702 assert(Ptr->getOpcode() == ISD::ADD);
3703 isInc = false;
3704 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3705 return true;
3706 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
3707 isInc = Ptr->getOpcode() == ISD::ADD;
3708 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
3709 return true;
3710 }
3711 }
3712
3713 return false;
3714}
3715
Evan Chenga8e29892007-01-19 07:51:42 +00003716/// getPreIndexedAddressParts - returns true by value, base pointer and
3717/// offset pointer and addressing mode by reference if the node's address
3718/// can be legally represented as pre-indexed load / store address.
3719bool
Dan Gohman475871a2008-07-27 21:46:04 +00003720ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
3721 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003722 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00003723 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003724 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00003725 return false;
3726
Owen Andersone50ed302009-08-10 22:56:29 +00003727 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00003728 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00003729 bool isSEXTLoad = false;
3730 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
3731 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003732 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003733 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
3734 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
3735 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003736 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003737 } else
3738 return false;
3739
3740 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00003741 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00003742 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00003743 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
3744 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00003745 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00003746 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00003747 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00003748 if (!isLegal)
3749 return false;
3750
3751 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
3752 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00003753}
3754
3755/// getPostIndexedAddressParts - returns true by value, base pointer and
3756/// offset pointer and addressing mode by reference if this node can be
3757/// combined with a load / store to form a post-indexed load / store.
3758bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00003759 SDValue &Base,
3760 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003761 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00003762 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003763 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00003764 return false;
3765
Owen Andersone50ed302009-08-10 22:56:29 +00003766 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00003767 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00003768 bool isSEXTLoad = false;
3769 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003770 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003771 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
3772 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003773 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003774 } else
3775 return false;
3776
3777 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00003778 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00003779 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00003780 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003781 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00003782 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00003783 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
3784 isInc, DAG);
3785 if (!isLegal)
3786 return false;
3787
3788 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
3789 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00003790}
3791
Dan Gohman475871a2008-07-27 21:46:04 +00003792void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00003793 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003794 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003795 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00003796 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00003797 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003798 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00003799 switch (Op.getOpcode()) {
3800 default: break;
3801 case ARMISD::CMOV: {
3802 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00003803 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00003804 if (KnownZero == 0 && KnownOne == 0) return;
3805
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003806 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00003807 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
3808 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00003809 KnownZero &= KnownZeroRHS;
3810 KnownOne &= KnownOneRHS;
3811 return;
3812 }
3813 }
3814}
3815
3816//===----------------------------------------------------------------------===//
3817// ARM Inline Assembly Support
3818//===----------------------------------------------------------------------===//
3819
3820/// getConstraintType - Given a constraint letter, return the type of
3821/// constraint it is for this target.
3822ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00003823ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
3824 if (Constraint.size() == 1) {
3825 switch (Constraint[0]) {
3826 default: break;
3827 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003828 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00003829 }
Evan Chenga8e29892007-01-19 07:51:42 +00003830 }
Chris Lattner4234f572007-03-25 02:14:49 +00003831 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00003832}
3833
Bob Wilson2dc4f542009-03-20 22:42:55 +00003834std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00003835ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00003836 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003837 if (Constraint.size() == 1) {
3838 // GCC RS6000 Constraint Letters
3839 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003840 case 'l':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003841 if (Subtarget->isThumb1Only())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00003842 return std::make_pair(0U, ARM::tGPRRegisterClass);
3843 else
3844 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003845 case 'r':
3846 return std::make_pair(0U, ARM::GPRRegisterClass);
3847 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00003848 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003849 return std::make_pair(0U, ARM::SPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00003850 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003851 return std::make_pair(0U, ARM::DPRRegisterClass);
3852 break;
Evan Chenga8e29892007-01-19 07:51:42 +00003853 }
3854 }
3855 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3856}
3857
3858std::vector<unsigned> ARMTargetLowering::
3859getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00003860 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003861 if (Constraint.size() != 1)
3862 return std::vector<unsigned>();
3863
3864 switch (Constraint[0]) { // GCC ARM Constraint Letters
3865 default: break;
3866 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00003867 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
3868 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
3869 0);
Evan Chenga8e29892007-01-19 07:51:42 +00003870 case 'r':
3871 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
3872 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
3873 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
3874 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003875 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00003876 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003877 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
3878 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
3879 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
3880 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
3881 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
3882 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
3883 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
3884 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00003885 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003886 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
3887 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
3888 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
3889 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
3890 break;
Evan Chenga8e29892007-01-19 07:51:42 +00003891 }
3892
3893 return std::vector<unsigned>();
3894}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003895
3896/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3897/// vector. If it is invalid, don't add anything to Ops.
3898void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3899 char Constraint,
3900 bool hasMemory,
3901 std::vector<SDValue>&Ops,
3902 SelectionDAG &DAG) const {
3903 SDValue Result(0, 0);
3904
3905 switch (Constraint) {
3906 default: break;
3907 case 'I': case 'J': case 'K': case 'L':
3908 case 'M': case 'N': case 'O':
3909 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
3910 if (!C)
3911 return;
3912
3913 int64_t CVal64 = C->getSExtValue();
3914 int CVal = (int) CVal64;
3915 // None of these constraints allow values larger than 32 bits. Check
3916 // that the value fits in an int.
3917 if (CVal != CVal64)
3918 return;
3919
3920 switch (Constraint) {
3921 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003922 if (Subtarget->isThumb1Only()) {
3923 // This must be a constant between 0 and 255, for ADD
3924 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003925 if (CVal >= 0 && CVal <= 255)
3926 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00003927 } else if (Subtarget->isThumb2()) {
3928 // A constant that can be used as an immediate value in a
3929 // data-processing instruction.
3930 if (ARM_AM::getT2SOImmVal(CVal) != -1)
3931 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003932 } else {
3933 // A constant that can be used as an immediate value in a
3934 // data-processing instruction.
3935 if (ARM_AM::getSOImmVal(CVal) != -1)
3936 break;
3937 }
3938 return;
3939
3940 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003941 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003942 // This must be a constant between -255 and -1, for negated ADD
3943 // immediates. This can be used in GCC with an "n" modifier that
3944 // prints the negated value, for use with SUB instructions. It is
3945 // not useful otherwise but is implemented for compatibility.
3946 if (CVal >= -255 && CVal <= -1)
3947 break;
3948 } else {
3949 // This must be a constant between -4095 and 4095. It is not clear
3950 // what this constraint is intended for. Implemented for
3951 // compatibility with GCC.
3952 if (CVal >= -4095 && CVal <= 4095)
3953 break;
3954 }
3955 return;
3956
3957 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003958 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003959 // A 32-bit value where only one byte has a nonzero value. Exclude
3960 // zero to match GCC. This constraint is used by GCC internally for
3961 // constants that can be loaded with a move/shift combination.
3962 // It is not useful otherwise but is implemented for compatibility.
3963 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
3964 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00003965 } else if (Subtarget->isThumb2()) {
3966 // A constant whose bitwise inverse can be used as an immediate
3967 // value in a data-processing instruction. This can be used in GCC
3968 // with a "B" modifier that prints the inverted value, for use with
3969 // BIC and MVN instructions. It is not useful otherwise but is
3970 // implemented for compatibility.
3971 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
3972 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003973 } else {
3974 // A constant whose bitwise inverse can be used as an immediate
3975 // value in a data-processing instruction. This can be used in GCC
3976 // with a "B" modifier that prints the inverted value, for use with
3977 // BIC and MVN instructions. It is not useful otherwise but is
3978 // implemented for compatibility.
3979 if (ARM_AM::getSOImmVal(~CVal) != -1)
3980 break;
3981 }
3982 return;
3983
3984 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003985 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003986 // This must be a constant between -7 and 7,
3987 // for 3-operand ADD/SUB immediate instructions.
3988 if (CVal >= -7 && CVal < 7)
3989 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00003990 } else if (Subtarget->isThumb2()) {
3991 // A constant whose negation can be used as an immediate value in a
3992 // data-processing instruction. This can be used in GCC with an "n"
3993 // modifier that prints the negated value, for use with SUB
3994 // instructions. It is not useful otherwise but is implemented for
3995 // compatibility.
3996 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
3997 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003998 } else {
3999 // A constant whose negation can be used as an immediate value in a
4000 // data-processing instruction. This can be used in GCC with an "n"
4001 // modifier that prints the negated value, for use with SUB
4002 // instructions. It is not useful otherwise but is implemented for
4003 // compatibility.
4004 if (ARM_AM::getSOImmVal(-CVal) != -1)
4005 break;
4006 }
4007 return;
4008
4009 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004010 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004011 // This must be a multiple of 4 between 0 and 1020, for
4012 // ADD sp + immediate.
4013 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
4014 break;
4015 } else {
4016 // A power of two or a constant between 0 and 32. This is used in
4017 // GCC for the shift amount on shifted register operands, but it is
4018 // useful in general for any shift amounts.
4019 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
4020 break;
4021 }
4022 return;
4023
4024 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004025 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004026 // This must be a constant between 0 and 31, for shift amounts.
4027 if (CVal >= 0 && CVal <= 31)
4028 break;
4029 }
4030 return;
4031
4032 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004033 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004034 // This must be a multiple of 4 between -508 and 508, for
4035 // ADD/SUB sp = sp + immediate.
4036 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
4037 break;
4038 }
4039 return;
4040 }
4041 Result = DAG.getTargetConstant(CVal, Op.getValueType());
4042 break;
4043 }
4044
4045 if (Result.getNode()) {
4046 Ops.push_back(Result);
4047 return;
4048 }
4049 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
4050 Ops, DAG);
4051}