blob: 78f054e62c96417905fbf6b5b61d4697b10f415b [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);
Anton Korobeynikov1cb852b2009-08-26 16:26:09 +000081 setOperationAction(ISD::SCALAR_TO_VECTOR, VT.getSimpleVT(), Expand);
Owen Anderson70671842009-08-10 20:18:46 +000082 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Custom);
Anton Korobeynikov8e6c2b92009-08-21 12:40:35 +000083 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +000084 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +000085 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
86 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
87 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +000088 }
89
90 // Promote all bit-wise operations.
91 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +000092 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000093 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
94 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +000095 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +000096 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +000097 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +000098 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +000099 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000100 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000101 }
102}
103
Owen Andersone50ed302009-08-10 22:56:29 +0000104void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000105 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000106 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000107}
108
Owen Andersone50ed302009-08-10 22:56:29 +0000109void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000110 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000111 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000112}
113
Chris Lattnerf0144122009-07-28 03:13:23 +0000114static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
115 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000116 return new TargetLoweringObjectFileMachO();
Chris Lattner80ec2792009-08-02 00:34:36 +0000117 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000118}
119
Evan Chenga8e29892007-01-19 07:51:42 +0000120ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000121 : TargetLowering(TM, createTLOF(TM)), ARMPCLabelIndex(0) {
Evan Chenga8e29892007-01-19 07:51:42 +0000122 Subtarget = &TM.getSubtarget<ARMSubtarget>();
123
Evan Chengb1df8f22007-04-27 08:15:43 +0000124 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000125 // Uses VFP for Thumb libfuncs if available.
126 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
127 // Single-precision floating-point arithmetic.
128 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
129 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
130 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
131 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000132
Evan Chengb1df8f22007-04-27 08:15:43 +0000133 // Double-precision floating-point arithmetic.
134 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
135 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
136 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
137 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000138
Evan Chengb1df8f22007-04-27 08:15:43 +0000139 // Single-precision comparisons.
140 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
141 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
142 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
143 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
144 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
145 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
146 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
147 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000148
Evan Chengb1df8f22007-04-27 08:15:43 +0000149 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
150 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
151 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
152 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
153 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
154 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
155 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
156 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000157
Evan Chengb1df8f22007-04-27 08:15:43 +0000158 // Double-precision comparisons.
159 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
160 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
161 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
162 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
163 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
164 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
165 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
166 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000167
Evan Chengb1df8f22007-04-27 08:15:43 +0000168 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
169 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
170 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
171 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
172 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
173 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
174 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
175 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000176
Evan Chengb1df8f22007-04-27 08:15:43 +0000177 // Floating-point to integer conversions.
178 // i64 conversions are done via library routines even when generating VFP
179 // instructions, so use the same ones.
180 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
181 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
182 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
183 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000184
Evan Chengb1df8f22007-04-27 08:15:43 +0000185 // Conversions between floating types.
186 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
187 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
188
189 // Integer to floating-point conversions.
190 // i64 conversions are done via library routines even when generating VFP
191 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000192 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
193 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000194 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
195 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
196 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
197 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
198 }
Evan Chenga8e29892007-01-19 07:51:42 +0000199 }
200
Bob Wilson2f954612009-05-22 17:38:41 +0000201 // These libcalls are not available in 32-bit.
202 setLibcallName(RTLIB::SHL_I128, 0);
203 setLibcallName(RTLIB::SRL_I128, 0);
204 setLibcallName(RTLIB::SRA_I128, 0);
205
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000206 // Libcalls should use the AAPCS base standard ABI, even if hard float
207 // is in effect, as per the ARM RTABI specification, section 4.1.2.
208 if (Subtarget->isAAPCS_ABI()) {
209 for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i) {
210 setLibcallCallingConv(static_cast<RTLIB::Libcall>(i),
211 CallingConv::ARM_AAPCS);
212 }
213 }
214
David Goodwinf1daf7d2009-07-08 23:10:31 +0000215 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000216 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000217 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000218 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000219 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000220 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
221 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000222
Owen Anderson825b72b2009-08-11 20:47:22 +0000223 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000224 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000225
226 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000227 addDRTypeForNEON(MVT::v2f32);
228 addDRTypeForNEON(MVT::v8i8);
229 addDRTypeForNEON(MVT::v4i16);
230 addDRTypeForNEON(MVT::v2i32);
231 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000232
Owen Anderson825b72b2009-08-11 20:47:22 +0000233 addQRTypeForNEON(MVT::v4f32);
234 addQRTypeForNEON(MVT::v2f64);
235 addQRTypeForNEON(MVT::v16i8);
236 addQRTypeForNEON(MVT::v8i16);
237 addQRTypeForNEON(MVT::v4i32);
238 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000239
240 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
241 setTargetDAGCombine(ISD::SHL);
242 setTargetDAGCombine(ISD::SRL);
243 setTargetDAGCombine(ISD::SRA);
244 setTargetDAGCombine(ISD::SIGN_EXTEND);
245 setTargetDAGCombine(ISD::ZERO_EXTEND);
246 setTargetDAGCombine(ISD::ANY_EXTEND);
247 }
248
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000249 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000250
251 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000252 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000253
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000254 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000255 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000256
Evan Chenga8e29892007-01-19 07:51:42 +0000257 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000258 if (!Subtarget->isThumb1Only()) {
259 for (unsigned im = (unsigned)ISD::PRE_INC;
260 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000261 setIndexedLoadAction(im, MVT::i1, Legal);
262 setIndexedLoadAction(im, MVT::i8, Legal);
263 setIndexedLoadAction(im, MVT::i16, Legal);
264 setIndexedLoadAction(im, MVT::i32, Legal);
265 setIndexedStoreAction(im, MVT::i1, Legal);
266 setIndexedStoreAction(im, MVT::i8, Legal);
267 setIndexedStoreAction(im, MVT::i16, Legal);
268 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000269 }
Evan Chenga8e29892007-01-19 07:51:42 +0000270 }
271
272 // i64 operation support.
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000273 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000274 setOperationAction(ISD::MUL, MVT::i64, Expand);
275 setOperationAction(ISD::MULHU, MVT::i32, Expand);
276 setOperationAction(ISD::MULHS, MVT::i32, Expand);
277 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
278 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000279 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000280 setOperationAction(ISD::MUL, MVT::i64, Expand);
281 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb6207242009-08-01 00:16:10 +0000282 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000283 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000284 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000285 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
286 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
287 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
288 setOperationAction(ISD::SRL, MVT::i64, Custom);
289 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000290
291 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000292 setOperationAction(ISD::ROTL, MVT::i32, Expand);
293 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
294 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000295 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000296 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000297
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000298 // Only ARMv6 has BSWAP.
299 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000300 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000301
Evan Chenga8e29892007-01-19 07:51:42 +0000302 // These are expanded into libcalls.
Owen Anderson825b72b2009-08-11 20:47:22 +0000303 setOperationAction(ISD::SDIV, MVT::i32, Expand);
304 setOperationAction(ISD::UDIV, MVT::i32, Expand);
305 setOperationAction(ISD::SREM, MVT::i32, Expand);
306 setOperationAction(ISD::UREM, MVT::i32, Expand);
307 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
308 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000309
Evan Chenga8e29892007-01-19 07:51:42 +0000310 // Support label based line numbers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000311 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
312 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000313
Owen Anderson825b72b2009-08-11 20:47:22 +0000314 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
315 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
316 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
317 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000318
Evan Chenga8e29892007-01-19 07:51:42 +0000319 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000320 setOperationAction(ISD::VASTART, MVT::Other, Custom);
321 setOperationAction(ISD::VAARG, MVT::Other, Expand);
322 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
323 setOperationAction(ISD::VAEND, MVT::Other, Expand);
324 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
325 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000326 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
327 // FIXME: Shouldn't need this, since no register is used, but the legalizer
328 // doesn't yet know how to not do that for SjLj.
329 setExceptionSelectorRegister(ARM::R0);
Evan Cheng86198642009-08-07 00:34:42 +0000330 if (Subtarget->isThumb())
Owen Anderson825b72b2009-08-11 20:47:22 +0000331 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Evan Cheng86198642009-08-07 00:34:42 +0000332 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000333 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
334 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000335
Evan Chengd27c9fc2009-07-03 01:43:10 +0000336 if (!Subtarget->hasV6Ops() && !Subtarget->isThumb2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000337 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
338 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000339 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000340 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000341
David Goodwinf1daf7d2009-07-08 23:10:31 +0000342 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only())
Evan Chengc7c77292008-11-04 19:57:48 +0000343 // Turn f64->i64 into FMRRD, i64 -> f64 to FMDRR iff target supports vfp2.
Owen Anderson825b72b2009-08-11 20:47:22 +0000344 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000345
346 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000347 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
348 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
349 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000350
Owen Anderson825b72b2009-08-11 20:47:22 +0000351 setOperationAction(ISD::SETCC, MVT::i32, Expand);
352 setOperationAction(ISD::SETCC, MVT::f32, Expand);
353 setOperationAction(ISD::SETCC, MVT::f64, Expand);
354 setOperationAction(ISD::SELECT, MVT::i32, Expand);
355 setOperationAction(ISD::SELECT, MVT::f32, Expand);
356 setOperationAction(ISD::SELECT, MVT::f64, Expand);
357 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
358 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
359 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000360
Owen Anderson825b72b2009-08-11 20:47:22 +0000361 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
362 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
363 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
364 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
365 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000366
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000367 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000368 setOperationAction(ISD::FSIN, MVT::f64, Expand);
369 setOperationAction(ISD::FSIN, MVT::f32, Expand);
370 setOperationAction(ISD::FCOS, MVT::f32, Expand);
371 setOperationAction(ISD::FCOS, MVT::f64, Expand);
372 setOperationAction(ISD::FREM, MVT::f64, Expand);
373 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000374 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000375 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
376 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000377 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000378 setOperationAction(ISD::FPOW, MVT::f64, Expand);
379 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000380
Evan Chenga8e29892007-01-19 07:51:42 +0000381 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
David Goodwinf1daf7d2009-07-08 23:10:31 +0000382 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000383 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
384 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
385 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
386 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000387 }
Evan Chenga8e29892007-01-19 07:51:42 +0000388
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000389 // We have target-specific dag combine patterns for the following nodes:
390 // ARMISD::FMRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000391 setTargetDAGCombine(ISD::ADD);
392 setTargetDAGCombine(ISD::SUB);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000393
Evan Chenga8e29892007-01-19 07:51:42 +0000394 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Chenga8e29892007-01-19 07:51:42 +0000395 setSchedulingPreference(SchedulingForRegPressure);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000396
Evan Chengbc9b7542009-08-15 07:59:10 +0000397 // FIXME: If-converter should use instruction latency to determine
398 // profitability rather than relying on fixed limits.
399 if (Subtarget->getCPUString() == "generic") {
400 // Generic (and overly aggressive) if-conversion limits.
401 setIfCvtBlockSizeLimit(10);
402 setIfCvtDupBlockSizeLimit(2);
403 } else if (Subtarget->hasV6Ops()) {
404 setIfCvtBlockSizeLimit(2);
405 setIfCvtDupBlockSizeLimit(1);
406 } else {
407 setIfCvtBlockSizeLimit(3);
408 setIfCvtDupBlockSizeLimit(2);
Evan Cheng8557c2b2009-06-19 01:51:50 +0000409 }
410
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000411 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Bob Wilsone6abdff2009-05-18 20:55:32 +0000412 // Do not enable CodePlacementOpt for now: it currently runs after the
413 // ARMConstantIslandPass and messes up branch relaxation and placement
414 // of constant islands.
415 // benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000416}
417
Evan Chenga8e29892007-01-19 07:51:42 +0000418const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
419 switch (Opcode) {
420 default: return 0;
421 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000422 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
423 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000424 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000425 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
426 case ARMISD::tCALL: return "ARMISD::tCALL";
427 case ARMISD::BRCOND: return "ARMISD::BRCOND";
428 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000429 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000430 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
431 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
432 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000433 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000434 case ARMISD::CMPFP: return "ARMISD::CMPFP";
435 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
436 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
437 case ARMISD::CMOV: return "ARMISD::CMOV";
438 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000439
Evan Chenga8e29892007-01-19 07:51:42 +0000440 case ARMISD::FTOSI: return "ARMISD::FTOSI";
441 case ARMISD::FTOUI: return "ARMISD::FTOUI";
442 case ARMISD::SITOF: return "ARMISD::SITOF";
443 case ARMISD::UITOF: return "ARMISD::UITOF";
Evan Chenga8e29892007-01-19 07:51:42 +0000444
445 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
446 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
447 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000448
Evan Chenga8e29892007-01-19 07:51:42 +0000449 case ARMISD::FMRRD: return "ARMISD::FMRRD";
450 case ARMISD::FMDRR: return "ARMISD::FMDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000451
452 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000453
Evan Cheng86198642009-08-07 00:34:42 +0000454 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
455
Bob Wilson5bafff32009-06-22 23:27:02 +0000456 case ARMISD::VCEQ: return "ARMISD::VCEQ";
457 case ARMISD::VCGE: return "ARMISD::VCGE";
458 case ARMISD::VCGEU: return "ARMISD::VCGEU";
459 case ARMISD::VCGT: return "ARMISD::VCGT";
460 case ARMISD::VCGTU: return "ARMISD::VCGTU";
461 case ARMISD::VTST: return "ARMISD::VTST";
462
463 case ARMISD::VSHL: return "ARMISD::VSHL";
464 case ARMISD::VSHRs: return "ARMISD::VSHRs";
465 case ARMISD::VSHRu: return "ARMISD::VSHRu";
466 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
467 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
468 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
469 case ARMISD::VSHRN: return "ARMISD::VSHRN";
470 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
471 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
472 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
473 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
474 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
475 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
476 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
477 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
478 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
479 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
480 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
481 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
482 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
483 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000484 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000485 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000486 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000487 case ARMISD::VREV64: return "ARMISD::VREV64";
488 case ARMISD::VREV32: return "ARMISD::VREV32";
489 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000490 case ARMISD::VZIP: return "ARMISD::VZIP";
491 case ARMISD::VUZP: return "ARMISD::VUZP";
492 case ARMISD::VTRN: return "ARMISD::VTRN";
Evan Chenga8e29892007-01-19 07:51:42 +0000493 }
494}
495
Bill Wendlingb4202b82009-07-01 18:50:55 +0000496/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000497unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
498 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 1 : 2;
499}
500
Evan Chenga8e29892007-01-19 07:51:42 +0000501//===----------------------------------------------------------------------===//
502// Lowering Code
503//===----------------------------------------------------------------------===//
504
Evan Chenga8e29892007-01-19 07:51:42 +0000505/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
506static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
507 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000508 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000509 case ISD::SETNE: return ARMCC::NE;
510 case ISD::SETEQ: return ARMCC::EQ;
511 case ISD::SETGT: return ARMCC::GT;
512 case ISD::SETGE: return ARMCC::GE;
513 case ISD::SETLT: return ARMCC::LT;
514 case ISD::SETLE: return ARMCC::LE;
515 case ISD::SETUGT: return ARMCC::HI;
516 case ISD::SETUGE: return ARMCC::HS;
517 case ISD::SETULT: return ARMCC::LO;
518 case ISD::SETULE: return ARMCC::LS;
519 }
520}
521
522/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC. It
523/// returns true if the operands should be inverted to form the proper
524/// comparison.
525static bool FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
526 ARMCC::CondCodes &CondCode2) {
527 bool Invert = false;
528 CondCode2 = ARMCC::AL;
529 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000530 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000531 case ISD::SETEQ:
532 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
533 case ISD::SETGT:
534 case ISD::SETOGT: CondCode = ARMCC::GT; break;
535 case ISD::SETGE:
536 case ISD::SETOGE: CondCode = ARMCC::GE; break;
537 case ISD::SETOLT: CondCode = ARMCC::MI; break;
538 case ISD::SETOLE: CondCode = ARMCC::GT; Invert = true; break;
539 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
540 case ISD::SETO: CondCode = ARMCC::VC; break;
541 case ISD::SETUO: CondCode = ARMCC::VS; break;
542 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
543 case ISD::SETUGT: CondCode = ARMCC::HI; break;
544 case ISD::SETUGE: CondCode = ARMCC::PL; break;
545 case ISD::SETLT:
546 case ISD::SETULT: CondCode = ARMCC::LT; break;
547 case ISD::SETLE:
548 case ISD::SETULE: CondCode = ARMCC::LE; break;
549 case ISD::SETNE:
550 case ISD::SETUNE: CondCode = ARMCC::NE; break;
551 }
552 return Invert;
553}
554
Bob Wilson1f595bb2009-04-17 19:07:39 +0000555//===----------------------------------------------------------------------===//
556// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +0000557//===----------------------------------------------------------------------===//
558
559#include "ARMGenCallingConv.inc"
560
561// APCS f64 is in register pairs, possibly split to stack
Owen Andersone50ed302009-08-10 22:56:29 +0000562static bool f64AssignAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000563 CCValAssign::LocInfo &LocInfo,
564 CCState &State, bool CanFail) {
565 static const unsigned RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
566
567 // Try to get the first register.
568 if (unsigned Reg = State.AllocateReg(RegList, 4))
569 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
570 else {
571 // For the 2nd half of a v2f64, do not fail.
572 if (CanFail)
573 return false;
574
575 // Put the whole thing on the stack.
576 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
577 State.AllocateStack(8, 4),
578 LocVT, LocInfo));
579 return true;
580 }
581
582 // Try to get the second register.
583 if (unsigned Reg = State.AllocateReg(RegList, 4))
584 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
585 else
586 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
587 State.AllocateStack(4, 4),
588 LocVT, LocInfo));
589 return true;
590}
591
Owen Andersone50ed302009-08-10 22:56:29 +0000592static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000593 CCValAssign::LocInfo &LocInfo,
594 ISD::ArgFlagsTy &ArgFlags,
595 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000596 if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
597 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000598 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000599 !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
600 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000601 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000602}
603
604// AAPCS f64 is in aligned register pairs
Owen Andersone50ed302009-08-10 22:56:29 +0000605static bool f64AssignAAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000606 CCValAssign::LocInfo &LocInfo,
607 CCState &State, bool CanFail) {
608 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
609 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
610
611 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
612 if (Reg == 0) {
613 // For the 2nd half of a v2f64, do not just fail.
614 if (CanFail)
615 return false;
616
617 // Put the whole thing on the stack.
618 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
619 State.AllocateStack(8, 8),
620 LocVT, LocInfo));
621 return true;
622 }
623
624 unsigned i;
625 for (i = 0; i < 2; ++i)
626 if (HiRegList[i] == Reg)
627 break;
628
629 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
630 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
631 LocVT, LocInfo));
632 return true;
633}
634
Owen Andersone50ed302009-08-10 22:56:29 +0000635static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000636 CCValAssign::LocInfo &LocInfo,
637 ISD::ArgFlagsTy &ArgFlags,
638 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000639 if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
640 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000641 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000642 !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
643 return false;
644 return true; // we handled it
645}
646
Owen Andersone50ed302009-08-10 22:56:29 +0000647static bool f64RetAssign(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000648 CCValAssign::LocInfo &LocInfo, CCState &State) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000649 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
650 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
651
Bob Wilsone65586b2009-04-17 20:40:45 +0000652 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
653 if (Reg == 0)
654 return false; // we didn't handle it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000655
Bob Wilsone65586b2009-04-17 20:40:45 +0000656 unsigned i;
657 for (i = 0; i < 2; ++i)
658 if (HiRegList[i] == Reg)
659 break;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000660
Bob Wilson5bafff32009-06-22 23:27:02 +0000661 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bob Wilsone65586b2009-04-17 20:40:45 +0000662 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000663 LocVT, LocInfo));
664 return true;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000665}
666
Owen Andersone50ed302009-08-10 22:56:29 +0000667static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000668 CCValAssign::LocInfo &LocInfo,
669 ISD::ArgFlagsTy &ArgFlags,
670 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000671 if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
672 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000673 if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
Bob Wilson5bafff32009-06-22 23:27:02 +0000674 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000675 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000676}
677
Owen Andersone50ed302009-08-10 22:56:29 +0000678static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000679 CCValAssign::LocInfo &LocInfo,
680 ISD::ArgFlagsTy &ArgFlags,
681 CCState &State) {
682 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
683 State);
684}
685
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000686/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
687/// given CallingConvention value.
688CCAssignFn *ARMTargetLowering::CCAssignFnForNode(unsigned CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000689 bool Return,
690 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000691 switch (CC) {
692 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000693 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000694 case CallingConv::C:
695 case CallingConv::Fast:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000696 // Use target triple & subtarget features to do actual dispatch.
697 if (Subtarget->isAAPCS_ABI()) {
698 if (Subtarget->hasVFP2() &&
699 FloatABIType == FloatABI::Hard && !isVarArg)
700 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
701 else
702 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
703 } else
704 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000705 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000706 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000707 case CallingConv::ARM_AAPCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000708 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000709 case CallingConv::ARM_APCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000710 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000711 }
712}
713
Dan Gohman98ca4f22009-08-05 01:29:28 +0000714/// LowerCallResult - Lower the result values of a call into the
715/// appropriate copies out of appropriate physical registers.
716SDValue
717ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
718 unsigned CallConv, bool isVarArg,
719 const SmallVectorImpl<ISD::InputArg> &Ins,
720 DebugLoc dl, SelectionDAG &DAG,
721 SmallVectorImpl<SDValue> &InVals) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000722
Bob Wilson1f595bb2009-04-17 19:07:39 +0000723 // Assign locations to each value returned by this call.
724 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000725 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000726 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000727 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000728 CCAssignFnForNode(CallConv, /* Return*/ true,
729 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000730
731 // Copy all of the result registers out of their specified physreg.
732 for (unsigned i = 0; i != RVLocs.size(); ++i) {
733 CCValAssign VA = RVLocs[i];
734
Bob Wilson80915242009-04-25 00:33:20 +0000735 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000736 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000737 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000738 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000739 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000740 Chain = Lo.getValue(1);
741 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000742 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000743 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000744 InFlag);
745 Chain = Hi.getValue(1);
746 InFlag = Hi.getValue(2);
Owen Anderson825b72b2009-08-11 20:47:22 +0000747 Val = DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +0000748
Owen Anderson825b72b2009-08-11 20:47:22 +0000749 if (VA.getLocVT() == MVT::v2f64) {
750 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
751 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
752 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000753
754 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000755 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000756 Chain = Lo.getValue(1);
757 InFlag = Lo.getValue(2);
758 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000759 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000760 Chain = Hi.getValue(1);
761 InFlag = Hi.getValue(2);
Owen Anderson825b72b2009-08-11 20:47:22 +0000762 Val = DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
763 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
764 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000765 }
Bob Wilson1f595bb2009-04-17 19:07:39 +0000766 } else {
Bob Wilson80915242009-04-25 00:33:20 +0000767 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
768 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000769 Chain = Val.getValue(1);
770 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000771 }
Bob Wilson80915242009-04-25 00:33:20 +0000772
773 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000774 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +0000775 case CCValAssign::Full: break;
776 case CCValAssign::BCvt:
777 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
778 break;
779 }
780
Dan Gohman98ca4f22009-08-05 01:29:28 +0000781 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000782 }
783
Dan Gohman98ca4f22009-08-05 01:29:28 +0000784 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000785}
786
787/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
788/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +0000789/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +0000790/// a byval function parameter.
791/// Sometimes what we are copying is the end of a larger object, the part that
792/// does not fit in registers.
793static SDValue
794CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
795 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
796 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000797 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000798 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
799 /*AlwaysInline=*/false, NULL, 0, NULL, 0);
800}
801
Bob Wilsondee46d72009-04-17 20:35:10 +0000802/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000803SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +0000804ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
805 SDValue StackPtr, SDValue Arg,
806 DebugLoc dl, SelectionDAG &DAG,
807 const CCValAssign &VA,
808 ISD::ArgFlagsTy Flags) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000809 unsigned LocMemOffset = VA.getLocMemOffset();
810 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
811 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
812 if (Flags.isByVal()) {
813 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
814 }
815 return DAG.getStore(Chain, dl, Arg, PtrOff,
816 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chenga8e29892007-01-19 07:51:42 +0000817}
818
Dan Gohman98ca4f22009-08-05 01:29:28 +0000819void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +0000820 SDValue Chain, SDValue &Arg,
821 RegsToPassVector &RegsToPass,
822 CCValAssign &VA, CCValAssign &NextVA,
823 SDValue &StackPtr,
824 SmallVector<SDValue, 8> &MemOpChains,
825 ISD::ArgFlagsTy Flags) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000826
827 SDValue fmrrd = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +0000828 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +0000829 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
830
831 if (NextVA.isRegLoc())
832 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
833 else {
834 assert(NextVA.isMemLoc());
835 if (StackPtr.getNode() == 0)
836 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
837
Dan Gohman98ca4f22009-08-05 01:29:28 +0000838 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
839 dl, DAG, NextVA,
840 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000841 }
842}
843
Dan Gohman98ca4f22009-08-05 01:29:28 +0000844/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +0000845/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
846/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000847SDValue
848ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
849 unsigned CallConv, bool isVarArg,
850 bool isTailCall,
851 const SmallVectorImpl<ISD::OutputArg> &Outs,
852 const SmallVectorImpl<ISD::InputArg> &Ins,
853 DebugLoc dl, SelectionDAG &DAG,
854 SmallVectorImpl<SDValue> &InVals) {
Evan Chenga8e29892007-01-19 07:51:42 +0000855
Bob Wilson1f595bb2009-04-17 19:07:39 +0000856 // Analyze operands of the call, assigning locations to each operand.
857 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000858 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
859 *DAG.getContext());
860 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000861 CCAssignFnForNode(CallConv, /* Return*/ false,
862 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +0000863
Bob Wilson1f595bb2009-04-17 19:07:39 +0000864 // Get a count of how many bytes are to be pushed on the stack.
865 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +0000866
867 // Adjust the stack pointer for the new arguments...
868 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattnere563bbc2008-10-11 22:08:30 +0000869 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +0000870
Owen Anderson825b72b2009-08-11 20:47:22 +0000871 SDValue StackPtr = DAG.getRegister(ARM::SP, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000872
Bob Wilson5bafff32009-06-22 23:27:02 +0000873 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000874 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +0000875
Bob Wilson1f595bb2009-04-17 19:07:39 +0000876 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +0000877 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000878 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
879 i != e;
880 ++i, ++realArgIdx) {
881 CCValAssign &VA = ArgLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +0000882 SDValue Arg = Outs[realArgIdx].Val;
883 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +0000884
Bob Wilson1f595bb2009-04-17 19:07:39 +0000885 // Promote the value if needed.
886 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000887 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +0000888 case CCValAssign::Full: break;
889 case CCValAssign::SExt:
890 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
891 break;
892 case CCValAssign::ZExt:
893 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
894 break;
895 case CCValAssign::AExt:
896 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
897 break;
898 case CCValAssign::BCvt:
899 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
900 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000901 }
902
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000903 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +0000904 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000905 if (VA.getLocVT() == MVT::v2f64) {
906 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
907 DAG.getConstant(0, MVT::i32));
908 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
909 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000910
Dan Gohman98ca4f22009-08-05 01:29:28 +0000911 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +0000912 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
913
914 VA = ArgLocs[++i]; // skip ahead to next loc
915 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000916 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +0000917 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
918 } else {
919 assert(VA.isMemLoc());
920 if (StackPtr.getNode() == 0)
921 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
922
Dan Gohman98ca4f22009-08-05 01:29:28 +0000923 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
924 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000925 }
926 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000927 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000928 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000929 }
930 } else if (VA.isRegLoc()) {
931 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
932 } else {
933 assert(VA.isMemLoc());
934 if (StackPtr.getNode() == 0)
935 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
936
Dan Gohman98ca4f22009-08-05 01:29:28 +0000937 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
938 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000939 }
Evan Chenga8e29892007-01-19 07:51:42 +0000940 }
941
942 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000943 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +0000944 &MemOpChains[0], MemOpChains.size());
945
946 // Build a sequence of copy-to-reg nodes chained together with token chain
947 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +0000948 SDValue InFlag;
Evan Chenga8e29892007-01-19 07:51:42 +0000949 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Bob Wilson2dc4f542009-03-20 22:42:55 +0000950 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000951 RegsToPass[i].second, InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +0000952 InFlag = Chain.getValue(1);
953 }
954
Bill Wendling056292f2008-09-16 21:48:12 +0000955 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
956 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
957 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +0000958 bool isDirect = false;
959 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +0000960 bool isLocalARMFunc = false;
Evan Chenga8e29892007-01-19 07:51:42 +0000961 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
962 GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +0000963 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +0000964 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +0000965 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +0000966 getTargetMachine().getRelocationModel() != Reloc::Static;
967 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +0000968 // ARM call to a local ARM function is predicable.
969 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
Evan Chengc60e76d2007-01-30 20:37:08 +0000970 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +0000971 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chengc60e76d2007-01-30 20:37:08 +0000972 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex,
973 ARMCP::CPStub, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000974 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +0000975 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000976 Callee = DAG.getLoad(getPointerTy(), dl,
977 DAG.getEntryNode(), CPAddr, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000978 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000979 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000980 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +0000981 } else
982 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +0000983 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000984 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +0000985 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +0000986 getTargetMachine().getRelocationModel() != Reloc::Static;
987 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +0000988 // tBX takes a register source operand.
989 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +0000990 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000991 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
992 Sym, ARMPCLabelIndex,
Evan Chengc60e76d2007-01-30 20:37:08 +0000993 ARMCP::CPStub, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000994 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +0000995 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000996 Callee = DAG.getLoad(getPointerTy(), dl,
Bob Wilson2dc4f542009-03-20 22:42:55 +0000997 DAG.getEntryNode(), CPAddr, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000998 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000999 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001000 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001001 } else
Bill Wendling056292f2008-09-16 21:48:12 +00001002 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001003 }
1004
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001005 // FIXME: handle tail calls differently.
1006 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001007 if (Subtarget->isThumb()) {
1008 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001009 CallOpc = ARMISD::CALL_NOLINK;
1010 else
1011 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1012 } else {
1013 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001014 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1015 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001016 }
David Goodwinf1daf7d2009-07-08 23:10:31 +00001017 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb1Only()) {
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +00001018 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Owen Anderson825b72b2009-08-11 20:47:22 +00001019 Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001020 InFlag = Chain.getValue(1);
1021 }
1022
Dan Gohman475871a2008-07-27 21:46:04 +00001023 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001024 Ops.push_back(Chain);
1025 Ops.push_back(Callee);
1026
1027 // Add argument registers to the end of the list so that they are known live
1028 // into the call.
1029 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1030 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1031 RegsToPass[i].second.getValueType()));
1032
Gabor Greifba36cb52008-08-28 21:40:38 +00001033 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001034 Ops.push_back(InFlag);
Duncan Sands4bdcb612008-07-02 17:40:58 +00001035 // Returns a chain and a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00001036 Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Flag),
Duncan Sands4bdcb612008-07-02 17:40:58 +00001037 &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001038 InFlag = Chain.getValue(1);
1039
Chris Lattnere563bbc2008-10-11 22:08:30 +00001040 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1041 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001042 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001043 InFlag = Chain.getValue(1);
1044
Bob Wilson1f595bb2009-04-17 19:07:39 +00001045 // Handle result values, copying them out of physregs into vregs that we
1046 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001047 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1048 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001049}
1050
Dan Gohman98ca4f22009-08-05 01:29:28 +00001051SDValue
1052ARMTargetLowering::LowerReturn(SDValue Chain,
1053 unsigned CallConv, bool isVarArg,
1054 const SmallVectorImpl<ISD::OutputArg> &Outs,
1055 DebugLoc dl, SelectionDAG &DAG) {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001056
Bob Wilsondee46d72009-04-17 20:35:10 +00001057 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001058 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001059
Bob Wilsondee46d72009-04-17 20:35:10 +00001060 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001061 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1062 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001063
Dan Gohman98ca4f22009-08-05 01:29:28 +00001064 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001065 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1066 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001067
1068 // If this is the first return lowered for this function, add
1069 // the regs to the liveout set for the function.
1070 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1071 for (unsigned i = 0; i != RVLocs.size(); ++i)
1072 if (RVLocs[i].isRegLoc())
1073 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001074 }
1075
Bob Wilson1f595bb2009-04-17 19:07:39 +00001076 SDValue Flag;
1077
1078 // Copy the result values into the output registers.
1079 for (unsigned i = 0, realRVLocIdx = 0;
1080 i != RVLocs.size();
1081 ++i, ++realRVLocIdx) {
1082 CCValAssign &VA = RVLocs[i];
1083 assert(VA.isRegLoc() && "Can only return in registers!");
1084
Dan Gohman98ca4f22009-08-05 01:29:28 +00001085 SDValue Arg = Outs[realRVLocIdx].Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001086
1087 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001088 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001089 case CCValAssign::Full: break;
1090 case CCValAssign::BCvt:
1091 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1092 break;
1093 }
1094
Bob Wilson1f595bb2009-04-17 19:07:39 +00001095 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001096 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001097 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001098 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1099 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001100 SDValue HalfGPRs = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001101 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001102
1103 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1104 Flag = Chain.getValue(1);
1105 VA = RVLocs[++i]; // skip ahead to next loc
1106 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1107 HalfGPRs.getValue(1), Flag);
1108 Flag = Chain.getValue(1);
1109 VA = RVLocs[++i]; // skip ahead to next loc
1110
1111 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001112 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1113 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001114 }
1115 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1116 // available.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001117 SDValue fmrrd = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001118 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001119 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001120 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001121 VA = RVLocs[++i]; // skip ahead to next loc
1122 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1123 Flag);
1124 } else
1125 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1126
Bob Wilsondee46d72009-04-17 20:35:10 +00001127 // Guarantee that all emitted copies are
1128 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001129 Flag = Chain.getValue(1);
1130 }
1131
1132 SDValue result;
1133 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001134 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001135 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001136 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001137
1138 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001139}
1140
Bob Wilson2dc4f542009-03-20 22:42:55 +00001141// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
Bob Wilsond2559bf2009-07-13 18:11:36 +00001142// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
Bill Wendling056292f2008-09-16 21:48:12 +00001143// one of the above mentioned nodes. It has to be wrapped because otherwise
1144// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1145// be used to form addressing mode. These wrapped nodes will be selected
1146// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001147static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001148 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001149 // FIXME there is no actual debug info here
1150 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001151 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001152 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001153 if (CP->isMachineConstantPoolEntry())
1154 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1155 CP->getAlignment());
1156 else
1157 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1158 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001159 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001160}
1161
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001162// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001163SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001164ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
1165 SelectionDAG &DAG) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001166 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001167 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001168 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1169 ARMConstantPoolValue *CPV =
1170 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex, ARMCP::CPValue,
1171 PCAdj, "tlsgd", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001172 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001173 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001174 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001175 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001176
Owen Anderson825b72b2009-08-11 20:47:22 +00001177 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001178 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001179
1180 // call __tls_get_addr.
1181 ArgListTy Args;
1182 ArgListEntry Entry;
1183 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001184 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001185 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001186 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001187 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001188 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1189 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001190 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001191 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001192 return CallResult.first;
1193}
1194
1195// Lower ISD::GlobalTLSAddress using the "initial exec" or
1196// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001197SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001198ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001199 SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001200 GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001201 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001202 SDValue Offset;
1203 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001204 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001205 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001206 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001207
Chris Lattner4fb63d02009-07-15 04:12:33 +00001208 if (GV->isDeclaration()) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001209 // initial exec model
1210 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1211 ARMConstantPoolValue *CPV =
1212 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex, ARMCP::CPValue,
1213 PCAdj, "gottpoff", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001214 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001215 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001216 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001217 Chain = Offset.getValue(1);
1218
Owen Anderson825b72b2009-08-11 20:47:22 +00001219 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001220 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001221
Dale Johannesen33c960f2009-02-04 20:06:27 +00001222 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001223 } else {
1224 // local exec model
1225 ARMConstantPoolValue *CPV =
1226 new ARMConstantPoolValue(GV, ARMCP::CPValue, "tpoff");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001227 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001228 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001229 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001230 }
1231
1232 // The address of the thread local variable is the add of the thread
1233 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001234 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001235}
1236
Dan Gohman475871a2008-07-27 21:46:04 +00001237SDValue
1238ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001239 // TODO: implement the "local dynamic" model
1240 assert(Subtarget->isTargetELF() &&
1241 "TLS not implemented for non-ELF targets");
1242 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1243 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1244 // otherwise use the "Local Exec" TLS Model
1245 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1246 return LowerToTLSGeneralDynamicModel(GA, DAG);
1247 else
1248 return LowerToTLSExecModels(GA, DAG);
1249}
1250
Dan Gohman475871a2008-07-27 21:46:04 +00001251SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001252 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001253 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001254 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001255 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1256 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1257 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001258 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001259 ARMConstantPoolValue *CPV =
1260 new ARMConstantPoolValue(GV, ARMCP::CPValue, UseGOTOFF ? "GOTOFF":"GOT");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001261 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001262 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001263 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Dale Johannesen33c960f2009-02-04 20:06:27 +00001264 CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001265 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001266 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001267 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001268 if (!UseGOTOFF)
Dale Johannesen33c960f2009-02-04 20:06:27 +00001269 Result = DAG.getLoad(PtrVT, dl, Chain, Result, NULL, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001270 return Result;
1271 } else {
Evan Cheng1606e8e2009-03-13 07:51:59 +00001272 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001273 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001274 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001275 }
1276}
1277
Evan Chenga8e29892007-01-19 07:51:42 +00001278/// GVIsIndirectSymbol - true if the GV will be accessed via an indirect symbol
Evan Cheng97c9bb52007-05-04 00:26:58 +00001279/// even in non-static mode.
1280static bool GVIsIndirectSymbol(GlobalValue *GV, Reloc::Model RelocM) {
Evan Chengae94e592008-12-05 01:06:39 +00001281 // If symbol visibility is hidden, the extra load is not needed if
1282 // the symbol is definitely defined in the current translation unit.
Chris Lattner4fb63d02009-07-15 04:12:33 +00001283 bool isDecl = GV->isDeclaration() || GV->hasAvailableExternallyLinkage();
Evan Chengae94e592008-12-05 01:06:39 +00001284 if (GV->hasHiddenVisibility() && (!isDecl && !GV->hasCommonLinkage()))
1285 return false;
Duncan Sands667d4b82009-03-07 15:45:40 +00001286 return RelocM != Reloc::Static && (isDecl || GV->isWeakForLinker());
Evan Chenga8e29892007-01-19 07:51:42 +00001287}
1288
Dan Gohman475871a2008-07-27 21:46:04 +00001289SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001290 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001291 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001292 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001293 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1294 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng97c9bb52007-05-04 00:26:58 +00001295 bool IsIndirect = GVIsIndirectSymbol(GV, RelocM);
Dan Gohman475871a2008-07-27 21:46:04 +00001296 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +00001297 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001298 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001299 else {
1300 unsigned PCAdj = (RelocM != Reloc::PIC_)
1301 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Evan Chengc60e76d2007-01-30 20:37:08 +00001302 ARMCP::ARMCPKind Kind = IsIndirect ? ARMCP::CPNonLazyPtr
1303 : ARMCP::CPValue;
Evan Chenga8e29892007-01-19 07:51:42 +00001304 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex,
Evan Chengc60e76d2007-01-30 20:37:08 +00001305 Kind, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001306 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001307 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001308 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001309
Dale Johannesen33c960f2009-02-04 20:06:27 +00001310 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001311 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001312
1313 if (RelocM == Reloc::PIC_) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001314 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001315 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001316 }
1317 if (IsIndirect)
Dale Johannesen33c960f2009-02-04 20:06:27 +00001318 Result = DAG.getLoad(PtrVT, dl, Chain, Result, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001319
1320 return Result;
1321}
1322
Dan Gohman475871a2008-07-27 21:46:04 +00001323SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001324 SelectionDAG &DAG){
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001325 assert(Subtarget->isTargetELF() &&
1326 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Owen Andersone50ed302009-08-10 22:56:29 +00001327 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001328 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001329 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00001330 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1331 "_GLOBAL_OFFSET_TABLE_",
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001332 ARMPCLabelIndex,
1333 ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001334 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001335 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001336 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001337 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001338 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001339}
1340
Bob Wilsona599bff2009-08-04 00:36:16 +00001341static SDValue LowerNeonVLDIntrinsic(SDValue Op, SelectionDAG &DAG,
Bob Wilson31fb12f2009-08-26 17:39:53 +00001342 unsigned NumVecs) {
Bob Wilsona599bff2009-08-04 00:36:16 +00001343 SDNode *Node = Op.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001344 EVT VT = Node->getValueType(0);
Bob Wilsona599bff2009-08-04 00:36:16 +00001345
Bob Wilson31fb12f2009-08-26 17:39:53 +00001346 // No expansion needed for 64-bit vectors.
1347 if (VT.is64BitVector())
1348 return SDValue();
Bob Wilsona599bff2009-08-04 00:36:16 +00001349
Bob Wilson31fb12f2009-08-26 17:39:53 +00001350 // FIXME: We need to expand VLD3 and VLD4 of 128-bit vectors into separate
1351 // operations to load the even and odd registers.
1352 return SDValue();
Bob Wilsona599bff2009-08-04 00:36:16 +00001353}
1354
Bob Wilsonb36ec862009-08-06 18:47:44 +00001355static SDValue LowerNeonVSTIntrinsic(SDValue Op, SelectionDAG &DAG,
Bob Wilson31fb12f2009-08-26 17:39:53 +00001356 unsigned NumVecs) {
Bob Wilsonb36ec862009-08-06 18:47:44 +00001357 SDNode *Node = Op.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001358 EVT VT = Node->getOperand(3).getValueType();
Bob Wilsonb36ec862009-08-06 18:47:44 +00001359
Bob Wilson31fb12f2009-08-26 17:39:53 +00001360 // No expansion needed for 64-bit vectors.
1361 if (VT.is64BitVector())
1362 return SDValue();
Bob Wilsonb36ec862009-08-06 18:47:44 +00001363
Bob Wilson31fb12f2009-08-26 17:39:53 +00001364 // FIXME: We need to expand VST3 and VST4 of 128-bit vectors into separate
1365 // operations to store the even and odd registers.
1366 return SDValue();
Bob Wilsonb36ec862009-08-06 18:47:44 +00001367}
1368
Bob Wilsona599bff2009-08-04 00:36:16 +00001369SDValue
1370ARMTargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) {
1371 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1372 switch (IntNo) {
Bob Wilsonb0abb4d2009-08-11 05:39:44 +00001373 case Intrinsic::arm_neon_vld3:
Bob Wilson31fb12f2009-08-26 17:39:53 +00001374 return LowerNeonVLDIntrinsic(Op, DAG, 3);
Bob Wilsonb0abb4d2009-08-11 05:39:44 +00001375 case Intrinsic::arm_neon_vld4:
Bob Wilson31fb12f2009-08-26 17:39:53 +00001376 return LowerNeonVLDIntrinsic(Op, DAG, 4);
Bob Wilsonb0abb4d2009-08-11 05:39:44 +00001377 case Intrinsic::arm_neon_vst3:
Bob Wilson31fb12f2009-08-26 17:39:53 +00001378 return LowerNeonVSTIntrinsic(Op, DAG, 3);
Bob Wilsonb0abb4d2009-08-11 05:39:44 +00001379 case Intrinsic::arm_neon_vst4:
Bob Wilson31fb12f2009-08-26 17:39:53 +00001380 return LowerNeonVSTIntrinsic(Op, DAG, 4);
Bob Wilsona599bff2009-08-04 00:36:16 +00001381 default: return SDValue(); // Don't custom lower most intrinsics.
1382 }
1383}
1384
Jim Grosbach0e0da732009-05-12 23:59:14 +00001385SDValue
1386ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001387 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001388 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001389 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001390 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00001391 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00001392 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00001393 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1394 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001395 case Intrinsic::eh_sjlj_lsda: {
1396 // blah. horrible, horrible hack with the forced magic name.
1397 // really need to clean this up. It belongs in the target-independent
1398 // layer somehow that doesn't require the coupling with the asm
1399 // printer.
1400 MachineFunction &MF = DAG.getMachineFunction();
1401 EVT PtrVT = getPointerTy();
1402 DebugLoc dl = Op.getDebugLoc();
1403 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1404 SDValue CPAddr;
1405 unsigned PCAdj = (RelocM != Reloc::PIC_)
1406 ? 0 : (Subtarget->isThumb() ? 4 : 8);
1407 ARMCP::ARMCPKind Kind = ARMCP::CPValue;
1408 // Save off the LSDA name for the AsmPrinter to use when it's time
1409 // to emit the table
1410 std::string LSDAName = "L_lsda_";
1411 LSDAName += MF.getFunction()->getName();
1412 ARMConstantPoolValue *CPV =
Owen Anderson1d0be152009-08-13 21:58:54 +00001413 new ARMConstantPoolValue(*DAG.getContext(), LSDAName.c_str(),
1414 ARMPCLabelIndex, Kind, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001415 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001416 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001417 SDValue Result =
1418 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
1419 SDValue Chain = Result.getValue(1);
1420
1421 if (RelocM == Reloc::PIC_) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001422 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001423 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1424 }
1425 return Result;
1426 }
Jim Grosbachf9570122009-05-14 00:46:35 +00001427 case Intrinsic::eh_sjlj_setjmp:
Owen Anderson825b72b2009-08-11 20:47:22 +00001428 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(1));
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001429 }
1430}
1431
Dan Gohman475871a2008-07-27 21:46:04 +00001432static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001433 unsigned VarArgsFrameIndex) {
Evan Chenga8e29892007-01-19 07:51:42 +00001434 // vastart just stores the address of the VarArgsFrameIndex slot into the
1435 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001436 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001437 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001438 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001439 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001440 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001441}
1442
Dan Gohman475871a2008-07-27 21:46:04 +00001443SDValue
Evan Cheng86198642009-08-07 00:34:42 +00001444ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) {
1445 SDNode *Node = Op.getNode();
1446 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001447 EVT VT = Node->getValueType(0);
Evan Cheng86198642009-08-07 00:34:42 +00001448 SDValue Chain = Op.getOperand(0);
1449 SDValue Size = Op.getOperand(1);
1450 SDValue Align = Op.getOperand(2);
1451
1452 // Chain the dynamic stack allocation so that it doesn't modify the stack
1453 // pointer when other instructions are using the stack.
1454 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1455
1456 unsigned AlignVal = cast<ConstantSDNode>(Align)->getZExtValue();
1457 unsigned StackAlign = getTargetMachine().getFrameInfo()->getStackAlignment();
1458 if (AlignVal > StackAlign)
1459 // Do this now since selection pass cannot introduce new target
1460 // independent node.
1461 Align = DAG.getConstant(-(uint64_t)AlignVal, VT);
1462
1463 // In Thumb1 mode, there isn't a "sub r, sp, r" instruction, we will end up
1464 // using a "add r, sp, r" instead. Negate the size now so we don't have to
1465 // do even more horrible hack later.
1466 MachineFunction &MF = DAG.getMachineFunction();
1467 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1468 if (AFI->isThumb1OnlyFunction()) {
1469 bool Negate = true;
1470 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Size);
1471 if (C) {
1472 uint32_t Val = C->getZExtValue();
1473 if (Val <= 508 && ((Val & 3) == 0))
1474 Negate = false;
1475 }
1476 if (Negate)
1477 Size = DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(0, VT), Size);
1478 }
1479
Owen Anderson825b72b2009-08-11 20:47:22 +00001480 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
Evan Cheng86198642009-08-07 00:34:42 +00001481 SDValue Ops1[] = { Chain, Size, Align };
1482 SDValue Res = DAG.getNode(ARMISD::DYN_ALLOC, dl, VTList, Ops1, 3);
1483 Chain = Res.getValue(1);
1484 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1485 DAG.getIntPtrConstant(0, true), SDValue());
1486 SDValue Ops2[] = { Res, Chain };
1487 return DAG.getMergeValues(Ops2, 2, dl);
1488}
1489
1490SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00001491ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
1492 SDValue &Root, SelectionDAG &DAG,
1493 DebugLoc dl) {
1494 MachineFunction &MF = DAG.getMachineFunction();
1495 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1496
1497 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001498 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00001499 RC = ARM::tGPRRegisterClass;
1500 else
1501 RC = ARM::GPRRegisterClass;
1502
1503 // Transform the arguments stored in physical registers into virtual ones.
1504 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001505 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001506
1507 SDValue ArgValue2;
1508 if (NextVA.isMemLoc()) {
1509 unsigned ArgSize = NextVA.getLocVT().getSizeInBits()/8;
1510 MachineFrameInfo *MFI = MF.getFrameInfo();
1511 int FI = MFI->CreateFixedObject(ArgSize, NextVA.getLocMemOffset());
1512
1513 // Create load node to retrieve arguments from the stack.
1514 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00001515 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN, NULL, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00001516 } else {
1517 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001518 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001519 }
1520
Owen Anderson825b72b2009-08-11 20:47:22 +00001521 return DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00001522}
1523
1524SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001525ARMTargetLowering::LowerFormalArguments(SDValue Chain,
1526 unsigned CallConv, bool isVarArg,
1527 const SmallVectorImpl<ISD::InputArg>
1528 &Ins,
1529 DebugLoc dl, SelectionDAG &DAG,
1530 SmallVectorImpl<SDValue> &InVals) {
1531
Bob Wilson1f595bb2009-04-17 19:07:39 +00001532 MachineFunction &MF = DAG.getMachineFunction();
1533 MachineFrameInfo *MFI = MF.getFrameInfo();
1534
Bob Wilson1f595bb2009-04-17 19:07:39 +00001535 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1536
1537 // Assign locations to all of the incoming arguments.
1538 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001539 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1540 *DAG.getContext());
1541 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001542 CCAssignFnForNode(CallConv, /* Return*/ false,
1543 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001544
1545 SmallVector<SDValue, 16> ArgValues;
1546
1547 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1548 CCValAssign &VA = ArgLocs[i];
1549
Bob Wilsondee46d72009-04-17 20:35:10 +00001550 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001551 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001552 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00001553
Bob Wilson5bafff32009-06-22 23:27:02 +00001554 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001555 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001556 // f64 and vector types are split up into multiple registers or
1557 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00001558 RegVT = MVT::i32;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001559
Owen Anderson825b72b2009-08-11 20:47:22 +00001560 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001561 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001562 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00001563 VA = ArgLocs[++i]; // skip ahead to next loc
1564 SDValue ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001565 Chain, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001566 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1567 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001568 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00001569 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001570 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
1571 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001572 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001573
Bob Wilson5bafff32009-06-22 23:27:02 +00001574 } else {
1575 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001576
Owen Anderson825b72b2009-08-11 20:47:22 +00001577 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00001578 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001579 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00001580 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001581 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001582 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001583 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001584 RC = (AFI->isThumb1OnlyFunction() ?
1585 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00001586 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001587 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00001588
1589 // Transform the arguments in physical registers into virtual ones.
1590 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001591 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001592 }
1593
1594 // If this is an 8 or 16-bit value, it is really passed promoted
1595 // to 32 bits. Insert an assert[sz]ext to capture this, then
1596 // truncate to the right size.
1597 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001598 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001599 case CCValAssign::Full: break;
1600 case CCValAssign::BCvt:
1601 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1602 break;
1603 case CCValAssign::SExt:
1604 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
1605 DAG.getValueType(VA.getValVT()));
1606 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1607 break;
1608 case CCValAssign::ZExt:
1609 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
1610 DAG.getValueType(VA.getValVT()));
1611 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1612 break;
1613 }
1614
Dan Gohman98ca4f22009-08-05 01:29:28 +00001615 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001616
1617 } else { // VA.isRegLoc()
1618
1619 // sanity check
1620 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00001621 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001622
1623 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
1624 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset());
1625
Bob Wilsondee46d72009-04-17 20:35:10 +00001626 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001627 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001628 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN, NULL, 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001629 }
1630 }
1631
1632 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00001633 if (isVarArg) {
1634 static const unsigned GPRArgRegs[] = {
1635 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1636 };
1637
Bob Wilsondee46d72009-04-17 20:35:10 +00001638 unsigned NumGPRs = CCInfo.getFirstUnallocated
1639 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001640
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001641 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1642 unsigned VARegSize = (4 - NumGPRs) * 4;
1643 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001644 unsigned ArgOffset = 0;
Evan Chenga8e29892007-01-19 07:51:42 +00001645 if (VARegSaveSize) {
1646 // If this function is vararg, store any remaining integer argument regs
1647 // to their spots on the stack so that they may be loaded by deferencing
1648 // the result of va_next.
1649 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001650 ArgOffset = CCInfo.getNextStackOffset();
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001651 VarArgsFrameIndex = MFI->CreateFixedObject(VARegSaveSize, ArgOffset +
1652 VARegSaveSize - VARegSize);
Dan Gohman475871a2008-07-27 21:46:04 +00001653 SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001654
Dan Gohman475871a2008-07-27 21:46:04 +00001655 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00001656 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001657 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001658 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00001659 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001660 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00001661 RC = ARM::GPRRegisterClass;
1662
Bob Wilson998e1252009-04-20 18:36:57 +00001663 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001664 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001665 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001666 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001667 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00001668 DAG.getConstant(4, getPointerTy()));
1669 }
1670 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001671 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001672 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001673 } else
1674 // This will point to the next argument passed via stack.
1675 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1676 }
1677
Dan Gohman98ca4f22009-08-05 01:29:28 +00001678 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00001679}
1680
1681/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00001682static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00001683 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001684 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00001685 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00001686 // Maybe this has already been legalized into the constant pool?
1687 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00001688 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001689 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
1690 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001691 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001692 }
1693 }
1694 return false;
1695}
1696
David Goodwinf1daf7d2009-07-08 23:10:31 +00001697static bool isLegalCmpImmediate(unsigned C, bool isThumb1Only) {
1698 return ( isThumb1Only && (C & ~255U) == 0) ||
1699 (!isThumb1Only && ARM_AM::getSOImmVal(C) != -1);
Evan Chenga8e29892007-01-19 07:51:42 +00001700}
1701
1702/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1703/// the given operands.
Dan Gohman475871a2008-07-27 21:46:04 +00001704static SDValue getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
David Goodwinf1daf7d2009-07-08 23:10:31 +00001705 SDValue &ARMCC, SelectionDAG &DAG, bool isThumb1Only,
Dale Johannesende064702009-02-06 21:50:26 +00001706 DebugLoc dl) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001707 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001708 unsigned C = RHSC->getZExtValue();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001709 if (!isLegalCmpImmediate(C, isThumb1Only)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001710 // Constant does not fit, try adjusting it by one?
1711 switch (CC) {
1712 default: break;
1713 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00001714 case ISD::SETGE:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001715 if (isLegalCmpImmediate(C-1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001716 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001717 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001718 }
1719 break;
1720 case ISD::SETULT:
1721 case ISD::SETUGE:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001722 if (C > 0 && isLegalCmpImmediate(C-1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001723 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001724 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001725 }
1726 break;
1727 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00001728 case ISD::SETGT:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001729 if (isLegalCmpImmediate(C+1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001730 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001731 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001732 }
1733 break;
1734 case ISD::SETULE:
1735 case ISD::SETUGT:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001736 if (C < 0xffffffff && isLegalCmpImmediate(C+1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001737 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001738 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001739 }
1740 break;
1741 }
1742 }
1743 }
1744
1745 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001746 ARMISD::NodeType CompareType;
1747 switch (CondCode) {
1748 default:
1749 CompareType = ARMISD::CMP;
1750 break;
1751 case ARMCC::EQ:
1752 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00001753 // Uses only Z Flag
1754 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001755 break;
1756 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001757 ARMCC = DAG.getConstant(CondCode, MVT::i32);
1758 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001759}
1760
1761/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001762static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Dale Johannesende064702009-02-06 21:50:26 +00001763 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001764 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00001765 if (!isFloatingPointZero(RHS))
Owen Anderson825b72b2009-08-11 20:47:22 +00001766 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001767 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001768 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
1769 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001770}
1771
Dan Gohman475871a2008-07-27 21:46:04 +00001772static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001773 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00001774 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001775 SDValue LHS = Op.getOperand(0);
1776 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001777 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001778 SDValue TrueVal = Op.getOperand(2);
1779 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00001780 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001781
Owen Anderson825b72b2009-08-11 20:47:22 +00001782 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001783 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001784 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
David Goodwinf1daf7d2009-07-08 23:10:31 +00001785 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb1Only(), dl);
Dale Johannesende064702009-02-06 21:50:26 +00001786 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001787 }
1788
1789 ARMCC::CondCodes CondCode, CondCode2;
1790 if (FPCCToARMCC(CC, CondCode, CondCode2))
1791 std::swap(TrueVal, FalseVal);
1792
Owen Anderson825b72b2009-08-11 20:47:22 +00001793 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1794 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001795 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
1796 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00001797 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001798 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001799 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001800 // FIXME: Needs another CMP because flag can have but one use.
Dale Johannesende064702009-02-06 21:50:26 +00001801 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001802 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Dale Johannesende064702009-02-06 21:50:26 +00001803 Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00001804 }
1805 return Result;
1806}
1807
Dan Gohman475871a2008-07-27 21:46:04 +00001808static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001809 const ARMSubtarget *ST) {
Dan Gohman475871a2008-07-27 21:46:04 +00001810 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001811 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001812 SDValue LHS = Op.getOperand(2);
1813 SDValue RHS = Op.getOperand(3);
1814 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00001815 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001816
Owen Anderson825b72b2009-08-11 20:47:22 +00001817 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001818 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001819 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
David Goodwinf1daf7d2009-07-08 23:10:31 +00001820 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb1Only(), dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001821 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Dale Johannesende064702009-02-06 21:50:26 +00001822 Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001823 }
1824
Owen Anderson825b72b2009-08-11 20:47:22 +00001825 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Chenga8e29892007-01-19 07:51:42 +00001826 ARMCC::CondCodes CondCode, CondCode2;
1827 if (FPCCToARMCC(CC, CondCode, CondCode2))
1828 // Swap the LHS/RHS of the comparison if needed.
1829 std::swap(LHS, RHS);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001830
Dale Johannesende064702009-02-06 21:50:26 +00001831 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001832 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1833 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1834 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00001835 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00001836 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001837 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001838 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00001839 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00001840 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001841 }
1842 return Res;
1843}
1844
Dan Gohman475871a2008-07-27 21:46:04 +00001845SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) {
1846 SDValue Chain = Op.getOperand(0);
1847 SDValue Table = Op.getOperand(1);
1848 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001849 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001850
Owen Andersone50ed302009-08-10 22:56:29 +00001851 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00001852 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
1853 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00001854 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00001855 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00001856 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00001857 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
1858 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00001859 if (Subtarget->isThumb2()) {
1860 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
1861 // which does another jump to the destination. This also makes it easier
1862 // to translate it to TBB / TBH later.
1863 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00001864 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00001865 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001866 }
Evan Cheng66ac5312009-07-25 00:33:29 +00001867 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001868 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr, NULL, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00001869 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001870 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00001871 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001872 } else {
1873 Addr = DAG.getLoad(PTy, dl, Chain, Addr, NULL, 0);
1874 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00001875 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001876 }
Evan Chenga8e29892007-01-19 07:51:42 +00001877}
1878
Dan Gohman475871a2008-07-27 21:46:04 +00001879static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Dale Johannesende064702009-02-06 21:50:26 +00001880 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001881 unsigned Opc =
1882 Op.getOpcode() == ISD::FP_TO_SINT ? ARMISD::FTOSI : ARMISD::FTOUI;
Owen Anderson825b72b2009-08-11 20:47:22 +00001883 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
1884 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001885}
1886
Dan Gohman475871a2008-07-27 21:46:04 +00001887static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001888 EVT VT = Op.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001889 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001890 unsigned Opc =
1891 Op.getOpcode() == ISD::SINT_TO_FP ? ARMISD::SITOF : ARMISD::UITOF;
1892
Owen Anderson825b72b2009-08-11 20:47:22 +00001893 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
Dale Johannesende064702009-02-06 21:50:26 +00001894 return DAG.getNode(Opc, dl, VT, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001895}
1896
Dan Gohman475871a2008-07-27 21:46:04 +00001897static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00001898 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00001899 SDValue Tmp0 = Op.getOperand(0);
1900 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00001901 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001902 EVT VT = Op.getValueType();
1903 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001904 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
1905 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001906 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
1907 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001908 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001909}
1910
Jim Grosbach0e0da732009-05-12 23:59:14 +00001911SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
1912 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1913 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00001914 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001915 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
1916 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00001917 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00001918 ? ARM::R7 : ARM::R11;
1919 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
1920 while (Depth--)
1921 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
1922 return FrameAddr;
1923}
1924
Dan Gohman475871a2008-07-27 21:46:04 +00001925SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00001926ARMTargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman475871a2008-07-27 21:46:04 +00001927 SDValue Chain,
1928 SDValue Dst, SDValue Src,
1929 SDValue Size, unsigned Align,
Dan Gohman707e0182008-04-12 04:36:06 +00001930 bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00001931 const Value *DstSV, uint64_t DstSVOff,
1932 const Value *SrcSV, uint64_t SrcSVOff){
Evan Cheng4102eb52007-10-22 22:11:27 +00001933 // Do repeated 4-byte loads and stores. To be improved.
Dan Gohman707e0182008-04-12 04:36:06 +00001934 // This requires 4-byte alignment.
1935 if ((Align & 3) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00001936 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001937 // This requires the copy size to be a constant, preferrably
1938 // within a subtarget-specific limit.
1939 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
1940 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00001941 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001942 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001943 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00001944 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001945
1946 unsigned BytesLeft = SizeVal & 3;
1947 unsigned NumMemOps = SizeVal >> 2;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001948 unsigned EmittedNumMemOps = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001949 EVT VT = MVT::i32;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001950 unsigned VTSize = 4;
Evan Cheng4102eb52007-10-22 22:11:27 +00001951 unsigned i = 0;
Evan Chenge5e7ce42007-05-18 01:19:57 +00001952 const unsigned MAX_LOADS_IN_LDM = 6;
Dan Gohman475871a2008-07-27 21:46:04 +00001953 SDValue TFOps[MAX_LOADS_IN_LDM];
1954 SDValue Loads[MAX_LOADS_IN_LDM];
Dan Gohman1f13c682008-04-28 17:15:20 +00001955 uint64_t SrcOff = 0, DstOff = 0;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001956
Evan Cheng4102eb52007-10-22 22:11:27 +00001957 // Emit up to MAX_LOADS_IN_LDM loads, then a TokenFactor barrier, then the
1958 // same number of stores. The loads and stores will get combined into
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001959 // ldm/stm later on.
Evan Cheng4102eb52007-10-22 22:11:27 +00001960 while (EmittedNumMemOps < NumMemOps) {
1961 for (i = 0;
1962 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00001963 Loads[i] = DAG.getLoad(VT, dl, Chain,
Owen Anderson825b72b2009-08-11 20:47:22 +00001964 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
1965 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001966 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00001967 TFOps[i] = Loads[i].getValue(1);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001968 SrcOff += VTSize;
1969 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001970 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001971
Evan Cheng4102eb52007-10-22 22:11:27 +00001972 for (i = 0;
1973 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00001974 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Owen Anderson825b72b2009-08-11 20:47:22 +00001975 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
1976 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001977 DstSV, DstSVOff + DstOff);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001978 DstOff += VTSize;
1979 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001980 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00001981
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001982 EmittedNumMemOps += i;
1983 }
1984
Bob Wilson2dc4f542009-03-20 22:42:55 +00001985 if (BytesLeft == 0)
Evan Cheng4102eb52007-10-22 22:11:27 +00001986 return Chain;
1987
1988 // Issue loads / stores for the trailing (1 - 3) bytes.
1989 unsigned BytesLeftSave = BytesLeft;
1990 i = 0;
1991 while (BytesLeft) {
1992 if (BytesLeft >= 2) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001993 VT = MVT::i16;
Evan Cheng4102eb52007-10-22 22:11:27 +00001994 VTSize = 2;
1995 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001996 VT = MVT::i8;
Evan Cheng4102eb52007-10-22 22:11:27 +00001997 VTSize = 1;
1998 }
1999
Dale Johannesen0f502f62009-02-03 22:26:09 +00002000 Loads[i] = DAG.getLoad(VT, dl, Chain,
Owen Anderson825b72b2009-08-11 20:47:22 +00002001 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
2002 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00002003 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00002004 TFOps[i] = Loads[i].getValue(1);
2005 ++i;
2006 SrcOff += VTSize;
2007 BytesLeft -= VTSize;
2008 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002009 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00002010
2011 i = 0;
2012 BytesLeft = BytesLeftSave;
2013 while (BytesLeft) {
2014 if (BytesLeft >= 2) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002015 VT = MVT::i16;
Evan Cheng4102eb52007-10-22 22:11:27 +00002016 VTSize = 2;
2017 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00002018 VT = MVT::i8;
Evan Cheng4102eb52007-10-22 22:11:27 +00002019 VTSize = 1;
2020 }
2021
Dale Johannesen0f502f62009-02-03 22:26:09 +00002022 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Owen Anderson825b72b2009-08-11 20:47:22 +00002023 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
2024 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00002025 DstSV, DstSVOff + DstOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00002026 ++i;
2027 DstOff += VTSize;
2028 BytesLeft -= VTSize;
2029 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002030 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002031}
2032
Duncan Sands1607f052008-12-01 11:39:25 +00002033static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00002034 SDValue Op = N->getOperand(0);
Dale Johannesende064702009-02-06 21:50:26 +00002035 DebugLoc dl = N->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00002036 if (N->getValueType(0) == MVT::f64) {
Evan Chengc7c77292008-11-04 19:57:48 +00002037 // Turn i64->f64 into FMDRR.
Owen Anderson825b72b2009-08-11 20:47:22 +00002038 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2039 DAG.getConstant(0, MVT::i32));
2040 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2041 DAG.getConstant(1, MVT::i32));
2042 return DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
Evan Chengc7c77292008-11-04 19:57:48 +00002043 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002044
Evan Chengc7c77292008-11-04 19:57:48 +00002045 // Turn f64->i64 into FMRRD.
Bob Wilson2dc4f542009-03-20 22:42:55 +00002046 SDValue Cvt = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00002047 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002048
Chris Lattner27a6c732007-11-24 07:07:01 +00002049 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002050 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
Chris Lattner27a6c732007-11-24 07:07:01 +00002051}
2052
Bob Wilson5bafff32009-06-22 23:27:02 +00002053/// getZeroVector - Returns a vector of specified type with all zero elements.
2054///
Owen Andersone50ed302009-08-10 22:56:29 +00002055static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002056 assert(VT.isVector() && "Expected a vector type");
2057
2058 // Zero vectors are used to represent vector negation and in those cases
2059 // will be implemented with the NEON VNEG instruction. However, VNEG does
2060 // not support i64 elements, so sometimes the zero vectors will need to be
2061 // explicitly constructed. For those cases, and potentially other uses in
2062 // the future, always build zero vectors as <4 x i32> or <2 x i32> bitcasted
2063 // to their dest type. This ensures they get CSE'd.
2064 SDValue Vec;
Owen Anderson825b72b2009-08-11 20:47:22 +00002065 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002066 if (VT.getSizeInBits() == 64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002067 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Bob Wilson5bafff32009-06-22 23:27:02 +00002068 else
Owen Anderson825b72b2009-08-11 20:47:22 +00002069 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Bob Wilson5bafff32009-06-22 23:27:02 +00002070
2071 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2072}
2073
2074/// getOnesVector - Returns a vector of specified type with all bits set.
2075///
Owen Andersone50ed302009-08-10 22:56:29 +00002076static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002077 assert(VT.isVector() && "Expected a vector type");
2078
2079 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2080 // type. This ensures they get CSE'd.
2081 SDValue Vec;
Owen Anderson825b72b2009-08-11 20:47:22 +00002082 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002083 if (VT.getSizeInBits() == 64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002084 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Bob Wilson5bafff32009-06-22 23:27:02 +00002085 else
Owen Anderson825b72b2009-08-11 20:47:22 +00002086 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Bob Wilson5bafff32009-06-22 23:27:02 +00002087
2088 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2089}
2090
2091static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2092 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00002093 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002094 DebugLoc dl = N->getDebugLoc();
2095
2096 // Lower vector shifts on NEON to use VSHL.
2097 if (VT.isVector()) {
2098 assert(ST->hasNEON() && "unexpected vector shift");
2099
2100 // Left shifts translate directly to the vshiftu intrinsic.
2101 if (N->getOpcode() == ISD::SHL)
2102 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002103 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002104 N->getOperand(0), N->getOperand(1));
2105
2106 assert((N->getOpcode() == ISD::SRA ||
2107 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2108
2109 // NEON uses the same intrinsics for both left and right shifts. For
2110 // right shifts, the shift amounts are negative, so negate the vector of
2111 // shift amounts.
Owen Andersone50ed302009-08-10 22:56:29 +00002112 EVT ShiftVT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002113 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2114 getZeroVector(ShiftVT, DAG, dl),
2115 N->getOperand(1));
2116 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2117 Intrinsic::arm_neon_vshifts :
2118 Intrinsic::arm_neon_vshiftu);
2119 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002120 DAG.getConstant(vshiftInt, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002121 N->getOperand(0), NegatedCount);
2122 }
2123
Eli Friedmance392eb2009-08-22 03:13:10 +00002124 // We can get here for a node like i32 = ISD::SHL i32, i64
2125 if (VT != MVT::i64)
2126 return SDValue();
2127
2128 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00002129 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00002130
Chris Lattner27a6c732007-11-24 07:07:01 +00002131 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2132 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002133 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00002134 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002135
Chris Lattner27a6c732007-11-24 07:07:01 +00002136 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00002137 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002138
Chris Lattner27a6c732007-11-24 07:07:01 +00002139 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00002140 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2141 DAG.getConstant(0, MVT::i32));
2142 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2143 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002144
Chris Lattner27a6c732007-11-24 07:07:01 +00002145 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2146 // captures the result into a carry flag.
2147 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Owen Anderson825b72b2009-08-11 20:47:22 +00002148 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002149
Chris Lattner27a6c732007-11-24 07:07:01 +00002150 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00002151 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002152
Chris Lattner27a6c732007-11-24 07:07:01 +00002153 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002154 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00002155}
2156
Bob Wilson5bafff32009-06-22 23:27:02 +00002157static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2158 SDValue TmpOp0, TmpOp1;
2159 bool Invert = false;
2160 bool Swap = false;
2161 unsigned Opc = 0;
2162
2163 SDValue Op0 = Op.getOperand(0);
2164 SDValue Op1 = Op.getOperand(1);
2165 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00002166 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002167 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2168 DebugLoc dl = Op.getDebugLoc();
2169
2170 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2171 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002172 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002173 case ISD::SETUNE:
2174 case ISD::SETNE: Invert = true; // Fallthrough
2175 case ISD::SETOEQ:
2176 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2177 case ISD::SETOLT:
2178 case ISD::SETLT: Swap = true; // Fallthrough
2179 case ISD::SETOGT:
2180 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2181 case ISD::SETOLE:
2182 case ISD::SETLE: Swap = true; // Fallthrough
2183 case ISD::SETOGE:
2184 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2185 case ISD::SETUGE: Swap = true; // Fallthrough
2186 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2187 case ISD::SETUGT: Swap = true; // Fallthrough
2188 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2189 case ISD::SETUEQ: Invert = true; // Fallthrough
2190 case ISD::SETONE:
2191 // Expand this to (OLT | OGT).
2192 TmpOp0 = Op0;
2193 TmpOp1 = Op1;
2194 Opc = ISD::OR;
2195 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2196 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2197 break;
2198 case ISD::SETUO: Invert = true; // Fallthrough
2199 case ISD::SETO:
2200 // Expand this to (OLT | OGE).
2201 TmpOp0 = Op0;
2202 TmpOp1 = Op1;
2203 Opc = ISD::OR;
2204 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2205 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2206 break;
2207 }
2208 } else {
2209 // Integer comparisons.
2210 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002211 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002212 case ISD::SETNE: Invert = true;
2213 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2214 case ISD::SETLT: Swap = true;
2215 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2216 case ISD::SETLE: Swap = true;
2217 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2218 case ISD::SETULT: Swap = true;
2219 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2220 case ISD::SETULE: Swap = true;
2221 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2222 }
2223
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002224 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00002225 if (Opc == ARMISD::VCEQ) {
2226
2227 SDValue AndOp;
2228 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2229 AndOp = Op0;
2230 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2231 AndOp = Op1;
2232
2233 // Ignore bitconvert.
2234 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2235 AndOp = AndOp.getOperand(0);
2236
2237 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2238 Opc = ARMISD::VTST;
2239 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2240 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2241 Invert = !Invert;
2242 }
2243 }
2244 }
2245
2246 if (Swap)
2247 std::swap(Op0, Op1);
2248
2249 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
2250
2251 if (Invert)
2252 Result = DAG.getNOT(dl, Result, VT);
2253
2254 return Result;
2255}
2256
2257/// isVMOVSplat - Check if the specified splat value corresponds to an immediate
2258/// VMOV instruction, and if so, return the constant being splatted.
2259static SDValue isVMOVSplat(uint64_t SplatBits, uint64_t SplatUndef,
2260 unsigned SplatBitSize, SelectionDAG &DAG) {
2261 switch (SplatBitSize) {
2262 case 8:
2263 // Any 1-byte value is OK.
2264 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Owen Anderson825b72b2009-08-11 20:47:22 +00002265 return DAG.getTargetConstant(SplatBits, MVT::i8);
Bob Wilson5bafff32009-06-22 23:27:02 +00002266
2267 case 16:
2268 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
2269 if ((SplatBits & ~0xff) == 0 ||
2270 (SplatBits & ~0xff00) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002271 return DAG.getTargetConstant(SplatBits, MVT::i16);
Bob Wilson5bafff32009-06-22 23:27:02 +00002272 break;
2273
2274 case 32:
2275 // NEON's 32-bit VMOV supports splat values where:
2276 // * only one byte is nonzero, or
2277 // * the least significant byte is 0xff and the second byte is nonzero, or
2278 // * the least significant 2 bytes are 0xff and the third is nonzero.
2279 if ((SplatBits & ~0xff) == 0 ||
2280 (SplatBits & ~0xff00) == 0 ||
2281 (SplatBits & ~0xff0000) == 0 ||
2282 (SplatBits & ~0xff000000) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002283 return DAG.getTargetConstant(SplatBits, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002284
2285 if ((SplatBits & ~0xffff) == 0 &&
2286 ((SplatBits | SplatUndef) & 0xff) == 0xff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002287 return DAG.getTargetConstant(SplatBits | 0xff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002288
2289 if ((SplatBits & ~0xffffff) == 0 &&
2290 ((SplatBits | SplatUndef) & 0xffff) == 0xffff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002291 return DAG.getTargetConstant(SplatBits | 0xffff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002292
2293 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
2294 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
2295 // VMOV.I32. A (very) minor optimization would be to replicate the value
2296 // and fall through here to test for a valid 64-bit splat. But, then the
2297 // caller would also need to check and handle the change in size.
2298 break;
2299
2300 case 64: {
2301 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
2302 uint64_t BitMask = 0xff;
2303 uint64_t Val = 0;
2304 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
2305 if (((SplatBits | SplatUndef) & BitMask) == BitMask)
2306 Val |= BitMask;
2307 else if ((SplatBits & BitMask) != 0)
2308 return SDValue();
2309 BitMask <<= 8;
2310 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002311 return DAG.getTargetConstant(Val, MVT::i64);
Bob Wilson5bafff32009-06-22 23:27:02 +00002312 }
2313
2314 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002315 llvm_unreachable("unexpected size for isVMOVSplat");
Bob Wilson5bafff32009-06-22 23:27:02 +00002316 break;
2317 }
2318
2319 return SDValue();
2320}
2321
2322/// getVMOVImm - If this is a build_vector of constants which can be
2323/// formed by using a VMOV instruction of the specified element size,
2324/// return the constant being splatted. The ByteSize field indicates the
2325/// number of bytes of each element [1248].
2326SDValue ARM::getVMOVImm(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
2327 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
2328 APInt SplatBits, SplatUndef;
2329 unsigned SplatBitSize;
2330 bool HasAnyUndefs;
2331 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
2332 HasAnyUndefs, ByteSize * 8))
2333 return SDValue();
2334
2335 if (SplatBitSize > ByteSize * 8)
2336 return SDValue();
2337
2338 return isVMOVSplat(SplatBits.getZExtValue(), SplatUndef.getZExtValue(),
2339 SplatBitSize, DAG);
2340}
2341
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002342static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
2343 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002344 unsigned NumElts = VT.getVectorNumElements();
2345 ReverseVEXT = false;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002346 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002347
2348 // If this is a VEXT shuffle, the immediate value is the index of the first
2349 // element. The other shuffle indices must be the successive elements after
2350 // the first one.
2351 unsigned ExpectedElt = Imm;
2352 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002353 // Increment the expected index. If it wraps around, it may still be
2354 // a VEXT but the source vectors must be swapped.
2355 ExpectedElt += 1;
2356 if (ExpectedElt == NumElts * 2) {
2357 ExpectedElt = 0;
2358 ReverseVEXT = true;
2359 }
2360
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002361 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002362 return false;
2363 }
2364
2365 // Adjust the index value if the source operands will be swapped.
2366 if (ReverseVEXT)
2367 Imm -= NumElts;
2368
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002369 return true;
2370}
2371
Bob Wilson8bb9e482009-07-26 00:39:34 +00002372/// isVREVMask - Check if a vector shuffle corresponds to a VREV
2373/// instruction with the specified blocksize. (The order of the elements
2374/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002375static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
2376 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00002377 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
2378 "Only possible block sizes for VREV are: 16, 32, 64");
2379
Bob Wilson8bb9e482009-07-26 00:39:34 +00002380 unsigned NumElts = VT.getVectorNumElements();
2381 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002382 unsigned BlockElts = M[0] + 1;
Bob Wilson8bb9e482009-07-26 00:39:34 +00002383
2384 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
2385 return false;
2386
2387 for (unsigned i = 0; i < NumElts; ++i) {
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002388 if ((unsigned) M[i] !=
Bob Wilson8bb9e482009-07-26 00:39:34 +00002389 (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
2390 return false;
2391 }
2392
2393 return true;
2394}
2395
Bob Wilsonc692cb72009-08-21 20:54:19 +00002396static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
2397 unsigned &WhichResult) {
2398 unsigned NumElts = VT.getVectorNumElements();
2399 WhichResult = (M[0] == 0 ? 0 : 1);
2400 for (unsigned i = 0; i < NumElts; i += 2) {
2401 if ((unsigned) M[i] != i + WhichResult ||
2402 (unsigned) M[i+1] != i + NumElts + WhichResult)
2403 return false;
2404 }
2405 return true;
2406}
2407
2408static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
2409 unsigned &WhichResult) {
2410 unsigned NumElts = VT.getVectorNumElements();
2411 WhichResult = (M[0] == 0 ? 0 : 1);
2412 for (unsigned i = 0; i != NumElts; ++i) {
2413 if ((unsigned) M[i] != 2 * i + WhichResult)
2414 return false;
2415 }
2416
2417 // VUZP.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
2424static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
2425 unsigned &WhichResult) {
2426 unsigned NumElts = VT.getVectorNumElements();
2427 WhichResult = (M[0] == 0 ? 0 : 1);
2428 unsigned Idx = WhichResult * NumElts / 2;
2429 for (unsigned i = 0; i != NumElts; i += 2) {
2430 if ((unsigned) M[i] != Idx ||
2431 (unsigned) M[i+1] != Idx + NumElts)
2432 return false;
2433 Idx += 1;
2434 }
2435
2436 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2437 if (VT.is64BitVector() && VT.getVectorElementType().getSizeInBits() == 32)
2438 return false;
2439
2440 return true;
2441}
2442
Owen Andersone50ed302009-08-10 22:56:29 +00002443static SDValue BuildSplat(SDValue Val, EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002444 // Canonicalize all-zeros and all-ones vectors.
Bob Wilsond06791f2009-08-13 01:57:47 +00002445 ConstantSDNode *ConstVal = cast<ConstantSDNode>(Val.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002446 if (ConstVal->isNullValue())
2447 return getZeroVector(VT, DAG, dl);
2448 if (ConstVal->isAllOnesValue())
2449 return getOnesVector(VT, DAG, dl);
2450
Owen Andersone50ed302009-08-10 22:56:29 +00002451 EVT CanonicalVT;
Bob Wilson5bafff32009-06-22 23:27:02 +00002452 if (VT.is64BitVector()) {
2453 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002454 case 8: CanonicalVT = MVT::v8i8; break;
2455 case 16: CanonicalVT = MVT::v4i16; break;
2456 case 32: CanonicalVT = MVT::v2i32; break;
2457 case 64: CanonicalVT = MVT::v1i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002458 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002459 }
2460 } else {
2461 assert(VT.is128BitVector() && "unknown splat vector size");
2462 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002463 case 8: CanonicalVT = MVT::v16i8; break;
2464 case 16: CanonicalVT = MVT::v8i16; break;
2465 case 32: CanonicalVT = MVT::v4i32; break;
2466 case 64: CanonicalVT = MVT::v2i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002467 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002468 }
2469 }
2470
2471 // Build a canonical splat for this value.
2472 SmallVector<SDValue, 8> Ops;
2473 Ops.assign(CanonicalVT.getVectorNumElements(), Val);
2474 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, &Ops[0],
2475 Ops.size());
2476 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Res);
2477}
2478
2479// If this is a case we can't handle, return null and let the default
2480// expansion code take care of it.
2481static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bob Wilsond06791f2009-08-13 01:57:47 +00002482 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002483 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002484 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002485
2486 APInt SplatBits, SplatUndef;
2487 unsigned SplatBitSize;
2488 bool HasAnyUndefs;
2489 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
2490 SDValue Val = isVMOVSplat(SplatBits.getZExtValue(),
2491 SplatUndef.getZExtValue(), SplatBitSize, DAG);
2492 if (Val.getNode())
Bob Wilsoncf661e22009-07-30 00:31:25 +00002493 return BuildSplat(Val, VT, DAG, dl);
2494 }
2495
2496 // If there are only 2 elements in a 128-bit vector, insert them into an
2497 // undef vector. This handles the common case for 128-bit vector argument
2498 // passing, where the insertions should be translated to subreg accesses
2499 // with no real instructions.
2500 if (VT.is128BitVector() && Op.getNumOperands() == 2) {
2501 SDValue Val = DAG.getUNDEF(VT);
2502 SDValue Op0 = Op.getOperand(0);
2503 SDValue Op1 = Op.getOperand(1);
2504 if (Op0.getOpcode() != ISD::UNDEF)
2505 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op0,
2506 DAG.getIntPtrConstant(0));
2507 if (Op1.getOpcode() != ISD::UNDEF)
2508 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op1,
2509 DAG.getIntPtrConstant(1));
2510 return Val;
Bob Wilson5bafff32009-06-22 23:27:02 +00002511 }
2512
2513 return SDValue();
2514}
2515
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002516/// isShuffleMaskLegal - Targets can use this to indicate that they only
2517/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
2518/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
2519/// are assumed to be legal.
2520bool
2521ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
2522 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002523 if (VT.getVectorNumElements() == 4 &&
2524 (VT.is128BitVector() || VT.is64BitVector())) {
2525 unsigned PFIndexes[4];
2526 for (unsigned i = 0; i != 4; ++i) {
2527 if (M[i] < 0)
2528 PFIndexes[i] = 8;
2529 else
2530 PFIndexes[i] = M[i];
2531 }
2532
2533 // Compute the index in the perfect shuffle table.
2534 unsigned PFTableIndex =
2535 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2536 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2537 unsigned Cost = (PFEntry >> 30);
2538
2539 if (Cost <= 4)
2540 return true;
2541 }
2542
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002543 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00002544 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002545
2546 return (ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
2547 isVREVMask(M, VT, 64) ||
2548 isVREVMask(M, VT, 32) ||
2549 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00002550 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
2551 isVTRNMask(M, VT, WhichResult) ||
2552 isVUZPMask(M, VT, WhichResult) ||
2553 isVZIPMask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002554}
2555
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002556/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
2557/// the specified operations to build the shuffle.
2558static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
2559 SDValue RHS, SelectionDAG &DAG,
2560 DebugLoc dl) {
2561 unsigned OpNum = (PFEntry >> 26) & 0x0F;
2562 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
2563 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
2564
2565 enum {
2566 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
2567 OP_VREV,
2568 OP_VDUP0,
2569 OP_VDUP1,
2570 OP_VDUP2,
2571 OP_VDUP3,
2572 OP_VEXT1,
2573 OP_VEXT2,
2574 OP_VEXT3,
2575 OP_VUZPL, // VUZP, left result
2576 OP_VUZPR, // VUZP, right result
2577 OP_VZIPL, // VZIP, left result
2578 OP_VZIPR, // VZIP, right result
2579 OP_VTRNL, // VTRN, left result
2580 OP_VTRNR // VTRN, right result
2581 };
2582
2583 if (OpNum == OP_COPY) {
2584 if (LHSID == (1*9+2)*9+3) return LHS;
2585 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
2586 return RHS;
2587 }
2588
2589 SDValue OpLHS, OpRHS;
2590 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
2591 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
2592 EVT VT = OpLHS.getValueType();
2593
2594 switch (OpNum) {
2595 default: llvm_unreachable("Unknown shuffle opcode!");
2596 case OP_VREV:
2597 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
2598 case OP_VDUP0:
2599 case OP_VDUP1:
2600 case OP_VDUP2:
2601 case OP_VDUP3:
2602 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002603 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002604 case OP_VEXT1:
2605 case OP_VEXT2:
2606 case OP_VEXT3:
2607 return DAG.getNode(ARMISD::VEXT, dl, VT,
2608 OpLHS, OpRHS,
2609 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
2610 case OP_VUZPL:
2611 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002612 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002613 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
2614 case OP_VZIPL:
2615 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002616 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002617 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
2618 case OP_VTRNL:
2619 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002620 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2621 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002622 }
2623}
2624
Bob Wilson5bafff32009-06-22 23:27:02 +00002625static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002626 SDValue V1 = Op.getOperand(0);
2627 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00002628 DebugLoc dl = Op.getDebugLoc();
2629 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002630 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002631 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00002632
Bob Wilson28865062009-08-13 02:13:04 +00002633 // Convert shuffles that are directly supported on NEON to target-specific
2634 // DAG nodes, instead of keeping them as shuffles and matching them again
2635 // during code selection. This is more efficient and avoids the possibility
2636 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00002637 // FIXME: floating-point vectors should be canonicalized to integer vectors
2638 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002639 SVN->getMask(ShuffleMask);
2640
2641 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
Bob Wilson0ce37102009-08-14 05:08:32 +00002642 int Lane = SVN->getSplatIndex();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002643 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
2644 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00002645 }
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002646 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002647 DAG.getConstant(Lane, MVT::i32));
Bob Wilson0ce37102009-08-14 05:08:32 +00002648 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002649
2650 bool ReverseVEXT;
2651 unsigned Imm;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002652 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002653 if (ReverseVEXT)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002654 std::swap(V1, V2);
2655 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002656 DAG.getConstant(Imm, MVT::i32));
2657 }
2658
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002659 if (isVREVMask(ShuffleMask, VT, 64))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002660 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002661 if (isVREVMask(ShuffleMask, VT, 32))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002662 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002663 if (isVREVMask(ShuffleMask, VT, 16))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002664 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
2665
Bob Wilsonc692cb72009-08-21 20:54:19 +00002666 // Check for Neon shuffles that modify both input vectors in place.
2667 // If both results are used, i.e., if there are two shuffles with the same
2668 // source operands and with masks corresponding to both results of one of
2669 // these operations, DAG memoization will ensure that a single node is
2670 // used for both shuffles.
2671 unsigned WhichResult;
2672 if (isVTRNMask(ShuffleMask, VT, WhichResult))
2673 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2674 V1, V2).getValue(WhichResult);
2675 if (isVUZPMask(ShuffleMask, VT, WhichResult))
2676 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
2677 V1, V2).getValue(WhichResult);
2678 if (isVZIPMask(ShuffleMask, VT, WhichResult))
2679 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
2680 V1, V2).getValue(WhichResult);
2681
2682 // If the shuffle is not directly supported and it has 4 elements, use
2683 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002684 if (VT.getVectorNumElements() == 4 &&
2685 (VT.is128BitVector() || VT.is64BitVector())) {
2686 unsigned PFIndexes[4];
2687 for (unsigned i = 0; i != 4; ++i) {
2688 if (ShuffleMask[i] < 0)
2689 PFIndexes[i] = 8;
2690 else
2691 PFIndexes[i] = ShuffleMask[i];
2692 }
2693
2694 // Compute the index in the perfect shuffle table.
2695 unsigned PFTableIndex =
2696 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2697
2698 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2699 unsigned Cost = (PFEntry >> 30);
2700
2701 if (Cost <= 4)
2702 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
2703 }
Bob Wilsond8e17572009-08-12 22:31:50 +00002704
Bob Wilson22cac0d2009-08-14 05:16:33 +00002705 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00002706}
2707
2708static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
2709 return Op;
2710}
2711
2712static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002713 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002714 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00002715 assert((VT == MVT::i8 || VT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00002716 "unexpected type for custom-lowering vector extract");
2717 SDValue Vec = Op.getOperand(0);
2718 SDValue Lane = Op.getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002719 Op = DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
2720 Op = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Op, DAG.getValueType(VT));
Bob Wilson5bafff32009-06-22 23:27:02 +00002721 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
2722}
2723
Bob Wilsona6d65862009-08-03 20:36:38 +00002724static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
2725 // The only time a CONCAT_VECTORS operation can have legal types is when
2726 // two 64-bit vectors are concatenated to a 128-bit vector.
2727 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
2728 "unexpected CONCAT_VECTORS");
2729 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00002730 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00002731 SDValue Op0 = Op.getOperand(0);
2732 SDValue Op1 = Op.getOperand(1);
2733 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00002734 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
2735 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00002736 DAG.getIntPtrConstant(0));
2737 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00002738 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
2739 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00002740 DAG.getIntPtrConstant(1));
2741 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00002742}
2743
Dan Gohman475871a2008-07-27 21:46:04 +00002744SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00002745 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002746 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00002747 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002748 case ISD::GlobalAddress:
2749 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
2750 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002751 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002752 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, Subtarget);
2753 case ISD::BR_CC: return LowerBR_CC(Op, DAG, Subtarget);
2754 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Evan Cheng86198642009-08-07 00:34:42 +00002755 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002756 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
2757 case ISD::SINT_TO_FP:
2758 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
2759 case ISD::FP_TO_SINT:
2760 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
2761 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00002762 case ISD::RETURNADDR: break;
Jim Grosbach0e0da732009-05-12 23:59:14 +00002763 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002764 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Bob Wilsonb36ec862009-08-06 18:47:44 +00002765 case ISD::INTRINSIC_VOID:
Bob Wilsona599bff2009-08-04 00:36:16 +00002766 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002767 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00002768 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00002769 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00002770 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00002771 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
2772 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
2773 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2774 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
2775 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
2776 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00002777 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002778 }
Dan Gohman475871a2008-07-27 21:46:04 +00002779 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00002780}
2781
Duncan Sands1607f052008-12-01 11:39:25 +00002782/// ReplaceNodeResults - Replace the results of node with an illegal result
2783/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00002784void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
2785 SmallVectorImpl<SDValue>&Results,
2786 SelectionDAG &DAG) {
Chris Lattner27a6c732007-11-24 07:07:01 +00002787 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00002788 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002789 llvm_unreachable("Don't know how to custom expand this!");
Duncan Sands1607f052008-12-01 11:39:25 +00002790 return;
2791 case ISD::BIT_CONVERT:
2792 Results.push_back(ExpandBIT_CONVERT(N, DAG));
2793 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00002794 case ISD::SRL:
Duncan Sands1607f052008-12-01 11:39:25 +00002795 case ISD::SRA: {
Bob Wilson5bafff32009-06-22 23:27:02 +00002796 SDValue Res = LowerShift(N, DAG, Subtarget);
Duncan Sands1607f052008-12-01 11:39:25 +00002797 if (Res.getNode())
2798 Results.push_back(Res);
2799 return;
2800 }
Chris Lattner27a6c732007-11-24 07:07:01 +00002801 }
2802}
Chris Lattner27a6c732007-11-24 07:07:01 +00002803
Evan Chenga8e29892007-01-19 07:51:42 +00002804//===----------------------------------------------------------------------===//
2805// ARM Scheduler Hooks
2806//===----------------------------------------------------------------------===//
2807
2808MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00002809ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00002810 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002811 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00002812 DebugLoc dl = MI->getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002813 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00002814 default:
2815 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng007ea272009-08-12 05:17:19 +00002816 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00002817 // To "insert" a SELECT_CC instruction, we actually have to insert the
2818 // diamond control-flow pattern. The incoming instruction knows the
2819 // destination vreg to set, the condition code register to branch on, the
2820 // true/false values to select between, and a branch opcode to use.
2821 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002822 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00002823 ++It;
2824
2825 // thisMBB:
2826 // ...
2827 // TrueVal = ...
2828 // cmpTY ccX, r1, r2
2829 // bCC copy1MBB
2830 // fallthrough --> copy0MBB
2831 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002832 MachineFunction *F = BB->getParent();
2833 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
2834 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenb6728402009-02-13 02:25:56 +00002835 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00002836 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002837 F->insert(It, copy0MBB);
2838 F->insert(It, sinkMBB);
Evan Chenga8e29892007-01-19 07:51:42 +00002839 // Update machine-CFG edges by first adding all successors of the current
2840 // block to the new block which will contain the Phi node for the select.
2841 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
2842 e = BB->succ_end(); i != e; ++i)
2843 sinkMBB->addSuccessor(*i);
2844 // Next, remove all successors of the current block, and add the true
2845 // and fallthrough blocks as its successors.
2846 while(!BB->succ_empty())
2847 BB->removeSuccessor(BB->succ_begin());
2848 BB->addSuccessor(copy0MBB);
2849 BB->addSuccessor(sinkMBB);
2850
2851 // copy0MBB:
2852 // %FalseValue = ...
2853 // # fallthrough to sinkMBB
2854 BB = copy0MBB;
2855
2856 // Update machine-CFG edges
2857 BB->addSuccessor(sinkMBB);
2858
2859 // sinkMBB:
2860 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2861 // ...
2862 BB = sinkMBB;
Dale Johannesenb6728402009-02-13 02:25:56 +00002863 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00002864 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
2865 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
2866
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002867 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00002868 return BB;
2869 }
Evan Cheng86198642009-08-07 00:34:42 +00002870
2871 case ARM::tANDsp:
2872 case ARM::tADDspr_:
2873 case ARM::tSUBspi_:
2874 case ARM::t2SUBrSPi_:
2875 case ARM::t2SUBrSPi12_:
2876 case ARM::t2SUBrSPs_: {
2877 MachineFunction *MF = BB->getParent();
2878 unsigned DstReg = MI->getOperand(0).getReg();
2879 unsigned SrcReg = MI->getOperand(1).getReg();
2880 bool DstIsDead = MI->getOperand(0).isDead();
2881 bool SrcIsKill = MI->getOperand(1).isKill();
2882
2883 if (SrcReg != ARM::SP) {
2884 // Copy the source to SP from virtual register.
2885 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(SrcReg);
2886 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
2887 ? ARM::tMOVtgpr2gpr : ARM::tMOVgpr2gpr;
2888 BuildMI(BB, dl, TII->get(CopyOpc), ARM::SP)
2889 .addReg(SrcReg, getKillRegState(SrcIsKill));
2890 }
2891
2892 unsigned OpOpc = 0;
2893 bool NeedPred = false, NeedCC = false, NeedOp3 = false;
2894 switch (MI->getOpcode()) {
2895 default:
2896 llvm_unreachable("Unexpected pseudo instruction!");
2897 case ARM::tANDsp:
2898 OpOpc = ARM::tAND;
2899 NeedPred = true;
2900 break;
2901 case ARM::tADDspr_:
2902 OpOpc = ARM::tADDspr;
2903 break;
2904 case ARM::tSUBspi_:
2905 OpOpc = ARM::tSUBspi;
2906 break;
2907 case ARM::t2SUBrSPi_:
2908 OpOpc = ARM::t2SUBrSPi;
2909 NeedPred = true; NeedCC = true;
2910 break;
2911 case ARM::t2SUBrSPi12_:
2912 OpOpc = ARM::t2SUBrSPi12;
2913 NeedPred = true;
2914 break;
2915 case ARM::t2SUBrSPs_:
2916 OpOpc = ARM::t2SUBrSPs;
2917 NeedPred = true; NeedCC = true; NeedOp3 = true;
2918 break;
2919 }
2920 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(OpOpc), ARM::SP);
2921 if (OpOpc == ARM::tAND)
2922 AddDefaultT1CC(MIB);
2923 MIB.addReg(ARM::SP);
2924 MIB.addOperand(MI->getOperand(2));
2925 if (NeedOp3)
2926 MIB.addOperand(MI->getOperand(3));
2927 if (NeedPred)
2928 AddDefaultPred(MIB);
2929 if (NeedCC)
2930 AddDefaultCC(MIB);
2931
2932 // Copy the result from SP to virtual register.
2933 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(DstReg);
2934 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
2935 ? ARM::tMOVgpr2tgpr : ARM::tMOVgpr2gpr;
2936 BuildMI(BB, dl, TII->get(CopyOpc))
2937 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
2938 .addReg(ARM::SP);
2939 MF->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
2940 return BB;
2941 }
Evan Chenga8e29892007-01-19 07:51:42 +00002942 }
2943}
2944
2945//===----------------------------------------------------------------------===//
2946// ARM Optimization Hooks
2947//===----------------------------------------------------------------------===//
2948
Chris Lattnerd1980a52009-03-12 06:52:53 +00002949static
2950SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
2951 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00002952 SelectionDAG &DAG = DCI.DAG;
2953 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00002954 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00002955 unsigned Opc = N->getOpcode();
2956 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
2957 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
2958 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
2959 ISD::CondCode CC = ISD::SETCC_INVALID;
2960
2961 if (isSlctCC) {
2962 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
2963 } else {
2964 SDValue CCOp = Slct.getOperand(0);
2965 if (CCOp.getOpcode() == ISD::SETCC)
2966 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
2967 }
2968
2969 bool DoXform = false;
2970 bool InvCC = false;
2971 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
2972 "Bad input!");
2973
2974 if (LHS.getOpcode() == ISD::Constant &&
2975 cast<ConstantSDNode>(LHS)->isNullValue()) {
2976 DoXform = true;
2977 } else if (CC != ISD::SETCC_INVALID &&
2978 RHS.getOpcode() == ISD::Constant &&
2979 cast<ConstantSDNode>(RHS)->isNullValue()) {
2980 std::swap(LHS, RHS);
2981 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00002982 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00002983 Op0.getOperand(0).getValueType();
2984 bool isInt = OpVT.isInteger();
2985 CC = ISD::getSetCCInverse(CC, isInt);
2986
2987 if (!TLI.isCondCodeLegal(CC, OpVT))
2988 return SDValue(); // Inverse operator isn't legal.
2989
2990 DoXform = true;
2991 InvCC = true;
2992 }
2993
2994 if (DoXform) {
2995 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
2996 if (isSlctCC)
2997 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
2998 Slct.getOperand(0), Slct.getOperand(1), CC);
2999 SDValue CCOp = Slct.getOperand(0);
3000 if (InvCC)
3001 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
3002 CCOp.getOperand(0), CCOp.getOperand(1), CC);
3003 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
3004 CCOp, OtherOp, Result);
3005 }
3006 return SDValue();
3007}
3008
3009/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
3010static SDValue PerformADDCombine(SDNode *N,
3011 TargetLowering::DAGCombinerInfo &DCI) {
3012 // added by evan in r37685 with no testcase.
3013 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003014
Chris Lattnerd1980a52009-03-12 06:52:53 +00003015 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
3016 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
3017 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
3018 if (Result.getNode()) return Result;
3019 }
3020 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3021 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3022 if (Result.getNode()) return Result;
3023 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003024
Chris Lattnerd1980a52009-03-12 06:52:53 +00003025 return SDValue();
3026}
3027
3028/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
3029static SDValue PerformSUBCombine(SDNode *N,
3030 TargetLowering::DAGCombinerInfo &DCI) {
3031 // added by evan in r37685 with no testcase.
3032 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003033
Chris Lattnerd1980a52009-03-12 06:52:53 +00003034 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
3035 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3036 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3037 if (Result.getNode()) return Result;
3038 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003039
Chris Lattnerd1980a52009-03-12 06:52:53 +00003040 return SDValue();
3041}
3042
3043
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003044/// PerformFMRRDCombine - Target-specific dag combine xforms for ARMISD::FMRRD.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003045static SDValue PerformFMRRDCombine(SDNode *N,
3046 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003047 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00003048 SDValue InDouble = N->getOperand(0);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003049 if (InDouble.getOpcode() == ARMISD::FMDRR)
3050 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003051 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003052}
3053
Bob Wilson5bafff32009-06-22 23:27:02 +00003054/// getVShiftImm - Check if this is a valid build_vector for the immediate
3055/// operand of a vector shift operation, where all the elements of the
3056/// build_vector must have the same constant integer value.
3057static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
3058 // Ignore bit_converts.
3059 while (Op.getOpcode() == ISD::BIT_CONVERT)
3060 Op = Op.getOperand(0);
3061 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
3062 APInt SplatBits, SplatUndef;
3063 unsigned SplatBitSize;
3064 bool HasAnyUndefs;
3065 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
3066 HasAnyUndefs, ElementBits) ||
3067 SplatBitSize > ElementBits)
3068 return false;
3069 Cnt = SplatBits.getSExtValue();
3070 return true;
3071}
3072
3073/// isVShiftLImm - Check if this is a valid build_vector for the immediate
3074/// operand of a vector shift left operation. That value must be in the range:
3075/// 0 <= Value < ElementBits for a left shift; or
3076/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003077static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003078 assert(VT.isVector() && "vector shift count is not a vector type");
3079 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3080 if (! getVShiftImm(Op, ElementBits, Cnt))
3081 return false;
3082 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
3083}
3084
3085/// isVShiftRImm - Check if this is a valid build_vector for the immediate
3086/// operand of a vector shift right operation. For a shift opcode, the value
3087/// is positive, but for an intrinsic the value count must be negative. The
3088/// absolute value must be in the range:
3089/// 1 <= |Value| <= ElementBits for a right shift; or
3090/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003091static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00003092 int64_t &Cnt) {
3093 assert(VT.isVector() && "vector shift count is not a vector type");
3094 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3095 if (! getVShiftImm(Op, ElementBits, Cnt))
3096 return false;
3097 if (isIntrinsic)
3098 Cnt = -Cnt;
3099 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
3100}
3101
3102/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
3103static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
3104 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
3105 switch (IntNo) {
3106 default:
3107 // Don't do anything for most intrinsics.
3108 break;
3109
3110 // Vector shifts: check for immediate versions and lower them.
3111 // Note: This is done during DAG combining instead of DAG legalizing because
3112 // the build_vectors for 64-bit vector element shift counts are generally
3113 // not legal, and it is hard to see their values after they get legalized to
3114 // loads from a constant pool.
3115 case Intrinsic::arm_neon_vshifts:
3116 case Intrinsic::arm_neon_vshiftu:
3117 case Intrinsic::arm_neon_vshiftls:
3118 case Intrinsic::arm_neon_vshiftlu:
3119 case Intrinsic::arm_neon_vshiftn:
3120 case Intrinsic::arm_neon_vrshifts:
3121 case Intrinsic::arm_neon_vrshiftu:
3122 case Intrinsic::arm_neon_vrshiftn:
3123 case Intrinsic::arm_neon_vqshifts:
3124 case Intrinsic::arm_neon_vqshiftu:
3125 case Intrinsic::arm_neon_vqshiftsu:
3126 case Intrinsic::arm_neon_vqshiftns:
3127 case Intrinsic::arm_neon_vqshiftnu:
3128 case Intrinsic::arm_neon_vqshiftnsu:
3129 case Intrinsic::arm_neon_vqrshiftns:
3130 case Intrinsic::arm_neon_vqrshiftnu:
3131 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00003132 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003133 int64_t Cnt;
3134 unsigned VShiftOpc = 0;
3135
3136 switch (IntNo) {
3137 case Intrinsic::arm_neon_vshifts:
3138 case Intrinsic::arm_neon_vshiftu:
3139 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
3140 VShiftOpc = ARMISD::VSHL;
3141 break;
3142 }
3143 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
3144 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
3145 ARMISD::VSHRs : ARMISD::VSHRu);
3146 break;
3147 }
3148 return SDValue();
3149
3150 case Intrinsic::arm_neon_vshiftls:
3151 case Intrinsic::arm_neon_vshiftlu:
3152 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
3153 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003154 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003155
3156 case Intrinsic::arm_neon_vrshifts:
3157 case Intrinsic::arm_neon_vrshiftu:
3158 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
3159 break;
3160 return SDValue();
3161
3162 case Intrinsic::arm_neon_vqshifts:
3163 case Intrinsic::arm_neon_vqshiftu:
3164 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3165 break;
3166 return SDValue();
3167
3168 case Intrinsic::arm_neon_vqshiftsu:
3169 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3170 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003171 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003172
3173 case Intrinsic::arm_neon_vshiftn:
3174 case Intrinsic::arm_neon_vrshiftn:
3175 case Intrinsic::arm_neon_vqshiftns:
3176 case Intrinsic::arm_neon_vqshiftnu:
3177 case Intrinsic::arm_neon_vqshiftnsu:
3178 case Intrinsic::arm_neon_vqrshiftns:
3179 case Intrinsic::arm_neon_vqrshiftnu:
3180 case Intrinsic::arm_neon_vqrshiftnsu:
3181 // Narrowing shifts require an immediate right shift.
3182 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
3183 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003184 llvm_unreachable("invalid shift count for narrowing vector shift intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003185
3186 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003187 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003188 }
3189
3190 switch (IntNo) {
3191 case Intrinsic::arm_neon_vshifts:
3192 case Intrinsic::arm_neon_vshiftu:
3193 // Opcode already set above.
3194 break;
3195 case Intrinsic::arm_neon_vshiftls:
3196 case Intrinsic::arm_neon_vshiftlu:
3197 if (Cnt == VT.getVectorElementType().getSizeInBits())
3198 VShiftOpc = ARMISD::VSHLLi;
3199 else
3200 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
3201 ARMISD::VSHLLs : ARMISD::VSHLLu);
3202 break;
3203 case Intrinsic::arm_neon_vshiftn:
3204 VShiftOpc = ARMISD::VSHRN; break;
3205 case Intrinsic::arm_neon_vrshifts:
3206 VShiftOpc = ARMISD::VRSHRs; break;
3207 case Intrinsic::arm_neon_vrshiftu:
3208 VShiftOpc = ARMISD::VRSHRu; break;
3209 case Intrinsic::arm_neon_vrshiftn:
3210 VShiftOpc = ARMISD::VRSHRN; break;
3211 case Intrinsic::arm_neon_vqshifts:
3212 VShiftOpc = ARMISD::VQSHLs; break;
3213 case Intrinsic::arm_neon_vqshiftu:
3214 VShiftOpc = ARMISD::VQSHLu; break;
3215 case Intrinsic::arm_neon_vqshiftsu:
3216 VShiftOpc = ARMISD::VQSHLsu; break;
3217 case Intrinsic::arm_neon_vqshiftns:
3218 VShiftOpc = ARMISD::VQSHRNs; break;
3219 case Intrinsic::arm_neon_vqshiftnu:
3220 VShiftOpc = ARMISD::VQSHRNu; break;
3221 case Intrinsic::arm_neon_vqshiftnsu:
3222 VShiftOpc = ARMISD::VQSHRNsu; break;
3223 case Intrinsic::arm_neon_vqrshiftns:
3224 VShiftOpc = ARMISD::VQRSHRNs; break;
3225 case Intrinsic::arm_neon_vqrshiftnu:
3226 VShiftOpc = ARMISD::VQRSHRNu; break;
3227 case Intrinsic::arm_neon_vqrshiftnsu:
3228 VShiftOpc = ARMISD::VQRSHRNsu; break;
3229 }
3230
3231 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003232 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003233 }
3234
3235 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00003236 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003237 int64_t Cnt;
3238 unsigned VShiftOpc = 0;
3239
3240 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
3241 VShiftOpc = ARMISD::VSLI;
3242 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
3243 VShiftOpc = ARMISD::VSRI;
3244 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00003245 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003246 }
3247
3248 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
3249 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00003250 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003251 }
3252
3253 case Intrinsic::arm_neon_vqrshifts:
3254 case Intrinsic::arm_neon_vqrshiftu:
3255 // No immediate versions of these to check for.
3256 break;
3257 }
3258
3259 return SDValue();
3260}
3261
3262/// PerformShiftCombine - Checks for immediate versions of vector shifts and
3263/// lowers them. As with the vector shift intrinsics, this is done during DAG
3264/// combining instead of DAG legalizing because the build_vectors for 64-bit
3265/// vector element shift counts are generally not legal, and it is hard to see
3266/// their values after they get legalized to loads from a constant pool.
3267static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
3268 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003269 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003270
3271 // Nothing to be done for scalar shifts.
3272 if (! VT.isVector())
3273 return SDValue();
3274
3275 assert(ST->hasNEON() && "unexpected vector shift");
3276 int64_t Cnt;
3277
3278 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003279 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003280
3281 case ISD::SHL:
3282 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
3283 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003284 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003285 break;
3286
3287 case ISD::SRA:
3288 case ISD::SRL:
3289 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
3290 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
3291 ARMISD::VSHRs : ARMISD::VSHRu);
3292 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003293 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003294 }
3295 }
3296 return SDValue();
3297}
3298
3299/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
3300/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
3301static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
3302 const ARMSubtarget *ST) {
3303 SDValue N0 = N->getOperand(0);
3304
3305 // Check for sign- and zero-extensions of vector extract operations of 8-
3306 // and 16-bit vector elements. NEON supports these directly. They are
3307 // handled during DAG combining because type legalization will promote them
3308 // to 32-bit types and it is messy to recognize the operations after that.
3309 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
3310 SDValue Vec = N0.getOperand(0);
3311 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00003312 EVT VT = N->getValueType(0);
3313 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003314 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3315
Owen Anderson825b72b2009-08-11 20:47:22 +00003316 if (VT == MVT::i32 &&
3317 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00003318 TLI.isTypeLegal(Vec.getValueType())) {
3319
3320 unsigned Opc = 0;
3321 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003322 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003323 case ISD::SIGN_EXTEND:
3324 Opc = ARMISD::VGETLANEs;
3325 break;
3326 case ISD::ZERO_EXTEND:
3327 case ISD::ANY_EXTEND:
3328 Opc = ARMISD::VGETLANEu;
3329 break;
3330 }
3331 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
3332 }
3333 }
3334
3335 return SDValue();
3336}
3337
Dan Gohman475871a2008-07-27 21:46:04 +00003338SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003339 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003340 switch (N->getOpcode()) {
3341 default: break;
Chris Lattnerd1980a52009-03-12 06:52:53 +00003342 case ISD::ADD: return PerformADDCombine(N, DCI);
3343 case ISD::SUB: return PerformSUBCombine(N, DCI);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003344 case ARMISD::FMRRD: return PerformFMRRDCombine(N, DCI);
Bob Wilson5bafff32009-06-22 23:27:02 +00003345 case ISD::INTRINSIC_WO_CHAIN:
3346 return PerformIntrinsicCombine(N, DCI.DAG);
3347 case ISD::SHL:
3348 case ISD::SRA:
3349 case ISD::SRL:
3350 return PerformShiftCombine(N, DCI.DAG, Subtarget);
3351 case ISD::SIGN_EXTEND:
3352 case ISD::ZERO_EXTEND:
3353 case ISD::ANY_EXTEND:
3354 return PerformExtendCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003355 }
Dan Gohman475871a2008-07-27 21:46:04 +00003356 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003357}
3358
Bill Wendlingaf566342009-08-15 21:21:19 +00003359bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
3360 if (!Subtarget->hasV6Ops())
3361 // Pre-v6 does not support unaligned mem access.
3362 return false;
3363 else if (!Subtarget->hasV6Ops()) {
3364 // v6 may or may not support unaligned mem access.
3365 if (!Subtarget->isTargetDarwin())
3366 return false;
3367 }
3368
3369 switch (VT.getSimpleVT().SimpleTy) {
3370 default:
3371 return false;
3372 case MVT::i8:
3373 case MVT::i16:
3374 case MVT::i32:
3375 return true;
3376 // FIXME: VLD1 etc with standard alignment is legal.
3377 }
3378}
3379
Evan Chenge6c835f2009-08-14 20:09:37 +00003380static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
3381 if (V < 0)
3382 return false;
3383
3384 unsigned Scale = 1;
3385 switch (VT.getSimpleVT().SimpleTy) {
3386 default: return false;
3387 case MVT::i1:
3388 case MVT::i8:
3389 // Scale == 1;
3390 break;
3391 case MVT::i16:
3392 // Scale == 2;
3393 Scale = 2;
3394 break;
3395 case MVT::i32:
3396 // Scale == 4;
3397 Scale = 4;
3398 break;
3399 }
3400
3401 if ((V & (Scale - 1)) != 0)
3402 return false;
3403 V /= Scale;
3404 return V == (V & ((1LL << 5) - 1));
3405}
3406
3407static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
3408 const ARMSubtarget *Subtarget) {
3409 bool isNeg = false;
3410 if (V < 0) {
3411 isNeg = true;
3412 V = - V;
3413 }
3414
3415 switch (VT.getSimpleVT().SimpleTy) {
3416 default: return false;
3417 case MVT::i1:
3418 case MVT::i8:
3419 case MVT::i16:
3420 case MVT::i32:
3421 // + imm12 or - imm8
3422 if (isNeg)
3423 return V == (V & ((1LL << 8) - 1));
3424 return V == (V & ((1LL << 12) - 1));
3425 case MVT::f32:
3426 case MVT::f64:
3427 // Same as ARM mode. FIXME: NEON?
3428 if (!Subtarget->hasVFP2())
3429 return false;
3430 if ((V & 3) != 0)
3431 return false;
3432 V >>= 2;
3433 return V == (V & ((1LL << 8) - 1));
3434 }
3435}
3436
Evan Chengb01fad62007-03-12 23:30:29 +00003437/// isLegalAddressImmediate - Return true if the integer value can be used
3438/// as the offset of the target addressing mode for load / store of the
3439/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00003440static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00003441 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00003442 if (V == 0)
3443 return true;
3444
Evan Cheng65011532009-03-09 19:15:00 +00003445 if (!VT.isSimple())
3446 return false;
3447
Evan Chenge6c835f2009-08-14 20:09:37 +00003448 if (Subtarget->isThumb1Only())
3449 return isLegalT1AddressImmediate(V, VT);
3450 else if (Subtarget->isThumb2())
3451 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00003452
Evan Chenge6c835f2009-08-14 20:09:37 +00003453 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00003454 if (V < 0)
3455 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00003456 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00003457 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00003458 case MVT::i1:
3459 case MVT::i8:
3460 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00003461 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003462 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003463 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00003464 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003465 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003466 case MVT::f32:
3467 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00003468 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00003469 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00003470 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00003471 return false;
3472 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003473 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00003474 }
Evan Chenga8e29892007-01-19 07:51:42 +00003475}
3476
Evan Chenge6c835f2009-08-14 20:09:37 +00003477bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
3478 EVT VT) const {
3479 int Scale = AM.Scale;
3480 if (Scale < 0)
3481 return false;
3482
3483 switch (VT.getSimpleVT().SimpleTy) {
3484 default: return false;
3485 case MVT::i1:
3486 case MVT::i8:
3487 case MVT::i16:
3488 case MVT::i32:
3489 if (Scale == 1)
3490 return true;
3491 // r + r << imm
3492 Scale = Scale & ~1;
3493 return Scale == 2 || Scale == 4 || Scale == 8;
3494 case MVT::i64:
3495 // r + r
3496 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
3497 return true;
3498 return false;
3499 case MVT::isVoid:
3500 // Note, we allow "void" uses (basically, uses that aren't loads or
3501 // stores), because arm allows folding a scale into many arithmetic
3502 // operations. This should be made more precise and revisited later.
3503
3504 // Allow r << imm, but the imm has to be a multiple of two.
3505 if (Scale & 1) return false;
3506 return isPowerOf2_32(Scale);
3507 }
3508}
3509
Chris Lattner37caf8c2007-04-09 23:33:39 +00003510/// isLegalAddressingMode - Return true if the addressing mode represented
3511/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003512bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00003513 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00003514 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00003515 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00003516 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003517
Chris Lattner37caf8c2007-04-09 23:33:39 +00003518 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003519 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003520 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003521
Chris Lattner37caf8c2007-04-09 23:33:39 +00003522 switch (AM.Scale) {
3523 case 0: // no scale reg, must be "r+i" or "r", or "i".
3524 break;
3525 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00003526 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00003527 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00003528 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00003529 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00003530 // ARM doesn't support any R+R*scale+imm addr modes.
3531 if (AM.BaseOffs)
3532 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003533
Bob Wilson2c7dab12009-04-08 17:55:28 +00003534 if (!VT.isSimple())
3535 return false;
3536
Evan Chenge6c835f2009-08-14 20:09:37 +00003537 if (Subtarget->isThumb2())
3538 return isLegalT2ScaledAddressingMode(AM, VT);
3539
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003540 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00003541 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00003542 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00003543 case MVT::i1:
3544 case MVT::i8:
3545 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003546 if (Scale < 0) Scale = -Scale;
3547 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003548 return true;
3549 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00003550 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003551 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00003552 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00003553 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003554 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003555 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00003556 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003557
Owen Anderson825b72b2009-08-11 20:47:22 +00003558 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00003559 // Note, we allow "void" uses (basically, uses that aren't loads or
3560 // stores), because arm allows folding a scale into many arithmetic
3561 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003562
Chris Lattner37caf8c2007-04-09 23:33:39 +00003563 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00003564 if (Scale & 1) return false;
3565 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00003566 }
3567 break;
Evan Chengb01fad62007-03-12 23:30:29 +00003568 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00003569 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00003570}
3571
Owen Andersone50ed302009-08-10 22:56:29 +00003572static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00003573 bool isSEXTLoad, SDValue &Base,
3574 SDValue &Offset, bool &isInc,
3575 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00003576 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
3577 return false;
3578
Owen Anderson825b72b2009-08-11 20:47:22 +00003579 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00003580 // AddressingMode 3
3581 Base = Ptr->getOperand(0);
3582 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003583 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003584 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003585 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00003586 isInc = false;
3587 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3588 return true;
3589 }
3590 }
3591 isInc = (Ptr->getOpcode() == ISD::ADD);
3592 Offset = Ptr->getOperand(1);
3593 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00003594 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00003595 // AddressingMode 2
3596 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003597 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003598 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003599 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00003600 isInc = false;
3601 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3602 Base = Ptr->getOperand(0);
3603 return true;
3604 }
3605 }
3606
3607 if (Ptr->getOpcode() == ISD::ADD) {
3608 isInc = true;
3609 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
3610 if (ShOpcVal != ARM_AM::no_shift) {
3611 Base = Ptr->getOperand(1);
3612 Offset = Ptr->getOperand(0);
3613 } else {
3614 Base = Ptr->getOperand(0);
3615 Offset = Ptr->getOperand(1);
3616 }
3617 return true;
3618 }
3619
3620 isInc = (Ptr->getOpcode() == ISD::ADD);
3621 Base = Ptr->getOperand(0);
3622 Offset = Ptr->getOperand(1);
3623 return true;
3624 }
3625
3626 // FIXME: Use FLDM / FSTM to emulate indexed FP load / store.
3627 return false;
3628}
3629
Owen Andersone50ed302009-08-10 22:56:29 +00003630static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00003631 bool isSEXTLoad, SDValue &Base,
3632 SDValue &Offset, bool &isInc,
3633 SelectionDAG &DAG) {
3634 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
3635 return false;
3636
3637 Base = Ptr->getOperand(0);
3638 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
3639 int RHSC = (int)RHS->getZExtValue();
3640 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
3641 assert(Ptr->getOpcode() == ISD::ADD);
3642 isInc = false;
3643 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3644 return true;
3645 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
3646 isInc = Ptr->getOpcode() == ISD::ADD;
3647 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
3648 return true;
3649 }
3650 }
3651
3652 return false;
3653}
3654
Evan Chenga8e29892007-01-19 07:51:42 +00003655/// getPreIndexedAddressParts - returns true by value, base pointer and
3656/// offset pointer and addressing mode by reference if the node's address
3657/// can be legally represented as pre-indexed load / store address.
3658bool
Dan Gohman475871a2008-07-27 21:46:04 +00003659ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
3660 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003661 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00003662 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003663 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00003664 return false;
3665
Owen Andersone50ed302009-08-10 22:56:29 +00003666 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00003667 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00003668 bool isSEXTLoad = false;
3669 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
3670 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003671 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003672 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
3673 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
3674 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003675 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003676 } else
3677 return false;
3678
3679 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00003680 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00003681 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00003682 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
3683 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00003684 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00003685 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00003686 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00003687 if (!isLegal)
3688 return false;
3689
3690 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
3691 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00003692}
3693
3694/// getPostIndexedAddressParts - returns true by value, base pointer and
3695/// offset pointer and addressing mode by reference if this node can be
3696/// combined with a load / store to form a post-indexed load / store.
3697bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00003698 SDValue &Base,
3699 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003700 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00003701 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003702 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00003703 return false;
3704
Owen Andersone50ed302009-08-10 22:56:29 +00003705 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00003706 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00003707 bool isSEXTLoad = false;
3708 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003709 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003710 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
3711 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003712 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003713 } else
3714 return false;
3715
3716 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00003717 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00003718 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00003719 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003720 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00003721 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00003722 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
3723 isInc, DAG);
3724 if (!isLegal)
3725 return false;
3726
3727 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
3728 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00003729}
3730
Dan Gohman475871a2008-07-27 21:46:04 +00003731void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00003732 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003733 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003734 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00003735 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00003736 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003737 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00003738 switch (Op.getOpcode()) {
3739 default: break;
3740 case ARMISD::CMOV: {
3741 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00003742 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00003743 if (KnownZero == 0 && KnownOne == 0) return;
3744
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003745 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00003746 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
3747 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00003748 KnownZero &= KnownZeroRHS;
3749 KnownOne &= KnownOneRHS;
3750 return;
3751 }
3752 }
3753}
3754
3755//===----------------------------------------------------------------------===//
3756// ARM Inline Assembly Support
3757//===----------------------------------------------------------------------===//
3758
3759/// getConstraintType - Given a constraint letter, return the type of
3760/// constraint it is for this target.
3761ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00003762ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
3763 if (Constraint.size() == 1) {
3764 switch (Constraint[0]) {
3765 default: break;
3766 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003767 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00003768 }
Evan Chenga8e29892007-01-19 07:51:42 +00003769 }
Chris Lattner4234f572007-03-25 02:14:49 +00003770 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00003771}
3772
Bob Wilson2dc4f542009-03-20 22:42:55 +00003773std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00003774ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00003775 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003776 if (Constraint.size() == 1) {
3777 // GCC RS6000 Constraint Letters
3778 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003779 case 'l':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003780 if (Subtarget->isThumb1Only())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00003781 return std::make_pair(0U, ARM::tGPRRegisterClass);
3782 else
3783 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003784 case 'r':
3785 return std::make_pair(0U, ARM::GPRRegisterClass);
3786 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00003787 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003788 return std::make_pair(0U, ARM::SPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00003789 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003790 return std::make_pair(0U, ARM::DPRRegisterClass);
3791 break;
Evan Chenga8e29892007-01-19 07:51:42 +00003792 }
3793 }
3794 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3795}
3796
3797std::vector<unsigned> ARMTargetLowering::
3798getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00003799 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003800 if (Constraint.size() != 1)
3801 return std::vector<unsigned>();
3802
3803 switch (Constraint[0]) { // GCC ARM Constraint Letters
3804 default: break;
3805 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00003806 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
3807 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
3808 0);
Evan Chenga8e29892007-01-19 07:51:42 +00003809 case 'r':
3810 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
3811 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
3812 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
3813 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003814 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00003815 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003816 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
3817 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
3818 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
3819 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
3820 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
3821 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
3822 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
3823 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00003824 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003825 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
3826 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
3827 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
3828 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
3829 break;
Evan Chenga8e29892007-01-19 07:51:42 +00003830 }
3831
3832 return std::vector<unsigned>();
3833}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003834
3835/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3836/// vector. If it is invalid, don't add anything to Ops.
3837void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3838 char Constraint,
3839 bool hasMemory,
3840 std::vector<SDValue>&Ops,
3841 SelectionDAG &DAG) const {
3842 SDValue Result(0, 0);
3843
3844 switch (Constraint) {
3845 default: break;
3846 case 'I': case 'J': case 'K': case 'L':
3847 case 'M': case 'N': case 'O':
3848 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
3849 if (!C)
3850 return;
3851
3852 int64_t CVal64 = C->getSExtValue();
3853 int CVal = (int) CVal64;
3854 // None of these constraints allow values larger than 32 bits. Check
3855 // that the value fits in an int.
3856 if (CVal != CVal64)
3857 return;
3858
3859 switch (Constraint) {
3860 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003861 if (Subtarget->isThumb1Only()) {
3862 // This must be a constant between 0 and 255, for ADD
3863 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003864 if (CVal >= 0 && CVal <= 255)
3865 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00003866 } else if (Subtarget->isThumb2()) {
3867 // A constant that can be used as an immediate value in a
3868 // data-processing instruction.
3869 if (ARM_AM::getT2SOImmVal(CVal) != -1)
3870 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003871 } else {
3872 // A constant that can be used as an immediate value in a
3873 // data-processing instruction.
3874 if (ARM_AM::getSOImmVal(CVal) != -1)
3875 break;
3876 }
3877 return;
3878
3879 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003880 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003881 // This must be a constant between -255 and -1, for negated ADD
3882 // immediates. This can be used in GCC with an "n" modifier that
3883 // prints the negated value, for use with SUB instructions. It is
3884 // not useful otherwise but is implemented for compatibility.
3885 if (CVal >= -255 && CVal <= -1)
3886 break;
3887 } else {
3888 // This must be a constant between -4095 and 4095. It is not clear
3889 // what this constraint is intended for. Implemented for
3890 // compatibility with GCC.
3891 if (CVal >= -4095 && CVal <= 4095)
3892 break;
3893 }
3894 return;
3895
3896 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003897 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003898 // A 32-bit value where only one byte has a nonzero value. Exclude
3899 // zero to match GCC. This constraint is used by GCC internally for
3900 // constants that can be loaded with a move/shift combination.
3901 // It is not useful otherwise but is implemented for compatibility.
3902 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
3903 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00003904 } else if (Subtarget->isThumb2()) {
3905 // A constant whose bitwise inverse can be used as an immediate
3906 // value in a data-processing instruction. This can be used in GCC
3907 // with a "B" modifier that prints the inverted value, for use with
3908 // BIC and MVN instructions. It is not useful otherwise but is
3909 // implemented for compatibility.
3910 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
3911 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003912 } else {
3913 // A constant whose bitwise inverse can be used as an immediate
3914 // value in a data-processing instruction. This can be used in GCC
3915 // with a "B" modifier that prints the inverted value, for use with
3916 // BIC and MVN instructions. It is not useful otherwise but is
3917 // implemented for compatibility.
3918 if (ARM_AM::getSOImmVal(~CVal) != -1)
3919 break;
3920 }
3921 return;
3922
3923 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003924 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003925 // This must be a constant between -7 and 7,
3926 // for 3-operand ADD/SUB immediate instructions.
3927 if (CVal >= -7 && CVal < 7)
3928 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00003929 } else if (Subtarget->isThumb2()) {
3930 // A constant whose negation can be used as an immediate value in a
3931 // data-processing instruction. This can be used in GCC with an "n"
3932 // modifier that prints the negated value, for use with SUB
3933 // instructions. It is not useful otherwise but is implemented for
3934 // compatibility.
3935 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
3936 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003937 } else {
3938 // A constant whose negation can be used as an immediate value in a
3939 // data-processing instruction. This can be used in GCC with an "n"
3940 // modifier that prints the negated value, for use with SUB
3941 // instructions. It is not useful otherwise but is implemented for
3942 // compatibility.
3943 if (ARM_AM::getSOImmVal(-CVal) != -1)
3944 break;
3945 }
3946 return;
3947
3948 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003949 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003950 // This must be a multiple of 4 between 0 and 1020, for
3951 // ADD sp + immediate.
3952 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
3953 break;
3954 } else {
3955 // A power of two or a constant between 0 and 32. This is used in
3956 // GCC for the shift amount on shifted register operands, but it is
3957 // useful in general for any shift amounts.
3958 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
3959 break;
3960 }
3961 return;
3962
3963 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003964 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003965 // This must be a constant between 0 and 31, for shift amounts.
3966 if (CVal >= 0 && CVal <= 31)
3967 break;
3968 }
3969 return;
3970
3971 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003972 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003973 // This must be a multiple of 4 between -508 and 508, for
3974 // ADD/SUB sp = sp + immediate.
3975 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
3976 break;
3977 }
3978 return;
3979 }
3980 Result = DAG.getTargetConstant(CVal, Op.getValueType());
3981 break;
3982 }
3983
3984 if (Result.getNode()) {
3985 Ops.push_back(Result);
3986 return;
3987 }
3988 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
3989 Ops, DAG);
3990}