blob: 12d7b2ee212d38573e5afdaf50a8f0eb95787487 [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"
Evan Chenga8e29892007-01-19 07:51:42 +000043using namespace llvm;
44
Owen Andersone50ed302009-08-10 22:56:29 +000045static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000046 CCValAssign::LocInfo &LocInfo,
47 ISD::ArgFlagsTy &ArgFlags,
48 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000049static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000050 CCValAssign::LocInfo &LocInfo,
51 ISD::ArgFlagsTy &ArgFlags,
52 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000053static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000054 CCValAssign::LocInfo &LocInfo,
55 ISD::ArgFlagsTy &ArgFlags,
56 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000057static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000058 CCValAssign::LocInfo &LocInfo,
59 ISD::ArgFlagsTy &ArgFlags,
60 CCState &State);
61
Owen Andersone50ed302009-08-10 22:56:29 +000062void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
63 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000064 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000065 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000066 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
67 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000068
Owen Anderson70671842009-08-10 20:18:46 +000069 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +000070 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +000071 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000072 }
73
Owen Andersone50ed302009-08-10 22:56:29 +000074 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +000075 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Owen Anderson70671842009-08-10 20:18:46 +000076 setOperationAction(ISD::VSETCC, VT.getSimpleVT(), Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +000077 if (ElemTy == MVT::i8 || ElemTy == MVT::i16)
Owen Anderson70671842009-08-10 20:18:46 +000078 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
79 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
80 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Owen Anderson70671842009-08-10 20:18:46 +000081 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Custom);
Anton Korobeynikov8e6c2b92009-08-21 12:40:35 +000082 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +000083 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +000084 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
85 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
86 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +000087 }
88
89 // Promote all bit-wise operations.
90 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +000091 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000092 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
93 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +000094 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +000095 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +000096 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +000097 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +000098 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +000099 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000100 }
101}
102
Owen Andersone50ed302009-08-10 22:56:29 +0000103void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000104 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000105 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000106}
107
Owen Andersone50ed302009-08-10 22:56:29 +0000108void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000109 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000110 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000111}
112
Chris Lattnerf0144122009-07-28 03:13:23 +0000113static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
114 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000115 return new TargetLoweringObjectFileMachO();
Chris Lattner80ec2792009-08-02 00:34:36 +0000116 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000117}
118
Evan Chenga8e29892007-01-19 07:51:42 +0000119ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000120 : TargetLowering(TM, createTLOF(TM)), ARMPCLabelIndex(0) {
Evan Chenga8e29892007-01-19 07:51:42 +0000121 Subtarget = &TM.getSubtarget<ARMSubtarget>();
122
Evan Chengb1df8f22007-04-27 08:15:43 +0000123 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000124 // Uses VFP for Thumb libfuncs if available.
125 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
126 // Single-precision floating-point arithmetic.
127 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
128 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
129 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
130 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000131
Evan Chengb1df8f22007-04-27 08:15:43 +0000132 // Double-precision floating-point arithmetic.
133 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
134 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
135 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
136 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000137
Evan Chengb1df8f22007-04-27 08:15:43 +0000138 // Single-precision comparisons.
139 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
140 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
141 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
142 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
143 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
144 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
145 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
146 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000147
Evan Chengb1df8f22007-04-27 08:15:43 +0000148 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
149 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
150 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
151 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
152 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
153 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
154 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
155 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000156
Evan Chengb1df8f22007-04-27 08:15:43 +0000157 // Double-precision comparisons.
158 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
159 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
160 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
161 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
162 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
163 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
164 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
165 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000166
Evan Chengb1df8f22007-04-27 08:15:43 +0000167 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
168 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
169 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
170 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
171 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
172 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
173 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
174 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000175
Evan Chengb1df8f22007-04-27 08:15:43 +0000176 // Floating-point to integer conversions.
177 // i64 conversions are done via library routines even when generating VFP
178 // instructions, so use the same ones.
179 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
180 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
181 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
182 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000183
Evan Chengb1df8f22007-04-27 08:15:43 +0000184 // Conversions between floating types.
185 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
186 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
187
188 // Integer to floating-point conversions.
189 // i64 conversions are done via library routines even when generating VFP
190 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000191 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
192 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000193 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
194 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
195 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
196 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
197 }
Evan Chenga8e29892007-01-19 07:51:42 +0000198 }
199
Bob Wilson2f954612009-05-22 17:38:41 +0000200 // These libcalls are not available in 32-bit.
201 setLibcallName(RTLIB::SHL_I128, 0);
202 setLibcallName(RTLIB::SRL_I128, 0);
203 setLibcallName(RTLIB::SRA_I128, 0);
204
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000205 // Libcalls should use the AAPCS base standard ABI, even if hard float
206 // is in effect, as per the ARM RTABI specification, section 4.1.2.
207 if (Subtarget->isAAPCS_ABI()) {
208 for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i) {
209 setLibcallCallingConv(static_cast<RTLIB::Libcall>(i),
210 CallingConv::ARM_AAPCS);
211 }
212 }
213
David Goodwinf1daf7d2009-07-08 23:10:31 +0000214 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000215 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000216 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000217 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000218 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000219 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
220 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000221
Owen Anderson825b72b2009-08-11 20:47:22 +0000222 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000223 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000224
225 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000226 addDRTypeForNEON(MVT::v2f32);
227 addDRTypeForNEON(MVT::v8i8);
228 addDRTypeForNEON(MVT::v4i16);
229 addDRTypeForNEON(MVT::v2i32);
230 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000231
Owen Anderson825b72b2009-08-11 20:47:22 +0000232 addQRTypeForNEON(MVT::v4f32);
233 addQRTypeForNEON(MVT::v2f64);
234 addQRTypeForNEON(MVT::v16i8);
235 addQRTypeForNEON(MVT::v8i16);
236 addQRTypeForNEON(MVT::v4i32);
237 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000238
239 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
240 setTargetDAGCombine(ISD::SHL);
241 setTargetDAGCombine(ISD::SRL);
242 setTargetDAGCombine(ISD::SRA);
243 setTargetDAGCombine(ISD::SIGN_EXTEND);
244 setTargetDAGCombine(ISD::ZERO_EXTEND);
245 setTargetDAGCombine(ISD::ANY_EXTEND);
246 }
247
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000248 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000249
250 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000251 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000252
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000253 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000254 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000255
Evan Chenga8e29892007-01-19 07:51:42 +0000256 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000257 if (!Subtarget->isThumb1Only()) {
258 for (unsigned im = (unsigned)ISD::PRE_INC;
259 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000260 setIndexedLoadAction(im, MVT::i1, Legal);
261 setIndexedLoadAction(im, MVT::i8, Legal);
262 setIndexedLoadAction(im, MVT::i16, Legal);
263 setIndexedLoadAction(im, MVT::i32, Legal);
264 setIndexedStoreAction(im, MVT::i1, Legal);
265 setIndexedStoreAction(im, MVT::i8, Legal);
266 setIndexedStoreAction(im, MVT::i16, Legal);
267 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000268 }
Evan Chenga8e29892007-01-19 07:51:42 +0000269 }
270
271 // i64 operation support.
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000272 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000273 setOperationAction(ISD::MUL, MVT::i64, Expand);
274 setOperationAction(ISD::MULHU, MVT::i32, Expand);
275 setOperationAction(ISD::MULHS, MVT::i32, Expand);
276 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
277 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000278 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000279 setOperationAction(ISD::MUL, MVT::i64, Expand);
280 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb6207242009-08-01 00:16:10 +0000281 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000282 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000283 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000284 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
285 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
286 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
287 setOperationAction(ISD::SRL, MVT::i64, Custom);
288 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000289
290 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000291 setOperationAction(ISD::ROTL, MVT::i32, Expand);
292 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
293 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000294 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000295 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000296
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000297 // Only ARMv6 has BSWAP.
298 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000299 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000300
Evan Chenga8e29892007-01-19 07:51:42 +0000301 // These are expanded into libcalls.
Owen Anderson825b72b2009-08-11 20:47:22 +0000302 setOperationAction(ISD::SDIV, MVT::i32, Expand);
303 setOperationAction(ISD::UDIV, MVT::i32, Expand);
304 setOperationAction(ISD::SREM, MVT::i32, Expand);
305 setOperationAction(ISD::UREM, MVT::i32, Expand);
306 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
307 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000308
Evan Chenga8e29892007-01-19 07:51:42 +0000309 // Support label based line numbers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000310 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
311 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000312
Owen Anderson825b72b2009-08-11 20:47:22 +0000313 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
314 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
315 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
316 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000317
Evan Chenga8e29892007-01-19 07:51:42 +0000318 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000319 setOperationAction(ISD::VASTART, MVT::Other, Custom);
320 setOperationAction(ISD::VAARG, MVT::Other, Expand);
321 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
322 setOperationAction(ISD::VAEND, MVT::Other, Expand);
323 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
324 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000325 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
326 // FIXME: Shouldn't need this, since no register is used, but the legalizer
327 // doesn't yet know how to not do that for SjLj.
328 setExceptionSelectorRegister(ARM::R0);
Evan Cheng86198642009-08-07 00:34:42 +0000329 if (Subtarget->isThumb())
Owen Anderson825b72b2009-08-11 20:47:22 +0000330 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Evan Cheng86198642009-08-07 00:34:42 +0000331 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000332 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
333 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000334
Evan Chengd27c9fc2009-07-03 01:43:10 +0000335 if (!Subtarget->hasV6Ops() && !Subtarget->isThumb2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000336 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
337 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000338 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000339 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000340
David Goodwinf1daf7d2009-07-08 23:10:31 +0000341 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only())
Evan Chengc7c77292008-11-04 19:57:48 +0000342 // Turn f64->i64 into FMRRD, i64 -> f64 to FMDRR iff target supports vfp2.
Owen Anderson825b72b2009-08-11 20:47:22 +0000343 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000344
345 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000346 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
347 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
348 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000349
Owen Anderson825b72b2009-08-11 20:47:22 +0000350 setOperationAction(ISD::SETCC, MVT::i32, Expand);
351 setOperationAction(ISD::SETCC, MVT::f32, Expand);
352 setOperationAction(ISD::SETCC, MVT::f64, Expand);
353 setOperationAction(ISD::SELECT, MVT::i32, Expand);
354 setOperationAction(ISD::SELECT, MVT::f32, Expand);
355 setOperationAction(ISD::SELECT, MVT::f64, Expand);
356 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
357 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
358 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000359
Owen Anderson825b72b2009-08-11 20:47:22 +0000360 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
361 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
362 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
363 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
364 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000365
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000366 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000367 setOperationAction(ISD::FSIN, MVT::f64, Expand);
368 setOperationAction(ISD::FSIN, MVT::f32, Expand);
369 setOperationAction(ISD::FCOS, MVT::f32, Expand);
370 setOperationAction(ISD::FCOS, MVT::f64, Expand);
371 setOperationAction(ISD::FREM, MVT::f64, Expand);
372 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000373 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000374 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
375 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000376 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000377 setOperationAction(ISD::FPOW, MVT::f64, Expand);
378 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000379
Evan Chenga8e29892007-01-19 07:51:42 +0000380 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
David Goodwinf1daf7d2009-07-08 23:10:31 +0000381 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000382 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
383 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
384 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
385 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000386 }
Evan Chenga8e29892007-01-19 07:51:42 +0000387
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000388 // We have target-specific dag combine patterns for the following nodes:
389 // ARMISD::FMRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000390 setTargetDAGCombine(ISD::ADD);
391 setTargetDAGCombine(ISD::SUB);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000392
Evan Chenga8e29892007-01-19 07:51:42 +0000393 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Chenga8e29892007-01-19 07:51:42 +0000394 setSchedulingPreference(SchedulingForRegPressure);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000395
Evan Chengbc9b7542009-08-15 07:59:10 +0000396 // FIXME: If-converter should use instruction latency to determine
397 // profitability rather than relying on fixed limits.
398 if (Subtarget->getCPUString() == "generic") {
399 // Generic (and overly aggressive) if-conversion limits.
400 setIfCvtBlockSizeLimit(10);
401 setIfCvtDupBlockSizeLimit(2);
402 } else if (Subtarget->hasV6Ops()) {
403 setIfCvtBlockSizeLimit(2);
404 setIfCvtDupBlockSizeLimit(1);
405 } else {
406 setIfCvtBlockSizeLimit(3);
407 setIfCvtDupBlockSizeLimit(2);
Evan Cheng8557c2b2009-06-19 01:51:50 +0000408 }
409
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000410 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Bob Wilsone6abdff2009-05-18 20:55:32 +0000411 // Do not enable CodePlacementOpt for now: it currently runs after the
412 // ARMConstantIslandPass and messes up branch relaxation and placement
413 // of constant islands.
414 // benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000415}
416
Evan Chenga8e29892007-01-19 07:51:42 +0000417const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
418 switch (Opcode) {
419 default: return 0;
420 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000421 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
422 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000423 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000424 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
425 case ARMISD::tCALL: return "ARMISD::tCALL";
426 case ARMISD::BRCOND: return "ARMISD::BRCOND";
427 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000428 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000429 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
430 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
431 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000432 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000433 case ARMISD::CMPFP: return "ARMISD::CMPFP";
434 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
435 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
436 case ARMISD::CMOV: return "ARMISD::CMOV";
437 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000438
Evan Chenga8e29892007-01-19 07:51:42 +0000439 case ARMISD::FTOSI: return "ARMISD::FTOSI";
440 case ARMISD::FTOUI: return "ARMISD::FTOUI";
441 case ARMISD::SITOF: return "ARMISD::SITOF";
442 case ARMISD::UITOF: return "ARMISD::UITOF";
Evan Chenga8e29892007-01-19 07:51:42 +0000443
444 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
445 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
446 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000447
Evan Chenga8e29892007-01-19 07:51:42 +0000448 case ARMISD::FMRRD: return "ARMISD::FMRRD";
449 case ARMISD::FMDRR: return "ARMISD::FMDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000450
451 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000452
Evan Cheng86198642009-08-07 00:34:42 +0000453 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
454
Bob Wilson5bafff32009-06-22 23:27:02 +0000455 case ARMISD::VCEQ: return "ARMISD::VCEQ";
456 case ARMISD::VCGE: return "ARMISD::VCGE";
457 case ARMISD::VCGEU: return "ARMISD::VCGEU";
458 case ARMISD::VCGT: return "ARMISD::VCGT";
459 case ARMISD::VCGTU: return "ARMISD::VCGTU";
460 case ARMISD::VTST: return "ARMISD::VTST";
461
462 case ARMISD::VSHL: return "ARMISD::VSHL";
463 case ARMISD::VSHRs: return "ARMISD::VSHRs";
464 case ARMISD::VSHRu: return "ARMISD::VSHRu";
465 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
466 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
467 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
468 case ARMISD::VSHRN: return "ARMISD::VSHRN";
469 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
470 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
471 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
472 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
473 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
474 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
475 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
476 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
477 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
478 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
479 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
480 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
481 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
482 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000483 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000484 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000485 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000486 case ARMISD::VREV64: return "ARMISD::VREV64";
487 case ARMISD::VREV32: return "ARMISD::VREV32";
488 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000489 case ARMISD::VZIP: return "ARMISD::VZIP";
490 case ARMISD::VUZP: return "ARMISD::VUZP";
491 case ARMISD::VTRN: return "ARMISD::VTRN";
Evan Chenga8e29892007-01-19 07:51:42 +0000492 }
493}
494
Bill Wendlingb4202b82009-07-01 18:50:55 +0000495/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000496unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
497 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 1 : 2;
498}
499
Evan Chenga8e29892007-01-19 07:51:42 +0000500//===----------------------------------------------------------------------===//
501// Lowering Code
502//===----------------------------------------------------------------------===//
503
Evan Chenga8e29892007-01-19 07:51:42 +0000504/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
505static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
506 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000507 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000508 case ISD::SETNE: return ARMCC::NE;
509 case ISD::SETEQ: return ARMCC::EQ;
510 case ISD::SETGT: return ARMCC::GT;
511 case ISD::SETGE: return ARMCC::GE;
512 case ISD::SETLT: return ARMCC::LT;
513 case ISD::SETLE: return ARMCC::LE;
514 case ISD::SETUGT: return ARMCC::HI;
515 case ISD::SETUGE: return ARMCC::HS;
516 case ISD::SETULT: return ARMCC::LO;
517 case ISD::SETULE: return ARMCC::LS;
518 }
519}
520
521/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC. It
522/// returns true if the operands should be inverted to form the proper
523/// comparison.
524static bool FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
525 ARMCC::CondCodes &CondCode2) {
526 bool Invert = false;
527 CondCode2 = ARMCC::AL;
528 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000529 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000530 case ISD::SETEQ:
531 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
532 case ISD::SETGT:
533 case ISD::SETOGT: CondCode = ARMCC::GT; break;
534 case ISD::SETGE:
535 case ISD::SETOGE: CondCode = ARMCC::GE; break;
536 case ISD::SETOLT: CondCode = ARMCC::MI; break;
537 case ISD::SETOLE: CondCode = ARMCC::GT; Invert = true; break;
538 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
539 case ISD::SETO: CondCode = ARMCC::VC; break;
540 case ISD::SETUO: CondCode = ARMCC::VS; break;
541 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
542 case ISD::SETUGT: CondCode = ARMCC::HI; break;
543 case ISD::SETUGE: CondCode = ARMCC::PL; break;
544 case ISD::SETLT:
545 case ISD::SETULT: CondCode = ARMCC::LT; break;
546 case ISD::SETLE:
547 case ISD::SETULE: CondCode = ARMCC::LE; break;
548 case ISD::SETNE:
549 case ISD::SETUNE: CondCode = ARMCC::NE; break;
550 }
551 return Invert;
552}
553
Bob Wilson1f595bb2009-04-17 19:07:39 +0000554//===----------------------------------------------------------------------===//
555// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +0000556//===----------------------------------------------------------------------===//
557
558#include "ARMGenCallingConv.inc"
559
560// APCS f64 is in register pairs, possibly split to stack
Owen Andersone50ed302009-08-10 22:56:29 +0000561static bool f64AssignAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000562 CCValAssign::LocInfo &LocInfo,
563 CCState &State, bool CanFail) {
564 static const unsigned RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
565
566 // Try to get the first register.
567 if (unsigned Reg = State.AllocateReg(RegList, 4))
568 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
569 else {
570 // For the 2nd half of a v2f64, do not fail.
571 if (CanFail)
572 return false;
573
574 // Put the whole thing on the stack.
575 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
576 State.AllocateStack(8, 4),
577 LocVT, LocInfo));
578 return true;
579 }
580
581 // Try to get the second register.
582 if (unsigned Reg = State.AllocateReg(RegList, 4))
583 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
584 else
585 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
586 State.AllocateStack(4, 4),
587 LocVT, LocInfo));
588 return true;
589}
590
Owen Andersone50ed302009-08-10 22:56:29 +0000591static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000592 CCValAssign::LocInfo &LocInfo,
593 ISD::ArgFlagsTy &ArgFlags,
594 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000595 if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
596 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000597 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000598 !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
599 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000600 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000601}
602
603// AAPCS f64 is in aligned register pairs
Owen Andersone50ed302009-08-10 22:56:29 +0000604static bool f64AssignAAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000605 CCValAssign::LocInfo &LocInfo,
606 CCState &State, bool CanFail) {
607 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
608 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
609
610 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
611 if (Reg == 0) {
612 // For the 2nd half of a v2f64, do not just 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, 8),
619 LocVT, LocInfo));
620 return true;
621 }
622
623 unsigned i;
624 for (i = 0; i < 2; ++i)
625 if (HiRegList[i] == Reg)
626 break;
627
628 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
629 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
630 LocVT, LocInfo));
631 return true;
632}
633
Owen Andersone50ed302009-08-10 22:56:29 +0000634static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000635 CCValAssign::LocInfo &LocInfo,
636 ISD::ArgFlagsTy &ArgFlags,
637 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000638 if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
639 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000640 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000641 !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
642 return false;
643 return true; // we handled it
644}
645
Owen Andersone50ed302009-08-10 22:56:29 +0000646static bool f64RetAssign(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000647 CCValAssign::LocInfo &LocInfo, CCState &State) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000648 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
649 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
650
Bob Wilsone65586b2009-04-17 20:40:45 +0000651 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
652 if (Reg == 0)
653 return false; // we didn't handle it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000654
Bob Wilsone65586b2009-04-17 20:40:45 +0000655 unsigned i;
656 for (i = 0; i < 2; ++i)
657 if (HiRegList[i] == Reg)
658 break;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000659
Bob Wilson5bafff32009-06-22 23:27:02 +0000660 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bob Wilsone65586b2009-04-17 20:40:45 +0000661 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000662 LocVT, LocInfo));
663 return true;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000664}
665
Owen Andersone50ed302009-08-10 22:56:29 +0000666static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000667 CCValAssign::LocInfo &LocInfo,
668 ISD::ArgFlagsTy &ArgFlags,
669 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000670 if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
671 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000672 if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
Bob Wilson5bafff32009-06-22 23:27:02 +0000673 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000674 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000675}
676
Owen Andersone50ed302009-08-10 22:56:29 +0000677static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000678 CCValAssign::LocInfo &LocInfo,
679 ISD::ArgFlagsTy &ArgFlags,
680 CCState &State) {
681 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
682 State);
683}
684
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000685/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
686/// given CallingConvention value.
687CCAssignFn *ARMTargetLowering::CCAssignFnForNode(unsigned CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000688 bool Return,
689 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000690 switch (CC) {
691 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000692 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000693 case CallingConv::C:
694 case CallingConv::Fast:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000695 // Use target triple & subtarget features to do actual dispatch.
696 if (Subtarget->isAAPCS_ABI()) {
697 if (Subtarget->hasVFP2() &&
698 FloatABIType == FloatABI::Hard && !isVarArg)
699 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
700 else
701 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
702 } else
703 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000704 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000705 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000706 case CallingConv::ARM_AAPCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000707 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000708 case CallingConv::ARM_APCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000709 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000710 }
711}
712
Dan Gohman98ca4f22009-08-05 01:29:28 +0000713/// LowerCallResult - Lower the result values of a call into the
714/// appropriate copies out of appropriate physical registers.
715SDValue
716ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
717 unsigned CallConv, bool isVarArg,
718 const SmallVectorImpl<ISD::InputArg> &Ins,
719 DebugLoc dl, SelectionDAG &DAG,
720 SmallVectorImpl<SDValue> &InVals) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000721
Bob Wilson1f595bb2009-04-17 19:07:39 +0000722 // Assign locations to each value returned by this call.
723 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000724 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000725 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000726 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000727 CCAssignFnForNode(CallConv, /* Return*/ true,
728 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000729
730 // Copy all of the result registers out of their specified physreg.
731 for (unsigned i = 0; i != RVLocs.size(); ++i) {
732 CCValAssign VA = RVLocs[i];
733
Bob Wilson80915242009-04-25 00:33:20 +0000734 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000735 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000736 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000737 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000738 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000739 Chain = Lo.getValue(1);
740 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000741 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000742 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000743 InFlag);
744 Chain = Hi.getValue(1);
745 InFlag = Hi.getValue(2);
Owen Anderson825b72b2009-08-11 20:47:22 +0000746 Val = DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +0000747
Owen Anderson825b72b2009-08-11 20:47:22 +0000748 if (VA.getLocVT() == MVT::v2f64) {
749 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
750 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
751 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000752
753 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000754 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000755 Chain = Lo.getValue(1);
756 InFlag = Lo.getValue(2);
757 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000758 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000759 Chain = Hi.getValue(1);
760 InFlag = Hi.getValue(2);
Owen Anderson825b72b2009-08-11 20:47:22 +0000761 Val = DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
762 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
763 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000764 }
Bob Wilson1f595bb2009-04-17 19:07:39 +0000765 } else {
Bob Wilson80915242009-04-25 00:33:20 +0000766 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
767 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000768 Chain = Val.getValue(1);
769 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000770 }
Bob Wilson80915242009-04-25 00:33:20 +0000771
772 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000773 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +0000774 case CCValAssign::Full: break;
775 case CCValAssign::BCvt:
776 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
777 break;
778 }
779
Dan Gohman98ca4f22009-08-05 01:29:28 +0000780 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000781 }
782
Dan Gohman98ca4f22009-08-05 01:29:28 +0000783 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000784}
785
786/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
787/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +0000788/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +0000789/// a byval function parameter.
790/// Sometimes what we are copying is the end of a larger object, the part that
791/// does not fit in registers.
792static SDValue
793CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
794 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
795 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000796 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000797 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
798 /*AlwaysInline=*/false, NULL, 0, NULL, 0);
799}
800
Bob Wilsondee46d72009-04-17 20:35:10 +0000801/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000802SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +0000803ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
804 SDValue StackPtr, SDValue Arg,
805 DebugLoc dl, SelectionDAG &DAG,
806 const CCValAssign &VA,
807 ISD::ArgFlagsTy Flags) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000808 unsigned LocMemOffset = VA.getLocMemOffset();
809 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
810 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
811 if (Flags.isByVal()) {
812 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
813 }
814 return DAG.getStore(Chain, dl, Arg, PtrOff,
815 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chenga8e29892007-01-19 07:51:42 +0000816}
817
Dan Gohman98ca4f22009-08-05 01:29:28 +0000818void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +0000819 SDValue Chain, SDValue &Arg,
820 RegsToPassVector &RegsToPass,
821 CCValAssign &VA, CCValAssign &NextVA,
822 SDValue &StackPtr,
823 SmallVector<SDValue, 8> &MemOpChains,
824 ISD::ArgFlagsTy Flags) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000825
826 SDValue fmrrd = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +0000827 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +0000828 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
829
830 if (NextVA.isRegLoc())
831 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
832 else {
833 assert(NextVA.isMemLoc());
834 if (StackPtr.getNode() == 0)
835 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
836
Dan Gohman98ca4f22009-08-05 01:29:28 +0000837 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
838 dl, DAG, NextVA,
839 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000840 }
841}
842
Dan Gohman98ca4f22009-08-05 01:29:28 +0000843/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +0000844/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
845/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000846SDValue
847ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
848 unsigned CallConv, bool isVarArg,
849 bool isTailCall,
850 const SmallVectorImpl<ISD::OutputArg> &Outs,
851 const SmallVectorImpl<ISD::InputArg> &Ins,
852 DebugLoc dl, SelectionDAG &DAG,
853 SmallVectorImpl<SDValue> &InVals) {
Evan Chenga8e29892007-01-19 07:51:42 +0000854
Bob Wilson1f595bb2009-04-17 19:07:39 +0000855 // Analyze operands of the call, assigning locations to each operand.
856 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000857 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
858 *DAG.getContext());
859 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000860 CCAssignFnForNode(CallConv, /* Return*/ false,
861 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +0000862
Bob Wilson1f595bb2009-04-17 19:07:39 +0000863 // Get a count of how many bytes are to be pushed on the stack.
864 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +0000865
866 // Adjust the stack pointer for the new arguments...
867 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattnere563bbc2008-10-11 22:08:30 +0000868 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +0000869
Owen Anderson825b72b2009-08-11 20:47:22 +0000870 SDValue StackPtr = DAG.getRegister(ARM::SP, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000871
Bob Wilson5bafff32009-06-22 23:27:02 +0000872 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000873 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +0000874
Bob Wilson1f595bb2009-04-17 19:07:39 +0000875 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +0000876 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000877 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
878 i != e;
879 ++i, ++realArgIdx) {
880 CCValAssign &VA = ArgLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +0000881 SDValue Arg = Outs[realArgIdx].Val;
882 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +0000883
Bob Wilson1f595bb2009-04-17 19:07:39 +0000884 // Promote the value if needed.
885 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000886 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +0000887 case CCValAssign::Full: break;
888 case CCValAssign::SExt:
889 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
890 break;
891 case CCValAssign::ZExt:
892 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
893 break;
894 case CCValAssign::AExt:
895 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
896 break;
897 case CCValAssign::BCvt:
898 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
899 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000900 }
901
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000902 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +0000903 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000904 if (VA.getLocVT() == MVT::v2f64) {
905 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
906 DAG.getConstant(0, MVT::i32));
907 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
908 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000909
Dan Gohman98ca4f22009-08-05 01:29:28 +0000910 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +0000911 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
912
913 VA = ArgLocs[++i]; // skip ahead to next loc
914 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000915 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +0000916 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
917 } else {
918 assert(VA.isMemLoc());
919 if (StackPtr.getNode() == 0)
920 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
921
Dan Gohman98ca4f22009-08-05 01:29:28 +0000922 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
923 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000924 }
925 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000926 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000927 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000928 }
929 } else if (VA.isRegLoc()) {
930 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
931 } else {
932 assert(VA.isMemLoc());
933 if (StackPtr.getNode() == 0)
934 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
935
Dan Gohman98ca4f22009-08-05 01:29:28 +0000936 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
937 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000938 }
Evan Chenga8e29892007-01-19 07:51:42 +0000939 }
940
941 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000942 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +0000943 &MemOpChains[0], MemOpChains.size());
944
945 // Build a sequence of copy-to-reg nodes chained together with token chain
946 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +0000947 SDValue InFlag;
Evan Chenga8e29892007-01-19 07:51:42 +0000948 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Bob Wilson2dc4f542009-03-20 22:42:55 +0000949 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000950 RegsToPass[i].second, InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +0000951 InFlag = Chain.getValue(1);
952 }
953
Bill Wendling056292f2008-09-16 21:48:12 +0000954 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
955 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
956 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +0000957 bool isDirect = false;
958 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +0000959 bool isLocalARMFunc = false;
Evan Chenga8e29892007-01-19 07:51:42 +0000960 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
961 GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +0000962 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +0000963 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +0000964 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +0000965 getTargetMachine().getRelocationModel() != Reloc::Static;
966 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +0000967 // ARM call to a local ARM function is predicable.
968 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
Evan Chengc60e76d2007-01-30 20:37:08 +0000969 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +0000970 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge4e4ed32009-08-28 23:18:09 +0000971 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
972 ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000973 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +0000974 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000975 Callee = DAG.getLoad(getPointerTy(), dl,
976 DAG.getEntryNode(), CPAddr, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000977 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000978 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000979 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +0000980 } else
981 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +0000982 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000983 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +0000984 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +0000985 getTargetMachine().getRelocationModel() != Reloc::Static;
986 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +0000987 // tBX takes a register source operand.
988 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +0000989 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000990 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +0000991 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000992 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +0000993 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000994 Callee = DAG.getLoad(getPointerTy(), dl,
Bob Wilson2dc4f542009-03-20 22:42:55 +0000995 DAG.getEntryNode(), CPAddr, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000996 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000997 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000998 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +0000999 } else
Bill Wendling056292f2008-09-16 21:48:12 +00001000 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001001 }
1002
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001003 // FIXME: handle tail calls differently.
1004 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001005 if (Subtarget->isThumb()) {
1006 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001007 CallOpc = ARMISD::CALL_NOLINK;
1008 else
1009 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1010 } else {
1011 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001012 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1013 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001014 }
David Goodwinf1daf7d2009-07-08 23:10:31 +00001015 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb1Only()) {
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +00001016 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Owen Anderson825b72b2009-08-11 20:47:22 +00001017 Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001018 InFlag = Chain.getValue(1);
1019 }
1020
Dan Gohman475871a2008-07-27 21:46:04 +00001021 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001022 Ops.push_back(Chain);
1023 Ops.push_back(Callee);
1024
1025 // Add argument registers to the end of the list so that they are known live
1026 // into the call.
1027 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1028 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1029 RegsToPass[i].second.getValueType()));
1030
Gabor Greifba36cb52008-08-28 21:40:38 +00001031 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001032 Ops.push_back(InFlag);
Duncan Sands4bdcb612008-07-02 17:40:58 +00001033 // Returns a chain and a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00001034 Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Flag),
Duncan Sands4bdcb612008-07-02 17:40:58 +00001035 &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001036 InFlag = Chain.getValue(1);
1037
Chris Lattnere563bbc2008-10-11 22:08:30 +00001038 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1039 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001040 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001041 InFlag = Chain.getValue(1);
1042
Bob Wilson1f595bb2009-04-17 19:07:39 +00001043 // Handle result values, copying them out of physregs into vregs that we
1044 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001045 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1046 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001047}
1048
Dan Gohman98ca4f22009-08-05 01:29:28 +00001049SDValue
1050ARMTargetLowering::LowerReturn(SDValue Chain,
1051 unsigned CallConv, bool isVarArg,
1052 const SmallVectorImpl<ISD::OutputArg> &Outs,
1053 DebugLoc dl, SelectionDAG &DAG) {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001054
Bob Wilsondee46d72009-04-17 20:35:10 +00001055 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001056 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001057
Bob Wilsondee46d72009-04-17 20:35:10 +00001058 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001059 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1060 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001061
Dan Gohman98ca4f22009-08-05 01:29:28 +00001062 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001063 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1064 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001065
1066 // If this is the first return lowered for this function, add
1067 // the regs to the liveout set for the function.
1068 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1069 for (unsigned i = 0; i != RVLocs.size(); ++i)
1070 if (RVLocs[i].isRegLoc())
1071 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001072 }
1073
Bob Wilson1f595bb2009-04-17 19:07:39 +00001074 SDValue Flag;
1075
1076 // Copy the result values into the output registers.
1077 for (unsigned i = 0, realRVLocIdx = 0;
1078 i != RVLocs.size();
1079 ++i, ++realRVLocIdx) {
1080 CCValAssign &VA = RVLocs[i];
1081 assert(VA.isRegLoc() && "Can only return in registers!");
1082
Dan Gohman98ca4f22009-08-05 01:29:28 +00001083 SDValue Arg = Outs[realRVLocIdx].Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001084
1085 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001086 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001087 case CCValAssign::Full: break;
1088 case CCValAssign::BCvt:
1089 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1090 break;
1091 }
1092
Bob Wilson1f595bb2009-04-17 19:07:39 +00001093 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001094 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001095 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001096 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1097 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001098 SDValue HalfGPRs = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001099 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001100
1101 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1102 Flag = Chain.getValue(1);
1103 VA = RVLocs[++i]; // skip ahead to next loc
1104 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1105 HalfGPRs.getValue(1), Flag);
1106 Flag = Chain.getValue(1);
1107 VA = RVLocs[++i]; // skip ahead to next loc
1108
1109 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001110 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1111 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001112 }
1113 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1114 // available.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001115 SDValue fmrrd = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001116 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001117 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001118 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001119 VA = RVLocs[++i]; // skip ahead to next loc
1120 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1121 Flag);
1122 } else
1123 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1124
Bob Wilsondee46d72009-04-17 20:35:10 +00001125 // Guarantee that all emitted copies are
1126 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001127 Flag = Chain.getValue(1);
1128 }
1129
1130 SDValue result;
1131 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001132 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001133 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001134 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001135
1136 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001137}
1138
Bob Wilson2dc4f542009-03-20 22:42:55 +00001139// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
Bob Wilsond2559bf2009-07-13 18:11:36 +00001140// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
Bill Wendling056292f2008-09-16 21:48:12 +00001141// one of the above mentioned nodes. It has to be wrapped because otherwise
1142// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1143// be used to form addressing mode. These wrapped nodes will be selected
1144// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001145static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001146 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001147 // FIXME there is no actual debug info here
1148 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001149 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001150 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001151 if (CP->isMachineConstantPoolEntry())
1152 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1153 CP->getAlignment());
1154 else
1155 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1156 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001157 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001158}
1159
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001160// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001161SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001162ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
1163 SelectionDAG &DAG) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001164 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001165 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001166 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1167 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001168 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001169 PCAdj, "tlsgd", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001170 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001171 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001172 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001173 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001174
Owen Anderson825b72b2009-08-11 20:47:22 +00001175 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001176 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001177
1178 // call __tls_get_addr.
1179 ArgListTy Args;
1180 ArgListEntry Entry;
1181 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001182 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001183 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001184 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001185 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001186 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1187 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001188 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001189 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001190 return CallResult.first;
1191}
1192
1193// Lower ISD::GlobalTLSAddress using the "initial exec" or
1194// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001195SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001196ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001197 SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001198 GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001199 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001200 SDValue Offset;
1201 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001202 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001203 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001204 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001205
Chris Lattner4fb63d02009-07-15 04:12:33 +00001206 if (GV->isDeclaration()) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001207 // initial exec model
1208 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1209 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001210 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001211 PCAdj, "gottpoff", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001212 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001213 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001214 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001215 Chain = Offset.getValue(1);
1216
Owen Anderson825b72b2009-08-11 20:47:22 +00001217 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001218 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001219
Dale Johannesen33c960f2009-02-04 20:06:27 +00001220 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001221 } else {
1222 // local exec model
Evan Chenge4e4ed32009-08-28 23:18:09 +00001223 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, "tpoff");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001224 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001225 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001226 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001227 }
1228
1229 // The address of the thread local variable is the add of the thread
1230 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001231 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001232}
1233
Dan Gohman475871a2008-07-27 21:46:04 +00001234SDValue
1235ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001236 // TODO: implement the "local dynamic" model
1237 assert(Subtarget->isTargetELF() &&
1238 "TLS not implemented for non-ELF targets");
1239 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1240 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1241 // otherwise use the "Local Exec" TLS Model
1242 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1243 return LowerToTLSGeneralDynamicModel(GA, DAG);
1244 else
1245 return LowerToTLSExecModels(GA, DAG);
1246}
1247
Dan Gohman475871a2008-07-27 21:46:04 +00001248SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001249 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001250 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001251 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001252 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1253 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1254 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001255 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001256 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001257 new ARMConstantPoolValue(GV, UseGOTOFF ? "GOTOFF" : "GOT");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001258 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001259 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001260 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Dale Johannesen33c960f2009-02-04 20:06:27 +00001261 CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001262 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001263 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001264 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001265 if (!UseGOTOFF)
Dale Johannesen33c960f2009-02-04 20:06:27 +00001266 Result = DAG.getLoad(PtrVT, dl, Chain, Result, NULL, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001267 return Result;
1268 } else {
Evan Cheng1606e8e2009-03-13 07:51:59 +00001269 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001270 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001271 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001272 }
1273}
1274
Dan Gohman475871a2008-07-27 21:46:04 +00001275SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001276 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001277 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001278 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001279 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1280 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Dan Gohman475871a2008-07-27 21:46:04 +00001281 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +00001282 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001283 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001284 else {
Evan Chenge4e4ed32009-08-28 23:18:09 +00001285 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
1286 ARMConstantPoolValue *CPV =
1287 new ARMConstantPoolValue(GV, ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001288 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001289 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001290 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001291
Dale Johannesen33c960f2009-02-04 20:06:27 +00001292 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001293 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001294
1295 if (RelocM == Reloc::PIC_) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001296 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001297 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001298 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00001299
1300 if (Subtarget->GVIsIndirectSymbol(GV, RelocM == Reloc::Static))
Dale Johannesen33c960f2009-02-04 20:06:27 +00001301 Result = DAG.getLoad(PtrVT, dl, Chain, Result, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001302
1303 return Result;
1304}
1305
Dan Gohman475871a2008-07-27 21:46:04 +00001306SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001307 SelectionDAG &DAG){
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001308 assert(Subtarget->isTargetELF() &&
1309 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Owen Andersone50ed302009-08-10 22:56:29 +00001310 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001311 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001312 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00001313 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1314 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00001315 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001316 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001317 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001318 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001319 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001320 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001321}
1322
Bob Wilsona599bff2009-08-04 00:36:16 +00001323static SDValue LowerNeonVLDIntrinsic(SDValue Op, SelectionDAG &DAG,
Bob Wilson31fb12f2009-08-26 17:39:53 +00001324 unsigned NumVecs) {
Bob Wilsona599bff2009-08-04 00:36:16 +00001325 SDNode *Node = Op.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001326 EVT VT = Node->getValueType(0);
Bob Wilsona599bff2009-08-04 00:36:16 +00001327
Bob Wilson31fb12f2009-08-26 17:39:53 +00001328 // No expansion needed for 64-bit vectors.
1329 if (VT.is64BitVector())
1330 return SDValue();
Bob Wilsona599bff2009-08-04 00:36:16 +00001331
Bob Wilson31fb12f2009-08-26 17:39:53 +00001332 // FIXME: We need to expand VLD3 and VLD4 of 128-bit vectors into separate
1333 // operations to load the even and odd registers.
1334 return SDValue();
Bob Wilsona599bff2009-08-04 00:36:16 +00001335}
1336
Bob Wilsonb36ec862009-08-06 18:47:44 +00001337static SDValue LowerNeonVSTIntrinsic(SDValue Op, SelectionDAG &DAG,
Bob Wilson31fb12f2009-08-26 17:39:53 +00001338 unsigned NumVecs) {
Bob Wilsonb36ec862009-08-06 18:47:44 +00001339 SDNode *Node = Op.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001340 EVT VT = Node->getOperand(3).getValueType();
Bob Wilsonb36ec862009-08-06 18:47:44 +00001341
Bob Wilson31fb12f2009-08-26 17:39:53 +00001342 // No expansion needed for 64-bit vectors.
1343 if (VT.is64BitVector())
1344 return SDValue();
Bob Wilsonb36ec862009-08-06 18:47:44 +00001345
Bob Wilson31fb12f2009-08-26 17:39:53 +00001346 // FIXME: We need to expand VST3 and VST4 of 128-bit vectors into separate
1347 // operations to store the even and odd registers.
1348 return SDValue();
Bob Wilsonb36ec862009-08-06 18:47:44 +00001349}
1350
Bob Wilsona599bff2009-08-04 00:36:16 +00001351SDValue
1352ARMTargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) {
1353 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1354 switch (IntNo) {
Bob Wilsonb0abb4d2009-08-11 05:39:44 +00001355 case Intrinsic::arm_neon_vld3:
Bob Wilson31fb12f2009-08-26 17:39:53 +00001356 return LowerNeonVLDIntrinsic(Op, DAG, 3);
Bob Wilsonb0abb4d2009-08-11 05:39:44 +00001357 case Intrinsic::arm_neon_vld4:
Bob Wilson31fb12f2009-08-26 17:39:53 +00001358 return LowerNeonVLDIntrinsic(Op, DAG, 4);
Bob Wilsonb0abb4d2009-08-11 05:39:44 +00001359 case Intrinsic::arm_neon_vst3:
Bob Wilson31fb12f2009-08-26 17:39:53 +00001360 return LowerNeonVSTIntrinsic(Op, DAG, 3);
Bob Wilsonb0abb4d2009-08-11 05:39:44 +00001361 case Intrinsic::arm_neon_vst4:
Bob Wilson31fb12f2009-08-26 17:39:53 +00001362 return LowerNeonVSTIntrinsic(Op, DAG, 4);
Bob Wilsona599bff2009-08-04 00:36:16 +00001363 default: return SDValue(); // Don't custom lower most intrinsics.
1364 }
1365}
1366
Jim Grosbach0e0da732009-05-12 23:59:14 +00001367SDValue
1368ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001369 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001370 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001371 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001372 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00001373 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00001374 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00001375 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1376 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001377 case Intrinsic::eh_sjlj_lsda: {
1378 // blah. horrible, horrible hack with the forced magic name.
1379 // really need to clean this up. It belongs in the target-independent
1380 // layer somehow that doesn't require the coupling with the asm
1381 // printer.
1382 MachineFunction &MF = DAG.getMachineFunction();
1383 EVT PtrVT = getPointerTy();
1384 DebugLoc dl = Op.getDebugLoc();
1385 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1386 SDValue CPAddr;
1387 unsigned PCAdj = (RelocM != Reloc::PIC_)
1388 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001389 // Save off the LSDA name for the AsmPrinter to use when it's time
1390 // to emit the table
1391 std::string LSDAName = "L_lsda_";
1392 LSDAName += MF.getFunction()->getName();
1393 ARMConstantPoolValue *CPV =
Owen Anderson1d0be152009-08-13 21:58:54 +00001394 new ARMConstantPoolValue(*DAG.getContext(), LSDAName.c_str(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001395 ARMPCLabelIndex, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001396 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001397 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001398 SDValue Result =
1399 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
1400 SDValue Chain = Result.getValue(1);
1401
1402 if (RelocM == Reloc::PIC_) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001403 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001404 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1405 }
1406 return Result;
1407 }
Jim Grosbachf9570122009-05-14 00:46:35 +00001408 case Intrinsic::eh_sjlj_setjmp:
Owen Anderson825b72b2009-08-11 20:47:22 +00001409 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(1));
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001410 }
1411}
1412
Dan Gohman475871a2008-07-27 21:46:04 +00001413static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001414 unsigned VarArgsFrameIndex) {
Evan Chenga8e29892007-01-19 07:51:42 +00001415 // vastart just stores the address of the VarArgsFrameIndex slot into the
1416 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001417 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001418 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001419 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001420 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001421 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001422}
1423
Dan Gohman475871a2008-07-27 21:46:04 +00001424SDValue
Evan Cheng86198642009-08-07 00:34:42 +00001425ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) {
1426 SDNode *Node = Op.getNode();
1427 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001428 EVT VT = Node->getValueType(0);
Evan Cheng86198642009-08-07 00:34:42 +00001429 SDValue Chain = Op.getOperand(0);
1430 SDValue Size = Op.getOperand(1);
1431 SDValue Align = Op.getOperand(2);
1432
1433 // Chain the dynamic stack allocation so that it doesn't modify the stack
1434 // pointer when other instructions are using the stack.
1435 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1436
1437 unsigned AlignVal = cast<ConstantSDNode>(Align)->getZExtValue();
1438 unsigned StackAlign = getTargetMachine().getFrameInfo()->getStackAlignment();
1439 if (AlignVal > StackAlign)
1440 // Do this now since selection pass cannot introduce new target
1441 // independent node.
1442 Align = DAG.getConstant(-(uint64_t)AlignVal, VT);
1443
1444 // In Thumb1 mode, there isn't a "sub r, sp, r" instruction, we will end up
1445 // using a "add r, sp, r" instead. Negate the size now so we don't have to
1446 // do even more horrible hack later.
1447 MachineFunction &MF = DAG.getMachineFunction();
1448 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1449 if (AFI->isThumb1OnlyFunction()) {
1450 bool Negate = true;
1451 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Size);
1452 if (C) {
1453 uint32_t Val = C->getZExtValue();
1454 if (Val <= 508 && ((Val & 3) == 0))
1455 Negate = false;
1456 }
1457 if (Negate)
1458 Size = DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(0, VT), Size);
1459 }
1460
Owen Anderson825b72b2009-08-11 20:47:22 +00001461 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
Evan Cheng86198642009-08-07 00:34:42 +00001462 SDValue Ops1[] = { Chain, Size, Align };
1463 SDValue Res = DAG.getNode(ARMISD::DYN_ALLOC, dl, VTList, Ops1, 3);
1464 Chain = Res.getValue(1);
1465 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1466 DAG.getIntPtrConstant(0, true), SDValue());
1467 SDValue Ops2[] = { Res, Chain };
1468 return DAG.getMergeValues(Ops2, 2, dl);
1469}
1470
1471SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00001472ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
1473 SDValue &Root, SelectionDAG &DAG,
1474 DebugLoc dl) {
1475 MachineFunction &MF = DAG.getMachineFunction();
1476 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1477
1478 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001479 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00001480 RC = ARM::tGPRRegisterClass;
1481 else
1482 RC = ARM::GPRRegisterClass;
1483
1484 // Transform the arguments stored in physical registers into virtual ones.
1485 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001486 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001487
1488 SDValue ArgValue2;
1489 if (NextVA.isMemLoc()) {
1490 unsigned ArgSize = NextVA.getLocVT().getSizeInBits()/8;
1491 MachineFrameInfo *MFI = MF.getFrameInfo();
1492 int FI = MFI->CreateFixedObject(ArgSize, NextVA.getLocMemOffset());
1493
1494 // Create load node to retrieve arguments from the stack.
1495 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00001496 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN, NULL, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00001497 } else {
1498 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001499 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001500 }
1501
Owen Anderson825b72b2009-08-11 20:47:22 +00001502 return DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00001503}
1504
1505SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001506ARMTargetLowering::LowerFormalArguments(SDValue Chain,
1507 unsigned CallConv, bool isVarArg,
1508 const SmallVectorImpl<ISD::InputArg>
1509 &Ins,
1510 DebugLoc dl, SelectionDAG &DAG,
1511 SmallVectorImpl<SDValue> &InVals) {
1512
Bob Wilson1f595bb2009-04-17 19:07:39 +00001513 MachineFunction &MF = DAG.getMachineFunction();
1514 MachineFrameInfo *MFI = MF.getFrameInfo();
1515
Bob Wilson1f595bb2009-04-17 19:07:39 +00001516 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1517
1518 // Assign locations to all of the incoming arguments.
1519 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001520 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1521 *DAG.getContext());
1522 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001523 CCAssignFnForNode(CallConv, /* Return*/ false,
1524 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001525
1526 SmallVector<SDValue, 16> ArgValues;
1527
1528 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1529 CCValAssign &VA = ArgLocs[i];
1530
Bob Wilsondee46d72009-04-17 20:35:10 +00001531 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001532 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001533 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00001534
Bob Wilson5bafff32009-06-22 23:27:02 +00001535 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001536 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001537 // f64 and vector types are split up into multiple registers or
1538 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00001539 RegVT = MVT::i32;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001540
Owen Anderson825b72b2009-08-11 20:47:22 +00001541 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001542 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001543 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00001544 VA = ArgLocs[++i]; // skip ahead to next loc
1545 SDValue ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001546 Chain, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001547 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1548 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001549 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00001550 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001551 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
1552 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001553 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001554
Bob Wilson5bafff32009-06-22 23:27:02 +00001555 } else {
1556 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001557
Owen Anderson825b72b2009-08-11 20:47:22 +00001558 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00001559 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001560 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00001561 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001562 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001563 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001564 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001565 RC = (AFI->isThumb1OnlyFunction() ?
1566 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00001567 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001568 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00001569
1570 // Transform the arguments in physical registers into virtual ones.
1571 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001572 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001573 }
1574
1575 // If this is an 8 or 16-bit value, it is really passed promoted
1576 // to 32 bits. Insert an assert[sz]ext to capture this, then
1577 // truncate to the right size.
1578 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001579 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001580 case CCValAssign::Full: break;
1581 case CCValAssign::BCvt:
1582 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1583 break;
1584 case CCValAssign::SExt:
1585 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
1586 DAG.getValueType(VA.getValVT()));
1587 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1588 break;
1589 case CCValAssign::ZExt:
1590 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
1591 DAG.getValueType(VA.getValVT()));
1592 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1593 break;
1594 }
1595
Dan Gohman98ca4f22009-08-05 01:29:28 +00001596 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001597
1598 } else { // VA.isRegLoc()
1599
1600 // sanity check
1601 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00001602 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001603
1604 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
1605 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset());
1606
Bob Wilsondee46d72009-04-17 20:35:10 +00001607 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001608 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001609 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN, NULL, 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001610 }
1611 }
1612
1613 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00001614 if (isVarArg) {
1615 static const unsigned GPRArgRegs[] = {
1616 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1617 };
1618
Bob Wilsondee46d72009-04-17 20:35:10 +00001619 unsigned NumGPRs = CCInfo.getFirstUnallocated
1620 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001621
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001622 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1623 unsigned VARegSize = (4 - NumGPRs) * 4;
1624 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001625 unsigned ArgOffset = 0;
Evan Chenga8e29892007-01-19 07:51:42 +00001626 if (VARegSaveSize) {
1627 // If this function is vararg, store any remaining integer argument regs
1628 // to their spots on the stack so that they may be loaded by deferencing
1629 // the result of va_next.
1630 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001631 ArgOffset = CCInfo.getNextStackOffset();
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001632 VarArgsFrameIndex = MFI->CreateFixedObject(VARegSaveSize, ArgOffset +
1633 VARegSaveSize - VARegSize);
Dan Gohman475871a2008-07-27 21:46:04 +00001634 SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001635
Dan Gohman475871a2008-07-27 21:46:04 +00001636 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00001637 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001638 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001639 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00001640 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001641 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00001642 RC = ARM::GPRRegisterClass;
1643
Bob Wilson998e1252009-04-20 18:36:57 +00001644 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001645 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001646 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001647 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001648 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00001649 DAG.getConstant(4, getPointerTy()));
1650 }
1651 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001652 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001653 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001654 } else
1655 // This will point to the next argument passed via stack.
1656 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1657 }
1658
Dan Gohman98ca4f22009-08-05 01:29:28 +00001659 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00001660}
1661
1662/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00001663static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00001664 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001665 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00001666 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00001667 // Maybe this has already been legalized into the constant pool?
1668 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00001669 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001670 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
1671 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001672 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001673 }
1674 }
1675 return false;
1676}
1677
David Goodwinf1daf7d2009-07-08 23:10:31 +00001678static bool isLegalCmpImmediate(unsigned C, bool isThumb1Only) {
1679 return ( isThumb1Only && (C & ~255U) == 0) ||
1680 (!isThumb1Only && ARM_AM::getSOImmVal(C) != -1);
Evan Chenga8e29892007-01-19 07:51:42 +00001681}
1682
1683/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1684/// the given operands.
Dan Gohman475871a2008-07-27 21:46:04 +00001685static SDValue getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
David Goodwinf1daf7d2009-07-08 23:10:31 +00001686 SDValue &ARMCC, SelectionDAG &DAG, bool isThumb1Only,
Dale Johannesende064702009-02-06 21:50:26 +00001687 DebugLoc dl) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001688 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001689 unsigned C = RHSC->getZExtValue();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001690 if (!isLegalCmpImmediate(C, isThumb1Only)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001691 // Constant does not fit, try adjusting it by one?
1692 switch (CC) {
1693 default: break;
1694 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00001695 case ISD::SETGE:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001696 if (isLegalCmpImmediate(C-1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001697 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001698 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001699 }
1700 break;
1701 case ISD::SETULT:
1702 case ISD::SETUGE:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001703 if (C > 0 && isLegalCmpImmediate(C-1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001704 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001705 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001706 }
1707 break;
1708 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00001709 case ISD::SETGT:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001710 if (isLegalCmpImmediate(C+1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001711 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001712 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001713 }
1714 break;
1715 case ISD::SETULE:
1716 case ISD::SETUGT:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001717 if (C < 0xffffffff && isLegalCmpImmediate(C+1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001718 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001719 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001720 }
1721 break;
1722 }
1723 }
1724 }
1725
1726 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001727 ARMISD::NodeType CompareType;
1728 switch (CondCode) {
1729 default:
1730 CompareType = ARMISD::CMP;
1731 break;
1732 case ARMCC::EQ:
1733 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00001734 // Uses only Z Flag
1735 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001736 break;
1737 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001738 ARMCC = DAG.getConstant(CondCode, MVT::i32);
1739 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001740}
1741
1742/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001743static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Dale Johannesende064702009-02-06 21:50:26 +00001744 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001745 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00001746 if (!isFloatingPointZero(RHS))
Owen Anderson825b72b2009-08-11 20:47:22 +00001747 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001748 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001749 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
1750 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001751}
1752
Dan Gohman475871a2008-07-27 21:46:04 +00001753static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001754 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00001755 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001756 SDValue LHS = Op.getOperand(0);
1757 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001758 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001759 SDValue TrueVal = Op.getOperand(2);
1760 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00001761 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001762
Owen Anderson825b72b2009-08-11 20:47:22 +00001763 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001764 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001765 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
David Goodwinf1daf7d2009-07-08 23:10:31 +00001766 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb1Only(), dl);
Dale Johannesende064702009-02-06 21:50:26 +00001767 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001768 }
1769
1770 ARMCC::CondCodes CondCode, CondCode2;
1771 if (FPCCToARMCC(CC, CondCode, CondCode2))
1772 std::swap(TrueVal, FalseVal);
1773
Owen Anderson825b72b2009-08-11 20:47:22 +00001774 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1775 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001776 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
1777 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00001778 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001779 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001780 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001781 // FIXME: Needs another CMP because flag can have but one use.
Dale Johannesende064702009-02-06 21:50:26 +00001782 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001783 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Dale Johannesende064702009-02-06 21:50:26 +00001784 Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00001785 }
1786 return Result;
1787}
1788
Dan Gohman475871a2008-07-27 21:46:04 +00001789static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001790 const ARMSubtarget *ST) {
Dan Gohman475871a2008-07-27 21:46:04 +00001791 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001792 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001793 SDValue LHS = Op.getOperand(2);
1794 SDValue RHS = Op.getOperand(3);
1795 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00001796 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001797
Owen Anderson825b72b2009-08-11 20:47:22 +00001798 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001799 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001800 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
David Goodwinf1daf7d2009-07-08 23:10:31 +00001801 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb1Only(), dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001802 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Dale Johannesende064702009-02-06 21:50:26 +00001803 Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001804 }
1805
Owen Anderson825b72b2009-08-11 20:47:22 +00001806 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Chenga8e29892007-01-19 07:51:42 +00001807 ARMCC::CondCodes CondCode, CondCode2;
1808 if (FPCCToARMCC(CC, CondCode, CondCode2))
1809 // Swap the LHS/RHS of the comparison if needed.
1810 std::swap(LHS, RHS);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001811
Dale Johannesende064702009-02-06 21:50:26 +00001812 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001813 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1814 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1815 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00001816 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00001817 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001818 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001819 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00001820 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00001821 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001822 }
1823 return Res;
1824}
1825
Dan Gohman475871a2008-07-27 21:46:04 +00001826SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) {
1827 SDValue Chain = Op.getOperand(0);
1828 SDValue Table = Op.getOperand(1);
1829 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001830 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001831
Owen Andersone50ed302009-08-10 22:56:29 +00001832 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00001833 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
1834 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00001835 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00001836 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00001837 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00001838 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
1839 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00001840 if (Subtarget->isThumb2()) {
1841 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
1842 // which does another jump to the destination. This also makes it easier
1843 // to translate it to TBB / TBH later.
1844 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00001845 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00001846 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001847 }
Evan Cheng66ac5312009-07-25 00:33:29 +00001848 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001849 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr, NULL, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00001850 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001851 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00001852 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001853 } else {
1854 Addr = DAG.getLoad(PTy, dl, Chain, Addr, NULL, 0);
1855 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00001856 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001857 }
Evan Chenga8e29892007-01-19 07:51:42 +00001858}
1859
Dan Gohman475871a2008-07-27 21:46:04 +00001860static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Dale Johannesende064702009-02-06 21:50:26 +00001861 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001862 unsigned Opc =
1863 Op.getOpcode() == ISD::FP_TO_SINT ? ARMISD::FTOSI : ARMISD::FTOUI;
Owen Anderson825b72b2009-08-11 20:47:22 +00001864 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
1865 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001866}
1867
Dan Gohman475871a2008-07-27 21:46:04 +00001868static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001869 EVT VT = Op.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001870 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001871 unsigned Opc =
1872 Op.getOpcode() == ISD::SINT_TO_FP ? ARMISD::SITOF : ARMISD::UITOF;
1873
Owen Anderson825b72b2009-08-11 20:47:22 +00001874 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
Dale Johannesende064702009-02-06 21:50:26 +00001875 return DAG.getNode(Opc, dl, VT, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001876}
1877
Dan Gohman475871a2008-07-27 21:46:04 +00001878static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00001879 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00001880 SDValue Tmp0 = Op.getOperand(0);
1881 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00001882 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001883 EVT VT = Op.getValueType();
1884 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001885 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
1886 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001887 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
1888 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001889 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001890}
1891
Jim Grosbach0e0da732009-05-12 23:59:14 +00001892SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
1893 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1894 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00001895 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001896 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
1897 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00001898 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00001899 ? ARM::R7 : ARM::R11;
1900 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
1901 while (Depth--)
1902 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
1903 return FrameAddr;
1904}
1905
Dan Gohman475871a2008-07-27 21:46:04 +00001906SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00001907ARMTargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman475871a2008-07-27 21:46:04 +00001908 SDValue Chain,
1909 SDValue Dst, SDValue Src,
1910 SDValue Size, unsigned Align,
Dan Gohman707e0182008-04-12 04:36:06 +00001911 bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00001912 const Value *DstSV, uint64_t DstSVOff,
1913 const Value *SrcSV, uint64_t SrcSVOff){
Evan Cheng4102eb52007-10-22 22:11:27 +00001914 // Do repeated 4-byte loads and stores. To be improved.
Dan Gohman707e0182008-04-12 04:36:06 +00001915 // This requires 4-byte alignment.
1916 if ((Align & 3) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00001917 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001918 // This requires the copy size to be a constant, preferrably
1919 // within a subtarget-specific limit.
1920 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
1921 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00001922 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001923 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001924 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00001925 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001926
1927 unsigned BytesLeft = SizeVal & 3;
1928 unsigned NumMemOps = SizeVal >> 2;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001929 unsigned EmittedNumMemOps = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001930 EVT VT = MVT::i32;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001931 unsigned VTSize = 4;
Evan Cheng4102eb52007-10-22 22:11:27 +00001932 unsigned i = 0;
Evan Chenge5e7ce42007-05-18 01:19:57 +00001933 const unsigned MAX_LOADS_IN_LDM = 6;
Dan Gohman475871a2008-07-27 21:46:04 +00001934 SDValue TFOps[MAX_LOADS_IN_LDM];
1935 SDValue Loads[MAX_LOADS_IN_LDM];
Dan Gohman1f13c682008-04-28 17:15:20 +00001936 uint64_t SrcOff = 0, DstOff = 0;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001937
Evan Cheng4102eb52007-10-22 22:11:27 +00001938 // Emit up to MAX_LOADS_IN_LDM loads, then a TokenFactor barrier, then the
1939 // same number of stores. The loads and stores will get combined into
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001940 // ldm/stm later on.
Evan Cheng4102eb52007-10-22 22:11:27 +00001941 while (EmittedNumMemOps < NumMemOps) {
1942 for (i = 0;
1943 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00001944 Loads[i] = DAG.getLoad(VT, dl, Chain,
Owen Anderson825b72b2009-08-11 20:47:22 +00001945 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
1946 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001947 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00001948 TFOps[i] = Loads[i].getValue(1);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001949 SrcOff += VTSize;
1950 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001951 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001952
Evan Cheng4102eb52007-10-22 22:11:27 +00001953 for (i = 0;
1954 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00001955 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Owen Anderson825b72b2009-08-11 20:47:22 +00001956 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
1957 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001958 DstSV, DstSVOff + DstOff);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001959 DstOff += VTSize;
1960 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001961 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00001962
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001963 EmittedNumMemOps += i;
1964 }
1965
Bob Wilson2dc4f542009-03-20 22:42:55 +00001966 if (BytesLeft == 0)
Evan Cheng4102eb52007-10-22 22:11:27 +00001967 return Chain;
1968
1969 // Issue loads / stores for the trailing (1 - 3) bytes.
1970 unsigned BytesLeftSave = BytesLeft;
1971 i = 0;
1972 while (BytesLeft) {
1973 if (BytesLeft >= 2) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001974 VT = MVT::i16;
Evan Cheng4102eb52007-10-22 22:11:27 +00001975 VTSize = 2;
1976 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001977 VT = MVT::i8;
Evan Cheng4102eb52007-10-22 22:11:27 +00001978 VTSize = 1;
1979 }
1980
Dale Johannesen0f502f62009-02-03 22:26:09 +00001981 Loads[i] = DAG.getLoad(VT, dl, Chain,
Owen Anderson825b72b2009-08-11 20:47:22 +00001982 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
1983 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001984 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00001985 TFOps[i] = Loads[i].getValue(1);
1986 ++i;
1987 SrcOff += VTSize;
1988 BytesLeft -= VTSize;
1989 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001990 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00001991
1992 i = 0;
1993 BytesLeft = BytesLeftSave;
1994 while (BytesLeft) {
1995 if (BytesLeft >= 2) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001996 VT = MVT::i16;
Evan Cheng4102eb52007-10-22 22:11:27 +00001997 VTSize = 2;
1998 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001999 VT = MVT::i8;
Evan Cheng4102eb52007-10-22 22:11:27 +00002000 VTSize = 1;
2001 }
2002
Dale Johannesen0f502f62009-02-03 22:26:09 +00002003 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Owen Anderson825b72b2009-08-11 20:47:22 +00002004 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
2005 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00002006 DstSV, DstSVOff + DstOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00002007 ++i;
2008 DstOff += VTSize;
2009 BytesLeft -= VTSize;
2010 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002011 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002012}
2013
Duncan Sands1607f052008-12-01 11:39:25 +00002014static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00002015 SDValue Op = N->getOperand(0);
Dale Johannesende064702009-02-06 21:50:26 +00002016 DebugLoc dl = N->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00002017 if (N->getValueType(0) == MVT::f64) {
Evan Chengc7c77292008-11-04 19:57:48 +00002018 // Turn i64->f64 into FMDRR.
Owen Anderson825b72b2009-08-11 20:47:22 +00002019 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2020 DAG.getConstant(0, MVT::i32));
2021 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2022 DAG.getConstant(1, MVT::i32));
2023 return DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
Evan Chengc7c77292008-11-04 19:57:48 +00002024 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002025
Evan Chengc7c77292008-11-04 19:57:48 +00002026 // Turn f64->i64 into FMRRD.
Bob Wilson2dc4f542009-03-20 22:42:55 +00002027 SDValue Cvt = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00002028 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002029
Chris Lattner27a6c732007-11-24 07:07:01 +00002030 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002031 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
Chris Lattner27a6c732007-11-24 07:07:01 +00002032}
2033
Bob Wilson5bafff32009-06-22 23:27:02 +00002034/// getZeroVector - Returns a vector of specified type with all zero elements.
2035///
Owen Andersone50ed302009-08-10 22:56:29 +00002036static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002037 assert(VT.isVector() && "Expected a vector type");
2038
2039 // Zero vectors are used to represent vector negation and in those cases
2040 // will be implemented with the NEON VNEG instruction. However, VNEG does
2041 // not support i64 elements, so sometimes the zero vectors will need to be
2042 // explicitly constructed. For those cases, and potentially other uses in
2043 // the future, always build zero vectors as <4 x i32> or <2 x i32> bitcasted
2044 // to their dest type. This ensures they get CSE'd.
2045 SDValue Vec;
Owen Anderson825b72b2009-08-11 20:47:22 +00002046 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002047 if (VT.getSizeInBits() == 64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002048 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Bob Wilson5bafff32009-06-22 23:27:02 +00002049 else
Owen Anderson825b72b2009-08-11 20:47:22 +00002050 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Bob Wilson5bafff32009-06-22 23:27:02 +00002051
2052 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2053}
2054
2055/// getOnesVector - Returns a vector of specified type with all bits set.
2056///
Owen Andersone50ed302009-08-10 22:56:29 +00002057static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002058 assert(VT.isVector() && "Expected a vector type");
2059
2060 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2061 // type. This ensures they get CSE'd.
2062 SDValue Vec;
Owen Anderson825b72b2009-08-11 20:47:22 +00002063 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002064 if (VT.getSizeInBits() == 64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002065 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Bob Wilson5bafff32009-06-22 23:27:02 +00002066 else
Owen Anderson825b72b2009-08-11 20:47:22 +00002067 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Bob Wilson5bafff32009-06-22 23:27:02 +00002068
2069 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2070}
2071
2072static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2073 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00002074 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002075 DebugLoc dl = N->getDebugLoc();
2076
2077 // Lower vector shifts on NEON to use VSHL.
2078 if (VT.isVector()) {
2079 assert(ST->hasNEON() && "unexpected vector shift");
2080
2081 // Left shifts translate directly to the vshiftu intrinsic.
2082 if (N->getOpcode() == ISD::SHL)
2083 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002084 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002085 N->getOperand(0), N->getOperand(1));
2086
2087 assert((N->getOpcode() == ISD::SRA ||
2088 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2089
2090 // NEON uses the same intrinsics for both left and right shifts. For
2091 // right shifts, the shift amounts are negative, so negate the vector of
2092 // shift amounts.
Owen Andersone50ed302009-08-10 22:56:29 +00002093 EVT ShiftVT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002094 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2095 getZeroVector(ShiftVT, DAG, dl),
2096 N->getOperand(1));
2097 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2098 Intrinsic::arm_neon_vshifts :
2099 Intrinsic::arm_neon_vshiftu);
2100 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002101 DAG.getConstant(vshiftInt, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002102 N->getOperand(0), NegatedCount);
2103 }
2104
Eli Friedmance392eb2009-08-22 03:13:10 +00002105 // We can get here for a node like i32 = ISD::SHL i32, i64
2106 if (VT != MVT::i64)
2107 return SDValue();
2108
2109 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00002110 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00002111
Chris Lattner27a6c732007-11-24 07:07:01 +00002112 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2113 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002114 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00002115 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002116
Chris Lattner27a6c732007-11-24 07:07:01 +00002117 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00002118 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002119
Chris Lattner27a6c732007-11-24 07:07:01 +00002120 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00002121 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2122 DAG.getConstant(0, MVT::i32));
2123 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2124 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002125
Chris Lattner27a6c732007-11-24 07:07:01 +00002126 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2127 // captures the result into a carry flag.
2128 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Owen Anderson825b72b2009-08-11 20:47:22 +00002129 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002130
Chris Lattner27a6c732007-11-24 07:07:01 +00002131 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00002132 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002133
Chris Lattner27a6c732007-11-24 07:07:01 +00002134 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002135 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00002136}
2137
Bob Wilson5bafff32009-06-22 23:27:02 +00002138static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2139 SDValue TmpOp0, TmpOp1;
2140 bool Invert = false;
2141 bool Swap = false;
2142 unsigned Opc = 0;
2143
2144 SDValue Op0 = Op.getOperand(0);
2145 SDValue Op1 = Op.getOperand(1);
2146 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00002147 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002148 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2149 DebugLoc dl = Op.getDebugLoc();
2150
2151 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2152 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002153 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002154 case ISD::SETUNE:
2155 case ISD::SETNE: Invert = true; // Fallthrough
2156 case ISD::SETOEQ:
2157 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2158 case ISD::SETOLT:
2159 case ISD::SETLT: Swap = true; // Fallthrough
2160 case ISD::SETOGT:
2161 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2162 case ISD::SETOLE:
2163 case ISD::SETLE: Swap = true; // Fallthrough
2164 case ISD::SETOGE:
2165 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2166 case ISD::SETUGE: Swap = true; // Fallthrough
2167 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2168 case ISD::SETUGT: Swap = true; // Fallthrough
2169 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2170 case ISD::SETUEQ: Invert = true; // Fallthrough
2171 case ISD::SETONE:
2172 // Expand this to (OLT | OGT).
2173 TmpOp0 = Op0;
2174 TmpOp1 = Op1;
2175 Opc = ISD::OR;
2176 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2177 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2178 break;
2179 case ISD::SETUO: Invert = true; // Fallthrough
2180 case ISD::SETO:
2181 // Expand this to (OLT | OGE).
2182 TmpOp0 = Op0;
2183 TmpOp1 = Op1;
2184 Opc = ISD::OR;
2185 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2186 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2187 break;
2188 }
2189 } else {
2190 // Integer comparisons.
2191 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002192 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002193 case ISD::SETNE: Invert = true;
2194 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2195 case ISD::SETLT: Swap = true;
2196 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2197 case ISD::SETLE: Swap = true;
2198 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2199 case ISD::SETULT: Swap = true;
2200 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2201 case ISD::SETULE: Swap = true;
2202 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2203 }
2204
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002205 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00002206 if (Opc == ARMISD::VCEQ) {
2207
2208 SDValue AndOp;
2209 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2210 AndOp = Op0;
2211 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2212 AndOp = Op1;
2213
2214 // Ignore bitconvert.
2215 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2216 AndOp = AndOp.getOperand(0);
2217
2218 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2219 Opc = ARMISD::VTST;
2220 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2221 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2222 Invert = !Invert;
2223 }
2224 }
2225 }
2226
2227 if (Swap)
2228 std::swap(Op0, Op1);
2229
2230 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
2231
2232 if (Invert)
2233 Result = DAG.getNOT(dl, Result, VT);
2234
2235 return Result;
2236}
2237
2238/// isVMOVSplat - Check if the specified splat value corresponds to an immediate
2239/// VMOV instruction, and if so, return the constant being splatted.
2240static SDValue isVMOVSplat(uint64_t SplatBits, uint64_t SplatUndef,
2241 unsigned SplatBitSize, SelectionDAG &DAG) {
2242 switch (SplatBitSize) {
2243 case 8:
2244 // Any 1-byte value is OK.
2245 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Owen Anderson825b72b2009-08-11 20:47:22 +00002246 return DAG.getTargetConstant(SplatBits, MVT::i8);
Bob Wilson5bafff32009-06-22 23:27:02 +00002247
2248 case 16:
2249 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
2250 if ((SplatBits & ~0xff) == 0 ||
2251 (SplatBits & ~0xff00) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002252 return DAG.getTargetConstant(SplatBits, MVT::i16);
Bob Wilson5bafff32009-06-22 23:27:02 +00002253 break;
2254
2255 case 32:
2256 // NEON's 32-bit VMOV supports splat values where:
2257 // * only one byte is nonzero, or
2258 // * the least significant byte is 0xff and the second byte is nonzero, or
2259 // * the least significant 2 bytes are 0xff and the third is nonzero.
2260 if ((SplatBits & ~0xff) == 0 ||
2261 (SplatBits & ~0xff00) == 0 ||
2262 (SplatBits & ~0xff0000) == 0 ||
2263 (SplatBits & ~0xff000000) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002264 return DAG.getTargetConstant(SplatBits, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002265
2266 if ((SplatBits & ~0xffff) == 0 &&
2267 ((SplatBits | SplatUndef) & 0xff) == 0xff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002268 return DAG.getTargetConstant(SplatBits | 0xff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002269
2270 if ((SplatBits & ~0xffffff) == 0 &&
2271 ((SplatBits | SplatUndef) & 0xffff) == 0xffff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002272 return DAG.getTargetConstant(SplatBits | 0xffff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002273
2274 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
2275 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
2276 // VMOV.I32. A (very) minor optimization would be to replicate the value
2277 // and fall through here to test for a valid 64-bit splat. But, then the
2278 // caller would also need to check and handle the change in size.
2279 break;
2280
2281 case 64: {
2282 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
2283 uint64_t BitMask = 0xff;
2284 uint64_t Val = 0;
2285 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
2286 if (((SplatBits | SplatUndef) & BitMask) == BitMask)
2287 Val |= BitMask;
2288 else if ((SplatBits & BitMask) != 0)
2289 return SDValue();
2290 BitMask <<= 8;
2291 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002292 return DAG.getTargetConstant(Val, MVT::i64);
Bob Wilson5bafff32009-06-22 23:27:02 +00002293 }
2294
2295 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002296 llvm_unreachable("unexpected size for isVMOVSplat");
Bob Wilson5bafff32009-06-22 23:27:02 +00002297 break;
2298 }
2299
2300 return SDValue();
2301}
2302
2303/// getVMOVImm - If this is a build_vector of constants which can be
2304/// formed by using a VMOV instruction of the specified element size,
2305/// return the constant being splatted. The ByteSize field indicates the
2306/// number of bytes of each element [1248].
2307SDValue ARM::getVMOVImm(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
2308 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
2309 APInt SplatBits, SplatUndef;
2310 unsigned SplatBitSize;
2311 bool HasAnyUndefs;
2312 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
2313 HasAnyUndefs, ByteSize * 8))
2314 return SDValue();
2315
2316 if (SplatBitSize > ByteSize * 8)
2317 return SDValue();
2318
2319 return isVMOVSplat(SplatBits.getZExtValue(), SplatUndef.getZExtValue(),
2320 SplatBitSize, DAG);
2321}
2322
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002323static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
2324 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002325 unsigned NumElts = VT.getVectorNumElements();
2326 ReverseVEXT = false;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002327 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002328
2329 // If this is a VEXT shuffle, the immediate value is the index of the first
2330 // element. The other shuffle indices must be the successive elements after
2331 // the first one.
2332 unsigned ExpectedElt = Imm;
2333 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002334 // Increment the expected index. If it wraps around, it may still be
2335 // a VEXT but the source vectors must be swapped.
2336 ExpectedElt += 1;
2337 if (ExpectedElt == NumElts * 2) {
2338 ExpectedElt = 0;
2339 ReverseVEXT = true;
2340 }
2341
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002342 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002343 return false;
2344 }
2345
2346 // Adjust the index value if the source operands will be swapped.
2347 if (ReverseVEXT)
2348 Imm -= NumElts;
2349
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002350 return true;
2351}
2352
Bob Wilson8bb9e482009-07-26 00:39:34 +00002353/// isVREVMask - Check if a vector shuffle corresponds to a VREV
2354/// instruction with the specified blocksize. (The order of the elements
2355/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002356static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
2357 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00002358 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
2359 "Only possible block sizes for VREV are: 16, 32, 64");
2360
Bob Wilson8bb9e482009-07-26 00:39:34 +00002361 unsigned NumElts = VT.getVectorNumElements();
2362 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002363 unsigned BlockElts = M[0] + 1;
Bob Wilson8bb9e482009-07-26 00:39:34 +00002364
2365 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
2366 return false;
2367
2368 for (unsigned i = 0; i < NumElts; ++i) {
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002369 if ((unsigned) M[i] !=
Bob Wilson8bb9e482009-07-26 00:39:34 +00002370 (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
2371 return false;
2372 }
2373
2374 return true;
2375}
2376
Bob Wilsonc692cb72009-08-21 20:54:19 +00002377static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
2378 unsigned &WhichResult) {
2379 unsigned NumElts = VT.getVectorNumElements();
2380 WhichResult = (M[0] == 0 ? 0 : 1);
2381 for (unsigned i = 0; i < NumElts; i += 2) {
2382 if ((unsigned) M[i] != i + WhichResult ||
2383 (unsigned) M[i+1] != i + NumElts + WhichResult)
2384 return false;
2385 }
2386 return true;
2387}
2388
2389static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
2390 unsigned &WhichResult) {
2391 unsigned NumElts = VT.getVectorNumElements();
2392 WhichResult = (M[0] == 0 ? 0 : 1);
2393 for (unsigned i = 0; i != NumElts; ++i) {
2394 if ((unsigned) M[i] != 2 * i + WhichResult)
2395 return false;
2396 }
2397
2398 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2399 if (VT.is64BitVector() && VT.getVectorElementType().getSizeInBits() == 32)
2400 return false;
2401
2402 return true;
2403}
2404
2405static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
2406 unsigned &WhichResult) {
2407 unsigned NumElts = VT.getVectorNumElements();
2408 WhichResult = (M[0] == 0 ? 0 : 1);
2409 unsigned Idx = WhichResult * NumElts / 2;
2410 for (unsigned i = 0; i != NumElts; i += 2) {
2411 if ((unsigned) M[i] != Idx ||
2412 (unsigned) M[i+1] != Idx + NumElts)
2413 return false;
2414 Idx += 1;
2415 }
2416
2417 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2418 if (VT.is64BitVector() && VT.getVectorElementType().getSizeInBits() == 32)
2419 return false;
2420
2421 return true;
2422}
2423
Owen Andersone50ed302009-08-10 22:56:29 +00002424static SDValue BuildSplat(SDValue Val, EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002425 // Canonicalize all-zeros and all-ones vectors.
Bob Wilsond06791f2009-08-13 01:57:47 +00002426 ConstantSDNode *ConstVal = cast<ConstantSDNode>(Val.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002427 if (ConstVal->isNullValue())
2428 return getZeroVector(VT, DAG, dl);
2429 if (ConstVal->isAllOnesValue())
2430 return getOnesVector(VT, DAG, dl);
2431
Owen Andersone50ed302009-08-10 22:56:29 +00002432 EVT CanonicalVT;
Bob Wilson5bafff32009-06-22 23:27:02 +00002433 if (VT.is64BitVector()) {
2434 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002435 case 8: CanonicalVT = MVT::v8i8; break;
2436 case 16: CanonicalVT = MVT::v4i16; break;
2437 case 32: CanonicalVT = MVT::v2i32; break;
2438 case 64: CanonicalVT = MVT::v1i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002439 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002440 }
2441 } else {
2442 assert(VT.is128BitVector() && "unknown splat vector size");
2443 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002444 case 8: CanonicalVT = MVT::v16i8; break;
2445 case 16: CanonicalVT = MVT::v8i16; break;
2446 case 32: CanonicalVT = MVT::v4i32; break;
2447 case 64: CanonicalVT = MVT::v2i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002448 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002449 }
2450 }
2451
2452 // Build a canonical splat for this value.
2453 SmallVector<SDValue, 8> Ops;
2454 Ops.assign(CanonicalVT.getVectorNumElements(), Val);
2455 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, &Ops[0],
2456 Ops.size());
2457 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Res);
2458}
2459
2460// If this is a case we can't handle, return null and let the default
2461// expansion code take care of it.
2462static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bob Wilsond06791f2009-08-13 01:57:47 +00002463 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002464 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002465 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002466
2467 APInt SplatBits, SplatUndef;
2468 unsigned SplatBitSize;
2469 bool HasAnyUndefs;
2470 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00002471 if (SplatBitSize <= 64) {
2472 SDValue Val = isVMOVSplat(SplatBits.getZExtValue(),
2473 SplatUndef.getZExtValue(), SplatBitSize, DAG);
2474 if (Val.getNode())
2475 return BuildSplat(Val, VT, DAG, dl);
2476 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00002477 }
2478
2479 // If there are only 2 elements in a 128-bit vector, insert them into an
2480 // undef vector. This handles the common case for 128-bit vector argument
2481 // passing, where the insertions should be translated to subreg accesses
2482 // with no real instructions.
2483 if (VT.is128BitVector() && Op.getNumOperands() == 2) {
2484 SDValue Val = DAG.getUNDEF(VT);
2485 SDValue Op0 = Op.getOperand(0);
2486 SDValue Op1 = Op.getOperand(1);
2487 if (Op0.getOpcode() != ISD::UNDEF)
2488 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op0,
2489 DAG.getIntPtrConstant(0));
2490 if (Op1.getOpcode() != ISD::UNDEF)
2491 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op1,
2492 DAG.getIntPtrConstant(1));
2493 return Val;
Bob Wilson5bafff32009-06-22 23:27:02 +00002494 }
2495
2496 return SDValue();
2497}
2498
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002499/// isShuffleMaskLegal - Targets can use this to indicate that they only
2500/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
2501/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
2502/// are assumed to be legal.
2503bool
2504ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
2505 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002506 if (VT.getVectorNumElements() == 4 &&
2507 (VT.is128BitVector() || VT.is64BitVector())) {
2508 unsigned PFIndexes[4];
2509 for (unsigned i = 0; i != 4; ++i) {
2510 if (M[i] < 0)
2511 PFIndexes[i] = 8;
2512 else
2513 PFIndexes[i] = M[i];
2514 }
2515
2516 // Compute the index in the perfect shuffle table.
2517 unsigned PFTableIndex =
2518 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2519 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2520 unsigned Cost = (PFEntry >> 30);
2521
2522 if (Cost <= 4)
2523 return true;
2524 }
2525
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002526 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00002527 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002528
2529 return (ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
2530 isVREVMask(M, VT, 64) ||
2531 isVREVMask(M, VT, 32) ||
2532 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00002533 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
2534 isVTRNMask(M, VT, WhichResult) ||
2535 isVUZPMask(M, VT, WhichResult) ||
2536 isVZIPMask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002537}
2538
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002539/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
2540/// the specified operations to build the shuffle.
2541static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
2542 SDValue RHS, SelectionDAG &DAG,
2543 DebugLoc dl) {
2544 unsigned OpNum = (PFEntry >> 26) & 0x0F;
2545 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
2546 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
2547
2548 enum {
2549 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
2550 OP_VREV,
2551 OP_VDUP0,
2552 OP_VDUP1,
2553 OP_VDUP2,
2554 OP_VDUP3,
2555 OP_VEXT1,
2556 OP_VEXT2,
2557 OP_VEXT3,
2558 OP_VUZPL, // VUZP, left result
2559 OP_VUZPR, // VUZP, right result
2560 OP_VZIPL, // VZIP, left result
2561 OP_VZIPR, // VZIP, right result
2562 OP_VTRNL, // VTRN, left result
2563 OP_VTRNR // VTRN, right result
2564 };
2565
2566 if (OpNum == OP_COPY) {
2567 if (LHSID == (1*9+2)*9+3) return LHS;
2568 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
2569 return RHS;
2570 }
2571
2572 SDValue OpLHS, OpRHS;
2573 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
2574 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
2575 EVT VT = OpLHS.getValueType();
2576
2577 switch (OpNum) {
2578 default: llvm_unreachable("Unknown shuffle opcode!");
2579 case OP_VREV:
2580 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
2581 case OP_VDUP0:
2582 case OP_VDUP1:
2583 case OP_VDUP2:
2584 case OP_VDUP3:
2585 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002586 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002587 case OP_VEXT1:
2588 case OP_VEXT2:
2589 case OP_VEXT3:
2590 return DAG.getNode(ARMISD::VEXT, dl, VT,
2591 OpLHS, OpRHS,
2592 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
2593 case OP_VUZPL:
2594 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002595 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002596 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
2597 case OP_VZIPL:
2598 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002599 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002600 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
2601 case OP_VTRNL:
2602 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002603 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2604 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002605 }
2606}
2607
Bob Wilson5bafff32009-06-22 23:27:02 +00002608static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002609 SDValue V1 = Op.getOperand(0);
2610 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00002611 DebugLoc dl = Op.getDebugLoc();
2612 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002613 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002614 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00002615
Bob Wilson28865062009-08-13 02:13:04 +00002616 // Convert shuffles that are directly supported on NEON to target-specific
2617 // DAG nodes, instead of keeping them as shuffles and matching them again
2618 // during code selection. This is more efficient and avoids the possibility
2619 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00002620 // FIXME: floating-point vectors should be canonicalized to integer vectors
2621 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002622 SVN->getMask(ShuffleMask);
2623
2624 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
Bob Wilson0ce37102009-08-14 05:08:32 +00002625 int Lane = SVN->getSplatIndex();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002626 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
2627 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00002628 }
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002629 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002630 DAG.getConstant(Lane, MVT::i32));
Bob Wilson0ce37102009-08-14 05:08:32 +00002631 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002632
2633 bool ReverseVEXT;
2634 unsigned Imm;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002635 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002636 if (ReverseVEXT)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002637 std::swap(V1, V2);
2638 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002639 DAG.getConstant(Imm, MVT::i32));
2640 }
2641
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002642 if (isVREVMask(ShuffleMask, VT, 64))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002643 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002644 if (isVREVMask(ShuffleMask, VT, 32))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002645 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002646 if (isVREVMask(ShuffleMask, VT, 16))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002647 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
2648
Bob Wilsonc692cb72009-08-21 20:54:19 +00002649 // Check for Neon shuffles that modify both input vectors in place.
2650 // If both results are used, i.e., if there are two shuffles with the same
2651 // source operands and with masks corresponding to both results of one of
2652 // these operations, DAG memoization will ensure that a single node is
2653 // used for both shuffles.
2654 unsigned WhichResult;
2655 if (isVTRNMask(ShuffleMask, VT, WhichResult))
2656 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2657 V1, V2).getValue(WhichResult);
2658 if (isVUZPMask(ShuffleMask, VT, WhichResult))
2659 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
2660 V1, V2).getValue(WhichResult);
2661 if (isVZIPMask(ShuffleMask, VT, WhichResult))
2662 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
2663 V1, V2).getValue(WhichResult);
2664
2665 // If the shuffle is not directly supported and it has 4 elements, use
2666 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002667 if (VT.getVectorNumElements() == 4 &&
2668 (VT.is128BitVector() || VT.is64BitVector())) {
2669 unsigned PFIndexes[4];
2670 for (unsigned i = 0; i != 4; ++i) {
2671 if (ShuffleMask[i] < 0)
2672 PFIndexes[i] = 8;
2673 else
2674 PFIndexes[i] = ShuffleMask[i];
2675 }
2676
2677 // Compute the index in the perfect shuffle table.
2678 unsigned PFTableIndex =
2679 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2680
2681 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2682 unsigned Cost = (PFEntry >> 30);
2683
2684 if (Cost <= 4)
2685 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
2686 }
Bob Wilsond8e17572009-08-12 22:31:50 +00002687
Bob Wilson22cac0d2009-08-14 05:16:33 +00002688 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00002689}
2690
Bob Wilson5bafff32009-06-22 23:27:02 +00002691static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002692 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002693 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00002694 assert((VT == MVT::i8 || VT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00002695 "unexpected type for custom-lowering vector extract");
2696 SDValue Vec = Op.getOperand(0);
2697 SDValue Lane = Op.getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002698 Op = DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
2699 Op = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Op, DAG.getValueType(VT));
Bob Wilson5bafff32009-06-22 23:27:02 +00002700 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
2701}
2702
Bob Wilsona6d65862009-08-03 20:36:38 +00002703static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
2704 // The only time a CONCAT_VECTORS operation can have legal types is when
2705 // two 64-bit vectors are concatenated to a 128-bit vector.
2706 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
2707 "unexpected CONCAT_VECTORS");
2708 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00002709 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00002710 SDValue Op0 = Op.getOperand(0);
2711 SDValue Op1 = Op.getOperand(1);
2712 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00002713 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
2714 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00002715 DAG.getIntPtrConstant(0));
2716 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00002717 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
2718 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00002719 DAG.getIntPtrConstant(1));
2720 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00002721}
2722
Dan Gohman475871a2008-07-27 21:46:04 +00002723SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00002724 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002725 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00002726 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002727 case ISD::GlobalAddress:
2728 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
2729 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002730 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002731 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, Subtarget);
2732 case ISD::BR_CC: return LowerBR_CC(Op, DAG, Subtarget);
2733 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Evan Cheng86198642009-08-07 00:34:42 +00002734 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002735 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
2736 case ISD::SINT_TO_FP:
2737 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
2738 case ISD::FP_TO_SINT:
2739 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
2740 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00002741 case ISD::RETURNADDR: break;
Jim Grosbach0e0da732009-05-12 23:59:14 +00002742 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002743 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Bob Wilsonb36ec862009-08-06 18:47:44 +00002744 case ISD::INTRINSIC_VOID:
Bob Wilsona599bff2009-08-04 00:36:16 +00002745 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002746 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00002747 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00002748 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00002749 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00002750 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
2751 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
2752 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2753 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00002754 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00002755 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002756 }
Dan Gohman475871a2008-07-27 21:46:04 +00002757 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00002758}
2759
Duncan Sands1607f052008-12-01 11:39:25 +00002760/// ReplaceNodeResults - Replace the results of node with an illegal result
2761/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00002762void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
2763 SmallVectorImpl<SDValue>&Results,
2764 SelectionDAG &DAG) {
Chris Lattner27a6c732007-11-24 07:07:01 +00002765 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00002766 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002767 llvm_unreachable("Don't know how to custom expand this!");
Duncan Sands1607f052008-12-01 11:39:25 +00002768 return;
2769 case ISD::BIT_CONVERT:
2770 Results.push_back(ExpandBIT_CONVERT(N, DAG));
2771 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00002772 case ISD::SRL:
Duncan Sands1607f052008-12-01 11:39:25 +00002773 case ISD::SRA: {
Bob Wilson5bafff32009-06-22 23:27:02 +00002774 SDValue Res = LowerShift(N, DAG, Subtarget);
Duncan Sands1607f052008-12-01 11:39:25 +00002775 if (Res.getNode())
2776 Results.push_back(Res);
2777 return;
2778 }
Chris Lattner27a6c732007-11-24 07:07:01 +00002779 }
2780}
Chris Lattner27a6c732007-11-24 07:07:01 +00002781
Evan Chenga8e29892007-01-19 07:51:42 +00002782//===----------------------------------------------------------------------===//
2783// ARM Scheduler Hooks
2784//===----------------------------------------------------------------------===//
2785
2786MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00002787ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00002788 MachineBasicBlock *BB) 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.
2819 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
2820 e = BB->succ_end(); i != e; ++i)
2821 sinkMBB->addSuccessor(*i);
2822 // Next, remove all successors of the current block, and add the true
2823 // and fallthrough blocks as its successors.
2824 while(!BB->succ_empty())
2825 BB->removeSuccessor(BB->succ_begin());
2826 BB->addSuccessor(copy0MBB);
2827 BB->addSuccessor(sinkMBB);
2828
2829 // copy0MBB:
2830 // %FalseValue = ...
2831 // # fallthrough to sinkMBB
2832 BB = copy0MBB;
2833
2834 // Update machine-CFG edges
2835 BB->addSuccessor(sinkMBB);
2836
2837 // sinkMBB:
2838 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2839 // ...
2840 BB = sinkMBB;
Dale Johannesenb6728402009-02-13 02:25:56 +00002841 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00002842 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
2843 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
2844
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002845 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00002846 return BB;
2847 }
Evan Cheng86198642009-08-07 00:34:42 +00002848
2849 case ARM::tANDsp:
2850 case ARM::tADDspr_:
2851 case ARM::tSUBspi_:
2852 case ARM::t2SUBrSPi_:
2853 case ARM::t2SUBrSPi12_:
2854 case ARM::t2SUBrSPs_: {
2855 MachineFunction *MF = BB->getParent();
2856 unsigned DstReg = MI->getOperand(0).getReg();
2857 unsigned SrcReg = MI->getOperand(1).getReg();
2858 bool DstIsDead = MI->getOperand(0).isDead();
2859 bool SrcIsKill = MI->getOperand(1).isKill();
2860
2861 if (SrcReg != ARM::SP) {
2862 // Copy the source to SP from virtual register.
2863 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(SrcReg);
2864 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
2865 ? ARM::tMOVtgpr2gpr : ARM::tMOVgpr2gpr;
2866 BuildMI(BB, dl, TII->get(CopyOpc), ARM::SP)
2867 .addReg(SrcReg, getKillRegState(SrcIsKill));
2868 }
2869
2870 unsigned OpOpc = 0;
2871 bool NeedPred = false, NeedCC = false, NeedOp3 = false;
2872 switch (MI->getOpcode()) {
2873 default:
2874 llvm_unreachable("Unexpected pseudo instruction!");
2875 case ARM::tANDsp:
2876 OpOpc = ARM::tAND;
2877 NeedPred = true;
2878 break;
2879 case ARM::tADDspr_:
2880 OpOpc = ARM::tADDspr;
2881 break;
2882 case ARM::tSUBspi_:
2883 OpOpc = ARM::tSUBspi;
2884 break;
2885 case ARM::t2SUBrSPi_:
2886 OpOpc = ARM::t2SUBrSPi;
2887 NeedPred = true; NeedCC = true;
2888 break;
2889 case ARM::t2SUBrSPi12_:
2890 OpOpc = ARM::t2SUBrSPi12;
2891 NeedPred = true;
2892 break;
2893 case ARM::t2SUBrSPs_:
2894 OpOpc = ARM::t2SUBrSPs;
2895 NeedPred = true; NeedCC = true; NeedOp3 = true;
2896 break;
2897 }
2898 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(OpOpc), ARM::SP);
2899 if (OpOpc == ARM::tAND)
2900 AddDefaultT1CC(MIB);
2901 MIB.addReg(ARM::SP);
2902 MIB.addOperand(MI->getOperand(2));
2903 if (NeedOp3)
2904 MIB.addOperand(MI->getOperand(3));
2905 if (NeedPred)
2906 AddDefaultPred(MIB);
2907 if (NeedCC)
2908 AddDefaultCC(MIB);
2909
2910 // Copy the result from SP to virtual register.
2911 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(DstReg);
2912 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
2913 ? ARM::tMOVgpr2tgpr : ARM::tMOVgpr2gpr;
2914 BuildMI(BB, dl, TII->get(CopyOpc))
2915 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
2916 .addReg(ARM::SP);
2917 MF->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
2918 return BB;
2919 }
Evan Chenga8e29892007-01-19 07:51:42 +00002920 }
2921}
2922
2923//===----------------------------------------------------------------------===//
2924// ARM Optimization Hooks
2925//===----------------------------------------------------------------------===//
2926
Chris Lattnerd1980a52009-03-12 06:52:53 +00002927static
2928SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
2929 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00002930 SelectionDAG &DAG = DCI.DAG;
2931 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00002932 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00002933 unsigned Opc = N->getOpcode();
2934 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
2935 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
2936 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
2937 ISD::CondCode CC = ISD::SETCC_INVALID;
2938
2939 if (isSlctCC) {
2940 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
2941 } else {
2942 SDValue CCOp = Slct.getOperand(0);
2943 if (CCOp.getOpcode() == ISD::SETCC)
2944 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
2945 }
2946
2947 bool DoXform = false;
2948 bool InvCC = false;
2949 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
2950 "Bad input!");
2951
2952 if (LHS.getOpcode() == ISD::Constant &&
2953 cast<ConstantSDNode>(LHS)->isNullValue()) {
2954 DoXform = true;
2955 } else if (CC != ISD::SETCC_INVALID &&
2956 RHS.getOpcode() == ISD::Constant &&
2957 cast<ConstantSDNode>(RHS)->isNullValue()) {
2958 std::swap(LHS, RHS);
2959 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00002960 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00002961 Op0.getOperand(0).getValueType();
2962 bool isInt = OpVT.isInteger();
2963 CC = ISD::getSetCCInverse(CC, isInt);
2964
2965 if (!TLI.isCondCodeLegal(CC, OpVT))
2966 return SDValue(); // Inverse operator isn't legal.
2967
2968 DoXform = true;
2969 InvCC = true;
2970 }
2971
2972 if (DoXform) {
2973 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
2974 if (isSlctCC)
2975 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
2976 Slct.getOperand(0), Slct.getOperand(1), CC);
2977 SDValue CCOp = Slct.getOperand(0);
2978 if (InvCC)
2979 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
2980 CCOp.getOperand(0), CCOp.getOperand(1), CC);
2981 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
2982 CCOp, OtherOp, Result);
2983 }
2984 return SDValue();
2985}
2986
2987/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
2988static SDValue PerformADDCombine(SDNode *N,
2989 TargetLowering::DAGCombinerInfo &DCI) {
2990 // added by evan in r37685 with no testcase.
2991 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002992
Chris Lattnerd1980a52009-03-12 06:52:53 +00002993 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
2994 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
2995 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
2996 if (Result.getNode()) return Result;
2997 }
2998 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
2999 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3000 if (Result.getNode()) return Result;
3001 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003002
Chris Lattnerd1980a52009-03-12 06:52:53 +00003003 return SDValue();
3004}
3005
3006/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
3007static SDValue PerformSUBCombine(SDNode *N,
3008 TargetLowering::DAGCombinerInfo &DCI) {
3009 // added by evan in r37685 with no testcase.
3010 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003011
Chris Lattnerd1980a52009-03-12 06:52:53 +00003012 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
3013 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3014 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3015 if (Result.getNode()) return Result;
3016 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003017
Chris Lattnerd1980a52009-03-12 06:52:53 +00003018 return SDValue();
3019}
3020
3021
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003022/// PerformFMRRDCombine - Target-specific dag combine xforms for ARMISD::FMRRD.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003023static SDValue PerformFMRRDCombine(SDNode *N,
3024 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003025 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00003026 SDValue InDouble = N->getOperand(0);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003027 if (InDouble.getOpcode() == ARMISD::FMDRR)
3028 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003029 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003030}
3031
Bob Wilson5bafff32009-06-22 23:27:02 +00003032/// getVShiftImm - Check if this is a valid build_vector for the immediate
3033/// operand of a vector shift operation, where all the elements of the
3034/// build_vector must have the same constant integer value.
3035static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
3036 // Ignore bit_converts.
3037 while (Op.getOpcode() == ISD::BIT_CONVERT)
3038 Op = Op.getOperand(0);
3039 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
3040 APInt SplatBits, SplatUndef;
3041 unsigned SplatBitSize;
3042 bool HasAnyUndefs;
3043 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
3044 HasAnyUndefs, ElementBits) ||
3045 SplatBitSize > ElementBits)
3046 return false;
3047 Cnt = SplatBits.getSExtValue();
3048 return true;
3049}
3050
3051/// isVShiftLImm - Check if this is a valid build_vector for the immediate
3052/// operand of a vector shift left operation. That value must be in the range:
3053/// 0 <= Value < ElementBits for a left shift; or
3054/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003055static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003056 assert(VT.isVector() && "vector shift count is not a vector type");
3057 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3058 if (! getVShiftImm(Op, ElementBits, Cnt))
3059 return false;
3060 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
3061}
3062
3063/// isVShiftRImm - Check if this is a valid build_vector for the immediate
3064/// operand of a vector shift right operation. For a shift opcode, the value
3065/// is positive, but for an intrinsic the value count must be negative. The
3066/// absolute value must be in the range:
3067/// 1 <= |Value| <= ElementBits for a right shift; or
3068/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003069static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00003070 int64_t &Cnt) {
3071 assert(VT.isVector() && "vector shift count is not a vector type");
3072 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3073 if (! getVShiftImm(Op, ElementBits, Cnt))
3074 return false;
3075 if (isIntrinsic)
3076 Cnt = -Cnt;
3077 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
3078}
3079
3080/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
3081static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
3082 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
3083 switch (IntNo) {
3084 default:
3085 // Don't do anything for most intrinsics.
3086 break;
3087
3088 // Vector shifts: check for immediate versions and lower them.
3089 // Note: This is done during DAG combining instead of DAG legalizing because
3090 // the build_vectors for 64-bit vector element shift counts are generally
3091 // not legal, and it is hard to see their values after they get legalized to
3092 // loads from a constant pool.
3093 case Intrinsic::arm_neon_vshifts:
3094 case Intrinsic::arm_neon_vshiftu:
3095 case Intrinsic::arm_neon_vshiftls:
3096 case Intrinsic::arm_neon_vshiftlu:
3097 case Intrinsic::arm_neon_vshiftn:
3098 case Intrinsic::arm_neon_vrshifts:
3099 case Intrinsic::arm_neon_vrshiftu:
3100 case Intrinsic::arm_neon_vrshiftn:
3101 case Intrinsic::arm_neon_vqshifts:
3102 case Intrinsic::arm_neon_vqshiftu:
3103 case Intrinsic::arm_neon_vqshiftsu:
3104 case Intrinsic::arm_neon_vqshiftns:
3105 case Intrinsic::arm_neon_vqshiftnu:
3106 case Intrinsic::arm_neon_vqshiftnsu:
3107 case Intrinsic::arm_neon_vqrshiftns:
3108 case Intrinsic::arm_neon_vqrshiftnu:
3109 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00003110 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003111 int64_t Cnt;
3112 unsigned VShiftOpc = 0;
3113
3114 switch (IntNo) {
3115 case Intrinsic::arm_neon_vshifts:
3116 case Intrinsic::arm_neon_vshiftu:
3117 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
3118 VShiftOpc = ARMISD::VSHL;
3119 break;
3120 }
3121 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
3122 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
3123 ARMISD::VSHRs : ARMISD::VSHRu);
3124 break;
3125 }
3126 return SDValue();
3127
3128 case Intrinsic::arm_neon_vshiftls:
3129 case Intrinsic::arm_neon_vshiftlu:
3130 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
3131 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003132 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003133
3134 case Intrinsic::arm_neon_vrshifts:
3135 case Intrinsic::arm_neon_vrshiftu:
3136 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
3137 break;
3138 return SDValue();
3139
3140 case Intrinsic::arm_neon_vqshifts:
3141 case Intrinsic::arm_neon_vqshiftu:
3142 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3143 break;
3144 return SDValue();
3145
3146 case Intrinsic::arm_neon_vqshiftsu:
3147 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3148 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003149 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003150
3151 case Intrinsic::arm_neon_vshiftn:
3152 case Intrinsic::arm_neon_vrshiftn:
3153 case Intrinsic::arm_neon_vqshiftns:
3154 case Intrinsic::arm_neon_vqshiftnu:
3155 case Intrinsic::arm_neon_vqshiftnsu:
3156 case Intrinsic::arm_neon_vqrshiftns:
3157 case Intrinsic::arm_neon_vqrshiftnu:
3158 case Intrinsic::arm_neon_vqrshiftnsu:
3159 // Narrowing shifts require an immediate right shift.
3160 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
3161 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003162 llvm_unreachable("invalid shift count for narrowing vector shift intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003163
3164 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003165 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003166 }
3167
3168 switch (IntNo) {
3169 case Intrinsic::arm_neon_vshifts:
3170 case Intrinsic::arm_neon_vshiftu:
3171 // Opcode already set above.
3172 break;
3173 case Intrinsic::arm_neon_vshiftls:
3174 case Intrinsic::arm_neon_vshiftlu:
3175 if (Cnt == VT.getVectorElementType().getSizeInBits())
3176 VShiftOpc = ARMISD::VSHLLi;
3177 else
3178 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
3179 ARMISD::VSHLLs : ARMISD::VSHLLu);
3180 break;
3181 case Intrinsic::arm_neon_vshiftn:
3182 VShiftOpc = ARMISD::VSHRN; break;
3183 case Intrinsic::arm_neon_vrshifts:
3184 VShiftOpc = ARMISD::VRSHRs; break;
3185 case Intrinsic::arm_neon_vrshiftu:
3186 VShiftOpc = ARMISD::VRSHRu; break;
3187 case Intrinsic::arm_neon_vrshiftn:
3188 VShiftOpc = ARMISD::VRSHRN; break;
3189 case Intrinsic::arm_neon_vqshifts:
3190 VShiftOpc = ARMISD::VQSHLs; break;
3191 case Intrinsic::arm_neon_vqshiftu:
3192 VShiftOpc = ARMISD::VQSHLu; break;
3193 case Intrinsic::arm_neon_vqshiftsu:
3194 VShiftOpc = ARMISD::VQSHLsu; break;
3195 case Intrinsic::arm_neon_vqshiftns:
3196 VShiftOpc = ARMISD::VQSHRNs; break;
3197 case Intrinsic::arm_neon_vqshiftnu:
3198 VShiftOpc = ARMISD::VQSHRNu; break;
3199 case Intrinsic::arm_neon_vqshiftnsu:
3200 VShiftOpc = ARMISD::VQSHRNsu; break;
3201 case Intrinsic::arm_neon_vqrshiftns:
3202 VShiftOpc = ARMISD::VQRSHRNs; break;
3203 case Intrinsic::arm_neon_vqrshiftnu:
3204 VShiftOpc = ARMISD::VQRSHRNu; break;
3205 case Intrinsic::arm_neon_vqrshiftnsu:
3206 VShiftOpc = ARMISD::VQRSHRNsu; break;
3207 }
3208
3209 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003210 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003211 }
3212
3213 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00003214 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003215 int64_t Cnt;
3216 unsigned VShiftOpc = 0;
3217
3218 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
3219 VShiftOpc = ARMISD::VSLI;
3220 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
3221 VShiftOpc = ARMISD::VSRI;
3222 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00003223 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003224 }
3225
3226 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
3227 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00003228 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003229 }
3230
3231 case Intrinsic::arm_neon_vqrshifts:
3232 case Intrinsic::arm_neon_vqrshiftu:
3233 // No immediate versions of these to check for.
3234 break;
3235 }
3236
3237 return SDValue();
3238}
3239
3240/// PerformShiftCombine - Checks for immediate versions of vector shifts and
3241/// lowers them. As with the vector shift intrinsics, this is done during DAG
3242/// combining instead of DAG legalizing because the build_vectors for 64-bit
3243/// vector element shift counts are generally not legal, and it is hard to see
3244/// their values after they get legalized to loads from a constant pool.
3245static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
3246 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003247 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003248
3249 // Nothing to be done for scalar shifts.
3250 if (! VT.isVector())
3251 return SDValue();
3252
3253 assert(ST->hasNEON() && "unexpected vector shift");
3254 int64_t Cnt;
3255
3256 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003257 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003258
3259 case ISD::SHL:
3260 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
3261 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003262 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003263 break;
3264
3265 case ISD::SRA:
3266 case ISD::SRL:
3267 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
3268 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
3269 ARMISD::VSHRs : ARMISD::VSHRu);
3270 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003271 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003272 }
3273 }
3274 return SDValue();
3275}
3276
3277/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
3278/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
3279static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
3280 const ARMSubtarget *ST) {
3281 SDValue N0 = N->getOperand(0);
3282
3283 // Check for sign- and zero-extensions of vector extract operations of 8-
3284 // and 16-bit vector elements. NEON supports these directly. They are
3285 // handled during DAG combining because type legalization will promote them
3286 // to 32-bit types and it is messy to recognize the operations after that.
3287 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
3288 SDValue Vec = N0.getOperand(0);
3289 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00003290 EVT VT = N->getValueType(0);
3291 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003292 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3293
Owen Anderson825b72b2009-08-11 20:47:22 +00003294 if (VT == MVT::i32 &&
3295 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00003296 TLI.isTypeLegal(Vec.getValueType())) {
3297
3298 unsigned Opc = 0;
3299 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003300 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003301 case ISD::SIGN_EXTEND:
3302 Opc = ARMISD::VGETLANEs;
3303 break;
3304 case ISD::ZERO_EXTEND:
3305 case ISD::ANY_EXTEND:
3306 Opc = ARMISD::VGETLANEu;
3307 break;
3308 }
3309 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
3310 }
3311 }
3312
3313 return SDValue();
3314}
3315
Dan Gohman475871a2008-07-27 21:46:04 +00003316SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003317 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003318 switch (N->getOpcode()) {
3319 default: break;
Chris Lattnerd1980a52009-03-12 06:52:53 +00003320 case ISD::ADD: return PerformADDCombine(N, DCI);
3321 case ISD::SUB: return PerformSUBCombine(N, DCI);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003322 case ARMISD::FMRRD: return PerformFMRRDCombine(N, DCI);
Bob Wilson5bafff32009-06-22 23:27:02 +00003323 case ISD::INTRINSIC_WO_CHAIN:
3324 return PerformIntrinsicCombine(N, DCI.DAG);
3325 case ISD::SHL:
3326 case ISD::SRA:
3327 case ISD::SRL:
3328 return PerformShiftCombine(N, DCI.DAG, Subtarget);
3329 case ISD::SIGN_EXTEND:
3330 case ISD::ZERO_EXTEND:
3331 case ISD::ANY_EXTEND:
3332 return PerformExtendCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003333 }
Dan Gohman475871a2008-07-27 21:46:04 +00003334 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003335}
3336
Bill Wendlingaf566342009-08-15 21:21:19 +00003337bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
3338 if (!Subtarget->hasV6Ops())
3339 // Pre-v6 does not support unaligned mem access.
3340 return false;
3341 else if (!Subtarget->hasV6Ops()) {
3342 // v6 may or may not support unaligned mem access.
3343 if (!Subtarget->isTargetDarwin())
3344 return false;
3345 }
3346
3347 switch (VT.getSimpleVT().SimpleTy) {
3348 default:
3349 return false;
3350 case MVT::i8:
3351 case MVT::i16:
3352 case MVT::i32:
3353 return true;
3354 // FIXME: VLD1 etc with standard alignment is legal.
3355 }
3356}
3357
Evan Chenge6c835f2009-08-14 20:09:37 +00003358static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
3359 if (V < 0)
3360 return false;
3361
3362 unsigned Scale = 1;
3363 switch (VT.getSimpleVT().SimpleTy) {
3364 default: return false;
3365 case MVT::i1:
3366 case MVT::i8:
3367 // Scale == 1;
3368 break;
3369 case MVT::i16:
3370 // Scale == 2;
3371 Scale = 2;
3372 break;
3373 case MVT::i32:
3374 // Scale == 4;
3375 Scale = 4;
3376 break;
3377 }
3378
3379 if ((V & (Scale - 1)) != 0)
3380 return false;
3381 V /= Scale;
3382 return V == (V & ((1LL << 5) - 1));
3383}
3384
3385static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
3386 const ARMSubtarget *Subtarget) {
3387 bool isNeg = false;
3388 if (V < 0) {
3389 isNeg = true;
3390 V = - V;
3391 }
3392
3393 switch (VT.getSimpleVT().SimpleTy) {
3394 default: return false;
3395 case MVT::i1:
3396 case MVT::i8:
3397 case MVT::i16:
3398 case MVT::i32:
3399 // + imm12 or - imm8
3400 if (isNeg)
3401 return V == (V & ((1LL << 8) - 1));
3402 return V == (V & ((1LL << 12) - 1));
3403 case MVT::f32:
3404 case MVT::f64:
3405 // Same as ARM mode. FIXME: NEON?
3406 if (!Subtarget->hasVFP2())
3407 return false;
3408 if ((V & 3) != 0)
3409 return false;
3410 V >>= 2;
3411 return V == (V & ((1LL << 8) - 1));
3412 }
3413}
3414
Evan Chengb01fad62007-03-12 23:30:29 +00003415/// isLegalAddressImmediate - Return true if the integer value can be used
3416/// as the offset of the target addressing mode for load / store of the
3417/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00003418static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00003419 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00003420 if (V == 0)
3421 return true;
3422
Evan Cheng65011532009-03-09 19:15:00 +00003423 if (!VT.isSimple())
3424 return false;
3425
Evan Chenge6c835f2009-08-14 20:09:37 +00003426 if (Subtarget->isThumb1Only())
3427 return isLegalT1AddressImmediate(V, VT);
3428 else if (Subtarget->isThumb2())
3429 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00003430
Evan Chenge6c835f2009-08-14 20:09:37 +00003431 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00003432 if (V < 0)
3433 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00003434 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00003435 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00003436 case MVT::i1:
3437 case MVT::i8:
3438 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00003439 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003440 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003441 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00003442 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003443 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003444 case MVT::f32:
3445 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00003446 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00003447 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00003448 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00003449 return false;
3450 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003451 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00003452 }
Evan Chenga8e29892007-01-19 07:51:42 +00003453}
3454
Evan Chenge6c835f2009-08-14 20:09:37 +00003455bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
3456 EVT VT) const {
3457 int Scale = AM.Scale;
3458 if (Scale < 0)
3459 return false;
3460
3461 switch (VT.getSimpleVT().SimpleTy) {
3462 default: return false;
3463 case MVT::i1:
3464 case MVT::i8:
3465 case MVT::i16:
3466 case MVT::i32:
3467 if (Scale == 1)
3468 return true;
3469 // r + r << imm
3470 Scale = Scale & ~1;
3471 return Scale == 2 || Scale == 4 || Scale == 8;
3472 case MVT::i64:
3473 // r + r
3474 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
3475 return true;
3476 return false;
3477 case MVT::isVoid:
3478 // Note, we allow "void" uses (basically, uses that aren't loads or
3479 // stores), because arm allows folding a scale into many arithmetic
3480 // operations. This should be made more precise and revisited later.
3481
3482 // Allow r << imm, but the imm has to be a multiple of two.
3483 if (Scale & 1) return false;
3484 return isPowerOf2_32(Scale);
3485 }
3486}
3487
Chris Lattner37caf8c2007-04-09 23:33:39 +00003488/// isLegalAddressingMode - Return true if the addressing mode represented
3489/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003490bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00003491 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00003492 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00003493 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00003494 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003495
Chris Lattner37caf8c2007-04-09 23:33:39 +00003496 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003497 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003498 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003499
Chris Lattner37caf8c2007-04-09 23:33:39 +00003500 switch (AM.Scale) {
3501 case 0: // no scale reg, must be "r+i" or "r", or "i".
3502 break;
3503 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00003504 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00003505 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00003506 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00003507 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00003508 // ARM doesn't support any R+R*scale+imm addr modes.
3509 if (AM.BaseOffs)
3510 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003511
Bob Wilson2c7dab12009-04-08 17:55:28 +00003512 if (!VT.isSimple())
3513 return false;
3514
Evan Chenge6c835f2009-08-14 20:09:37 +00003515 if (Subtarget->isThumb2())
3516 return isLegalT2ScaledAddressingMode(AM, VT);
3517
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003518 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00003519 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00003520 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00003521 case MVT::i1:
3522 case MVT::i8:
3523 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003524 if (Scale < 0) Scale = -Scale;
3525 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003526 return true;
3527 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00003528 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003529 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00003530 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00003531 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003532 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003533 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00003534 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003535
Owen Anderson825b72b2009-08-11 20:47:22 +00003536 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00003537 // Note, we allow "void" uses (basically, uses that aren't loads or
3538 // stores), because arm allows folding a scale into many arithmetic
3539 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003540
Chris Lattner37caf8c2007-04-09 23:33:39 +00003541 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00003542 if (Scale & 1) return false;
3543 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00003544 }
3545 break;
Evan Chengb01fad62007-03-12 23:30:29 +00003546 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00003547 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00003548}
3549
Owen Andersone50ed302009-08-10 22:56:29 +00003550static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00003551 bool isSEXTLoad, SDValue &Base,
3552 SDValue &Offset, bool &isInc,
3553 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00003554 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
3555 return false;
3556
Owen Anderson825b72b2009-08-11 20:47:22 +00003557 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00003558 // AddressingMode 3
3559 Base = Ptr->getOperand(0);
3560 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003561 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003562 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003563 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00003564 isInc = false;
3565 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3566 return true;
3567 }
3568 }
3569 isInc = (Ptr->getOpcode() == ISD::ADD);
3570 Offset = Ptr->getOperand(1);
3571 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00003572 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00003573 // AddressingMode 2
3574 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003575 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003576 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003577 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00003578 isInc = false;
3579 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3580 Base = Ptr->getOperand(0);
3581 return true;
3582 }
3583 }
3584
3585 if (Ptr->getOpcode() == ISD::ADD) {
3586 isInc = true;
3587 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
3588 if (ShOpcVal != ARM_AM::no_shift) {
3589 Base = Ptr->getOperand(1);
3590 Offset = Ptr->getOperand(0);
3591 } else {
3592 Base = Ptr->getOperand(0);
3593 Offset = Ptr->getOperand(1);
3594 }
3595 return true;
3596 }
3597
3598 isInc = (Ptr->getOpcode() == ISD::ADD);
3599 Base = Ptr->getOperand(0);
3600 Offset = Ptr->getOperand(1);
3601 return true;
3602 }
3603
3604 // FIXME: Use FLDM / FSTM to emulate indexed FP load / store.
3605 return false;
3606}
3607
Owen Andersone50ed302009-08-10 22:56:29 +00003608static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00003609 bool isSEXTLoad, SDValue &Base,
3610 SDValue &Offset, bool &isInc,
3611 SelectionDAG &DAG) {
3612 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
3613 return false;
3614
3615 Base = Ptr->getOperand(0);
3616 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
3617 int RHSC = (int)RHS->getZExtValue();
3618 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
3619 assert(Ptr->getOpcode() == ISD::ADD);
3620 isInc = false;
3621 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3622 return true;
3623 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
3624 isInc = Ptr->getOpcode() == ISD::ADD;
3625 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
3626 return true;
3627 }
3628 }
3629
3630 return false;
3631}
3632
Evan Chenga8e29892007-01-19 07:51:42 +00003633/// getPreIndexedAddressParts - returns true by value, base pointer and
3634/// offset pointer and addressing mode by reference if the node's address
3635/// can be legally represented as pre-indexed load / store address.
3636bool
Dan Gohman475871a2008-07-27 21:46:04 +00003637ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
3638 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003639 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00003640 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003641 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00003642 return false;
3643
Owen Andersone50ed302009-08-10 22:56:29 +00003644 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00003645 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00003646 bool isSEXTLoad = false;
3647 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
3648 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003649 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003650 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
3651 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
3652 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003653 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003654 } else
3655 return false;
3656
3657 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00003658 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00003659 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00003660 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
3661 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00003662 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00003663 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00003664 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00003665 if (!isLegal)
3666 return false;
3667
3668 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
3669 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00003670}
3671
3672/// getPostIndexedAddressParts - returns true by value, base pointer and
3673/// offset pointer and addressing mode by reference if this node can be
3674/// combined with a load / store to form a post-indexed load / store.
3675bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00003676 SDValue &Base,
3677 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003678 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00003679 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003680 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00003681 return false;
3682
Owen Andersone50ed302009-08-10 22:56:29 +00003683 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00003684 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00003685 bool isSEXTLoad = false;
3686 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003687 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003688 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
3689 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003690 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003691 } else
3692 return false;
3693
3694 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00003695 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00003696 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00003697 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003698 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00003699 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00003700 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
3701 isInc, DAG);
3702 if (!isLegal)
3703 return false;
3704
3705 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
3706 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00003707}
3708
Dan Gohman475871a2008-07-27 21:46:04 +00003709void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00003710 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003711 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003712 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00003713 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00003714 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003715 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00003716 switch (Op.getOpcode()) {
3717 default: break;
3718 case ARMISD::CMOV: {
3719 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00003720 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00003721 if (KnownZero == 0 && KnownOne == 0) return;
3722
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003723 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00003724 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
3725 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00003726 KnownZero &= KnownZeroRHS;
3727 KnownOne &= KnownOneRHS;
3728 return;
3729 }
3730 }
3731}
3732
3733//===----------------------------------------------------------------------===//
3734// ARM Inline Assembly Support
3735//===----------------------------------------------------------------------===//
3736
3737/// getConstraintType - Given a constraint letter, return the type of
3738/// constraint it is for this target.
3739ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00003740ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
3741 if (Constraint.size() == 1) {
3742 switch (Constraint[0]) {
3743 default: break;
3744 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003745 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00003746 }
Evan Chenga8e29892007-01-19 07:51:42 +00003747 }
Chris Lattner4234f572007-03-25 02:14:49 +00003748 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00003749}
3750
Bob Wilson2dc4f542009-03-20 22:42:55 +00003751std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00003752ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00003753 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003754 if (Constraint.size() == 1) {
3755 // GCC RS6000 Constraint Letters
3756 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003757 case 'l':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003758 if (Subtarget->isThumb1Only())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00003759 return std::make_pair(0U, ARM::tGPRRegisterClass);
3760 else
3761 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003762 case 'r':
3763 return std::make_pair(0U, ARM::GPRRegisterClass);
3764 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00003765 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003766 return std::make_pair(0U, ARM::SPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00003767 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003768 return std::make_pair(0U, ARM::DPRRegisterClass);
3769 break;
Evan Chenga8e29892007-01-19 07:51:42 +00003770 }
3771 }
3772 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3773}
3774
3775std::vector<unsigned> ARMTargetLowering::
3776getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00003777 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003778 if (Constraint.size() != 1)
3779 return std::vector<unsigned>();
3780
3781 switch (Constraint[0]) { // GCC ARM Constraint Letters
3782 default: break;
3783 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00003784 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
3785 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
3786 0);
Evan Chenga8e29892007-01-19 07:51:42 +00003787 case 'r':
3788 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
3789 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
3790 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
3791 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003792 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00003793 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003794 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
3795 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
3796 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
3797 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
3798 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
3799 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
3800 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
3801 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00003802 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003803 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
3804 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
3805 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
3806 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
3807 break;
Evan Chenga8e29892007-01-19 07:51:42 +00003808 }
3809
3810 return std::vector<unsigned>();
3811}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003812
3813/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3814/// vector. If it is invalid, don't add anything to Ops.
3815void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3816 char Constraint,
3817 bool hasMemory,
3818 std::vector<SDValue>&Ops,
3819 SelectionDAG &DAG) const {
3820 SDValue Result(0, 0);
3821
3822 switch (Constraint) {
3823 default: break;
3824 case 'I': case 'J': case 'K': case 'L':
3825 case 'M': case 'N': case 'O':
3826 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
3827 if (!C)
3828 return;
3829
3830 int64_t CVal64 = C->getSExtValue();
3831 int CVal = (int) CVal64;
3832 // None of these constraints allow values larger than 32 bits. Check
3833 // that the value fits in an int.
3834 if (CVal != CVal64)
3835 return;
3836
3837 switch (Constraint) {
3838 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003839 if (Subtarget->isThumb1Only()) {
3840 // This must be a constant between 0 and 255, for ADD
3841 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003842 if (CVal >= 0 && CVal <= 255)
3843 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00003844 } else if (Subtarget->isThumb2()) {
3845 // A constant that can be used as an immediate value in a
3846 // data-processing instruction.
3847 if (ARM_AM::getT2SOImmVal(CVal) != -1)
3848 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003849 } else {
3850 // A constant that can be used as an immediate value in a
3851 // data-processing instruction.
3852 if (ARM_AM::getSOImmVal(CVal) != -1)
3853 break;
3854 }
3855 return;
3856
3857 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003858 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003859 // This must be a constant between -255 and -1, for negated ADD
3860 // immediates. This can be used in GCC with an "n" modifier that
3861 // prints the negated value, for use with SUB instructions. It is
3862 // not useful otherwise but is implemented for compatibility.
3863 if (CVal >= -255 && CVal <= -1)
3864 break;
3865 } else {
3866 // This must be a constant between -4095 and 4095. It is not clear
3867 // what this constraint is intended for. Implemented for
3868 // compatibility with GCC.
3869 if (CVal >= -4095 && CVal <= 4095)
3870 break;
3871 }
3872 return;
3873
3874 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003875 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003876 // A 32-bit value where only one byte has a nonzero value. Exclude
3877 // zero to match GCC. This constraint is used by GCC internally for
3878 // constants that can be loaded with a move/shift combination.
3879 // It is not useful otherwise but is implemented for compatibility.
3880 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
3881 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00003882 } else if (Subtarget->isThumb2()) {
3883 // A constant whose bitwise inverse can be used as an immediate
3884 // value in a data-processing instruction. This can be used in GCC
3885 // with a "B" modifier that prints the inverted value, for use with
3886 // BIC and MVN instructions. It is not useful otherwise but is
3887 // implemented for compatibility.
3888 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
3889 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003890 } else {
3891 // A constant whose bitwise inverse can be used as an immediate
3892 // value in a data-processing instruction. This can be used in GCC
3893 // with a "B" modifier that prints the inverted value, for use with
3894 // BIC and MVN instructions. It is not useful otherwise but is
3895 // implemented for compatibility.
3896 if (ARM_AM::getSOImmVal(~CVal) != -1)
3897 break;
3898 }
3899 return;
3900
3901 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003902 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003903 // This must be a constant between -7 and 7,
3904 // for 3-operand ADD/SUB immediate instructions.
3905 if (CVal >= -7 && CVal < 7)
3906 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00003907 } else if (Subtarget->isThumb2()) {
3908 // A constant whose negation can be used as an immediate value in a
3909 // data-processing instruction. This can be used in GCC with an "n"
3910 // modifier that prints the negated value, for use with SUB
3911 // instructions. It is not useful otherwise but is implemented for
3912 // compatibility.
3913 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
3914 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003915 } else {
3916 // A constant whose negation can be used as an immediate value in a
3917 // data-processing instruction. This can be used in GCC with an "n"
3918 // modifier that prints the negated value, for use with SUB
3919 // instructions. It is not useful otherwise but is implemented for
3920 // compatibility.
3921 if (ARM_AM::getSOImmVal(-CVal) != -1)
3922 break;
3923 }
3924 return;
3925
3926 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003927 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003928 // This must be a multiple of 4 between 0 and 1020, for
3929 // ADD sp + immediate.
3930 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
3931 break;
3932 } else {
3933 // A power of two or a constant between 0 and 32. This is used in
3934 // GCC for the shift amount on shifted register operands, but it is
3935 // useful in general for any shift amounts.
3936 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
3937 break;
3938 }
3939 return;
3940
3941 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003942 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003943 // This must be a constant between 0 and 31, for shift amounts.
3944 if (CVal >= 0 && CVal <= 31)
3945 break;
3946 }
3947 return;
3948
3949 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003950 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003951 // This must be a multiple of 4 between -508 and 508, for
3952 // ADD/SUB sp = sp + immediate.
3953 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
3954 break;
3955 }
3956 return;
3957 }
3958 Result = DAG.getTargetConstant(CVal, Op.getValueType());
3959 break;
3960 }
3961
3962 if (Result.getNode()) {
3963 Ops.push_back(Result);
3964 return;
3965 }
3966 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
3967 Ops, DAG);
3968}