blob: b82a2209e550bc7f7d15e32ed50efa4d53a44176 [file] [log] [blame]
Evan Chenga8e29892007-01-19 07:51:42 +00001//===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chenga8e29892007-01-19 07:51:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that ARM uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "ARM.h"
16#include "ARMAddressingModes.h"
17#include "ARMConstantPoolValue.h"
18#include "ARMISelLowering.h"
19#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +000020#include "ARMPerfectShuffle.h"
Evan Chenga8e29892007-01-19 07:51:42 +000021#include "ARMRegisterInfo.h"
22#include "ARMSubtarget.h"
23#include "ARMTargetMachine.h"
Chris Lattner80ec2792009-08-02 00:34:36 +000024#include "ARMTargetObjectFile.h"
Evan Chenga8e29892007-01-19 07:51:42 +000025#include "llvm/CallingConv.h"
26#include "llvm/Constants.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000027#include "llvm/Function.h"
Evan Cheng27707472007-03-16 08:43:56 +000028#include "llvm/Instruction.h"
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +000029#include "llvm/Intrinsics.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000030#include "llvm/GlobalValue.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000031#include "llvm/CodeGen/CallingConvLower.h"
Evan Chenga8e29892007-01-19 07:51:42 +000032#include "llvm/CodeGen/MachineBasicBlock.h"
33#include "llvm/CodeGen/MachineFrameInfo.h"
34#include "llvm/CodeGen/MachineFunction.h"
35#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000036#include "llvm/CodeGen/MachineRegisterInfo.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000037#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chenga8e29892007-01-19 07:51:42 +000038#include "llvm/CodeGen/SelectionDAG.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000039#include "llvm/Target/TargetOptions.h"
Evan Chenga8e29892007-01-19 07:51:42 +000040#include "llvm/ADT/VectorExtras.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000041#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000042#include "llvm/Support/MathExtras.h"
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000043#include <sstream>
Evan Chenga8e29892007-01-19 07:51:42 +000044using namespace llvm;
45
Owen Andersone50ed302009-08-10 22:56:29 +000046static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000047 CCValAssign::LocInfo &LocInfo,
48 ISD::ArgFlagsTy &ArgFlags,
49 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000050static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000051 CCValAssign::LocInfo &LocInfo,
52 ISD::ArgFlagsTy &ArgFlags,
53 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000054static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000055 CCValAssign::LocInfo &LocInfo,
56 ISD::ArgFlagsTy &ArgFlags,
57 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000058static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000059 CCValAssign::LocInfo &LocInfo,
60 ISD::ArgFlagsTy &ArgFlags,
61 CCState &State);
62
Owen Andersone50ed302009-08-10 22:56:29 +000063void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
64 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000065 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000066 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000067 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
68 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000069
Owen Anderson70671842009-08-10 20:18:46 +000070 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +000071 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +000072 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000073 }
74
Owen Andersone50ed302009-08-10 22:56:29 +000075 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +000076 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Owen Anderson70671842009-08-10 20:18:46 +000077 setOperationAction(ISD::VSETCC, VT.getSimpleVT(), Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +000078 if (ElemTy == MVT::i8 || ElemTy == MVT::i16)
Owen Anderson70671842009-08-10 20:18:46 +000079 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson0696fdf2009-09-16 20:20:44 +000080 if (ElemTy != MVT::i32) {
81 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
82 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
83 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
84 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
85 }
Owen Anderson70671842009-08-10 20:18:46 +000086 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
87 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Owen Anderson70671842009-08-10 20:18:46 +000088 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Custom);
Anton Korobeynikov8e6c2b92009-08-21 12:40:35 +000089 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +000090 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +000091 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
92 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
93 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +000094 }
95
96 // Promote all bit-wise operations.
97 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +000098 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000099 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
100 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000101 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000102 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000103 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000104 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000105 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000106 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000107 }
Bob Wilson16330762009-09-16 00:17:28 +0000108
109 // Neon does not support vector divide/remainder operations.
110 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
111 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
112 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
113 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
114 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
115 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000116}
117
Owen Andersone50ed302009-08-10 22:56:29 +0000118void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000119 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000120 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000121}
122
Owen Andersone50ed302009-08-10 22:56:29 +0000123void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000124 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000125 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000126}
127
Chris Lattnerf0144122009-07-28 03:13:23 +0000128static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
129 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000130 return new TargetLoweringObjectFileMachO();
Chris Lattner80ec2792009-08-02 00:34:36 +0000131 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000132}
133
Evan Chenga8e29892007-01-19 07:51:42 +0000134ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000135 : TargetLowering(TM, createTLOF(TM)), ARMPCLabelIndex(0) {
Evan Chenga8e29892007-01-19 07:51:42 +0000136 Subtarget = &TM.getSubtarget<ARMSubtarget>();
137
Evan Chengb1df8f22007-04-27 08:15:43 +0000138 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000139 // Uses VFP for Thumb libfuncs if available.
140 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
141 // Single-precision floating-point arithmetic.
142 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
143 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
144 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
145 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000146
Evan Chengb1df8f22007-04-27 08:15:43 +0000147 // Double-precision floating-point arithmetic.
148 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
149 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
150 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
151 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000152
Evan Chengb1df8f22007-04-27 08:15:43 +0000153 // Single-precision comparisons.
154 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
155 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
156 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
157 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
158 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
159 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
160 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
161 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000162
Evan Chengb1df8f22007-04-27 08:15:43 +0000163 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
164 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
165 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
166 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
167 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
168 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
169 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
170 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000171
Evan Chengb1df8f22007-04-27 08:15:43 +0000172 // Double-precision comparisons.
173 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
174 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
175 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
176 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
177 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
178 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
179 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
180 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000181
Evan Chengb1df8f22007-04-27 08:15:43 +0000182 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
183 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
184 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
185 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
186 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
187 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
188 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
189 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000190
Evan Chengb1df8f22007-04-27 08:15:43 +0000191 // Floating-point to integer conversions.
192 // i64 conversions are done via library routines even when generating VFP
193 // instructions, so use the same ones.
194 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
195 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
196 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
197 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000198
Evan Chengb1df8f22007-04-27 08:15:43 +0000199 // Conversions between floating types.
200 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
201 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
202
203 // Integer to floating-point conversions.
204 // i64 conversions are done via library routines even when generating VFP
205 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000206 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
207 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000208 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
209 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
210 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
211 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
212 }
Evan Chenga8e29892007-01-19 07:51:42 +0000213 }
214
Bob Wilson2f954612009-05-22 17:38:41 +0000215 // These libcalls are not available in 32-bit.
216 setLibcallName(RTLIB::SHL_I128, 0);
217 setLibcallName(RTLIB::SRL_I128, 0);
218 setLibcallName(RTLIB::SRA_I128, 0);
219
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000220 // Libcalls should use the AAPCS base standard ABI, even if hard float
221 // is in effect, as per the ARM RTABI specification, section 4.1.2.
222 if (Subtarget->isAAPCS_ABI()) {
223 for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i) {
224 setLibcallCallingConv(static_cast<RTLIB::Libcall>(i),
225 CallingConv::ARM_AAPCS);
226 }
227 }
228
David Goodwinf1daf7d2009-07-08 23:10:31 +0000229 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000230 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000231 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000232 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000233 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000234 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
235 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000236
Owen Anderson825b72b2009-08-11 20:47:22 +0000237 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000238 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000239
240 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000241 addDRTypeForNEON(MVT::v2f32);
242 addDRTypeForNEON(MVT::v8i8);
243 addDRTypeForNEON(MVT::v4i16);
244 addDRTypeForNEON(MVT::v2i32);
245 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000246
Owen Anderson825b72b2009-08-11 20:47:22 +0000247 addQRTypeForNEON(MVT::v4f32);
248 addQRTypeForNEON(MVT::v2f64);
249 addQRTypeForNEON(MVT::v16i8);
250 addQRTypeForNEON(MVT::v8i16);
251 addQRTypeForNEON(MVT::v4i32);
252 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000253
Bob Wilson74dc72e2009-09-15 23:55:57 +0000254 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
255 // neither Neon nor VFP support any arithmetic operations on it.
256 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
257 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
258 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
259 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
260 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
261 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
262 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
263 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
264 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
265 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
266 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
267 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
268 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
269 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
270 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
271 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
272 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
273 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
274 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
275 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
276 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
277 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
278 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
279 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
280
Bob Wilson642b3292009-09-16 00:32:15 +0000281 // Neon does not support some operations on v1i64 and v2i64 types.
282 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
283 setOperationAction(ISD::MUL, MVT::v2i64, Expand);
284 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
285 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
286
Bob Wilson5bafff32009-06-22 23:27:02 +0000287 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
288 setTargetDAGCombine(ISD::SHL);
289 setTargetDAGCombine(ISD::SRL);
290 setTargetDAGCombine(ISD::SRA);
291 setTargetDAGCombine(ISD::SIGN_EXTEND);
292 setTargetDAGCombine(ISD::ZERO_EXTEND);
293 setTargetDAGCombine(ISD::ANY_EXTEND);
294 }
295
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000296 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000297
298 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000299 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000300
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000301 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000302 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000303
Evan Chenga8e29892007-01-19 07:51:42 +0000304 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000305 if (!Subtarget->isThumb1Only()) {
306 for (unsigned im = (unsigned)ISD::PRE_INC;
307 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000308 setIndexedLoadAction(im, MVT::i1, Legal);
309 setIndexedLoadAction(im, MVT::i8, Legal);
310 setIndexedLoadAction(im, MVT::i16, Legal);
311 setIndexedLoadAction(im, MVT::i32, Legal);
312 setIndexedStoreAction(im, MVT::i1, Legal);
313 setIndexedStoreAction(im, MVT::i8, Legal);
314 setIndexedStoreAction(im, MVT::i16, Legal);
315 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000316 }
Evan Chenga8e29892007-01-19 07:51:42 +0000317 }
318
319 // i64 operation support.
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000320 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000321 setOperationAction(ISD::MUL, MVT::i64, Expand);
322 setOperationAction(ISD::MULHU, MVT::i32, Expand);
323 setOperationAction(ISD::MULHS, MVT::i32, Expand);
324 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
325 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000326 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000327 setOperationAction(ISD::MUL, MVT::i64, Expand);
328 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb6207242009-08-01 00:16:10 +0000329 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000330 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000331 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000332 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
333 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
334 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
335 setOperationAction(ISD::SRL, MVT::i64, Custom);
336 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000337
338 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000339 setOperationAction(ISD::ROTL, MVT::i32, Expand);
340 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
341 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000342 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000343 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000344
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000345 // Only ARMv6 has BSWAP.
346 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000347 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000348
Evan Chenga8e29892007-01-19 07:51:42 +0000349 // These are expanded into libcalls.
Owen Anderson825b72b2009-08-11 20:47:22 +0000350 setOperationAction(ISD::SDIV, MVT::i32, Expand);
351 setOperationAction(ISD::UDIV, MVT::i32, Expand);
352 setOperationAction(ISD::SREM, MVT::i32, Expand);
353 setOperationAction(ISD::UREM, MVT::i32, Expand);
354 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
355 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000356
Evan Chenga8e29892007-01-19 07:51:42 +0000357 // Support label based line numbers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000358 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
359 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000360
Owen Anderson825b72b2009-08-11 20:47:22 +0000361 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
362 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
363 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
364 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000365
Evan Chenga8e29892007-01-19 07:51:42 +0000366 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000367 setOperationAction(ISD::VASTART, MVT::Other, Custom);
368 setOperationAction(ISD::VAARG, MVT::Other, Expand);
369 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
370 setOperationAction(ISD::VAEND, MVT::Other, Expand);
371 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
372 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000373 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
374 // FIXME: Shouldn't need this, since no register is used, but the legalizer
375 // doesn't yet know how to not do that for SjLj.
376 setExceptionSelectorRegister(ARM::R0);
Evan Cheng86198642009-08-07 00:34:42 +0000377 if (Subtarget->isThumb())
Owen Anderson825b72b2009-08-11 20:47:22 +0000378 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Evan Cheng86198642009-08-07 00:34:42 +0000379 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000380 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
381 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000382
Evan Chengd27c9fc2009-07-03 01:43:10 +0000383 if (!Subtarget->hasV6Ops() && !Subtarget->isThumb2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000384 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
385 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000386 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000387 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000388
David Goodwinf1daf7d2009-07-08 23:10:31 +0000389 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only())
Evan Chengc7c77292008-11-04 19:57:48 +0000390 // Turn f64->i64 into FMRRD, i64 -> f64 to FMDRR iff target supports vfp2.
Owen Anderson825b72b2009-08-11 20:47:22 +0000391 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000392
393 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000394 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000395
Owen Anderson825b72b2009-08-11 20:47:22 +0000396 setOperationAction(ISD::SETCC, MVT::i32, Expand);
397 setOperationAction(ISD::SETCC, MVT::f32, Expand);
398 setOperationAction(ISD::SETCC, MVT::f64, Expand);
399 setOperationAction(ISD::SELECT, MVT::i32, Expand);
400 setOperationAction(ISD::SELECT, MVT::f32, Expand);
401 setOperationAction(ISD::SELECT, MVT::f64, Expand);
402 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
403 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
404 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000405
Owen Anderson825b72b2009-08-11 20:47:22 +0000406 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
407 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
408 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
409 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
410 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000411
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000412 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000413 setOperationAction(ISD::FSIN, MVT::f64, Expand);
414 setOperationAction(ISD::FSIN, MVT::f32, Expand);
415 setOperationAction(ISD::FCOS, MVT::f32, Expand);
416 setOperationAction(ISD::FCOS, MVT::f64, Expand);
417 setOperationAction(ISD::FREM, MVT::f64, Expand);
418 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000419 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000420 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
421 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000422 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000423 setOperationAction(ISD::FPOW, MVT::f64, Expand);
424 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000425
Evan Chenga8e29892007-01-19 07:51:42 +0000426 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
David Goodwinf1daf7d2009-07-08 23:10:31 +0000427 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000428 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
429 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
430 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
431 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000432 }
Evan Chenga8e29892007-01-19 07:51:42 +0000433
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000434 // We have target-specific dag combine patterns for the following nodes:
435 // ARMISD::FMRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000436 setTargetDAGCombine(ISD::ADD);
437 setTargetDAGCombine(ISD::SUB);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000438
Evan Chenga8e29892007-01-19 07:51:42 +0000439 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Chenga8e29892007-01-19 07:51:42 +0000440 setSchedulingPreference(SchedulingForRegPressure);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000441
Evan Chengbc9b7542009-08-15 07:59:10 +0000442 // FIXME: If-converter should use instruction latency to determine
443 // profitability rather than relying on fixed limits.
444 if (Subtarget->getCPUString() == "generic") {
445 // Generic (and overly aggressive) if-conversion limits.
446 setIfCvtBlockSizeLimit(10);
447 setIfCvtDupBlockSizeLimit(2);
448 } else if (Subtarget->hasV6Ops()) {
449 setIfCvtBlockSizeLimit(2);
450 setIfCvtDupBlockSizeLimit(1);
451 } else {
452 setIfCvtBlockSizeLimit(3);
453 setIfCvtDupBlockSizeLimit(2);
Evan Cheng8557c2b2009-06-19 01:51:50 +0000454 }
455
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000456 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Bob Wilsone6abdff2009-05-18 20:55:32 +0000457 // Do not enable CodePlacementOpt for now: it currently runs after the
458 // ARMConstantIslandPass and messes up branch relaxation and placement
459 // of constant islands.
460 // benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000461}
462
Evan Chenga8e29892007-01-19 07:51:42 +0000463const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
464 switch (Opcode) {
465 default: return 0;
466 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000467 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
468 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000469 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000470 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
471 case ARMISD::tCALL: return "ARMISD::tCALL";
472 case ARMISD::BRCOND: return "ARMISD::BRCOND";
473 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000474 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000475 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
476 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
477 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000478 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000479 case ARMISD::CMPFP: return "ARMISD::CMPFP";
480 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
481 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
482 case ARMISD::CMOV: return "ARMISD::CMOV";
483 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000484
Evan Chenga8e29892007-01-19 07:51:42 +0000485 case ARMISD::FTOSI: return "ARMISD::FTOSI";
486 case ARMISD::FTOUI: return "ARMISD::FTOUI";
487 case ARMISD::SITOF: return "ARMISD::SITOF";
488 case ARMISD::UITOF: return "ARMISD::UITOF";
Evan Chenga8e29892007-01-19 07:51:42 +0000489
490 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
491 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
492 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000493
Evan Chenga8e29892007-01-19 07:51:42 +0000494 case ARMISD::FMRRD: return "ARMISD::FMRRD";
495 case ARMISD::FMDRR: return "ARMISD::FMDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000496
497 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000498
Evan Cheng86198642009-08-07 00:34:42 +0000499 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
500
Bob Wilson5bafff32009-06-22 23:27:02 +0000501 case ARMISD::VCEQ: return "ARMISD::VCEQ";
502 case ARMISD::VCGE: return "ARMISD::VCGE";
503 case ARMISD::VCGEU: return "ARMISD::VCGEU";
504 case ARMISD::VCGT: return "ARMISD::VCGT";
505 case ARMISD::VCGTU: return "ARMISD::VCGTU";
506 case ARMISD::VTST: return "ARMISD::VTST";
507
508 case ARMISD::VSHL: return "ARMISD::VSHL";
509 case ARMISD::VSHRs: return "ARMISD::VSHRs";
510 case ARMISD::VSHRu: return "ARMISD::VSHRu";
511 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
512 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
513 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
514 case ARMISD::VSHRN: return "ARMISD::VSHRN";
515 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
516 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
517 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
518 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
519 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
520 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
521 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
522 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
523 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
524 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
525 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
526 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
527 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
528 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000529 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000530 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000531 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000532 case ARMISD::VREV64: return "ARMISD::VREV64";
533 case ARMISD::VREV32: return "ARMISD::VREV32";
534 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000535 case ARMISD::VZIP: return "ARMISD::VZIP";
536 case ARMISD::VUZP: return "ARMISD::VUZP";
537 case ARMISD::VTRN: return "ARMISD::VTRN";
Evan Chenga8e29892007-01-19 07:51:42 +0000538 }
539}
540
Bill Wendlingb4202b82009-07-01 18:50:55 +0000541/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000542unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Evan Cheng048e36f2009-10-02 06:57:25 +0000543 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 0 : 1;
Bill Wendling20c568f2009-06-30 22:38:32 +0000544}
545
Evan Chenga8e29892007-01-19 07:51:42 +0000546//===----------------------------------------------------------------------===//
547// Lowering Code
548//===----------------------------------------------------------------------===//
549
Evan Chenga8e29892007-01-19 07:51:42 +0000550/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
551static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
552 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000553 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000554 case ISD::SETNE: return ARMCC::NE;
555 case ISD::SETEQ: return ARMCC::EQ;
556 case ISD::SETGT: return ARMCC::GT;
557 case ISD::SETGE: return ARMCC::GE;
558 case ISD::SETLT: return ARMCC::LT;
559 case ISD::SETLE: return ARMCC::LE;
560 case ISD::SETUGT: return ARMCC::HI;
561 case ISD::SETUGE: return ARMCC::HS;
562 case ISD::SETULT: return ARMCC::LO;
563 case ISD::SETULE: return ARMCC::LS;
564 }
565}
566
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000567/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
568static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000569 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000570 CondCode2 = ARMCC::AL;
571 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000572 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000573 case ISD::SETEQ:
574 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
575 case ISD::SETGT:
576 case ISD::SETOGT: CondCode = ARMCC::GT; break;
577 case ISD::SETGE:
578 case ISD::SETOGE: CondCode = ARMCC::GE; break;
579 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000580 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000581 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
582 case ISD::SETO: CondCode = ARMCC::VC; break;
583 case ISD::SETUO: CondCode = ARMCC::VS; break;
584 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
585 case ISD::SETUGT: CondCode = ARMCC::HI; break;
586 case ISD::SETUGE: CondCode = ARMCC::PL; break;
587 case ISD::SETLT:
588 case ISD::SETULT: CondCode = ARMCC::LT; break;
589 case ISD::SETLE:
590 case ISD::SETULE: CondCode = ARMCC::LE; break;
591 case ISD::SETNE:
592 case ISD::SETUNE: CondCode = ARMCC::NE; break;
593 }
Evan Chenga8e29892007-01-19 07:51:42 +0000594}
595
Bob Wilson1f595bb2009-04-17 19:07:39 +0000596//===----------------------------------------------------------------------===//
597// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +0000598//===----------------------------------------------------------------------===//
599
600#include "ARMGenCallingConv.inc"
601
602// APCS f64 is in register pairs, possibly split to stack
Owen Andersone50ed302009-08-10 22:56:29 +0000603static bool f64AssignAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000604 CCValAssign::LocInfo &LocInfo,
605 CCState &State, bool CanFail) {
606 static const unsigned RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
607
608 // Try to get the first register.
609 if (unsigned Reg = State.AllocateReg(RegList, 4))
610 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
611 else {
612 // For the 2nd half of a v2f64, do not fail.
613 if (CanFail)
614 return false;
615
616 // Put the whole thing on the stack.
617 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
618 State.AllocateStack(8, 4),
619 LocVT, LocInfo));
620 return true;
621 }
622
623 // Try to get the second register.
624 if (unsigned Reg = State.AllocateReg(RegList, 4))
625 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
626 else
627 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
628 State.AllocateStack(4, 4),
629 LocVT, LocInfo));
630 return true;
631}
632
Owen Andersone50ed302009-08-10 22:56:29 +0000633static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000634 CCValAssign::LocInfo &LocInfo,
635 ISD::ArgFlagsTy &ArgFlags,
636 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000637 if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
638 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000639 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000640 !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
641 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000642 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000643}
644
645// AAPCS f64 is in aligned register pairs
Owen Andersone50ed302009-08-10 22:56:29 +0000646static bool f64AssignAAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000647 CCValAssign::LocInfo &LocInfo,
648 CCState &State, bool CanFail) {
649 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
650 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
651
652 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
653 if (Reg == 0) {
654 // For the 2nd half of a v2f64, do not just fail.
655 if (CanFail)
656 return false;
657
658 // Put the whole thing on the stack.
659 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
660 State.AllocateStack(8, 8),
661 LocVT, LocInfo));
662 return true;
663 }
664
665 unsigned i;
666 for (i = 0; i < 2; ++i)
667 if (HiRegList[i] == Reg)
668 break;
669
670 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
671 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
672 LocVT, LocInfo));
673 return true;
674}
675
Owen Andersone50ed302009-08-10 22:56:29 +0000676static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000677 CCValAssign::LocInfo &LocInfo,
678 ISD::ArgFlagsTy &ArgFlags,
679 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000680 if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
681 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000682 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000683 !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
684 return false;
685 return true; // we handled it
686}
687
Owen Andersone50ed302009-08-10 22:56:29 +0000688static bool f64RetAssign(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000689 CCValAssign::LocInfo &LocInfo, CCState &State) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000690 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
691 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
692
Bob Wilsone65586b2009-04-17 20:40:45 +0000693 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
694 if (Reg == 0)
695 return false; // we didn't handle it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000696
Bob Wilsone65586b2009-04-17 20:40:45 +0000697 unsigned i;
698 for (i = 0; i < 2; ++i)
699 if (HiRegList[i] == Reg)
700 break;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000701
Bob Wilson5bafff32009-06-22 23:27:02 +0000702 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bob Wilsone65586b2009-04-17 20:40:45 +0000703 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000704 LocVT, LocInfo));
705 return true;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000706}
707
Owen Andersone50ed302009-08-10 22:56:29 +0000708static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000709 CCValAssign::LocInfo &LocInfo,
710 ISD::ArgFlagsTy &ArgFlags,
711 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000712 if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
713 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000714 if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
Bob Wilson5bafff32009-06-22 23:27:02 +0000715 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000716 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000717}
718
Owen Andersone50ed302009-08-10 22:56:29 +0000719static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000720 CCValAssign::LocInfo &LocInfo,
721 ISD::ArgFlagsTy &ArgFlags,
722 CCState &State) {
723 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
724 State);
725}
726
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000727/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
728/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000729CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000730 bool Return,
731 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000732 switch (CC) {
733 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000734 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000735 case CallingConv::C:
736 case CallingConv::Fast:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000737 // Use target triple & subtarget features to do actual dispatch.
738 if (Subtarget->isAAPCS_ABI()) {
739 if (Subtarget->hasVFP2() &&
740 FloatABIType == FloatABI::Hard && !isVarArg)
741 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
742 else
743 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
744 } else
745 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000746 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000747 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000748 case CallingConv::ARM_AAPCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000749 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000750 case CallingConv::ARM_APCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000751 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000752 }
753}
754
Dan Gohman98ca4f22009-08-05 01:29:28 +0000755/// LowerCallResult - Lower the result values of a call into the
756/// appropriate copies out of appropriate physical registers.
757SDValue
758ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000759 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000760 const SmallVectorImpl<ISD::InputArg> &Ins,
761 DebugLoc dl, SelectionDAG &DAG,
762 SmallVectorImpl<SDValue> &InVals) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000763
Bob Wilson1f595bb2009-04-17 19:07:39 +0000764 // Assign locations to each value returned by this call.
765 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000766 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000767 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000768 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000769 CCAssignFnForNode(CallConv, /* Return*/ true,
770 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000771
772 // Copy all of the result registers out of their specified physreg.
773 for (unsigned i = 0; i != RVLocs.size(); ++i) {
774 CCValAssign VA = RVLocs[i];
775
Bob Wilson80915242009-04-25 00:33:20 +0000776 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000777 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000778 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000779 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000780 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000781 Chain = Lo.getValue(1);
782 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000783 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000784 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000785 InFlag);
786 Chain = Hi.getValue(1);
787 InFlag = Hi.getValue(2);
Owen Anderson825b72b2009-08-11 20:47:22 +0000788 Val = DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +0000789
Owen Anderson825b72b2009-08-11 20:47:22 +0000790 if (VA.getLocVT() == MVT::v2f64) {
791 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
792 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
793 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000794
795 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000796 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000797 Chain = Lo.getValue(1);
798 InFlag = Lo.getValue(2);
799 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000800 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000801 Chain = Hi.getValue(1);
802 InFlag = Hi.getValue(2);
Owen Anderson825b72b2009-08-11 20:47:22 +0000803 Val = DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
804 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
805 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000806 }
Bob Wilson1f595bb2009-04-17 19:07:39 +0000807 } else {
Bob Wilson80915242009-04-25 00:33:20 +0000808 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
809 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000810 Chain = Val.getValue(1);
811 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000812 }
Bob Wilson80915242009-04-25 00:33:20 +0000813
814 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000815 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +0000816 case CCValAssign::Full: break;
817 case CCValAssign::BCvt:
818 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
819 break;
820 }
821
Dan Gohman98ca4f22009-08-05 01:29:28 +0000822 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000823 }
824
Dan Gohman98ca4f22009-08-05 01:29:28 +0000825 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000826}
827
828/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
829/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +0000830/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +0000831/// a byval function parameter.
832/// Sometimes what we are copying is the end of a larger object, the part that
833/// does not fit in registers.
834static SDValue
835CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
836 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
837 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000838 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000839 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
840 /*AlwaysInline=*/false, NULL, 0, NULL, 0);
841}
842
Bob Wilsondee46d72009-04-17 20:35:10 +0000843/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000844SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +0000845ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
846 SDValue StackPtr, SDValue Arg,
847 DebugLoc dl, SelectionDAG &DAG,
848 const CCValAssign &VA,
849 ISD::ArgFlagsTy Flags) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000850 unsigned LocMemOffset = VA.getLocMemOffset();
851 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
852 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
853 if (Flags.isByVal()) {
854 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
855 }
856 return DAG.getStore(Chain, dl, Arg, PtrOff,
857 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chenga8e29892007-01-19 07:51:42 +0000858}
859
Dan Gohman98ca4f22009-08-05 01:29:28 +0000860void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +0000861 SDValue Chain, SDValue &Arg,
862 RegsToPassVector &RegsToPass,
863 CCValAssign &VA, CCValAssign &NextVA,
864 SDValue &StackPtr,
865 SmallVector<SDValue, 8> &MemOpChains,
866 ISD::ArgFlagsTy Flags) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000867
868 SDValue fmrrd = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +0000869 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +0000870 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
871
872 if (NextVA.isRegLoc())
873 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
874 else {
875 assert(NextVA.isMemLoc());
876 if (StackPtr.getNode() == 0)
877 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
878
Dan Gohman98ca4f22009-08-05 01:29:28 +0000879 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
880 dl, DAG, NextVA,
881 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000882 }
883}
884
Dan Gohman98ca4f22009-08-05 01:29:28 +0000885/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +0000886/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
887/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000888SDValue
889ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000890 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000891 bool isTailCall,
892 const SmallVectorImpl<ISD::OutputArg> &Outs,
893 const SmallVectorImpl<ISD::InputArg> &Ins,
894 DebugLoc dl, SelectionDAG &DAG,
895 SmallVectorImpl<SDValue> &InVals) {
Evan Chenga8e29892007-01-19 07:51:42 +0000896
Bob Wilson1f595bb2009-04-17 19:07:39 +0000897 // Analyze operands of the call, assigning locations to each operand.
898 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000899 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
900 *DAG.getContext());
901 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000902 CCAssignFnForNode(CallConv, /* Return*/ false,
903 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +0000904
Bob Wilson1f595bb2009-04-17 19:07:39 +0000905 // Get a count of how many bytes are to be pushed on the stack.
906 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +0000907
908 // Adjust the stack pointer for the new arguments...
909 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattnere563bbc2008-10-11 22:08:30 +0000910 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +0000911
Owen Anderson825b72b2009-08-11 20:47:22 +0000912 SDValue StackPtr = DAG.getRegister(ARM::SP, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000913
Bob Wilson5bafff32009-06-22 23:27:02 +0000914 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000915 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +0000916
Bob Wilson1f595bb2009-04-17 19:07:39 +0000917 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +0000918 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000919 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
920 i != e;
921 ++i, ++realArgIdx) {
922 CCValAssign &VA = ArgLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +0000923 SDValue Arg = Outs[realArgIdx].Val;
924 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +0000925
Bob Wilson1f595bb2009-04-17 19:07:39 +0000926 // Promote the value if needed.
927 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000928 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +0000929 case CCValAssign::Full: break;
930 case CCValAssign::SExt:
931 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
932 break;
933 case CCValAssign::ZExt:
934 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
935 break;
936 case CCValAssign::AExt:
937 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
938 break;
939 case CCValAssign::BCvt:
940 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
941 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000942 }
943
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000944 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +0000945 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000946 if (VA.getLocVT() == MVT::v2f64) {
947 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
948 DAG.getConstant(0, MVT::i32));
949 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
950 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000951
Dan Gohman98ca4f22009-08-05 01:29:28 +0000952 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +0000953 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
954
955 VA = ArgLocs[++i]; // skip ahead to next loc
956 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000957 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +0000958 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
959 } else {
960 assert(VA.isMemLoc());
961 if (StackPtr.getNode() == 0)
962 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
963
Dan Gohman98ca4f22009-08-05 01:29:28 +0000964 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
965 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000966 }
967 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000968 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000969 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000970 }
971 } else if (VA.isRegLoc()) {
972 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
973 } else {
974 assert(VA.isMemLoc());
975 if (StackPtr.getNode() == 0)
976 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
977
Dan Gohman98ca4f22009-08-05 01:29:28 +0000978 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
979 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000980 }
Evan Chenga8e29892007-01-19 07:51:42 +0000981 }
982
983 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000984 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +0000985 &MemOpChains[0], MemOpChains.size());
986
987 // Build a sequence of copy-to-reg nodes chained together with token chain
988 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +0000989 SDValue InFlag;
Evan Chenga8e29892007-01-19 07:51:42 +0000990 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Bob Wilson2dc4f542009-03-20 22:42:55 +0000991 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000992 RegsToPass[i].second, InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +0000993 InFlag = Chain.getValue(1);
994 }
995
Bill Wendling056292f2008-09-16 21:48:12 +0000996 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
997 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
998 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +0000999 bool isDirect = false;
1000 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001001 bool isLocalARMFunc = false;
Evan Chenga8e29892007-01-19 07:51:42 +00001002 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1003 GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001004 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001005 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001006 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001007 getTargetMachine().getRelocationModel() != Reloc::Static;
1008 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001009 // ARM call to a local ARM function is predicable.
1010 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
Evan Chengc60e76d2007-01-30 20:37:08 +00001011 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001012 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge4e4ed32009-08-28 23:18:09 +00001013 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001014 ARMPCLabelIndex,
1015 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001016 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001017 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001018 Callee = DAG.getLoad(getPointerTy(), dl,
1019 DAG.getEntryNode(), CPAddr, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001020 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001021 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001022 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001023 } else
1024 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +00001025 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001026 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001027 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001028 getTargetMachine().getRelocationModel() != Reloc::Static;
1029 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001030 // tBX takes a register source operand.
1031 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001032 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Owen Anderson1d0be152009-08-13 21:58:54 +00001033 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001034 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001035 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001036 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001037 Callee = DAG.getLoad(getPointerTy(), dl,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001038 DAG.getEntryNode(), CPAddr, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001039 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001040 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001041 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001042 } else
Bill Wendling056292f2008-09-16 21:48:12 +00001043 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001044 }
1045
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001046 // FIXME: handle tail calls differently.
1047 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001048 if (Subtarget->isThumb()) {
1049 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001050 CallOpc = ARMISD::CALL_NOLINK;
1051 else
1052 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1053 } else {
1054 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001055 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1056 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001057 }
David Goodwinf1daf7d2009-07-08 23:10:31 +00001058 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb1Only()) {
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +00001059 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Owen Anderson825b72b2009-08-11 20:47:22 +00001060 Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001061 InFlag = Chain.getValue(1);
1062 }
1063
Dan Gohman475871a2008-07-27 21:46:04 +00001064 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001065 Ops.push_back(Chain);
1066 Ops.push_back(Callee);
1067
1068 // Add argument registers to the end of the list so that they are known live
1069 // into the call.
1070 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1071 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1072 RegsToPass[i].second.getValueType()));
1073
Gabor Greifba36cb52008-08-28 21:40:38 +00001074 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001075 Ops.push_back(InFlag);
Duncan Sands4bdcb612008-07-02 17:40:58 +00001076 // Returns a chain and a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00001077 Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Flag),
Duncan Sands4bdcb612008-07-02 17:40:58 +00001078 &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001079 InFlag = Chain.getValue(1);
1080
Chris Lattnere563bbc2008-10-11 22:08:30 +00001081 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1082 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001083 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001084 InFlag = Chain.getValue(1);
1085
Bob Wilson1f595bb2009-04-17 19:07:39 +00001086 // Handle result values, copying them out of physregs into vregs that we
1087 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001088 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1089 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001090}
1091
Dan Gohman98ca4f22009-08-05 01:29:28 +00001092SDValue
1093ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001094 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001095 const SmallVectorImpl<ISD::OutputArg> &Outs,
1096 DebugLoc dl, SelectionDAG &DAG) {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001097
Bob Wilsondee46d72009-04-17 20:35:10 +00001098 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001099 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001100
Bob Wilsondee46d72009-04-17 20:35:10 +00001101 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001102 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1103 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001104
Dan Gohman98ca4f22009-08-05 01:29:28 +00001105 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001106 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1107 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001108
1109 // If this is the first return lowered for this function, add
1110 // the regs to the liveout set for the function.
1111 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1112 for (unsigned i = 0; i != RVLocs.size(); ++i)
1113 if (RVLocs[i].isRegLoc())
1114 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001115 }
1116
Bob Wilson1f595bb2009-04-17 19:07:39 +00001117 SDValue Flag;
1118
1119 // Copy the result values into the output registers.
1120 for (unsigned i = 0, realRVLocIdx = 0;
1121 i != RVLocs.size();
1122 ++i, ++realRVLocIdx) {
1123 CCValAssign &VA = RVLocs[i];
1124 assert(VA.isRegLoc() && "Can only return in registers!");
1125
Dan Gohman98ca4f22009-08-05 01:29:28 +00001126 SDValue Arg = Outs[realRVLocIdx].Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001127
1128 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001129 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001130 case CCValAssign::Full: break;
1131 case CCValAssign::BCvt:
1132 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1133 break;
1134 }
1135
Bob Wilson1f595bb2009-04-17 19:07:39 +00001136 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001137 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001138 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001139 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1140 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001141 SDValue HalfGPRs = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001142 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001143
1144 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1145 Flag = Chain.getValue(1);
1146 VA = RVLocs[++i]; // skip ahead to next loc
1147 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1148 HalfGPRs.getValue(1), Flag);
1149 Flag = Chain.getValue(1);
1150 VA = RVLocs[++i]; // skip ahead to next loc
1151
1152 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001153 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1154 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001155 }
1156 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1157 // available.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001158 SDValue fmrrd = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001159 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001160 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001161 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001162 VA = RVLocs[++i]; // skip ahead to next loc
1163 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1164 Flag);
1165 } else
1166 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1167
Bob Wilsondee46d72009-04-17 20:35:10 +00001168 // Guarantee that all emitted copies are
1169 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001170 Flag = Chain.getValue(1);
1171 }
1172
1173 SDValue result;
1174 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001175 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001176 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001177 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001178
1179 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001180}
1181
Bob Wilson2dc4f542009-03-20 22:42:55 +00001182// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
Bob Wilsond2559bf2009-07-13 18:11:36 +00001183// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
Bill Wendling056292f2008-09-16 21:48:12 +00001184// one of the above mentioned nodes. It has to be wrapped because otherwise
1185// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1186// be used to form addressing mode. These wrapped nodes will be selected
1187// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001188static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001189 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001190 // FIXME there is no actual debug info here
1191 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001192 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001193 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001194 if (CP->isMachineConstantPoolEntry())
1195 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1196 CP->getAlignment());
1197 else
1198 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1199 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001200 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001201}
1202
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001203// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001204SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001205ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
1206 SelectionDAG &DAG) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001207 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001208 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001209 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1210 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001211 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001212 ARMCP::CPValue, PCAdj, "tlsgd", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001213 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001214 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001215 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001216 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001217
Owen Anderson825b72b2009-08-11 20:47:22 +00001218 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001219 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001220
1221 // call __tls_get_addr.
1222 ArgListTy Args;
1223 ArgListEntry Entry;
1224 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001225 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001226 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001227 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001228 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001229 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1230 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001231 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001232 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001233 return CallResult.first;
1234}
1235
1236// Lower ISD::GlobalTLSAddress using the "initial exec" or
1237// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001238SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001239ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001240 SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001241 GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001242 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001243 SDValue Offset;
1244 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001245 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001246 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001247 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001248
Chris Lattner4fb63d02009-07-15 04:12:33 +00001249 if (GV->isDeclaration()) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001250 // initial exec model
1251 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1252 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001253 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001254 ARMCP::CPValue, PCAdj, "gottpoff", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001255 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001256 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001257 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001258 Chain = Offset.getValue(1);
1259
Owen Anderson825b72b2009-08-11 20:47:22 +00001260 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001261 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001262
Dale Johannesen33c960f2009-02-04 20:06:27 +00001263 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001264 } else {
1265 // local exec model
Evan Chenge4e4ed32009-08-28 23:18:09 +00001266 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, "tpoff");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001267 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001268 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001269 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001270 }
1271
1272 // The address of the thread local variable is the add of the thread
1273 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001274 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001275}
1276
Dan Gohman475871a2008-07-27 21:46:04 +00001277SDValue
1278ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001279 // TODO: implement the "local dynamic" model
1280 assert(Subtarget->isTargetELF() &&
1281 "TLS not implemented for non-ELF targets");
1282 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1283 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1284 // otherwise use the "Local Exec" TLS Model
1285 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1286 return LowerToTLSGeneralDynamicModel(GA, DAG);
1287 else
1288 return LowerToTLSExecModels(GA, DAG);
1289}
1290
Dan Gohman475871a2008-07-27 21:46:04 +00001291SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001292 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001293 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001294 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001295 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1296 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1297 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001298 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001299 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001300 new ARMConstantPoolValue(GV, UseGOTOFF ? "GOTOFF" : "GOT");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001301 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001302 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001303 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001304 CPAddr,
1305 PseudoSourceValue::getConstantPool(), 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001306 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001307 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001308 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001309 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001310 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
1311 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001312 return Result;
1313 } else {
Evan Cheng1606e8e2009-03-13 07:51:59 +00001314 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001315 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001316 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1317 PseudoSourceValue::getConstantPool(), 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001318 }
1319}
1320
Dan Gohman475871a2008-07-27 21:46:04 +00001321SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001322 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001323 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001324 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001325 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1326 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Dan Gohman475871a2008-07-27 21:46:04 +00001327 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +00001328 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001329 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001330 else {
Evan Chenge4e4ed32009-08-28 23:18:09 +00001331 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
1332 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001333 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001334 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001335 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001336 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001337
Dale Johannesen33c960f2009-02-04 20:06:27 +00001338 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001339 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001340
1341 if (RelocM == Reloc::PIC_) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001342 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001343 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001344 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00001345
Evan Cheng63476a82009-09-03 07:04:02 +00001346 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Dale Johannesen33c960f2009-02-04 20:06:27 +00001347 Result = DAG.getLoad(PtrVT, dl, Chain, Result, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001348
1349 return Result;
1350}
1351
Dan Gohman475871a2008-07-27 21:46:04 +00001352SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001353 SelectionDAG &DAG){
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001354 assert(Subtarget->isTargetELF() &&
1355 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Owen Andersone50ed302009-08-10 22:56:29 +00001356 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001357 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001358 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00001359 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1360 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00001361 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001362 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001363 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001364 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1365 PseudoSourceValue::getConstantPool(), 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001366 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001367 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001368}
1369
Jim Grosbach0e0da732009-05-12 23:59:14 +00001370SDValue
1371ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001372 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001373 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001374 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001375 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00001376 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00001377 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00001378 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1379 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001380 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001381 MachineFunction &MF = DAG.getMachineFunction();
1382 EVT PtrVT = getPointerTy();
1383 DebugLoc dl = Op.getDebugLoc();
1384 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1385 SDValue CPAddr;
1386 unsigned PCAdj = (RelocM != Reloc::PIC_)
1387 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001388 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001389 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
1390 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001391 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001392 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001393 SDValue Result =
1394 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
1395 SDValue Chain = Result.getValue(1);
1396
1397 if (RelocM == Reloc::PIC_) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001398 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001399 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1400 }
1401 return Result;
1402 }
Jim Grosbachf9570122009-05-14 00:46:35 +00001403 case Intrinsic::eh_sjlj_setjmp:
Owen Anderson825b72b2009-08-11 20:47:22 +00001404 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(1));
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001405 }
1406}
1407
Dan Gohman475871a2008-07-27 21:46:04 +00001408static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001409 unsigned VarArgsFrameIndex) {
Evan Chenga8e29892007-01-19 07:51:42 +00001410 // vastart just stores the address of the VarArgsFrameIndex slot into the
1411 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001412 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001413 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001414 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001415 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001416 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001417}
1418
Dan Gohman475871a2008-07-27 21:46:04 +00001419SDValue
Evan Cheng86198642009-08-07 00:34:42 +00001420ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) {
1421 SDNode *Node = Op.getNode();
1422 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001423 EVT VT = Node->getValueType(0);
Evan Cheng86198642009-08-07 00:34:42 +00001424 SDValue Chain = Op.getOperand(0);
1425 SDValue Size = Op.getOperand(1);
1426 SDValue Align = Op.getOperand(2);
1427
1428 // Chain the dynamic stack allocation so that it doesn't modify the stack
1429 // pointer when other instructions are using the stack.
1430 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1431
1432 unsigned AlignVal = cast<ConstantSDNode>(Align)->getZExtValue();
1433 unsigned StackAlign = getTargetMachine().getFrameInfo()->getStackAlignment();
1434 if (AlignVal > StackAlign)
1435 // Do this now since selection pass cannot introduce new target
1436 // independent node.
1437 Align = DAG.getConstant(-(uint64_t)AlignVal, VT);
1438
1439 // In Thumb1 mode, there isn't a "sub r, sp, r" instruction, we will end up
1440 // using a "add r, sp, r" instead. Negate the size now so we don't have to
1441 // do even more horrible hack later.
1442 MachineFunction &MF = DAG.getMachineFunction();
1443 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1444 if (AFI->isThumb1OnlyFunction()) {
1445 bool Negate = true;
1446 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Size);
1447 if (C) {
1448 uint32_t Val = C->getZExtValue();
1449 if (Val <= 508 && ((Val & 3) == 0))
1450 Negate = false;
1451 }
1452 if (Negate)
1453 Size = DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(0, VT), Size);
1454 }
1455
Owen Anderson825b72b2009-08-11 20:47:22 +00001456 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
Evan Cheng86198642009-08-07 00:34:42 +00001457 SDValue Ops1[] = { Chain, Size, Align };
1458 SDValue Res = DAG.getNode(ARMISD::DYN_ALLOC, dl, VTList, Ops1, 3);
1459 Chain = Res.getValue(1);
1460 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1461 DAG.getIntPtrConstant(0, true), SDValue());
1462 SDValue Ops2[] = { Res, Chain };
1463 return DAG.getMergeValues(Ops2, 2, dl);
1464}
1465
1466SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00001467ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
1468 SDValue &Root, SelectionDAG &DAG,
1469 DebugLoc dl) {
1470 MachineFunction &MF = DAG.getMachineFunction();
1471 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1472
1473 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001474 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00001475 RC = ARM::tGPRRegisterClass;
1476 else
1477 RC = ARM::GPRRegisterClass;
1478
1479 // Transform the arguments stored in physical registers into virtual ones.
1480 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001481 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001482
1483 SDValue ArgValue2;
1484 if (NextVA.isMemLoc()) {
1485 unsigned ArgSize = NextVA.getLocVT().getSizeInBits()/8;
1486 MachineFrameInfo *MFI = MF.getFrameInfo();
1487 int FI = MFI->CreateFixedObject(ArgSize, NextVA.getLocMemOffset());
1488
1489 // Create load node to retrieve arguments from the stack.
1490 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00001491 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN, NULL, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00001492 } else {
1493 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001494 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001495 }
1496
Owen Anderson825b72b2009-08-11 20:47:22 +00001497 return DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00001498}
1499
1500SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001501ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001502 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001503 const SmallVectorImpl<ISD::InputArg>
1504 &Ins,
1505 DebugLoc dl, SelectionDAG &DAG,
1506 SmallVectorImpl<SDValue> &InVals) {
1507
Bob Wilson1f595bb2009-04-17 19:07:39 +00001508 MachineFunction &MF = DAG.getMachineFunction();
1509 MachineFrameInfo *MFI = MF.getFrameInfo();
1510
Bob Wilson1f595bb2009-04-17 19:07:39 +00001511 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1512
1513 // Assign locations to all of the incoming arguments.
1514 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001515 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1516 *DAG.getContext());
1517 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001518 CCAssignFnForNode(CallConv, /* Return*/ false,
1519 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001520
1521 SmallVector<SDValue, 16> ArgValues;
1522
1523 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1524 CCValAssign &VA = ArgLocs[i];
1525
Bob Wilsondee46d72009-04-17 20:35:10 +00001526 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001527 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001528 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00001529
Bob Wilson5bafff32009-06-22 23:27:02 +00001530 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001531 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001532 // f64 and vector types are split up into multiple registers or
1533 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00001534 RegVT = MVT::i32;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001535
Owen Anderson825b72b2009-08-11 20:47:22 +00001536 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001537 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001538 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00001539 VA = ArgLocs[++i]; // skip ahead to next loc
1540 SDValue ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001541 Chain, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001542 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1543 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001544 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00001545 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001546 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
1547 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001548 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001549
Bob Wilson5bafff32009-06-22 23:27:02 +00001550 } else {
1551 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001552
Owen Anderson825b72b2009-08-11 20:47:22 +00001553 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00001554 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001555 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00001556 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001557 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001558 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001559 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001560 RC = (AFI->isThumb1OnlyFunction() ?
1561 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00001562 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001563 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00001564
1565 // Transform the arguments in physical registers into virtual ones.
1566 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001567 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001568 }
1569
1570 // If this is an 8 or 16-bit value, it is really passed promoted
1571 // to 32 bits. Insert an assert[sz]ext to capture this, then
1572 // truncate to the right size.
1573 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001574 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001575 case CCValAssign::Full: break;
1576 case CCValAssign::BCvt:
1577 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1578 break;
1579 case CCValAssign::SExt:
1580 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
1581 DAG.getValueType(VA.getValVT()));
1582 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1583 break;
1584 case CCValAssign::ZExt:
1585 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
1586 DAG.getValueType(VA.getValVT()));
1587 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1588 break;
1589 }
1590
Dan Gohman98ca4f22009-08-05 01:29:28 +00001591 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001592
1593 } else { // VA.isRegLoc()
1594
1595 // sanity check
1596 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00001597 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001598
1599 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
1600 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset());
1601
Bob Wilsondee46d72009-04-17 20:35:10 +00001602 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001603 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001604 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN, NULL, 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001605 }
1606 }
1607
1608 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00001609 if (isVarArg) {
1610 static const unsigned GPRArgRegs[] = {
1611 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1612 };
1613
Bob Wilsondee46d72009-04-17 20:35:10 +00001614 unsigned NumGPRs = CCInfo.getFirstUnallocated
1615 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001616
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001617 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1618 unsigned VARegSize = (4 - NumGPRs) * 4;
1619 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001620 unsigned ArgOffset = 0;
Evan Chenga8e29892007-01-19 07:51:42 +00001621 if (VARegSaveSize) {
1622 // If this function is vararg, store any remaining integer argument regs
1623 // to their spots on the stack so that they may be loaded by deferencing
1624 // the result of va_next.
1625 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001626 ArgOffset = CCInfo.getNextStackOffset();
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001627 VarArgsFrameIndex = MFI->CreateFixedObject(VARegSaveSize, ArgOffset +
1628 VARegSaveSize - VARegSize);
Dan Gohman475871a2008-07-27 21:46:04 +00001629 SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001630
Dan Gohman475871a2008-07-27 21:46:04 +00001631 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00001632 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001633 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001634 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00001635 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001636 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00001637 RC = ARM::GPRRegisterClass;
1638
Bob Wilson998e1252009-04-20 18:36:57 +00001639 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001640 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001641 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001642 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001643 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00001644 DAG.getConstant(4, getPointerTy()));
1645 }
1646 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001647 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001648 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001649 } else
1650 // This will point to the next argument passed via stack.
1651 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1652 }
1653
Dan Gohman98ca4f22009-08-05 01:29:28 +00001654 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00001655}
1656
1657/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00001658static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00001659 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001660 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00001661 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00001662 // Maybe this has already been legalized into the constant pool?
1663 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00001664 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001665 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
1666 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001667 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001668 }
1669 }
1670 return false;
1671}
1672
David Goodwinf1daf7d2009-07-08 23:10:31 +00001673static bool isLegalCmpImmediate(unsigned C, bool isThumb1Only) {
1674 return ( isThumb1Only && (C & ~255U) == 0) ||
1675 (!isThumb1Only && ARM_AM::getSOImmVal(C) != -1);
Evan Chenga8e29892007-01-19 07:51:42 +00001676}
1677
1678/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1679/// the given operands.
Dan Gohman475871a2008-07-27 21:46:04 +00001680static SDValue getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
David Goodwinf1daf7d2009-07-08 23:10:31 +00001681 SDValue &ARMCC, SelectionDAG &DAG, bool isThumb1Only,
Dale Johannesende064702009-02-06 21:50:26 +00001682 DebugLoc dl) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001683 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001684 unsigned C = RHSC->getZExtValue();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001685 if (!isLegalCmpImmediate(C, isThumb1Only)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001686 // Constant does not fit, try adjusting it by one?
1687 switch (CC) {
1688 default: break;
1689 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00001690 case ISD::SETGE:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001691 if (isLegalCmpImmediate(C-1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001692 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001693 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001694 }
1695 break;
1696 case ISD::SETULT:
1697 case ISD::SETUGE:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001698 if (C > 0 && isLegalCmpImmediate(C-1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001699 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001700 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001701 }
1702 break;
1703 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00001704 case ISD::SETGT:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001705 if (isLegalCmpImmediate(C+1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001706 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001707 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001708 }
1709 break;
1710 case ISD::SETULE:
1711 case ISD::SETUGT:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001712 if (C < 0xffffffff && isLegalCmpImmediate(C+1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001713 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001714 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001715 }
1716 break;
1717 }
1718 }
1719 }
1720
1721 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001722 ARMISD::NodeType CompareType;
1723 switch (CondCode) {
1724 default:
1725 CompareType = ARMISD::CMP;
1726 break;
1727 case ARMCC::EQ:
1728 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00001729 // Uses only Z Flag
1730 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001731 break;
1732 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001733 ARMCC = DAG.getConstant(CondCode, MVT::i32);
1734 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001735}
1736
1737/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001738static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Dale Johannesende064702009-02-06 21:50:26 +00001739 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001740 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00001741 if (!isFloatingPointZero(RHS))
Owen Anderson825b72b2009-08-11 20:47:22 +00001742 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001743 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001744 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
1745 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001746}
1747
Dan Gohman475871a2008-07-27 21:46:04 +00001748static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001749 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00001750 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001751 SDValue LHS = Op.getOperand(0);
1752 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001753 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001754 SDValue TrueVal = Op.getOperand(2);
1755 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00001756 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001757
Owen Anderson825b72b2009-08-11 20:47:22 +00001758 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001759 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001760 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
David Goodwinf1daf7d2009-07-08 23:10:31 +00001761 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb1Only(), dl);
Dale Johannesende064702009-02-06 21:50:26 +00001762 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001763 }
1764
1765 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001766 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00001767
Owen Anderson825b72b2009-08-11 20:47:22 +00001768 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1769 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001770 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
1771 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00001772 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001773 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001774 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001775 // FIXME: Needs another CMP because flag can have but one use.
Dale Johannesende064702009-02-06 21:50:26 +00001776 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001777 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Dale Johannesende064702009-02-06 21:50:26 +00001778 Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00001779 }
1780 return Result;
1781}
1782
Dan Gohman475871a2008-07-27 21:46:04 +00001783static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001784 const ARMSubtarget *ST) {
Dan Gohman475871a2008-07-27 21:46:04 +00001785 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001786 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001787 SDValue LHS = Op.getOperand(2);
1788 SDValue RHS = Op.getOperand(3);
1789 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00001790 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001791
Owen Anderson825b72b2009-08-11 20:47:22 +00001792 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001793 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001794 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
David Goodwinf1daf7d2009-07-08 23:10:31 +00001795 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb1Only(), dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001796 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Dale Johannesende064702009-02-06 21:50:26 +00001797 Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001798 }
1799
Owen Anderson825b72b2009-08-11 20:47:22 +00001800 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Chenga8e29892007-01-19 07:51:42 +00001801 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001802 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001803
Dale Johannesende064702009-02-06 21:50:26 +00001804 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001805 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1806 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1807 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00001808 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00001809 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001810 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001811 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00001812 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00001813 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001814 }
1815 return Res;
1816}
1817
Dan Gohman475871a2008-07-27 21:46:04 +00001818SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) {
1819 SDValue Chain = Op.getOperand(0);
1820 SDValue Table = Op.getOperand(1);
1821 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001822 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001823
Owen Andersone50ed302009-08-10 22:56:29 +00001824 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00001825 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
1826 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00001827 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00001828 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00001829 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00001830 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
1831 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00001832 if (Subtarget->isThumb2()) {
1833 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
1834 // which does another jump to the destination. This also makes it easier
1835 // to translate it to TBB / TBH later.
1836 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00001837 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00001838 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001839 }
Evan Cheng66ac5312009-07-25 00:33:29 +00001840 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001841 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr, NULL, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00001842 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001843 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00001844 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001845 } else {
1846 Addr = DAG.getLoad(PTy, dl, Chain, Addr, NULL, 0);
1847 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00001848 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001849 }
Evan Chenga8e29892007-01-19 07:51:42 +00001850}
1851
Dan Gohman475871a2008-07-27 21:46:04 +00001852static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Dale Johannesende064702009-02-06 21:50:26 +00001853 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001854 unsigned Opc =
1855 Op.getOpcode() == ISD::FP_TO_SINT ? ARMISD::FTOSI : ARMISD::FTOUI;
Owen Anderson825b72b2009-08-11 20:47:22 +00001856 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
1857 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001858}
1859
Dan Gohman475871a2008-07-27 21:46:04 +00001860static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001861 EVT VT = Op.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001862 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001863 unsigned Opc =
1864 Op.getOpcode() == ISD::SINT_TO_FP ? ARMISD::SITOF : ARMISD::UITOF;
1865
Owen Anderson825b72b2009-08-11 20:47:22 +00001866 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
Dale Johannesende064702009-02-06 21:50:26 +00001867 return DAG.getNode(Opc, dl, VT, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001868}
1869
Dan Gohman475871a2008-07-27 21:46:04 +00001870static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00001871 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00001872 SDValue Tmp0 = Op.getOperand(0);
1873 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00001874 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001875 EVT VT = Op.getValueType();
1876 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001877 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
1878 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001879 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
1880 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001881 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001882}
1883
Jim Grosbach0e0da732009-05-12 23:59:14 +00001884SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
1885 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1886 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00001887 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001888 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
1889 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00001890 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00001891 ? ARM::R7 : ARM::R11;
1892 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
1893 while (Depth--)
1894 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
1895 return FrameAddr;
1896}
1897
Dan Gohman475871a2008-07-27 21:46:04 +00001898SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00001899ARMTargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman475871a2008-07-27 21:46:04 +00001900 SDValue Chain,
1901 SDValue Dst, SDValue Src,
1902 SDValue Size, unsigned Align,
Dan Gohman707e0182008-04-12 04:36:06 +00001903 bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00001904 const Value *DstSV, uint64_t DstSVOff,
1905 const Value *SrcSV, uint64_t SrcSVOff){
Evan Cheng4102eb52007-10-22 22:11:27 +00001906 // Do repeated 4-byte loads and stores. To be improved.
Dan Gohman707e0182008-04-12 04:36:06 +00001907 // This requires 4-byte alignment.
1908 if ((Align & 3) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00001909 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001910 // This requires the copy size to be a constant, preferrably
1911 // within a subtarget-specific limit.
1912 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
1913 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00001914 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001915 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001916 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00001917 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001918
1919 unsigned BytesLeft = SizeVal & 3;
1920 unsigned NumMemOps = SizeVal >> 2;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001921 unsigned EmittedNumMemOps = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001922 EVT VT = MVT::i32;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001923 unsigned VTSize = 4;
Evan Cheng4102eb52007-10-22 22:11:27 +00001924 unsigned i = 0;
Evan Chenge5e7ce42007-05-18 01:19:57 +00001925 const unsigned MAX_LOADS_IN_LDM = 6;
Dan Gohman475871a2008-07-27 21:46:04 +00001926 SDValue TFOps[MAX_LOADS_IN_LDM];
1927 SDValue Loads[MAX_LOADS_IN_LDM];
Dan Gohman1f13c682008-04-28 17:15:20 +00001928 uint64_t SrcOff = 0, DstOff = 0;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001929
Evan Cheng4102eb52007-10-22 22:11:27 +00001930 // Emit up to MAX_LOADS_IN_LDM loads, then a TokenFactor barrier, then the
1931 // same number of stores. The loads and stores will get combined into
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001932 // ldm/stm later on.
Evan Cheng4102eb52007-10-22 22:11:27 +00001933 while (EmittedNumMemOps < NumMemOps) {
1934 for (i = 0;
1935 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00001936 Loads[i] = DAG.getLoad(VT, dl, Chain,
Owen Anderson825b72b2009-08-11 20:47:22 +00001937 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
1938 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001939 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00001940 TFOps[i] = Loads[i].getValue(1);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001941 SrcOff += VTSize;
1942 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001943 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001944
Evan Cheng4102eb52007-10-22 22:11:27 +00001945 for (i = 0;
1946 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00001947 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Owen Anderson825b72b2009-08-11 20:47:22 +00001948 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
1949 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001950 DstSV, DstSVOff + DstOff);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001951 DstOff += VTSize;
1952 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001953 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00001954
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001955 EmittedNumMemOps += i;
1956 }
1957
Bob Wilson2dc4f542009-03-20 22:42:55 +00001958 if (BytesLeft == 0)
Evan Cheng4102eb52007-10-22 22:11:27 +00001959 return Chain;
1960
1961 // Issue loads / stores for the trailing (1 - 3) bytes.
1962 unsigned BytesLeftSave = BytesLeft;
1963 i = 0;
1964 while (BytesLeft) {
1965 if (BytesLeft >= 2) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001966 VT = MVT::i16;
Evan Cheng4102eb52007-10-22 22:11:27 +00001967 VTSize = 2;
1968 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001969 VT = MVT::i8;
Evan Cheng4102eb52007-10-22 22:11:27 +00001970 VTSize = 1;
1971 }
1972
Dale Johannesen0f502f62009-02-03 22:26:09 +00001973 Loads[i] = DAG.getLoad(VT, dl, Chain,
Owen Anderson825b72b2009-08-11 20:47:22 +00001974 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
1975 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001976 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00001977 TFOps[i] = Loads[i].getValue(1);
1978 ++i;
1979 SrcOff += VTSize;
1980 BytesLeft -= VTSize;
1981 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001982 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00001983
1984 i = 0;
1985 BytesLeft = BytesLeftSave;
1986 while (BytesLeft) {
1987 if (BytesLeft >= 2) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001988 VT = MVT::i16;
Evan Cheng4102eb52007-10-22 22:11:27 +00001989 VTSize = 2;
1990 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001991 VT = MVT::i8;
Evan Cheng4102eb52007-10-22 22:11:27 +00001992 VTSize = 1;
1993 }
1994
Dale Johannesen0f502f62009-02-03 22:26:09 +00001995 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Owen Anderson825b72b2009-08-11 20:47:22 +00001996 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
1997 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001998 DstSV, DstSVOff + DstOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00001999 ++i;
2000 DstOff += VTSize;
2001 BytesLeft -= VTSize;
2002 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002003 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002004}
2005
Duncan Sands1607f052008-12-01 11:39:25 +00002006static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00002007 SDValue Op = N->getOperand(0);
Dale Johannesende064702009-02-06 21:50:26 +00002008 DebugLoc dl = N->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00002009 if (N->getValueType(0) == MVT::f64) {
Evan Chengc7c77292008-11-04 19:57:48 +00002010 // Turn i64->f64 into FMDRR.
Owen Anderson825b72b2009-08-11 20:47:22 +00002011 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2012 DAG.getConstant(0, MVT::i32));
2013 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2014 DAG.getConstant(1, MVT::i32));
2015 return DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
Evan Chengc7c77292008-11-04 19:57:48 +00002016 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002017
Evan Chengc7c77292008-11-04 19:57:48 +00002018 // Turn f64->i64 into FMRRD.
Bob Wilson2dc4f542009-03-20 22:42:55 +00002019 SDValue Cvt = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00002020 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002021
Chris Lattner27a6c732007-11-24 07:07:01 +00002022 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002023 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
Chris Lattner27a6c732007-11-24 07:07:01 +00002024}
2025
Bob Wilson5bafff32009-06-22 23:27:02 +00002026/// getZeroVector - Returns a vector of specified type with all zero elements.
2027///
Owen Andersone50ed302009-08-10 22:56:29 +00002028static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002029 assert(VT.isVector() && "Expected a vector type");
2030
2031 // Zero vectors are used to represent vector negation and in those cases
2032 // will be implemented with the NEON VNEG instruction. However, VNEG does
2033 // not support i64 elements, so sometimes the zero vectors will need to be
2034 // explicitly constructed. For those cases, and potentially other uses in
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002035 // the future, always build zero vectors as <16 x i8> or <8 x i8> bitcasted
Bob Wilson5bafff32009-06-22 23:27:02 +00002036 // to their dest type. This ensures they get CSE'd.
2037 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002038 SDValue Cst = DAG.getTargetConstant(0, MVT::i8);
2039 SmallVector<SDValue, 8> Ops;
2040 MVT TVT;
2041
2042 if (VT.getSizeInBits() == 64) {
2043 Ops.assign(8, Cst); TVT = MVT::v8i8;
2044 } else {
2045 Ops.assign(16, Cst); TVT = MVT::v16i8;
2046 }
2047 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002048
2049 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2050}
2051
2052/// getOnesVector - Returns a vector of specified type with all bits set.
2053///
Owen Andersone50ed302009-08-10 22:56:29 +00002054static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002055 assert(VT.isVector() && "Expected a vector type");
2056
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002057 // Always build ones vectors as <16 x i32> or <8 x i32> bitcasted to their
2058 // dest type. This ensures they get CSE'd.
Bob Wilson5bafff32009-06-22 23:27:02 +00002059 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002060 SDValue Cst = DAG.getTargetConstant(0xFF, MVT::i8);
2061 SmallVector<SDValue, 8> Ops;
2062 MVT TVT;
2063
2064 if (VT.getSizeInBits() == 64) {
2065 Ops.assign(8, Cst); TVT = MVT::v8i8;
2066 } else {
2067 Ops.assign(16, Cst); TVT = MVT::v16i8;
2068 }
2069 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002070
2071 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2072}
2073
2074static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2075 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00002076 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002077 DebugLoc dl = N->getDebugLoc();
2078
2079 // Lower vector shifts on NEON to use VSHL.
2080 if (VT.isVector()) {
2081 assert(ST->hasNEON() && "unexpected vector shift");
2082
2083 // Left shifts translate directly to the vshiftu intrinsic.
2084 if (N->getOpcode() == ISD::SHL)
2085 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002086 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002087 N->getOperand(0), N->getOperand(1));
2088
2089 assert((N->getOpcode() == ISD::SRA ||
2090 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2091
2092 // NEON uses the same intrinsics for both left and right shifts. For
2093 // right shifts, the shift amounts are negative, so negate the vector of
2094 // shift amounts.
Owen Andersone50ed302009-08-10 22:56:29 +00002095 EVT ShiftVT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002096 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2097 getZeroVector(ShiftVT, DAG, dl),
2098 N->getOperand(1));
2099 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2100 Intrinsic::arm_neon_vshifts :
2101 Intrinsic::arm_neon_vshiftu);
2102 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002103 DAG.getConstant(vshiftInt, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002104 N->getOperand(0), NegatedCount);
2105 }
2106
Eli Friedmance392eb2009-08-22 03:13:10 +00002107 // We can get here for a node like i32 = ISD::SHL i32, i64
2108 if (VT != MVT::i64)
2109 return SDValue();
2110
2111 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00002112 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00002113
Chris Lattner27a6c732007-11-24 07:07:01 +00002114 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2115 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002116 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00002117 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002118
Chris Lattner27a6c732007-11-24 07:07:01 +00002119 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00002120 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002121
Chris Lattner27a6c732007-11-24 07:07:01 +00002122 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00002123 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2124 DAG.getConstant(0, MVT::i32));
2125 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2126 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002127
Chris Lattner27a6c732007-11-24 07:07:01 +00002128 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2129 // captures the result into a carry flag.
2130 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Owen Anderson825b72b2009-08-11 20:47:22 +00002131 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002132
Chris Lattner27a6c732007-11-24 07:07:01 +00002133 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00002134 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002135
Chris Lattner27a6c732007-11-24 07:07:01 +00002136 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002137 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00002138}
2139
Bob Wilson5bafff32009-06-22 23:27:02 +00002140static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2141 SDValue TmpOp0, TmpOp1;
2142 bool Invert = false;
2143 bool Swap = false;
2144 unsigned Opc = 0;
2145
2146 SDValue Op0 = Op.getOperand(0);
2147 SDValue Op1 = Op.getOperand(1);
2148 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00002149 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002150 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2151 DebugLoc dl = Op.getDebugLoc();
2152
2153 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2154 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002155 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002156 case ISD::SETUNE:
2157 case ISD::SETNE: Invert = true; // Fallthrough
2158 case ISD::SETOEQ:
2159 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2160 case ISD::SETOLT:
2161 case ISD::SETLT: Swap = true; // Fallthrough
2162 case ISD::SETOGT:
2163 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2164 case ISD::SETOLE:
2165 case ISD::SETLE: Swap = true; // Fallthrough
2166 case ISD::SETOGE:
2167 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2168 case ISD::SETUGE: Swap = true; // Fallthrough
2169 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2170 case ISD::SETUGT: Swap = true; // Fallthrough
2171 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2172 case ISD::SETUEQ: Invert = true; // Fallthrough
2173 case ISD::SETONE:
2174 // Expand this to (OLT | OGT).
2175 TmpOp0 = Op0;
2176 TmpOp1 = Op1;
2177 Opc = ISD::OR;
2178 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2179 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2180 break;
2181 case ISD::SETUO: Invert = true; // Fallthrough
2182 case ISD::SETO:
2183 // Expand this to (OLT | OGE).
2184 TmpOp0 = Op0;
2185 TmpOp1 = Op1;
2186 Opc = ISD::OR;
2187 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2188 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2189 break;
2190 }
2191 } else {
2192 // Integer comparisons.
2193 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002194 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002195 case ISD::SETNE: Invert = true;
2196 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2197 case ISD::SETLT: Swap = true;
2198 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2199 case ISD::SETLE: Swap = true;
2200 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2201 case ISD::SETULT: Swap = true;
2202 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2203 case ISD::SETULE: Swap = true;
2204 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2205 }
2206
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002207 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00002208 if (Opc == ARMISD::VCEQ) {
2209
2210 SDValue AndOp;
2211 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2212 AndOp = Op0;
2213 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2214 AndOp = Op1;
2215
2216 // Ignore bitconvert.
2217 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2218 AndOp = AndOp.getOperand(0);
2219
2220 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2221 Opc = ARMISD::VTST;
2222 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2223 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2224 Invert = !Invert;
2225 }
2226 }
2227 }
2228
2229 if (Swap)
2230 std::swap(Op0, Op1);
2231
2232 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
2233
2234 if (Invert)
2235 Result = DAG.getNOT(dl, Result, VT);
2236
2237 return Result;
2238}
2239
2240/// isVMOVSplat - Check if the specified splat value corresponds to an immediate
2241/// VMOV instruction, and if so, return the constant being splatted.
2242static SDValue isVMOVSplat(uint64_t SplatBits, uint64_t SplatUndef,
2243 unsigned SplatBitSize, SelectionDAG &DAG) {
2244 switch (SplatBitSize) {
2245 case 8:
2246 // Any 1-byte value is OK.
2247 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Owen Anderson825b72b2009-08-11 20:47:22 +00002248 return DAG.getTargetConstant(SplatBits, MVT::i8);
Bob Wilson5bafff32009-06-22 23:27:02 +00002249
2250 case 16:
2251 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
2252 if ((SplatBits & ~0xff) == 0 ||
2253 (SplatBits & ~0xff00) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002254 return DAG.getTargetConstant(SplatBits, MVT::i16);
Bob Wilson5bafff32009-06-22 23:27:02 +00002255 break;
2256
2257 case 32:
2258 // NEON's 32-bit VMOV supports splat values where:
2259 // * only one byte is nonzero, or
2260 // * the least significant byte is 0xff and the second byte is nonzero, or
2261 // * the least significant 2 bytes are 0xff and the third is nonzero.
2262 if ((SplatBits & ~0xff) == 0 ||
2263 (SplatBits & ~0xff00) == 0 ||
2264 (SplatBits & ~0xff0000) == 0 ||
2265 (SplatBits & ~0xff000000) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002266 return DAG.getTargetConstant(SplatBits, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002267
2268 if ((SplatBits & ~0xffff) == 0 &&
2269 ((SplatBits | SplatUndef) & 0xff) == 0xff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002270 return DAG.getTargetConstant(SplatBits | 0xff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002271
2272 if ((SplatBits & ~0xffffff) == 0 &&
2273 ((SplatBits | SplatUndef) & 0xffff) == 0xffff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002274 return DAG.getTargetConstant(SplatBits | 0xffff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002275
2276 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
2277 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
2278 // VMOV.I32. A (very) minor optimization would be to replicate the value
2279 // and fall through here to test for a valid 64-bit splat. But, then the
2280 // caller would also need to check and handle the change in size.
2281 break;
2282
2283 case 64: {
2284 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
2285 uint64_t BitMask = 0xff;
2286 uint64_t Val = 0;
2287 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
2288 if (((SplatBits | SplatUndef) & BitMask) == BitMask)
2289 Val |= BitMask;
2290 else if ((SplatBits & BitMask) != 0)
2291 return SDValue();
2292 BitMask <<= 8;
2293 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002294 return DAG.getTargetConstant(Val, MVT::i64);
Bob Wilson5bafff32009-06-22 23:27:02 +00002295 }
2296
2297 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002298 llvm_unreachable("unexpected size for isVMOVSplat");
Bob Wilson5bafff32009-06-22 23:27:02 +00002299 break;
2300 }
2301
2302 return SDValue();
2303}
2304
2305/// getVMOVImm - If this is a build_vector of constants which can be
2306/// formed by using a VMOV instruction of the specified element size,
2307/// return the constant being splatted. The ByteSize field indicates the
2308/// number of bytes of each element [1248].
2309SDValue ARM::getVMOVImm(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
2310 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
2311 APInt SplatBits, SplatUndef;
2312 unsigned SplatBitSize;
2313 bool HasAnyUndefs;
2314 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
2315 HasAnyUndefs, ByteSize * 8))
2316 return SDValue();
2317
2318 if (SplatBitSize > ByteSize * 8)
2319 return SDValue();
2320
2321 return isVMOVSplat(SplatBits.getZExtValue(), SplatUndef.getZExtValue(),
2322 SplatBitSize, DAG);
2323}
2324
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002325static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
2326 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002327 unsigned NumElts = VT.getVectorNumElements();
2328 ReverseVEXT = false;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002329 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002330
2331 // If this is a VEXT shuffle, the immediate value is the index of the first
2332 // element. The other shuffle indices must be the successive elements after
2333 // the first one.
2334 unsigned ExpectedElt = Imm;
2335 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002336 // Increment the expected index. If it wraps around, it may still be
2337 // a VEXT but the source vectors must be swapped.
2338 ExpectedElt += 1;
2339 if (ExpectedElt == NumElts * 2) {
2340 ExpectedElt = 0;
2341 ReverseVEXT = true;
2342 }
2343
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002344 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002345 return false;
2346 }
2347
2348 // Adjust the index value if the source operands will be swapped.
2349 if (ReverseVEXT)
2350 Imm -= NumElts;
2351
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002352 return true;
2353}
2354
Bob Wilson8bb9e482009-07-26 00:39:34 +00002355/// isVREVMask - Check if a vector shuffle corresponds to a VREV
2356/// instruction with the specified blocksize. (The order of the elements
2357/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002358static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
2359 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00002360 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
2361 "Only possible block sizes for VREV are: 16, 32, 64");
2362
Bob Wilson8bb9e482009-07-26 00:39:34 +00002363 unsigned NumElts = VT.getVectorNumElements();
2364 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002365 unsigned BlockElts = M[0] + 1;
Bob Wilson8bb9e482009-07-26 00:39:34 +00002366
2367 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
2368 return false;
2369
2370 for (unsigned i = 0; i < NumElts; ++i) {
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002371 if ((unsigned) M[i] !=
Bob Wilson8bb9e482009-07-26 00:39:34 +00002372 (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
2373 return false;
2374 }
2375
2376 return true;
2377}
2378
Bob Wilsonc692cb72009-08-21 20:54:19 +00002379static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
2380 unsigned &WhichResult) {
2381 unsigned NumElts = VT.getVectorNumElements();
2382 WhichResult = (M[0] == 0 ? 0 : 1);
2383 for (unsigned i = 0; i < NumElts; i += 2) {
2384 if ((unsigned) M[i] != i + WhichResult ||
2385 (unsigned) M[i+1] != i + NumElts + WhichResult)
2386 return false;
2387 }
2388 return true;
2389}
2390
2391static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
2392 unsigned &WhichResult) {
2393 unsigned NumElts = VT.getVectorNumElements();
2394 WhichResult = (M[0] == 0 ? 0 : 1);
2395 for (unsigned i = 0; i != NumElts; ++i) {
2396 if ((unsigned) M[i] != 2 * i + WhichResult)
2397 return false;
2398 }
2399
2400 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2401 if (VT.is64BitVector() && VT.getVectorElementType().getSizeInBits() == 32)
2402 return false;
2403
2404 return true;
2405}
2406
2407static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
2408 unsigned &WhichResult) {
2409 unsigned NumElts = VT.getVectorNumElements();
2410 WhichResult = (M[0] == 0 ? 0 : 1);
2411 unsigned Idx = WhichResult * NumElts / 2;
2412 for (unsigned i = 0; i != NumElts; i += 2) {
2413 if ((unsigned) M[i] != Idx ||
2414 (unsigned) M[i+1] != Idx + NumElts)
2415 return false;
2416 Idx += 1;
2417 }
2418
2419 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2420 if (VT.is64BitVector() && VT.getVectorElementType().getSizeInBits() == 32)
2421 return false;
2422
2423 return true;
2424}
2425
Owen Andersone50ed302009-08-10 22:56:29 +00002426static SDValue BuildSplat(SDValue Val, EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002427 // Canonicalize all-zeros and all-ones vectors.
Bob Wilsond06791f2009-08-13 01:57:47 +00002428 ConstantSDNode *ConstVal = cast<ConstantSDNode>(Val.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002429 if (ConstVal->isNullValue())
2430 return getZeroVector(VT, DAG, dl);
2431 if (ConstVal->isAllOnesValue())
2432 return getOnesVector(VT, DAG, dl);
2433
Owen Andersone50ed302009-08-10 22:56:29 +00002434 EVT CanonicalVT;
Bob Wilson5bafff32009-06-22 23:27:02 +00002435 if (VT.is64BitVector()) {
2436 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002437 case 8: CanonicalVT = MVT::v8i8; break;
2438 case 16: CanonicalVT = MVT::v4i16; break;
2439 case 32: CanonicalVT = MVT::v2i32; break;
2440 case 64: CanonicalVT = MVT::v1i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002441 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002442 }
2443 } else {
2444 assert(VT.is128BitVector() && "unknown splat vector size");
2445 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002446 case 8: CanonicalVT = MVT::v16i8; break;
2447 case 16: CanonicalVT = MVT::v8i16; break;
2448 case 32: CanonicalVT = MVT::v4i32; break;
2449 case 64: CanonicalVT = MVT::v2i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002450 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002451 }
2452 }
2453
2454 // Build a canonical splat for this value.
2455 SmallVector<SDValue, 8> Ops;
2456 Ops.assign(CanonicalVT.getVectorNumElements(), Val);
2457 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, &Ops[0],
2458 Ops.size());
2459 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Res);
2460}
2461
2462// If this is a case we can't handle, return null and let the default
2463// expansion code take care of it.
2464static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bob Wilsond06791f2009-08-13 01:57:47 +00002465 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002466 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002467 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002468
2469 APInt SplatBits, SplatUndef;
2470 unsigned SplatBitSize;
2471 bool HasAnyUndefs;
2472 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00002473 if (SplatBitSize <= 64) {
2474 SDValue Val = isVMOVSplat(SplatBits.getZExtValue(),
2475 SplatUndef.getZExtValue(), SplatBitSize, DAG);
2476 if (Val.getNode())
2477 return BuildSplat(Val, VT, DAG, dl);
2478 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00002479 }
2480
2481 // If there are only 2 elements in a 128-bit vector, insert them into an
2482 // undef vector. This handles the common case for 128-bit vector argument
2483 // passing, where the insertions should be translated to subreg accesses
2484 // with no real instructions.
2485 if (VT.is128BitVector() && Op.getNumOperands() == 2) {
2486 SDValue Val = DAG.getUNDEF(VT);
2487 SDValue Op0 = Op.getOperand(0);
2488 SDValue Op1 = Op.getOperand(1);
2489 if (Op0.getOpcode() != ISD::UNDEF)
2490 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op0,
2491 DAG.getIntPtrConstant(0));
2492 if (Op1.getOpcode() != ISD::UNDEF)
2493 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op1,
2494 DAG.getIntPtrConstant(1));
2495 return Val;
Bob Wilson5bafff32009-06-22 23:27:02 +00002496 }
2497
2498 return SDValue();
2499}
2500
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002501/// isShuffleMaskLegal - Targets can use this to indicate that they only
2502/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
2503/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
2504/// are assumed to be legal.
2505bool
2506ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
2507 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002508 if (VT.getVectorNumElements() == 4 &&
2509 (VT.is128BitVector() || VT.is64BitVector())) {
2510 unsigned PFIndexes[4];
2511 for (unsigned i = 0; i != 4; ++i) {
2512 if (M[i] < 0)
2513 PFIndexes[i] = 8;
2514 else
2515 PFIndexes[i] = M[i];
2516 }
2517
2518 // Compute the index in the perfect shuffle table.
2519 unsigned PFTableIndex =
2520 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2521 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2522 unsigned Cost = (PFEntry >> 30);
2523
2524 if (Cost <= 4)
2525 return true;
2526 }
2527
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002528 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00002529 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002530
2531 return (ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
2532 isVREVMask(M, VT, 64) ||
2533 isVREVMask(M, VT, 32) ||
2534 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00002535 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
2536 isVTRNMask(M, VT, WhichResult) ||
2537 isVUZPMask(M, VT, WhichResult) ||
2538 isVZIPMask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002539}
2540
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002541/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
2542/// the specified operations to build the shuffle.
2543static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
2544 SDValue RHS, SelectionDAG &DAG,
2545 DebugLoc dl) {
2546 unsigned OpNum = (PFEntry >> 26) & 0x0F;
2547 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
2548 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
2549
2550 enum {
2551 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
2552 OP_VREV,
2553 OP_VDUP0,
2554 OP_VDUP1,
2555 OP_VDUP2,
2556 OP_VDUP3,
2557 OP_VEXT1,
2558 OP_VEXT2,
2559 OP_VEXT3,
2560 OP_VUZPL, // VUZP, left result
2561 OP_VUZPR, // VUZP, right result
2562 OP_VZIPL, // VZIP, left result
2563 OP_VZIPR, // VZIP, right result
2564 OP_VTRNL, // VTRN, left result
2565 OP_VTRNR // VTRN, right result
2566 };
2567
2568 if (OpNum == OP_COPY) {
2569 if (LHSID == (1*9+2)*9+3) return LHS;
2570 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
2571 return RHS;
2572 }
2573
2574 SDValue OpLHS, OpRHS;
2575 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
2576 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
2577 EVT VT = OpLHS.getValueType();
2578
2579 switch (OpNum) {
2580 default: llvm_unreachable("Unknown shuffle opcode!");
2581 case OP_VREV:
2582 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
2583 case OP_VDUP0:
2584 case OP_VDUP1:
2585 case OP_VDUP2:
2586 case OP_VDUP3:
2587 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002588 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002589 case OP_VEXT1:
2590 case OP_VEXT2:
2591 case OP_VEXT3:
2592 return DAG.getNode(ARMISD::VEXT, dl, VT,
2593 OpLHS, OpRHS,
2594 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
2595 case OP_VUZPL:
2596 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002597 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002598 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
2599 case OP_VZIPL:
2600 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002601 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002602 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
2603 case OP_VTRNL:
2604 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002605 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2606 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002607 }
2608}
2609
Bob Wilson5bafff32009-06-22 23:27:02 +00002610static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002611 SDValue V1 = Op.getOperand(0);
2612 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00002613 DebugLoc dl = Op.getDebugLoc();
2614 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002615 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002616 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00002617
Bob Wilson28865062009-08-13 02:13:04 +00002618 // Convert shuffles that are directly supported on NEON to target-specific
2619 // DAG nodes, instead of keeping them as shuffles and matching them again
2620 // during code selection. This is more efficient and avoids the possibility
2621 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00002622 // FIXME: floating-point vectors should be canonicalized to integer vectors
2623 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002624 SVN->getMask(ShuffleMask);
2625
2626 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
Bob Wilson0ce37102009-08-14 05:08:32 +00002627 int Lane = SVN->getSplatIndex();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002628 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
2629 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00002630 }
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002631 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002632 DAG.getConstant(Lane, MVT::i32));
Bob Wilson0ce37102009-08-14 05:08:32 +00002633 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002634
2635 bool ReverseVEXT;
2636 unsigned Imm;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002637 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002638 if (ReverseVEXT)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002639 std::swap(V1, V2);
2640 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002641 DAG.getConstant(Imm, MVT::i32));
2642 }
2643
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002644 if (isVREVMask(ShuffleMask, VT, 64))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002645 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002646 if (isVREVMask(ShuffleMask, VT, 32))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002647 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002648 if (isVREVMask(ShuffleMask, VT, 16))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002649 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
2650
Bob Wilsonc692cb72009-08-21 20:54:19 +00002651 // Check for Neon shuffles that modify both input vectors in place.
2652 // If both results are used, i.e., if there are two shuffles with the same
2653 // source operands and with masks corresponding to both results of one of
2654 // these operations, DAG memoization will ensure that a single node is
2655 // used for both shuffles.
2656 unsigned WhichResult;
2657 if (isVTRNMask(ShuffleMask, VT, WhichResult))
2658 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2659 V1, V2).getValue(WhichResult);
2660 if (isVUZPMask(ShuffleMask, VT, WhichResult))
2661 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
2662 V1, V2).getValue(WhichResult);
2663 if (isVZIPMask(ShuffleMask, VT, WhichResult))
2664 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
2665 V1, V2).getValue(WhichResult);
2666
2667 // If the shuffle is not directly supported and it has 4 elements, use
2668 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002669 if (VT.getVectorNumElements() == 4 &&
2670 (VT.is128BitVector() || VT.is64BitVector())) {
2671 unsigned PFIndexes[4];
2672 for (unsigned i = 0; i != 4; ++i) {
2673 if (ShuffleMask[i] < 0)
2674 PFIndexes[i] = 8;
2675 else
2676 PFIndexes[i] = ShuffleMask[i];
2677 }
2678
2679 // Compute the index in the perfect shuffle table.
2680 unsigned PFTableIndex =
2681 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2682
2683 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2684 unsigned Cost = (PFEntry >> 30);
2685
2686 if (Cost <= 4)
2687 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
2688 }
Bob Wilsond8e17572009-08-12 22:31:50 +00002689
Bob Wilson22cac0d2009-08-14 05:16:33 +00002690 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00002691}
2692
Bob Wilson5bafff32009-06-22 23:27:02 +00002693static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002694 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002695 DebugLoc dl = Op.getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00002696 SDValue Vec = Op.getOperand(0);
2697 SDValue Lane = Op.getOperand(1);
Bob Wilson934f98b2009-10-15 23:12:05 +00002698 assert(VT == MVT::i32 &&
2699 Vec.getValueType().getVectorElementType().getSizeInBits() < 32 &&
2700 "unexpected type for custom-lowering vector extract");
2701 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
Bob Wilson5bafff32009-06-22 23:27:02 +00002702}
2703
Bob Wilsona6d65862009-08-03 20:36:38 +00002704static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
2705 // The only time a CONCAT_VECTORS operation can have legal types is when
2706 // two 64-bit vectors are concatenated to a 128-bit vector.
2707 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
2708 "unexpected CONCAT_VECTORS");
2709 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00002710 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00002711 SDValue Op0 = Op.getOperand(0);
2712 SDValue Op1 = Op.getOperand(1);
2713 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00002714 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
2715 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00002716 DAG.getIntPtrConstant(0));
2717 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00002718 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
2719 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00002720 DAG.getIntPtrConstant(1));
2721 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00002722}
2723
Dan Gohman475871a2008-07-27 21:46:04 +00002724SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00002725 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002726 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00002727 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002728 case ISD::GlobalAddress:
2729 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
2730 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002731 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002732 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, Subtarget);
2733 case ISD::BR_CC: return LowerBR_CC(Op, DAG, Subtarget);
2734 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Evan Cheng86198642009-08-07 00:34:42 +00002735 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002736 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
2737 case ISD::SINT_TO_FP:
2738 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
2739 case ISD::FP_TO_SINT:
2740 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
2741 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00002742 case ISD::RETURNADDR: break;
Jim Grosbach0e0da732009-05-12 23:59:14 +00002743 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002744 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002745 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00002746 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00002747 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00002748 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00002749 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
2750 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
2751 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2752 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00002753 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00002754 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002755 }
Dan Gohman475871a2008-07-27 21:46:04 +00002756 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00002757}
2758
Duncan Sands1607f052008-12-01 11:39:25 +00002759/// ReplaceNodeResults - Replace the results of node with an illegal result
2760/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00002761void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
2762 SmallVectorImpl<SDValue>&Results,
2763 SelectionDAG &DAG) {
Chris Lattner27a6c732007-11-24 07:07:01 +00002764 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00002765 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002766 llvm_unreachable("Don't know how to custom expand this!");
Duncan Sands1607f052008-12-01 11:39:25 +00002767 return;
2768 case ISD::BIT_CONVERT:
2769 Results.push_back(ExpandBIT_CONVERT(N, DAG));
2770 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00002771 case ISD::SRL:
Duncan Sands1607f052008-12-01 11:39:25 +00002772 case ISD::SRA: {
Bob Wilson5bafff32009-06-22 23:27:02 +00002773 SDValue Res = LowerShift(N, DAG, Subtarget);
Duncan Sands1607f052008-12-01 11:39:25 +00002774 if (Res.getNode())
2775 Results.push_back(Res);
2776 return;
2777 }
Chris Lattner27a6c732007-11-24 07:07:01 +00002778 }
2779}
Chris Lattner27a6c732007-11-24 07:07:01 +00002780
Evan Chenga8e29892007-01-19 07:51:42 +00002781//===----------------------------------------------------------------------===//
2782// ARM Scheduler Hooks
2783//===----------------------------------------------------------------------===//
2784
2785MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00002786ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +00002787 MachineBasicBlock *BB,
2788 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002789 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00002790 DebugLoc dl = MI->getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002791 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00002792 default:
2793 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng007ea272009-08-12 05:17:19 +00002794 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00002795 // To "insert" a SELECT_CC instruction, we actually have to insert the
2796 // diamond control-flow pattern. The incoming instruction knows the
2797 // destination vreg to set, the condition code register to branch on, the
2798 // true/false values to select between, and a branch opcode to use.
2799 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002800 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00002801 ++It;
2802
2803 // thisMBB:
2804 // ...
2805 // TrueVal = ...
2806 // cmpTY ccX, r1, r2
2807 // bCC copy1MBB
2808 // fallthrough --> copy0MBB
2809 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002810 MachineFunction *F = BB->getParent();
2811 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
2812 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenb6728402009-02-13 02:25:56 +00002813 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00002814 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002815 F->insert(It, copy0MBB);
2816 F->insert(It, sinkMBB);
Evan Chenga8e29892007-01-19 07:51:42 +00002817 // Update machine-CFG edges by first adding all successors of the current
2818 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00002819 // Also inform sdisel of the edge changes.
2820 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
2821 E = BB->succ_end(); I != E; ++I) {
2822 EM->insert(std::make_pair(*I, sinkMBB));
2823 sinkMBB->addSuccessor(*I);
2824 }
Evan Chenga8e29892007-01-19 07:51:42 +00002825 // Next, remove all successors of the current block, and add the true
2826 // and fallthrough blocks as its successors.
Evan Chengce319102009-09-19 09:51:03 +00002827 while (!BB->succ_empty())
Evan Chenga8e29892007-01-19 07:51:42 +00002828 BB->removeSuccessor(BB->succ_begin());
2829 BB->addSuccessor(copy0MBB);
2830 BB->addSuccessor(sinkMBB);
2831
2832 // copy0MBB:
2833 // %FalseValue = ...
2834 // # fallthrough to sinkMBB
2835 BB = copy0MBB;
2836
2837 // Update machine-CFG edges
2838 BB->addSuccessor(sinkMBB);
2839
2840 // sinkMBB:
2841 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2842 // ...
2843 BB = sinkMBB;
Dale Johannesenb6728402009-02-13 02:25:56 +00002844 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00002845 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
2846 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
2847
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002848 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00002849 return BB;
2850 }
Evan Cheng86198642009-08-07 00:34:42 +00002851
2852 case ARM::tANDsp:
2853 case ARM::tADDspr_:
2854 case ARM::tSUBspi_:
2855 case ARM::t2SUBrSPi_:
2856 case ARM::t2SUBrSPi12_:
2857 case ARM::t2SUBrSPs_: {
2858 MachineFunction *MF = BB->getParent();
2859 unsigned DstReg = MI->getOperand(0).getReg();
2860 unsigned SrcReg = MI->getOperand(1).getReg();
2861 bool DstIsDead = MI->getOperand(0).isDead();
2862 bool SrcIsKill = MI->getOperand(1).isKill();
2863
2864 if (SrcReg != ARM::SP) {
2865 // Copy the source to SP from virtual register.
2866 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(SrcReg);
2867 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
2868 ? ARM::tMOVtgpr2gpr : ARM::tMOVgpr2gpr;
2869 BuildMI(BB, dl, TII->get(CopyOpc), ARM::SP)
2870 .addReg(SrcReg, getKillRegState(SrcIsKill));
2871 }
2872
2873 unsigned OpOpc = 0;
2874 bool NeedPred = false, NeedCC = false, NeedOp3 = false;
2875 switch (MI->getOpcode()) {
2876 default:
2877 llvm_unreachable("Unexpected pseudo instruction!");
2878 case ARM::tANDsp:
2879 OpOpc = ARM::tAND;
2880 NeedPred = true;
2881 break;
2882 case ARM::tADDspr_:
2883 OpOpc = ARM::tADDspr;
2884 break;
2885 case ARM::tSUBspi_:
2886 OpOpc = ARM::tSUBspi;
2887 break;
2888 case ARM::t2SUBrSPi_:
2889 OpOpc = ARM::t2SUBrSPi;
2890 NeedPred = true; NeedCC = true;
2891 break;
2892 case ARM::t2SUBrSPi12_:
2893 OpOpc = ARM::t2SUBrSPi12;
2894 NeedPred = true;
2895 break;
2896 case ARM::t2SUBrSPs_:
2897 OpOpc = ARM::t2SUBrSPs;
2898 NeedPred = true; NeedCC = true; NeedOp3 = true;
2899 break;
2900 }
2901 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(OpOpc), ARM::SP);
2902 if (OpOpc == ARM::tAND)
2903 AddDefaultT1CC(MIB);
2904 MIB.addReg(ARM::SP);
2905 MIB.addOperand(MI->getOperand(2));
2906 if (NeedOp3)
2907 MIB.addOperand(MI->getOperand(3));
2908 if (NeedPred)
2909 AddDefaultPred(MIB);
2910 if (NeedCC)
2911 AddDefaultCC(MIB);
2912
2913 // Copy the result from SP to virtual register.
2914 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(DstReg);
2915 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
2916 ? ARM::tMOVgpr2tgpr : ARM::tMOVgpr2gpr;
2917 BuildMI(BB, dl, TII->get(CopyOpc))
2918 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
2919 .addReg(ARM::SP);
2920 MF->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
2921 return BB;
2922 }
Evan Chenga8e29892007-01-19 07:51:42 +00002923 }
2924}
2925
2926//===----------------------------------------------------------------------===//
2927// ARM Optimization Hooks
2928//===----------------------------------------------------------------------===//
2929
Chris Lattnerd1980a52009-03-12 06:52:53 +00002930static
2931SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
2932 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00002933 SelectionDAG &DAG = DCI.DAG;
2934 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00002935 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00002936 unsigned Opc = N->getOpcode();
2937 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
2938 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
2939 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
2940 ISD::CondCode CC = ISD::SETCC_INVALID;
2941
2942 if (isSlctCC) {
2943 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
2944 } else {
2945 SDValue CCOp = Slct.getOperand(0);
2946 if (CCOp.getOpcode() == ISD::SETCC)
2947 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
2948 }
2949
2950 bool DoXform = false;
2951 bool InvCC = false;
2952 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
2953 "Bad input!");
2954
2955 if (LHS.getOpcode() == ISD::Constant &&
2956 cast<ConstantSDNode>(LHS)->isNullValue()) {
2957 DoXform = true;
2958 } else if (CC != ISD::SETCC_INVALID &&
2959 RHS.getOpcode() == ISD::Constant &&
2960 cast<ConstantSDNode>(RHS)->isNullValue()) {
2961 std::swap(LHS, RHS);
2962 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00002963 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00002964 Op0.getOperand(0).getValueType();
2965 bool isInt = OpVT.isInteger();
2966 CC = ISD::getSetCCInverse(CC, isInt);
2967
2968 if (!TLI.isCondCodeLegal(CC, OpVT))
2969 return SDValue(); // Inverse operator isn't legal.
2970
2971 DoXform = true;
2972 InvCC = true;
2973 }
2974
2975 if (DoXform) {
2976 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
2977 if (isSlctCC)
2978 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
2979 Slct.getOperand(0), Slct.getOperand(1), CC);
2980 SDValue CCOp = Slct.getOperand(0);
2981 if (InvCC)
2982 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
2983 CCOp.getOperand(0), CCOp.getOperand(1), CC);
2984 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
2985 CCOp, OtherOp, Result);
2986 }
2987 return SDValue();
2988}
2989
2990/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
2991static SDValue PerformADDCombine(SDNode *N,
2992 TargetLowering::DAGCombinerInfo &DCI) {
2993 // added by evan in r37685 with no testcase.
2994 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002995
Chris Lattnerd1980a52009-03-12 06:52:53 +00002996 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
2997 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
2998 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
2999 if (Result.getNode()) return Result;
3000 }
3001 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3002 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3003 if (Result.getNode()) return Result;
3004 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003005
Chris Lattnerd1980a52009-03-12 06:52:53 +00003006 return SDValue();
3007}
3008
3009/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
3010static SDValue PerformSUBCombine(SDNode *N,
3011 TargetLowering::DAGCombinerInfo &DCI) {
3012 // added by evan in r37685 with no testcase.
3013 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003014
Chris Lattnerd1980a52009-03-12 06:52:53 +00003015 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
3016 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3017 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3018 if (Result.getNode()) return Result;
3019 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003020
Chris Lattnerd1980a52009-03-12 06:52:53 +00003021 return SDValue();
3022}
3023
3024
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003025/// PerformFMRRDCombine - Target-specific dag combine xforms for ARMISD::FMRRD.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003026static SDValue PerformFMRRDCombine(SDNode *N,
3027 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003028 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00003029 SDValue InDouble = N->getOperand(0);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003030 if (InDouble.getOpcode() == ARMISD::FMDRR)
3031 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003032 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003033}
3034
Bob Wilson5bafff32009-06-22 23:27:02 +00003035/// getVShiftImm - Check if this is a valid build_vector for the immediate
3036/// operand of a vector shift operation, where all the elements of the
3037/// build_vector must have the same constant integer value.
3038static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
3039 // Ignore bit_converts.
3040 while (Op.getOpcode() == ISD::BIT_CONVERT)
3041 Op = Op.getOperand(0);
3042 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
3043 APInt SplatBits, SplatUndef;
3044 unsigned SplatBitSize;
3045 bool HasAnyUndefs;
3046 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
3047 HasAnyUndefs, ElementBits) ||
3048 SplatBitSize > ElementBits)
3049 return false;
3050 Cnt = SplatBits.getSExtValue();
3051 return true;
3052}
3053
3054/// isVShiftLImm - Check if this is a valid build_vector for the immediate
3055/// operand of a vector shift left operation. That value must be in the range:
3056/// 0 <= Value < ElementBits for a left shift; or
3057/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003058static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003059 assert(VT.isVector() && "vector shift count is not a vector type");
3060 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3061 if (! getVShiftImm(Op, ElementBits, Cnt))
3062 return false;
3063 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
3064}
3065
3066/// isVShiftRImm - Check if this is a valid build_vector for the immediate
3067/// operand of a vector shift right operation. For a shift opcode, the value
3068/// is positive, but for an intrinsic the value count must be negative. The
3069/// absolute value must be in the range:
3070/// 1 <= |Value| <= ElementBits for a right shift; or
3071/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003072static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00003073 int64_t &Cnt) {
3074 assert(VT.isVector() && "vector shift count is not a vector type");
3075 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3076 if (! getVShiftImm(Op, ElementBits, Cnt))
3077 return false;
3078 if (isIntrinsic)
3079 Cnt = -Cnt;
3080 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
3081}
3082
3083/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
3084static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
3085 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
3086 switch (IntNo) {
3087 default:
3088 // Don't do anything for most intrinsics.
3089 break;
3090
3091 // Vector shifts: check for immediate versions and lower them.
3092 // Note: This is done during DAG combining instead of DAG legalizing because
3093 // the build_vectors for 64-bit vector element shift counts are generally
3094 // not legal, and it is hard to see their values after they get legalized to
3095 // loads from a constant pool.
3096 case Intrinsic::arm_neon_vshifts:
3097 case Intrinsic::arm_neon_vshiftu:
3098 case Intrinsic::arm_neon_vshiftls:
3099 case Intrinsic::arm_neon_vshiftlu:
3100 case Intrinsic::arm_neon_vshiftn:
3101 case Intrinsic::arm_neon_vrshifts:
3102 case Intrinsic::arm_neon_vrshiftu:
3103 case Intrinsic::arm_neon_vrshiftn:
3104 case Intrinsic::arm_neon_vqshifts:
3105 case Intrinsic::arm_neon_vqshiftu:
3106 case Intrinsic::arm_neon_vqshiftsu:
3107 case Intrinsic::arm_neon_vqshiftns:
3108 case Intrinsic::arm_neon_vqshiftnu:
3109 case Intrinsic::arm_neon_vqshiftnsu:
3110 case Intrinsic::arm_neon_vqrshiftns:
3111 case Intrinsic::arm_neon_vqrshiftnu:
3112 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00003113 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003114 int64_t Cnt;
3115 unsigned VShiftOpc = 0;
3116
3117 switch (IntNo) {
3118 case Intrinsic::arm_neon_vshifts:
3119 case Intrinsic::arm_neon_vshiftu:
3120 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
3121 VShiftOpc = ARMISD::VSHL;
3122 break;
3123 }
3124 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
3125 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
3126 ARMISD::VSHRs : ARMISD::VSHRu);
3127 break;
3128 }
3129 return SDValue();
3130
3131 case Intrinsic::arm_neon_vshiftls:
3132 case Intrinsic::arm_neon_vshiftlu:
3133 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
3134 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003135 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003136
3137 case Intrinsic::arm_neon_vrshifts:
3138 case Intrinsic::arm_neon_vrshiftu:
3139 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
3140 break;
3141 return SDValue();
3142
3143 case Intrinsic::arm_neon_vqshifts:
3144 case Intrinsic::arm_neon_vqshiftu:
3145 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3146 break;
3147 return SDValue();
3148
3149 case Intrinsic::arm_neon_vqshiftsu:
3150 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3151 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003152 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003153
3154 case Intrinsic::arm_neon_vshiftn:
3155 case Intrinsic::arm_neon_vrshiftn:
3156 case Intrinsic::arm_neon_vqshiftns:
3157 case Intrinsic::arm_neon_vqshiftnu:
3158 case Intrinsic::arm_neon_vqshiftnsu:
3159 case Intrinsic::arm_neon_vqrshiftns:
3160 case Intrinsic::arm_neon_vqrshiftnu:
3161 case Intrinsic::arm_neon_vqrshiftnsu:
3162 // Narrowing shifts require an immediate right shift.
3163 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
3164 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003165 llvm_unreachable("invalid shift count for narrowing vector shift intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003166
3167 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003168 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003169 }
3170
3171 switch (IntNo) {
3172 case Intrinsic::arm_neon_vshifts:
3173 case Intrinsic::arm_neon_vshiftu:
3174 // Opcode already set above.
3175 break;
3176 case Intrinsic::arm_neon_vshiftls:
3177 case Intrinsic::arm_neon_vshiftlu:
3178 if (Cnt == VT.getVectorElementType().getSizeInBits())
3179 VShiftOpc = ARMISD::VSHLLi;
3180 else
3181 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
3182 ARMISD::VSHLLs : ARMISD::VSHLLu);
3183 break;
3184 case Intrinsic::arm_neon_vshiftn:
3185 VShiftOpc = ARMISD::VSHRN; break;
3186 case Intrinsic::arm_neon_vrshifts:
3187 VShiftOpc = ARMISD::VRSHRs; break;
3188 case Intrinsic::arm_neon_vrshiftu:
3189 VShiftOpc = ARMISD::VRSHRu; break;
3190 case Intrinsic::arm_neon_vrshiftn:
3191 VShiftOpc = ARMISD::VRSHRN; break;
3192 case Intrinsic::arm_neon_vqshifts:
3193 VShiftOpc = ARMISD::VQSHLs; break;
3194 case Intrinsic::arm_neon_vqshiftu:
3195 VShiftOpc = ARMISD::VQSHLu; break;
3196 case Intrinsic::arm_neon_vqshiftsu:
3197 VShiftOpc = ARMISD::VQSHLsu; break;
3198 case Intrinsic::arm_neon_vqshiftns:
3199 VShiftOpc = ARMISD::VQSHRNs; break;
3200 case Intrinsic::arm_neon_vqshiftnu:
3201 VShiftOpc = ARMISD::VQSHRNu; break;
3202 case Intrinsic::arm_neon_vqshiftnsu:
3203 VShiftOpc = ARMISD::VQSHRNsu; break;
3204 case Intrinsic::arm_neon_vqrshiftns:
3205 VShiftOpc = ARMISD::VQRSHRNs; break;
3206 case Intrinsic::arm_neon_vqrshiftnu:
3207 VShiftOpc = ARMISD::VQRSHRNu; break;
3208 case Intrinsic::arm_neon_vqrshiftnsu:
3209 VShiftOpc = ARMISD::VQRSHRNsu; break;
3210 }
3211
3212 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003213 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003214 }
3215
3216 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00003217 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003218 int64_t Cnt;
3219 unsigned VShiftOpc = 0;
3220
3221 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
3222 VShiftOpc = ARMISD::VSLI;
3223 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
3224 VShiftOpc = ARMISD::VSRI;
3225 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00003226 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003227 }
3228
3229 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
3230 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00003231 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003232 }
3233
3234 case Intrinsic::arm_neon_vqrshifts:
3235 case Intrinsic::arm_neon_vqrshiftu:
3236 // No immediate versions of these to check for.
3237 break;
3238 }
3239
3240 return SDValue();
3241}
3242
3243/// PerformShiftCombine - Checks for immediate versions of vector shifts and
3244/// lowers them. As with the vector shift intrinsics, this is done during DAG
3245/// combining instead of DAG legalizing because the build_vectors for 64-bit
3246/// vector element shift counts are generally not legal, and it is hard to see
3247/// their values after they get legalized to loads from a constant pool.
3248static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
3249 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003250 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003251
3252 // Nothing to be done for scalar shifts.
3253 if (! VT.isVector())
3254 return SDValue();
3255
3256 assert(ST->hasNEON() && "unexpected vector shift");
3257 int64_t Cnt;
3258
3259 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003260 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003261
3262 case ISD::SHL:
3263 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
3264 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003265 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003266 break;
3267
3268 case ISD::SRA:
3269 case ISD::SRL:
3270 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
3271 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
3272 ARMISD::VSHRs : ARMISD::VSHRu);
3273 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003274 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003275 }
3276 }
3277 return SDValue();
3278}
3279
3280/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
3281/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
3282static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
3283 const ARMSubtarget *ST) {
3284 SDValue N0 = N->getOperand(0);
3285
3286 // Check for sign- and zero-extensions of vector extract operations of 8-
3287 // and 16-bit vector elements. NEON supports these directly. They are
3288 // handled during DAG combining because type legalization will promote them
3289 // to 32-bit types and it is messy to recognize the operations after that.
3290 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
3291 SDValue Vec = N0.getOperand(0);
3292 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00003293 EVT VT = N->getValueType(0);
3294 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003295 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3296
Owen Anderson825b72b2009-08-11 20:47:22 +00003297 if (VT == MVT::i32 &&
3298 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00003299 TLI.isTypeLegal(Vec.getValueType())) {
3300
3301 unsigned Opc = 0;
3302 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003303 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003304 case ISD::SIGN_EXTEND:
3305 Opc = ARMISD::VGETLANEs;
3306 break;
3307 case ISD::ZERO_EXTEND:
3308 case ISD::ANY_EXTEND:
3309 Opc = ARMISD::VGETLANEu;
3310 break;
3311 }
3312 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
3313 }
3314 }
3315
3316 return SDValue();
3317}
3318
Dan Gohman475871a2008-07-27 21:46:04 +00003319SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003320 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003321 switch (N->getOpcode()) {
3322 default: break;
Chris Lattnerd1980a52009-03-12 06:52:53 +00003323 case ISD::ADD: return PerformADDCombine(N, DCI);
3324 case ISD::SUB: return PerformSUBCombine(N, DCI);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003325 case ARMISD::FMRRD: return PerformFMRRDCombine(N, DCI);
Bob Wilson5bafff32009-06-22 23:27:02 +00003326 case ISD::INTRINSIC_WO_CHAIN:
3327 return PerformIntrinsicCombine(N, DCI.DAG);
3328 case ISD::SHL:
3329 case ISD::SRA:
3330 case ISD::SRL:
3331 return PerformShiftCombine(N, DCI.DAG, Subtarget);
3332 case ISD::SIGN_EXTEND:
3333 case ISD::ZERO_EXTEND:
3334 case ISD::ANY_EXTEND:
3335 return PerformExtendCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003336 }
Dan Gohman475871a2008-07-27 21:46:04 +00003337 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003338}
3339
Bill Wendlingaf566342009-08-15 21:21:19 +00003340bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
3341 if (!Subtarget->hasV6Ops())
3342 // Pre-v6 does not support unaligned mem access.
3343 return false;
3344 else if (!Subtarget->hasV6Ops()) {
3345 // v6 may or may not support unaligned mem access.
3346 if (!Subtarget->isTargetDarwin())
3347 return false;
3348 }
3349
3350 switch (VT.getSimpleVT().SimpleTy) {
3351 default:
3352 return false;
3353 case MVT::i8:
3354 case MVT::i16:
3355 case MVT::i32:
3356 return true;
3357 // FIXME: VLD1 etc with standard alignment is legal.
3358 }
3359}
3360
Evan Chenge6c835f2009-08-14 20:09:37 +00003361static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
3362 if (V < 0)
3363 return false;
3364
3365 unsigned Scale = 1;
3366 switch (VT.getSimpleVT().SimpleTy) {
3367 default: return false;
3368 case MVT::i1:
3369 case MVT::i8:
3370 // Scale == 1;
3371 break;
3372 case MVT::i16:
3373 // Scale == 2;
3374 Scale = 2;
3375 break;
3376 case MVT::i32:
3377 // Scale == 4;
3378 Scale = 4;
3379 break;
3380 }
3381
3382 if ((V & (Scale - 1)) != 0)
3383 return false;
3384 V /= Scale;
3385 return V == (V & ((1LL << 5) - 1));
3386}
3387
3388static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
3389 const ARMSubtarget *Subtarget) {
3390 bool isNeg = false;
3391 if (V < 0) {
3392 isNeg = true;
3393 V = - V;
3394 }
3395
3396 switch (VT.getSimpleVT().SimpleTy) {
3397 default: return false;
3398 case MVT::i1:
3399 case MVT::i8:
3400 case MVT::i16:
3401 case MVT::i32:
3402 // + imm12 or - imm8
3403 if (isNeg)
3404 return V == (V & ((1LL << 8) - 1));
3405 return V == (V & ((1LL << 12) - 1));
3406 case MVT::f32:
3407 case MVT::f64:
3408 // Same as ARM mode. FIXME: NEON?
3409 if (!Subtarget->hasVFP2())
3410 return false;
3411 if ((V & 3) != 0)
3412 return false;
3413 V >>= 2;
3414 return V == (V & ((1LL << 8) - 1));
3415 }
3416}
3417
Evan Chengb01fad62007-03-12 23:30:29 +00003418/// isLegalAddressImmediate - Return true if the integer value can be used
3419/// as the offset of the target addressing mode for load / store of the
3420/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00003421static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00003422 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00003423 if (V == 0)
3424 return true;
3425
Evan Cheng65011532009-03-09 19:15:00 +00003426 if (!VT.isSimple())
3427 return false;
3428
Evan Chenge6c835f2009-08-14 20:09:37 +00003429 if (Subtarget->isThumb1Only())
3430 return isLegalT1AddressImmediate(V, VT);
3431 else if (Subtarget->isThumb2())
3432 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00003433
Evan Chenge6c835f2009-08-14 20:09:37 +00003434 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00003435 if (V < 0)
3436 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00003437 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00003438 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00003439 case MVT::i1:
3440 case MVT::i8:
3441 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00003442 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003443 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003444 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00003445 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003446 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003447 case MVT::f32:
3448 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00003449 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00003450 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00003451 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00003452 return false;
3453 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003454 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00003455 }
Evan Chenga8e29892007-01-19 07:51:42 +00003456}
3457
Evan Chenge6c835f2009-08-14 20:09:37 +00003458bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
3459 EVT VT) const {
3460 int Scale = AM.Scale;
3461 if (Scale < 0)
3462 return false;
3463
3464 switch (VT.getSimpleVT().SimpleTy) {
3465 default: return false;
3466 case MVT::i1:
3467 case MVT::i8:
3468 case MVT::i16:
3469 case MVT::i32:
3470 if (Scale == 1)
3471 return true;
3472 // r + r << imm
3473 Scale = Scale & ~1;
3474 return Scale == 2 || Scale == 4 || Scale == 8;
3475 case MVT::i64:
3476 // r + r
3477 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
3478 return true;
3479 return false;
3480 case MVT::isVoid:
3481 // Note, we allow "void" uses (basically, uses that aren't loads or
3482 // stores), because arm allows folding a scale into many arithmetic
3483 // operations. This should be made more precise and revisited later.
3484
3485 // Allow r << imm, but the imm has to be a multiple of two.
3486 if (Scale & 1) return false;
3487 return isPowerOf2_32(Scale);
3488 }
3489}
3490
Chris Lattner37caf8c2007-04-09 23:33:39 +00003491/// isLegalAddressingMode - Return true if the addressing mode represented
3492/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003493bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00003494 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00003495 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00003496 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00003497 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003498
Chris Lattner37caf8c2007-04-09 23:33:39 +00003499 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003500 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003501 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003502
Chris Lattner37caf8c2007-04-09 23:33:39 +00003503 switch (AM.Scale) {
3504 case 0: // no scale reg, must be "r+i" or "r", or "i".
3505 break;
3506 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00003507 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00003508 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00003509 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00003510 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00003511 // ARM doesn't support any R+R*scale+imm addr modes.
3512 if (AM.BaseOffs)
3513 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003514
Bob Wilson2c7dab12009-04-08 17:55:28 +00003515 if (!VT.isSimple())
3516 return false;
3517
Evan Chenge6c835f2009-08-14 20:09:37 +00003518 if (Subtarget->isThumb2())
3519 return isLegalT2ScaledAddressingMode(AM, VT);
3520
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003521 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00003522 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00003523 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00003524 case MVT::i1:
3525 case MVT::i8:
3526 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003527 if (Scale < 0) Scale = -Scale;
3528 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003529 return true;
3530 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00003531 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003532 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00003533 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00003534 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003535 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003536 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00003537 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003538
Owen Anderson825b72b2009-08-11 20:47:22 +00003539 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00003540 // Note, we allow "void" uses (basically, uses that aren't loads or
3541 // stores), because arm allows folding a scale into many arithmetic
3542 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003543
Chris Lattner37caf8c2007-04-09 23:33:39 +00003544 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00003545 if (Scale & 1) return false;
3546 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00003547 }
3548 break;
Evan Chengb01fad62007-03-12 23:30:29 +00003549 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00003550 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00003551}
3552
Owen Andersone50ed302009-08-10 22:56:29 +00003553static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00003554 bool isSEXTLoad, SDValue &Base,
3555 SDValue &Offset, bool &isInc,
3556 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00003557 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
3558 return false;
3559
Owen Anderson825b72b2009-08-11 20:47:22 +00003560 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00003561 // AddressingMode 3
3562 Base = Ptr->getOperand(0);
3563 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003564 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003565 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003566 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00003567 isInc = false;
3568 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3569 return true;
3570 }
3571 }
3572 isInc = (Ptr->getOpcode() == ISD::ADD);
3573 Offset = Ptr->getOperand(1);
3574 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00003575 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00003576 // AddressingMode 2
3577 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003578 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003579 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003580 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00003581 isInc = false;
3582 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3583 Base = Ptr->getOperand(0);
3584 return true;
3585 }
3586 }
3587
3588 if (Ptr->getOpcode() == ISD::ADD) {
3589 isInc = true;
3590 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
3591 if (ShOpcVal != ARM_AM::no_shift) {
3592 Base = Ptr->getOperand(1);
3593 Offset = Ptr->getOperand(0);
3594 } else {
3595 Base = Ptr->getOperand(0);
3596 Offset = Ptr->getOperand(1);
3597 }
3598 return true;
3599 }
3600
3601 isInc = (Ptr->getOpcode() == ISD::ADD);
3602 Base = Ptr->getOperand(0);
3603 Offset = Ptr->getOperand(1);
3604 return true;
3605 }
3606
3607 // FIXME: Use FLDM / FSTM to emulate indexed FP load / store.
3608 return false;
3609}
3610
Owen Andersone50ed302009-08-10 22:56:29 +00003611static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00003612 bool isSEXTLoad, SDValue &Base,
3613 SDValue &Offset, bool &isInc,
3614 SelectionDAG &DAG) {
3615 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
3616 return false;
3617
3618 Base = Ptr->getOperand(0);
3619 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
3620 int RHSC = (int)RHS->getZExtValue();
3621 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
3622 assert(Ptr->getOpcode() == ISD::ADD);
3623 isInc = false;
3624 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3625 return true;
3626 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
3627 isInc = Ptr->getOpcode() == ISD::ADD;
3628 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
3629 return true;
3630 }
3631 }
3632
3633 return false;
3634}
3635
Evan Chenga8e29892007-01-19 07:51:42 +00003636/// getPreIndexedAddressParts - returns true by value, base pointer and
3637/// offset pointer and addressing mode by reference if the node's address
3638/// can be legally represented as pre-indexed load / store address.
3639bool
Dan Gohman475871a2008-07-27 21:46:04 +00003640ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
3641 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003642 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00003643 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003644 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00003645 return false;
3646
Owen Andersone50ed302009-08-10 22:56:29 +00003647 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00003648 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00003649 bool isSEXTLoad = false;
3650 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
3651 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003652 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003653 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
3654 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
3655 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003656 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003657 } else
3658 return false;
3659
3660 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00003661 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00003662 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00003663 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
3664 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00003665 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00003666 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00003667 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00003668 if (!isLegal)
3669 return false;
3670
3671 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
3672 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00003673}
3674
3675/// getPostIndexedAddressParts - returns true by value, base pointer and
3676/// offset pointer and addressing mode by reference if this node can be
3677/// combined with a load / store to form a post-indexed load / store.
3678bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00003679 SDValue &Base,
3680 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003681 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00003682 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003683 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00003684 return false;
3685
Owen Andersone50ed302009-08-10 22:56:29 +00003686 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00003687 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00003688 bool isSEXTLoad = false;
3689 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003690 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003691 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
3692 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003693 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003694 } else
3695 return false;
3696
3697 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00003698 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00003699 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00003700 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003701 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00003702 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00003703 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
3704 isInc, DAG);
3705 if (!isLegal)
3706 return false;
3707
3708 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
3709 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00003710}
3711
Dan Gohman475871a2008-07-27 21:46:04 +00003712void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00003713 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003714 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003715 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00003716 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00003717 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003718 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00003719 switch (Op.getOpcode()) {
3720 default: break;
3721 case ARMISD::CMOV: {
3722 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00003723 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00003724 if (KnownZero == 0 && KnownOne == 0) return;
3725
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003726 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00003727 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
3728 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00003729 KnownZero &= KnownZeroRHS;
3730 KnownOne &= KnownOneRHS;
3731 return;
3732 }
3733 }
3734}
3735
3736//===----------------------------------------------------------------------===//
3737// ARM Inline Assembly Support
3738//===----------------------------------------------------------------------===//
3739
3740/// getConstraintType - Given a constraint letter, return the type of
3741/// constraint it is for this target.
3742ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00003743ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
3744 if (Constraint.size() == 1) {
3745 switch (Constraint[0]) {
3746 default: break;
3747 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003748 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00003749 }
Evan Chenga8e29892007-01-19 07:51:42 +00003750 }
Chris Lattner4234f572007-03-25 02:14:49 +00003751 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00003752}
3753
Bob Wilson2dc4f542009-03-20 22:42:55 +00003754std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00003755ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00003756 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003757 if (Constraint.size() == 1) {
3758 // GCC RS6000 Constraint Letters
3759 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003760 case 'l':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003761 if (Subtarget->isThumb1Only())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00003762 return std::make_pair(0U, ARM::tGPRRegisterClass);
3763 else
3764 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003765 case 'r':
3766 return std::make_pair(0U, ARM::GPRRegisterClass);
3767 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00003768 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003769 return std::make_pair(0U, ARM::SPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00003770 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003771 return std::make_pair(0U, ARM::DPRRegisterClass);
3772 break;
Evan Chenga8e29892007-01-19 07:51:42 +00003773 }
3774 }
3775 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3776}
3777
3778std::vector<unsigned> ARMTargetLowering::
3779getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00003780 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003781 if (Constraint.size() != 1)
3782 return std::vector<unsigned>();
3783
3784 switch (Constraint[0]) { // GCC ARM Constraint Letters
3785 default: break;
3786 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00003787 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
3788 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
3789 0);
Evan Chenga8e29892007-01-19 07:51:42 +00003790 case 'r':
3791 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
3792 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
3793 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
3794 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003795 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00003796 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003797 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
3798 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
3799 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
3800 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
3801 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
3802 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
3803 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
3804 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00003805 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003806 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
3807 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
3808 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
3809 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
3810 break;
Evan Chenga8e29892007-01-19 07:51:42 +00003811 }
3812
3813 return std::vector<unsigned>();
3814}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003815
3816/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3817/// vector. If it is invalid, don't add anything to Ops.
3818void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3819 char Constraint,
3820 bool hasMemory,
3821 std::vector<SDValue>&Ops,
3822 SelectionDAG &DAG) const {
3823 SDValue Result(0, 0);
3824
3825 switch (Constraint) {
3826 default: break;
3827 case 'I': case 'J': case 'K': case 'L':
3828 case 'M': case 'N': case 'O':
3829 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
3830 if (!C)
3831 return;
3832
3833 int64_t CVal64 = C->getSExtValue();
3834 int CVal = (int) CVal64;
3835 // None of these constraints allow values larger than 32 bits. Check
3836 // that the value fits in an int.
3837 if (CVal != CVal64)
3838 return;
3839
3840 switch (Constraint) {
3841 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003842 if (Subtarget->isThumb1Only()) {
3843 // This must be a constant between 0 and 255, for ADD
3844 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003845 if (CVal >= 0 && CVal <= 255)
3846 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00003847 } else if (Subtarget->isThumb2()) {
3848 // A constant that can be used as an immediate value in a
3849 // data-processing instruction.
3850 if (ARM_AM::getT2SOImmVal(CVal) != -1)
3851 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003852 } else {
3853 // A constant that can be used as an immediate value in a
3854 // data-processing instruction.
3855 if (ARM_AM::getSOImmVal(CVal) != -1)
3856 break;
3857 }
3858 return;
3859
3860 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003861 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003862 // This must be a constant between -255 and -1, for negated ADD
3863 // immediates. This can be used in GCC with an "n" modifier that
3864 // prints the negated value, for use with SUB instructions. It is
3865 // not useful otherwise but is implemented for compatibility.
3866 if (CVal >= -255 && CVal <= -1)
3867 break;
3868 } else {
3869 // This must be a constant between -4095 and 4095. It is not clear
3870 // what this constraint is intended for. Implemented for
3871 // compatibility with GCC.
3872 if (CVal >= -4095 && CVal <= 4095)
3873 break;
3874 }
3875 return;
3876
3877 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003878 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003879 // A 32-bit value where only one byte has a nonzero value. Exclude
3880 // zero to match GCC. This constraint is used by GCC internally for
3881 // constants that can be loaded with a move/shift combination.
3882 // It is not useful otherwise but is implemented for compatibility.
3883 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
3884 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00003885 } else if (Subtarget->isThumb2()) {
3886 // A constant whose bitwise inverse can be used as an immediate
3887 // value in a data-processing instruction. This can be used in GCC
3888 // with a "B" modifier that prints the inverted value, for use with
3889 // BIC and MVN instructions. It is not useful otherwise but is
3890 // implemented for compatibility.
3891 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
3892 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003893 } else {
3894 // A constant whose bitwise inverse can be used as an immediate
3895 // value in a data-processing instruction. This can be used in GCC
3896 // with a "B" modifier that prints the inverted value, for use with
3897 // BIC and MVN instructions. It is not useful otherwise but is
3898 // implemented for compatibility.
3899 if (ARM_AM::getSOImmVal(~CVal) != -1)
3900 break;
3901 }
3902 return;
3903
3904 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003905 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003906 // This must be a constant between -7 and 7,
3907 // for 3-operand ADD/SUB immediate instructions.
3908 if (CVal >= -7 && CVal < 7)
3909 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00003910 } else if (Subtarget->isThumb2()) {
3911 // A constant whose negation can be used as an immediate value in a
3912 // data-processing instruction. This can be used in GCC with an "n"
3913 // modifier that prints the negated value, for use with SUB
3914 // instructions. It is not useful otherwise but is implemented for
3915 // compatibility.
3916 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
3917 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003918 } else {
3919 // A constant whose negation can be used as an immediate value in a
3920 // data-processing instruction. This can be used in GCC with an "n"
3921 // modifier that prints the negated value, for use with SUB
3922 // instructions. It is not useful otherwise but is implemented for
3923 // compatibility.
3924 if (ARM_AM::getSOImmVal(-CVal) != -1)
3925 break;
3926 }
3927 return;
3928
3929 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003930 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003931 // This must be a multiple of 4 between 0 and 1020, for
3932 // ADD sp + immediate.
3933 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
3934 break;
3935 } else {
3936 // A power of two or a constant between 0 and 32. This is used in
3937 // GCC for the shift amount on shifted register operands, but it is
3938 // useful in general for any shift amounts.
3939 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
3940 break;
3941 }
3942 return;
3943
3944 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003945 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003946 // This must be a constant between 0 and 31, for shift amounts.
3947 if (CVal >= 0 && CVal <= 31)
3948 break;
3949 }
3950 return;
3951
3952 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003953 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003954 // This must be a multiple of 4 between -508 and 508, for
3955 // ADD/SUB sp = sp + immediate.
3956 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
3957 break;
3958 }
3959 return;
3960 }
3961 Result = DAG.getTargetConstant(CVal, Op.getValueType());
3962 break;
3963 }
3964
3965 if (Result.getNode()) {
3966 Ops.push_back(Result);
3967 return;
3968 }
3969 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
3970 Ops, DAG);
3971}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00003972
3973bool
3974ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3975 // The ARM target isn't yet aware of offsets.
3976 return false;
3977}