blob: 7d308ca805afbe030b6f701e7023b4165899275a [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 Wilsona599bff2009-08-04 00:36:16 +0000486 case ARMISD::VLD2D: return "ARMISD::VLD2D";
487 case ARMISD::VLD3D: return "ARMISD::VLD3D";
488 case ARMISD::VLD4D: return "ARMISD::VLD4D";
Bob Wilsonb36ec862009-08-06 18:47:44 +0000489 case ARMISD::VST2D: return "ARMISD::VST2D";
490 case ARMISD::VST3D: return "ARMISD::VST3D";
491 case ARMISD::VST4D: return "ARMISD::VST4D";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000492 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000493 case ARMISD::VREV64: return "ARMISD::VREV64";
494 case ARMISD::VREV32: return "ARMISD::VREV32";
495 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000496 case ARMISD::VZIP: return "ARMISD::VZIP";
497 case ARMISD::VUZP: return "ARMISD::VUZP";
498 case ARMISD::VTRN: return "ARMISD::VTRN";
Evan Chenga8e29892007-01-19 07:51:42 +0000499 }
500}
501
Bill Wendlingb4202b82009-07-01 18:50:55 +0000502/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000503unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
504 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 1 : 2;
505}
506
Evan Chenga8e29892007-01-19 07:51:42 +0000507//===----------------------------------------------------------------------===//
508// Lowering Code
509//===----------------------------------------------------------------------===//
510
Evan Chenga8e29892007-01-19 07:51:42 +0000511/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
512static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
513 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000514 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000515 case ISD::SETNE: return ARMCC::NE;
516 case ISD::SETEQ: return ARMCC::EQ;
517 case ISD::SETGT: return ARMCC::GT;
518 case ISD::SETGE: return ARMCC::GE;
519 case ISD::SETLT: return ARMCC::LT;
520 case ISD::SETLE: return ARMCC::LE;
521 case ISD::SETUGT: return ARMCC::HI;
522 case ISD::SETUGE: return ARMCC::HS;
523 case ISD::SETULT: return ARMCC::LO;
524 case ISD::SETULE: return ARMCC::LS;
525 }
526}
527
528/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC. It
529/// returns true if the operands should be inverted to form the proper
530/// comparison.
531static bool FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
532 ARMCC::CondCodes &CondCode2) {
533 bool Invert = false;
534 CondCode2 = ARMCC::AL;
535 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000536 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000537 case ISD::SETEQ:
538 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
539 case ISD::SETGT:
540 case ISD::SETOGT: CondCode = ARMCC::GT; break;
541 case ISD::SETGE:
542 case ISD::SETOGE: CondCode = ARMCC::GE; break;
543 case ISD::SETOLT: CondCode = ARMCC::MI; break;
544 case ISD::SETOLE: CondCode = ARMCC::GT; Invert = true; break;
545 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
546 case ISD::SETO: CondCode = ARMCC::VC; break;
547 case ISD::SETUO: CondCode = ARMCC::VS; break;
548 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
549 case ISD::SETUGT: CondCode = ARMCC::HI; break;
550 case ISD::SETUGE: CondCode = ARMCC::PL; break;
551 case ISD::SETLT:
552 case ISD::SETULT: CondCode = ARMCC::LT; break;
553 case ISD::SETLE:
554 case ISD::SETULE: CondCode = ARMCC::LE; break;
555 case ISD::SETNE:
556 case ISD::SETUNE: CondCode = ARMCC::NE; break;
557 }
558 return Invert;
559}
560
Bob Wilson1f595bb2009-04-17 19:07:39 +0000561//===----------------------------------------------------------------------===//
562// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +0000563//===----------------------------------------------------------------------===//
564
565#include "ARMGenCallingConv.inc"
566
567// APCS f64 is in register pairs, possibly split to stack
Owen Andersone50ed302009-08-10 22:56:29 +0000568static bool f64AssignAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000569 CCValAssign::LocInfo &LocInfo,
570 CCState &State, bool CanFail) {
571 static const unsigned RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
572
573 // Try to get the first register.
574 if (unsigned Reg = State.AllocateReg(RegList, 4))
575 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
576 else {
577 // For the 2nd half of a v2f64, do not fail.
578 if (CanFail)
579 return false;
580
581 // Put the whole thing on the stack.
582 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
583 State.AllocateStack(8, 4),
584 LocVT, LocInfo));
585 return true;
586 }
587
588 // Try to get the second register.
589 if (unsigned Reg = State.AllocateReg(RegList, 4))
590 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
591 else
592 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
593 State.AllocateStack(4, 4),
594 LocVT, LocInfo));
595 return true;
596}
597
Owen Andersone50ed302009-08-10 22:56:29 +0000598static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000599 CCValAssign::LocInfo &LocInfo,
600 ISD::ArgFlagsTy &ArgFlags,
601 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000602 if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
603 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000604 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000605 !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
606 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000607 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000608}
609
610// AAPCS f64 is in aligned register pairs
Owen Andersone50ed302009-08-10 22:56:29 +0000611static bool f64AssignAAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000612 CCValAssign::LocInfo &LocInfo,
613 CCState &State, bool CanFail) {
614 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
615 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
616
617 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
618 if (Reg == 0) {
619 // For the 2nd half of a v2f64, do not just fail.
620 if (CanFail)
621 return false;
622
623 // Put the whole thing on the stack.
624 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
625 State.AllocateStack(8, 8),
626 LocVT, LocInfo));
627 return true;
628 }
629
630 unsigned i;
631 for (i = 0; i < 2; ++i)
632 if (HiRegList[i] == Reg)
633 break;
634
635 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
636 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
637 LocVT, LocInfo));
638 return true;
639}
640
Owen Andersone50ed302009-08-10 22:56:29 +0000641static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000642 CCValAssign::LocInfo &LocInfo,
643 ISD::ArgFlagsTy &ArgFlags,
644 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000645 if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
646 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000647 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000648 !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
649 return false;
650 return true; // we handled it
651}
652
Owen Andersone50ed302009-08-10 22:56:29 +0000653static bool f64RetAssign(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000654 CCValAssign::LocInfo &LocInfo, CCState &State) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000655 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
656 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
657
Bob Wilsone65586b2009-04-17 20:40:45 +0000658 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
659 if (Reg == 0)
660 return false; // we didn't handle it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000661
Bob Wilsone65586b2009-04-17 20:40:45 +0000662 unsigned i;
663 for (i = 0; i < 2; ++i)
664 if (HiRegList[i] == Reg)
665 break;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000666
Bob Wilson5bafff32009-06-22 23:27:02 +0000667 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bob Wilsone65586b2009-04-17 20:40:45 +0000668 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000669 LocVT, LocInfo));
670 return true;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000671}
672
Owen Andersone50ed302009-08-10 22:56:29 +0000673static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000674 CCValAssign::LocInfo &LocInfo,
675 ISD::ArgFlagsTy &ArgFlags,
676 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000677 if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
678 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000679 if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
Bob Wilson5bafff32009-06-22 23:27:02 +0000680 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000681 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000682}
683
Owen Andersone50ed302009-08-10 22:56:29 +0000684static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000685 CCValAssign::LocInfo &LocInfo,
686 ISD::ArgFlagsTy &ArgFlags,
687 CCState &State) {
688 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
689 State);
690}
691
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000692/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
693/// given CallingConvention value.
694CCAssignFn *ARMTargetLowering::CCAssignFnForNode(unsigned CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000695 bool Return,
696 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000697 switch (CC) {
698 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000699 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000700 case CallingConv::C:
701 case CallingConv::Fast:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000702 // Use target triple & subtarget features to do actual dispatch.
703 if (Subtarget->isAAPCS_ABI()) {
704 if (Subtarget->hasVFP2() &&
705 FloatABIType == FloatABI::Hard && !isVarArg)
706 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
707 else
708 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
709 } else
710 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000711 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000712 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000713 case CallingConv::ARM_AAPCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000714 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000715 case CallingConv::ARM_APCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000716 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000717 }
718}
719
Dan Gohman98ca4f22009-08-05 01:29:28 +0000720/// LowerCallResult - Lower the result values of a call into the
721/// appropriate copies out of appropriate physical registers.
722SDValue
723ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
724 unsigned CallConv, bool isVarArg,
725 const SmallVectorImpl<ISD::InputArg> &Ins,
726 DebugLoc dl, SelectionDAG &DAG,
727 SmallVectorImpl<SDValue> &InVals) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000728
Bob Wilson1f595bb2009-04-17 19:07:39 +0000729 // Assign locations to each value returned by this call.
730 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000731 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000732 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000733 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000734 CCAssignFnForNode(CallConv, /* Return*/ true,
735 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000736
737 // Copy all of the result registers out of their specified physreg.
738 for (unsigned i = 0; i != RVLocs.size(); ++i) {
739 CCValAssign VA = RVLocs[i];
740
Bob Wilson80915242009-04-25 00:33:20 +0000741 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000742 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000743 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000744 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000745 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000746 Chain = Lo.getValue(1);
747 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000748 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000749 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000750 InFlag);
751 Chain = Hi.getValue(1);
752 InFlag = Hi.getValue(2);
Owen Anderson825b72b2009-08-11 20:47:22 +0000753 Val = DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +0000754
Owen Anderson825b72b2009-08-11 20:47:22 +0000755 if (VA.getLocVT() == MVT::v2f64) {
756 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
757 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
758 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000759
760 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000761 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000762 Chain = Lo.getValue(1);
763 InFlag = Lo.getValue(2);
764 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000765 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000766 Chain = Hi.getValue(1);
767 InFlag = Hi.getValue(2);
Owen Anderson825b72b2009-08-11 20:47:22 +0000768 Val = DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
769 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
770 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000771 }
Bob Wilson1f595bb2009-04-17 19:07:39 +0000772 } else {
Bob Wilson80915242009-04-25 00:33:20 +0000773 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
774 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000775 Chain = Val.getValue(1);
776 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000777 }
Bob Wilson80915242009-04-25 00:33:20 +0000778
779 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000780 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +0000781 case CCValAssign::Full: break;
782 case CCValAssign::BCvt:
783 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
784 break;
785 }
786
Dan Gohman98ca4f22009-08-05 01:29:28 +0000787 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000788 }
789
Dan Gohman98ca4f22009-08-05 01:29:28 +0000790 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000791}
792
793/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
794/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +0000795/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +0000796/// a byval function parameter.
797/// Sometimes what we are copying is the end of a larger object, the part that
798/// does not fit in registers.
799static SDValue
800CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
801 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
802 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000803 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000804 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
805 /*AlwaysInline=*/false, NULL, 0, NULL, 0);
806}
807
Bob Wilsondee46d72009-04-17 20:35:10 +0000808/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000809SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +0000810ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
811 SDValue StackPtr, SDValue Arg,
812 DebugLoc dl, SelectionDAG &DAG,
813 const CCValAssign &VA,
814 ISD::ArgFlagsTy Flags) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000815 unsigned LocMemOffset = VA.getLocMemOffset();
816 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
817 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
818 if (Flags.isByVal()) {
819 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
820 }
821 return DAG.getStore(Chain, dl, Arg, PtrOff,
822 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chenga8e29892007-01-19 07:51:42 +0000823}
824
Dan Gohman98ca4f22009-08-05 01:29:28 +0000825void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +0000826 SDValue Chain, SDValue &Arg,
827 RegsToPassVector &RegsToPass,
828 CCValAssign &VA, CCValAssign &NextVA,
829 SDValue &StackPtr,
830 SmallVector<SDValue, 8> &MemOpChains,
831 ISD::ArgFlagsTy Flags) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000832
833 SDValue fmrrd = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +0000834 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +0000835 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
836
837 if (NextVA.isRegLoc())
838 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
839 else {
840 assert(NextVA.isMemLoc());
841 if (StackPtr.getNode() == 0)
842 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
843
Dan Gohman98ca4f22009-08-05 01:29:28 +0000844 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
845 dl, DAG, NextVA,
846 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000847 }
848}
849
Dan Gohman98ca4f22009-08-05 01:29:28 +0000850/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +0000851/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
852/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000853SDValue
854ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
855 unsigned CallConv, bool isVarArg,
856 bool isTailCall,
857 const SmallVectorImpl<ISD::OutputArg> &Outs,
858 const SmallVectorImpl<ISD::InputArg> &Ins,
859 DebugLoc dl, SelectionDAG &DAG,
860 SmallVectorImpl<SDValue> &InVals) {
Evan Chenga8e29892007-01-19 07:51:42 +0000861
Bob Wilson1f595bb2009-04-17 19:07:39 +0000862 // Analyze operands of the call, assigning locations to each operand.
863 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000864 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
865 *DAG.getContext());
866 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000867 CCAssignFnForNode(CallConv, /* Return*/ false,
868 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +0000869
Bob Wilson1f595bb2009-04-17 19:07:39 +0000870 // Get a count of how many bytes are to be pushed on the stack.
871 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +0000872
873 // Adjust the stack pointer for the new arguments...
874 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattnere563bbc2008-10-11 22:08:30 +0000875 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +0000876
Owen Anderson825b72b2009-08-11 20:47:22 +0000877 SDValue StackPtr = DAG.getRegister(ARM::SP, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000878
Bob Wilson5bafff32009-06-22 23:27:02 +0000879 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000880 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +0000881
Bob Wilson1f595bb2009-04-17 19:07:39 +0000882 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +0000883 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000884 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
885 i != e;
886 ++i, ++realArgIdx) {
887 CCValAssign &VA = ArgLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +0000888 SDValue Arg = Outs[realArgIdx].Val;
889 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +0000890
Bob Wilson1f595bb2009-04-17 19:07:39 +0000891 // Promote the value if needed.
892 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000893 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +0000894 case CCValAssign::Full: break;
895 case CCValAssign::SExt:
896 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
897 break;
898 case CCValAssign::ZExt:
899 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
900 break;
901 case CCValAssign::AExt:
902 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
903 break;
904 case CCValAssign::BCvt:
905 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
906 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000907 }
908
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000909 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +0000910 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000911 if (VA.getLocVT() == MVT::v2f64) {
912 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
913 DAG.getConstant(0, MVT::i32));
914 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
915 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000916
Dan Gohman98ca4f22009-08-05 01:29:28 +0000917 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +0000918 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
919
920 VA = ArgLocs[++i]; // skip ahead to next loc
921 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000922 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +0000923 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
924 } else {
925 assert(VA.isMemLoc());
926 if (StackPtr.getNode() == 0)
927 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
928
Dan Gohman98ca4f22009-08-05 01:29:28 +0000929 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
930 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000931 }
932 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000933 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000934 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000935 }
936 } else if (VA.isRegLoc()) {
937 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
938 } else {
939 assert(VA.isMemLoc());
940 if (StackPtr.getNode() == 0)
941 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
942
Dan Gohman98ca4f22009-08-05 01:29:28 +0000943 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
944 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000945 }
Evan Chenga8e29892007-01-19 07:51:42 +0000946 }
947
948 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000949 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +0000950 &MemOpChains[0], MemOpChains.size());
951
952 // Build a sequence of copy-to-reg nodes chained together with token chain
953 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +0000954 SDValue InFlag;
Evan Chenga8e29892007-01-19 07:51:42 +0000955 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Bob Wilson2dc4f542009-03-20 22:42:55 +0000956 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000957 RegsToPass[i].second, InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +0000958 InFlag = Chain.getValue(1);
959 }
960
Bill Wendling056292f2008-09-16 21:48:12 +0000961 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
962 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
963 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +0000964 bool isDirect = false;
965 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +0000966 bool isLocalARMFunc = false;
Evan Chenga8e29892007-01-19 07:51:42 +0000967 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
968 GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +0000969 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +0000970 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +0000971 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +0000972 getTargetMachine().getRelocationModel() != Reloc::Static;
973 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +0000974 // ARM call to a local ARM function is predicable.
975 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
Evan Chengc60e76d2007-01-30 20:37:08 +0000976 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +0000977 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chengc60e76d2007-01-30 20:37:08 +0000978 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex,
979 ARMCP::CPStub, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000980 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +0000981 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000982 Callee = DAG.getLoad(getPointerTy(), dl,
983 DAG.getEntryNode(), CPAddr, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000984 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000985 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000986 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +0000987 } else
988 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +0000989 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000990 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +0000991 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +0000992 getTargetMachine().getRelocationModel() != Reloc::Static;
993 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +0000994 // tBX takes a register source operand.
995 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +0000996 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000997 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
998 Sym, ARMPCLabelIndex,
Evan Chengc60e76d2007-01-30 20:37:08 +0000999 ARMCP::CPStub, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001000 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001001 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001002 Callee = DAG.getLoad(getPointerTy(), dl,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001003 DAG.getEntryNode(), CPAddr, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001004 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001005 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001006 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001007 } else
Bill Wendling056292f2008-09-16 21:48:12 +00001008 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001009 }
1010
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001011 // FIXME: handle tail calls differently.
1012 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001013 if (Subtarget->isThumb()) {
1014 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001015 CallOpc = ARMISD::CALL_NOLINK;
1016 else
1017 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1018 } else {
1019 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001020 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1021 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001022 }
David Goodwinf1daf7d2009-07-08 23:10:31 +00001023 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb1Only()) {
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +00001024 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Owen Anderson825b72b2009-08-11 20:47:22 +00001025 Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001026 InFlag = Chain.getValue(1);
1027 }
1028
Dan Gohman475871a2008-07-27 21:46:04 +00001029 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001030 Ops.push_back(Chain);
1031 Ops.push_back(Callee);
1032
1033 // Add argument registers to the end of the list so that they are known live
1034 // into the call.
1035 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1036 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1037 RegsToPass[i].second.getValueType()));
1038
Gabor Greifba36cb52008-08-28 21:40:38 +00001039 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001040 Ops.push_back(InFlag);
Duncan Sands4bdcb612008-07-02 17:40:58 +00001041 // Returns a chain and a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00001042 Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Flag),
Duncan Sands4bdcb612008-07-02 17:40:58 +00001043 &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001044 InFlag = Chain.getValue(1);
1045
Chris Lattnere563bbc2008-10-11 22:08:30 +00001046 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1047 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001048 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001049 InFlag = Chain.getValue(1);
1050
Bob Wilson1f595bb2009-04-17 19:07:39 +00001051 // Handle result values, copying them out of physregs into vregs that we
1052 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001053 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1054 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001055}
1056
Dan Gohman98ca4f22009-08-05 01:29:28 +00001057SDValue
1058ARMTargetLowering::LowerReturn(SDValue Chain,
1059 unsigned CallConv, bool isVarArg,
1060 const SmallVectorImpl<ISD::OutputArg> &Outs,
1061 DebugLoc dl, SelectionDAG &DAG) {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001062
Bob Wilsondee46d72009-04-17 20:35:10 +00001063 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001064 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001065
Bob Wilsondee46d72009-04-17 20:35:10 +00001066 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001067 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1068 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001069
Dan Gohman98ca4f22009-08-05 01:29:28 +00001070 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001071 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1072 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001073
1074 // If this is the first return lowered for this function, add
1075 // the regs to the liveout set for the function.
1076 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1077 for (unsigned i = 0; i != RVLocs.size(); ++i)
1078 if (RVLocs[i].isRegLoc())
1079 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001080 }
1081
Bob Wilson1f595bb2009-04-17 19:07:39 +00001082 SDValue Flag;
1083
1084 // Copy the result values into the output registers.
1085 for (unsigned i = 0, realRVLocIdx = 0;
1086 i != RVLocs.size();
1087 ++i, ++realRVLocIdx) {
1088 CCValAssign &VA = RVLocs[i];
1089 assert(VA.isRegLoc() && "Can only return in registers!");
1090
Dan Gohman98ca4f22009-08-05 01:29:28 +00001091 SDValue Arg = Outs[realRVLocIdx].Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001092
1093 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001094 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001095 case CCValAssign::Full: break;
1096 case CCValAssign::BCvt:
1097 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1098 break;
1099 }
1100
Bob Wilson1f595bb2009-04-17 19:07:39 +00001101 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001102 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001103 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001104 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1105 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001106 SDValue HalfGPRs = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001107 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001108
1109 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1110 Flag = Chain.getValue(1);
1111 VA = RVLocs[++i]; // skip ahead to next loc
1112 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1113 HalfGPRs.getValue(1), Flag);
1114 Flag = Chain.getValue(1);
1115 VA = RVLocs[++i]; // skip ahead to next loc
1116
1117 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001118 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1119 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001120 }
1121 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1122 // available.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001123 SDValue fmrrd = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001124 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001125 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001126 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001127 VA = RVLocs[++i]; // skip ahead to next loc
1128 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1129 Flag);
1130 } else
1131 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1132
Bob Wilsondee46d72009-04-17 20:35:10 +00001133 // Guarantee that all emitted copies are
1134 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001135 Flag = Chain.getValue(1);
1136 }
1137
1138 SDValue result;
1139 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001140 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001141 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001142 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001143
1144 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001145}
1146
Bob Wilson2dc4f542009-03-20 22:42:55 +00001147// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
Bob Wilsond2559bf2009-07-13 18:11:36 +00001148// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
Bill Wendling056292f2008-09-16 21:48:12 +00001149// one of the above mentioned nodes. It has to be wrapped because otherwise
1150// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1151// be used to form addressing mode. These wrapped nodes will be selected
1152// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001153static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001154 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001155 // FIXME there is no actual debug info here
1156 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001157 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001158 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001159 if (CP->isMachineConstantPoolEntry())
1160 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1161 CP->getAlignment());
1162 else
1163 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1164 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001165 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001166}
1167
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001168// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001169SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001170ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
1171 SelectionDAG &DAG) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001172 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001173 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001174 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1175 ARMConstantPoolValue *CPV =
1176 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex, ARMCP::CPValue,
1177 PCAdj, "tlsgd", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001178 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001179 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001180 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001181 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001182
Owen Anderson825b72b2009-08-11 20:47:22 +00001183 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001184 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001185
1186 // call __tls_get_addr.
1187 ArgListTy Args;
1188 ArgListEntry Entry;
1189 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001190 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001191 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001192 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001193 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001194 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1195 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001196 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001197 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001198 return CallResult.first;
1199}
1200
1201// Lower ISD::GlobalTLSAddress using the "initial exec" or
1202// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001203SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001204ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001205 SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001206 GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001207 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001208 SDValue Offset;
1209 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001210 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001211 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001212 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001213
Chris Lattner4fb63d02009-07-15 04:12:33 +00001214 if (GV->isDeclaration()) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001215 // initial exec model
1216 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1217 ARMConstantPoolValue *CPV =
1218 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex, ARMCP::CPValue,
1219 PCAdj, "gottpoff", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001220 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001221 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
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 Chain = Offset.getValue(1);
1224
Owen Anderson825b72b2009-08-11 20:47:22 +00001225 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001226 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001227
Dale Johannesen33c960f2009-02-04 20:06:27 +00001228 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001229 } else {
1230 // local exec model
1231 ARMConstantPoolValue *CPV =
1232 new ARMConstantPoolValue(GV, ARMCP::CPValue, "tpoff");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001233 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001234 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001235 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001236 }
1237
1238 // The address of the thread local variable is the add of the thread
1239 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001240 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001241}
1242
Dan Gohman475871a2008-07-27 21:46:04 +00001243SDValue
1244ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001245 // TODO: implement the "local dynamic" model
1246 assert(Subtarget->isTargetELF() &&
1247 "TLS not implemented for non-ELF targets");
1248 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1249 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1250 // otherwise use the "Local Exec" TLS Model
1251 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1252 return LowerToTLSGeneralDynamicModel(GA, DAG);
1253 else
1254 return LowerToTLSExecModels(GA, DAG);
1255}
1256
Dan Gohman475871a2008-07-27 21:46:04 +00001257SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001258 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001259 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001260 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001261 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1262 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1263 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001264 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001265 ARMConstantPoolValue *CPV =
1266 new ARMConstantPoolValue(GV, ARMCP::CPValue, UseGOTOFF ? "GOTOFF":"GOT");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001267 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001268 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001269 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Dale Johannesen33c960f2009-02-04 20:06:27 +00001270 CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001271 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001272 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001273 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001274 if (!UseGOTOFF)
Dale Johannesen33c960f2009-02-04 20:06:27 +00001275 Result = DAG.getLoad(PtrVT, dl, Chain, Result, NULL, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001276 return Result;
1277 } else {
Evan Cheng1606e8e2009-03-13 07:51:59 +00001278 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001279 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001280 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001281 }
1282}
1283
Evan Chenga8e29892007-01-19 07:51:42 +00001284/// GVIsIndirectSymbol - true if the GV will be accessed via an indirect symbol
Evan Cheng97c9bb52007-05-04 00:26:58 +00001285/// even in non-static mode.
1286static bool GVIsIndirectSymbol(GlobalValue *GV, Reloc::Model RelocM) {
Evan Chengae94e592008-12-05 01:06:39 +00001287 // If symbol visibility is hidden, the extra load is not needed if
1288 // the symbol is definitely defined in the current translation unit.
Chris Lattner4fb63d02009-07-15 04:12:33 +00001289 bool isDecl = GV->isDeclaration() || GV->hasAvailableExternallyLinkage();
Evan Chengae94e592008-12-05 01:06:39 +00001290 if (GV->hasHiddenVisibility() && (!isDecl && !GV->hasCommonLinkage()))
1291 return false;
Duncan Sands667d4b82009-03-07 15:45:40 +00001292 return RelocM != Reloc::Static && (isDecl || GV->isWeakForLinker());
Evan Chenga8e29892007-01-19 07:51:42 +00001293}
1294
Dan Gohman475871a2008-07-27 21:46:04 +00001295SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001296 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001297 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001298 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001299 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1300 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng97c9bb52007-05-04 00:26:58 +00001301 bool IsIndirect = GVIsIndirectSymbol(GV, RelocM);
Dan Gohman475871a2008-07-27 21:46:04 +00001302 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +00001303 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001304 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001305 else {
1306 unsigned PCAdj = (RelocM != Reloc::PIC_)
1307 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Evan Chengc60e76d2007-01-30 20:37:08 +00001308 ARMCP::ARMCPKind Kind = IsIndirect ? ARMCP::CPNonLazyPtr
1309 : ARMCP::CPValue;
Evan Chenga8e29892007-01-19 07:51:42 +00001310 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex,
Evan Chengc60e76d2007-01-30 20:37:08 +00001311 Kind, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001312 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001313 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001314 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001315
Dale Johannesen33c960f2009-02-04 20:06:27 +00001316 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001317 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001318
1319 if (RelocM == Reloc::PIC_) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001320 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001321 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001322 }
1323 if (IsIndirect)
Dale Johannesen33c960f2009-02-04 20:06:27 +00001324 Result = DAG.getLoad(PtrVT, dl, Chain, Result, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001325
1326 return Result;
1327}
1328
Dan Gohman475871a2008-07-27 21:46:04 +00001329SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001330 SelectionDAG &DAG){
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001331 assert(Subtarget->isTargetELF() &&
1332 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Owen Andersone50ed302009-08-10 22:56:29 +00001333 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001334 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001335 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00001336 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1337 "_GLOBAL_OFFSET_TABLE_",
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001338 ARMPCLabelIndex,
1339 ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001340 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001341 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001342 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001343 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001344 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001345}
1346
Bob Wilsona599bff2009-08-04 00:36:16 +00001347static SDValue LowerNeonVLDIntrinsic(SDValue Op, SelectionDAG &DAG,
Bob Wilson4a3d35a2009-08-05 00:49:09 +00001348 unsigned Opcode) {
Bob Wilsona599bff2009-08-04 00:36:16 +00001349 SDNode *Node = Op.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001350 EVT VT = Node->getValueType(0);
Bob Wilsona599bff2009-08-04 00:36:16 +00001351 DebugLoc dl = Op.getDebugLoc();
1352
1353 if (!VT.is64BitVector())
1354 return SDValue(); // unimplemented
1355
1356 SDValue Ops[] = { Node->getOperand(0),
Bob Wilson4a3d35a2009-08-05 00:49:09 +00001357 Node->getOperand(2) };
1358 return DAG.getNode(Opcode, dl, Node->getVTList(), Ops, 2);
Bob Wilsona599bff2009-08-04 00:36:16 +00001359}
1360
Bob Wilsonb36ec862009-08-06 18:47:44 +00001361static SDValue LowerNeonVSTIntrinsic(SDValue Op, SelectionDAG &DAG,
1362 unsigned Opcode, unsigned NumVecs) {
1363 SDNode *Node = Op.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001364 EVT VT = Node->getOperand(3).getValueType();
Bob Wilsonb36ec862009-08-06 18:47:44 +00001365 DebugLoc dl = Op.getDebugLoc();
1366
1367 if (!VT.is64BitVector())
1368 return SDValue(); // unimplemented
1369
1370 SmallVector<SDValue, 6> Ops;
1371 Ops.push_back(Node->getOperand(0));
1372 Ops.push_back(Node->getOperand(2));
1373 for (unsigned N = 0; N < NumVecs; ++N)
1374 Ops.push_back(Node->getOperand(N + 3));
Owen Anderson825b72b2009-08-11 20:47:22 +00001375 return DAG.getNode(Opcode, dl, MVT::Other, Ops.data(), Ops.size());
Bob Wilsonb36ec862009-08-06 18:47:44 +00001376}
1377
Bob Wilsona599bff2009-08-04 00:36:16 +00001378SDValue
1379ARMTargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) {
1380 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1381 switch (IntNo) {
Bob Wilsonb0abb4d2009-08-11 05:39:44 +00001382 case Intrinsic::arm_neon_vld2:
Bob Wilson4a3d35a2009-08-05 00:49:09 +00001383 return LowerNeonVLDIntrinsic(Op, DAG, ARMISD::VLD2D);
Bob Wilsonb0abb4d2009-08-11 05:39:44 +00001384 case Intrinsic::arm_neon_vld3:
Bob Wilson4a3d35a2009-08-05 00:49:09 +00001385 return LowerNeonVLDIntrinsic(Op, DAG, ARMISD::VLD3D);
Bob Wilsonb0abb4d2009-08-11 05:39:44 +00001386 case Intrinsic::arm_neon_vld4:
Bob Wilson4a3d35a2009-08-05 00:49:09 +00001387 return LowerNeonVLDIntrinsic(Op, DAG, ARMISD::VLD4D);
Bob Wilsonb0abb4d2009-08-11 05:39:44 +00001388 case Intrinsic::arm_neon_vst2:
Bob Wilsonb36ec862009-08-06 18:47:44 +00001389 return LowerNeonVSTIntrinsic(Op, DAG, ARMISD::VST2D, 2);
Bob Wilsonb0abb4d2009-08-11 05:39:44 +00001390 case Intrinsic::arm_neon_vst3:
Bob Wilsonb36ec862009-08-06 18:47:44 +00001391 return LowerNeonVSTIntrinsic(Op, DAG, ARMISD::VST3D, 3);
Bob Wilsonb0abb4d2009-08-11 05:39:44 +00001392 case Intrinsic::arm_neon_vst4:
Bob Wilsonb36ec862009-08-06 18:47:44 +00001393 return LowerNeonVSTIntrinsic(Op, DAG, ARMISD::VST4D, 4);
Bob Wilsona599bff2009-08-04 00:36:16 +00001394 default: return SDValue(); // Don't custom lower most intrinsics.
1395 }
1396}
1397
Jim Grosbach0e0da732009-05-12 23:59:14 +00001398SDValue
1399ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001400 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001401 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001402 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001403 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00001404 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00001405 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00001406 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1407 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001408 case Intrinsic::eh_sjlj_lsda: {
1409 // blah. horrible, horrible hack with the forced magic name.
1410 // really need to clean this up. It belongs in the target-independent
1411 // layer somehow that doesn't require the coupling with the asm
1412 // printer.
1413 MachineFunction &MF = DAG.getMachineFunction();
1414 EVT PtrVT = getPointerTy();
1415 DebugLoc dl = Op.getDebugLoc();
1416 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1417 SDValue CPAddr;
1418 unsigned PCAdj = (RelocM != Reloc::PIC_)
1419 ? 0 : (Subtarget->isThumb() ? 4 : 8);
1420 ARMCP::ARMCPKind Kind = ARMCP::CPValue;
1421 // Save off the LSDA name for the AsmPrinter to use when it's time
1422 // to emit the table
1423 std::string LSDAName = "L_lsda_";
1424 LSDAName += MF.getFunction()->getName();
1425 ARMConstantPoolValue *CPV =
Owen Anderson1d0be152009-08-13 21:58:54 +00001426 new ARMConstantPoolValue(*DAG.getContext(), LSDAName.c_str(),
1427 ARMPCLabelIndex, Kind, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001428 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001429 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001430 SDValue Result =
1431 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
1432 SDValue Chain = Result.getValue(1);
1433
1434 if (RelocM == Reloc::PIC_) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001435 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001436 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1437 }
1438 return Result;
1439 }
Jim Grosbachf9570122009-05-14 00:46:35 +00001440 case Intrinsic::eh_sjlj_setjmp:
Owen Anderson825b72b2009-08-11 20:47:22 +00001441 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(1));
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001442 }
1443}
1444
Dan Gohman475871a2008-07-27 21:46:04 +00001445static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001446 unsigned VarArgsFrameIndex) {
Evan Chenga8e29892007-01-19 07:51:42 +00001447 // vastart just stores the address of the VarArgsFrameIndex slot into the
1448 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001449 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001450 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001451 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001452 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001453 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001454}
1455
Dan Gohman475871a2008-07-27 21:46:04 +00001456SDValue
Evan Cheng86198642009-08-07 00:34:42 +00001457ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) {
1458 SDNode *Node = Op.getNode();
1459 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001460 EVT VT = Node->getValueType(0);
Evan Cheng86198642009-08-07 00:34:42 +00001461 SDValue Chain = Op.getOperand(0);
1462 SDValue Size = Op.getOperand(1);
1463 SDValue Align = Op.getOperand(2);
1464
1465 // Chain the dynamic stack allocation so that it doesn't modify the stack
1466 // pointer when other instructions are using the stack.
1467 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1468
1469 unsigned AlignVal = cast<ConstantSDNode>(Align)->getZExtValue();
1470 unsigned StackAlign = getTargetMachine().getFrameInfo()->getStackAlignment();
1471 if (AlignVal > StackAlign)
1472 // Do this now since selection pass cannot introduce new target
1473 // independent node.
1474 Align = DAG.getConstant(-(uint64_t)AlignVal, VT);
1475
1476 // In Thumb1 mode, there isn't a "sub r, sp, r" instruction, we will end up
1477 // using a "add r, sp, r" instead. Negate the size now so we don't have to
1478 // do even more horrible hack later.
1479 MachineFunction &MF = DAG.getMachineFunction();
1480 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1481 if (AFI->isThumb1OnlyFunction()) {
1482 bool Negate = true;
1483 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Size);
1484 if (C) {
1485 uint32_t Val = C->getZExtValue();
1486 if (Val <= 508 && ((Val & 3) == 0))
1487 Negate = false;
1488 }
1489 if (Negate)
1490 Size = DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(0, VT), Size);
1491 }
1492
Owen Anderson825b72b2009-08-11 20:47:22 +00001493 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
Evan Cheng86198642009-08-07 00:34:42 +00001494 SDValue Ops1[] = { Chain, Size, Align };
1495 SDValue Res = DAG.getNode(ARMISD::DYN_ALLOC, dl, VTList, Ops1, 3);
1496 Chain = Res.getValue(1);
1497 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1498 DAG.getIntPtrConstant(0, true), SDValue());
1499 SDValue Ops2[] = { Res, Chain };
1500 return DAG.getMergeValues(Ops2, 2, dl);
1501}
1502
1503SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00001504ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
1505 SDValue &Root, SelectionDAG &DAG,
1506 DebugLoc dl) {
1507 MachineFunction &MF = DAG.getMachineFunction();
1508 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1509
1510 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001511 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00001512 RC = ARM::tGPRRegisterClass;
1513 else
1514 RC = ARM::GPRRegisterClass;
1515
1516 // Transform the arguments stored in physical registers into virtual ones.
1517 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001518 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001519
1520 SDValue ArgValue2;
1521 if (NextVA.isMemLoc()) {
1522 unsigned ArgSize = NextVA.getLocVT().getSizeInBits()/8;
1523 MachineFrameInfo *MFI = MF.getFrameInfo();
1524 int FI = MFI->CreateFixedObject(ArgSize, NextVA.getLocMemOffset());
1525
1526 // Create load node to retrieve arguments from the stack.
1527 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00001528 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN, NULL, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00001529 } else {
1530 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001531 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001532 }
1533
Owen Anderson825b72b2009-08-11 20:47:22 +00001534 return DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00001535}
1536
1537SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001538ARMTargetLowering::LowerFormalArguments(SDValue Chain,
1539 unsigned CallConv, bool isVarArg,
1540 const SmallVectorImpl<ISD::InputArg>
1541 &Ins,
1542 DebugLoc dl, SelectionDAG &DAG,
1543 SmallVectorImpl<SDValue> &InVals) {
1544
Bob Wilson1f595bb2009-04-17 19:07:39 +00001545 MachineFunction &MF = DAG.getMachineFunction();
1546 MachineFrameInfo *MFI = MF.getFrameInfo();
1547
Bob Wilson1f595bb2009-04-17 19:07:39 +00001548 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1549
1550 // Assign locations to all of the incoming arguments.
1551 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001552 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1553 *DAG.getContext());
1554 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001555 CCAssignFnForNode(CallConv, /* Return*/ false,
1556 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001557
1558 SmallVector<SDValue, 16> ArgValues;
1559
1560 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1561 CCValAssign &VA = ArgLocs[i];
1562
Bob Wilsondee46d72009-04-17 20:35:10 +00001563 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001564 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001565 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00001566
Bob Wilson5bafff32009-06-22 23:27:02 +00001567 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001568 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001569 // f64 and vector types are split up into multiple registers or
1570 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00001571 RegVT = MVT::i32;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001572
Owen Anderson825b72b2009-08-11 20:47:22 +00001573 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001574 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001575 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00001576 VA = ArgLocs[++i]; // skip ahead to next loc
1577 SDValue ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001578 Chain, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001579 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1580 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001581 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00001582 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001583 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
1584 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001585 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001586
Bob Wilson5bafff32009-06-22 23:27:02 +00001587 } else {
1588 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001589
Owen Anderson825b72b2009-08-11 20:47:22 +00001590 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00001591 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001592 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00001593 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001594 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001595 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001596 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001597 RC = (AFI->isThumb1OnlyFunction() ?
1598 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00001599 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001600 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00001601
1602 // Transform the arguments in physical registers into virtual ones.
1603 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001604 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001605 }
1606
1607 // If this is an 8 or 16-bit value, it is really passed promoted
1608 // to 32 bits. Insert an assert[sz]ext to capture this, then
1609 // truncate to the right size.
1610 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001611 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001612 case CCValAssign::Full: break;
1613 case CCValAssign::BCvt:
1614 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1615 break;
1616 case CCValAssign::SExt:
1617 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
1618 DAG.getValueType(VA.getValVT()));
1619 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1620 break;
1621 case CCValAssign::ZExt:
1622 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
1623 DAG.getValueType(VA.getValVT()));
1624 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1625 break;
1626 }
1627
Dan Gohman98ca4f22009-08-05 01:29:28 +00001628 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001629
1630 } else { // VA.isRegLoc()
1631
1632 // sanity check
1633 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00001634 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001635
1636 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
1637 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset());
1638
Bob Wilsondee46d72009-04-17 20:35:10 +00001639 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001640 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001641 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN, NULL, 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001642 }
1643 }
1644
1645 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00001646 if (isVarArg) {
1647 static const unsigned GPRArgRegs[] = {
1648 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1649 };
1650
Bob Wilsondee46d72009-04-17 20:35:10 +00001651 unsigned NumGPRs = CCInfo.getFirstUnallocated
1652 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001653
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001654 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1655 unsigned VARegSize = (4 - NumGPRs) * 4;
1656 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001657 unsigned ArgOffset = 0;
Evan Chenga8e29892007-01-19 07:51:42 +00001658 if (VARegSaveSize) {
1659 // If this function is vararg, store any remaining integer argument regs
1660 // to their spots on the stack so that they may be loaded by deferencing
1661 // the result of va_next.
1662 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001663 ArgOffset = CCInfo.getNextStackOffset();
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001664 VarArgsFrameIndex = MFI->CreateFixedObject(VARegSaveSize, ArgOffset +
1665 VARegSaveSize - VARegSize);
Dan Gohman475871a2008-07-27 21:46:04 +00001666 SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001667
Dan Gohman475871a2008-07-27 21:46:04 +00001668 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00001669 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001670 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001671 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00001672 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001673 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00001674 RC = ARM::GPRRegisterClass;
1675
Bob Wilson998e1252009-04-20 18:36:57 +00001676 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001677 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001678 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001679 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001680 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00001681 DAG.getConstant(4, getPointerTy()));
1682 }
1683 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001684 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001685 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001686 } else
1687 // This will point to the next argument passed via stack.
1688 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1689 }
1690
Dan Gohman98ca4f22009-08-05 01:29:28 +00001691 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00001692}
1693
1694/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00001695static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00001696 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001697 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00001698 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00001699 // Maybe this has already been legalized into the constant pool?
1700 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00001701 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001702 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
1703 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001704 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001705 }
1706 }
1707 return false;
1708}
1709
David Goodwinf1daf7d2009-07-08 23:10:31 +00001710static bool isLegalCmpImmediate(unsigned C, bool isThumb1Only) {
1711 return ( isThumb1Only && (C & ~255U) == 0) ||
1712 (!isThumb1Only && ARM_AM::getSOImmVal(C) != -1);
Evan Chenga8e29892007-01-19 07:51:42 +00001713}
1714
1715/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1716/// the given operands.
Dan Gohman475871a2008-07-27 21:46:04 +00001717static SDValue getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
David Goodwinf1daf7d2009-07-08 23:10:31 +00001718 SDValue &ARMCC, SelectionDAG &DAG, bool isThumb1Only,
Dale Johannesende064702009-02-06 21:50:26 +00001719 DebugLoc dl) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001720 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001721 unsigned C = RHSC->getZExtValue();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001722 if (!isLegalCmpImmediate(C, isThumb1Only)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001723 // Constant does not fit, try adjusting it by one?
1724 switch (CC) {
1725 default: break;
1726 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00001727 case ISD::SETGE:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001728 if (isLegalCmpImmediate(C-1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001729 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001730 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001731 }
1732 break;
1733 case ISD::SETULT:
1734 case ISD::SETUGE:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001735 if (C > 0 && isLegalCmpImmediate(C-1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001736 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001737 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001738 }
1739 break;
1740 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00001741 case ISD::SETGT:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001742 if (isLegalCmpImmediate(C+1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001743 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001744 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001745 }
1746 break;
1747 case ISD::SETULE:
1748 case ISD::SETUGT:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001749 if (C < 0xffffffff && isLegalCmpImmediate(C+1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001750 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001751 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001752 }
1753 break;
1754 }
1755 }
1756 }
1757
1758 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001759 ARMISD::NodeType CompareType;
1760 switch (CondCode) {
1761 default:
1762 CompareType = ARMISD::CMP;
1763 break;
1764 case ARMCC::EQ:
1765 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00001766 // Uses only Z Flag
1767 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001768 break;
1769 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001770 ARMCC = DAG.getConstant(CondCode, MVT::i32);
1771 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001772}
1773
1774/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001775static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Dale Johannesende064702009-02-06 21:50:26 +00001776 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001777 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00001778 if (!isFloatingPointZero(RHS))
Owen Anderson825b72b2009-08-11 20:47:22 +00001779 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001780 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001781 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
1782 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001783}
1784
Dan Gohman475871a2008-07-27 21:46:04 +00001785static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001786 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00001787 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001788 SDValue LHS = Op.getOperand(0);
1789 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001790 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001791 SDValue TrueVal = Op.getOperand(2);
1792 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00001793 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001794
Owen Anderson825b72b2009-08-11 20:47:22 +00001795 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001796 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001797 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
David Goodwinf1daf7d2009-07-08 23:10:31 +00001798 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb1Only(), dl);
Dale Johannesende064702009-02-06 21:50:26 +00001799 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001800 }
1801
1802 ARMCC::CondCodes CondCode, CondCode2;
1803 if (FPCCToARMCC(CC, CondCode, CondCode2))
1804 std::swap(TrueVal, FalseVal);
1805
Owen Anderson825b72b2009-08-11 20:47:22 +00001806 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1807 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001808 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
1809 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00001810 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001811 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001812 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001813 // FIXME: Needs another CMP because flag can have but one use.
Dale Johannesende064702009-02-06 21:50:26 +00001814 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001815 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Dale Johannesende064702009-02-06 21:50:26 +00001816 Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00001817 }
1818 return Result;
1819}
1820
Dan Gohman475871a2008-07-27 21:46:04 +00001821static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001822 const ARMSubtarget *ST) {
Dan Gohman475871a2008-07-27 21:46:04 +00001823 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001824 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001825 SDValue LHS = Op.getOperand(2);
1826 SDValue RHS = Op.getOperand(3);
1827 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00001828 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001829
Owen Anderson825b72b2009-08-11 20:47:22 +00001830 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001831 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001832 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
David Goodwinf1daf7d2009-07-08 23:10:31 +00001833 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb1Only(), dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001834 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Dale Johannesende064702009-02-06 21:50:26 +00001835 Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001836 }
1837
Owen Anderson825b72b2009-08-11 20:47:22 +00001838 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Chenga8e29892007-01-19 07:51:42 +00001839 ARMCC::CondCodes CondCode, CondCode2;
1840 if (FPCCToARMCC(CC, CondCode, CondCode2))
1841 // Swap the LHS/RHS of the comparison if needed.
1842 std::swap(LHS, RHS);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001843
Dale Johannesende064702009-02-06 21:50:26 +00001844 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001845 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1846 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1847 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00001848 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00001849 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001850 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001851 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00001852 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00001853 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001854 }
1855 return Res;
1856}
1857
Dan Gohman475871a2008-07-27 21:46:04 +00001858SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) {
1859 SDValue Chain = Op.getOperand(0);
1860 SDValue Table = Op.getOperand(1);
1861 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001862 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001863
Owen Andersone50ed302009-08-10 22:56:29 +00001864 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00001865 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
1866 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00001867 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00001868 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00001869 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00001870 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
1871 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00001872 if (Subtarget->isThumb2()) {
1873 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
1874 // which does another jump to the destination. This also makes it easier
1875 // to translate it to TBB / TBH later.
1876 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00001877 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00001878 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001879 }
Evan Cheng66ac5312009-07-25 00:33:29 +00001880 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001881 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr, NULL, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00001882 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001883 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00001884 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001885 } else {
1886 Addr = DAG.getLoad(PTy, dl, Chain, Addr, NULL, 0);
1887 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00001888 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001889 }
Evan Chenga8e29892007-01-19 07:51:42 +00001890}
1891
Dan Gohman475871a2008-07-27 21:46:04 +00001892static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Dale Johannesende064702009-02-06 21:50:26 +00001893 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001894 unsigned Opc =
1895 Op.getOpcode() == ISD::FP_TO_SINT ? ARMISD::FTOSI : ARMISD::FTOUI;
Owen Anderson825b72b2009-08-11 20:47:22 +00001896 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
1897 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001898}
1899
Dan Gohman475871a2008-07-27 21:46:04 +00001900static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001901 EVT VT = Op.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001902 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001903 unsigned Opc =
1904 Op.getOpcode() == ISD::SINT_TO_FP ? ARMISD::SITOF : ARMISD::UITOF;
1905
Owen Anderson825b72b2009-08-11 20:47:22 +00001906 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
Dale Johannesende064702009-02-06 21:50:26 +00001907 return DAG.getNode(Opc, dl, VT, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001908}
1909
Dan Gohman475871a2008-07-27 21:46:04 +00001910static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00001911 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00001912 SDValue Tmp0 = Op.getOperand(0);
1913 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00001914 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001915 EVT VT = Op.getValueType();
1916 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001917 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
1918 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001919 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
1920 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001921 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001922}
1923
Jim Grosbach0e0da732009-05-12 23:59:14 +00001924SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
1925 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1926 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00001927 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001928 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
1929 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00001930 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00001931 ? ARM::R7 : ARM::R11;
1932 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
1933 while (Depth--)
1934 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
1935 return FrameAddr;
1936}
1937
Dan Gohman475871a2008-07-27 21:46:04 +00001938SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00001939ARMTargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman475871a2008-07-27 21:46:04 +00001940 SDValue Chain,
1941 SDValue Dst, SDValue Src,
1942 SDValue Size, unsigned Align,
Dan Gohman707e0182008-04-12 04:36:06 +00001943 bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00001944 const Value *DstSV, uint64_t DstSVOff,
1945 const Value *SrcSV, uint64_t SrcSVOff){
Evan Cheng4102eb52007-10-22 22:11:27 +00001946 // Do repeated 4-byte loads and stores. To be improved.
Dan Gohman707e0182008-04-12 04:36:06 +00001947 // This requires 4-byte alignment.
1948 if ((Align & 3) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00001949 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001950 // This requires the copy size to be a constant, preferrably
1951 // within a subtarget-specific limit.
1952 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
1953 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00001954 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001955 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001956 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00001957 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001958
1959 unsigned BytesLeft = SizeVal & 3;
1960 unsigned NumMemOps = SizeVal >> 2;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001961 unsigned EmittedNumMemOps = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001962 EVT VT = MVT::i32;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001963 unsigned VTSize = 4;
Evan Cheng4102eb52007-10-22 22:11:27 +00001964 unsigned i = 0;
Evan Chenge5e7ce42007-05-18 01:19:57 +00001965 const unsigned MAX_LOADS_IN_LDM = 6;
Dan Gohman475871a2008-07-27 21:46:04 +00001966 SDValue TFOps[MAX_LOADS_IN_LDM];
1967 SDValue Loads[MAX_LOADS_IN_LDM];
Dan Gohman1f13c682008-04-28 17:15:20 +00001968 uint64_t SrcOff = 0, DstOff = 0;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001969
Evan Cheng4102eb52007-10-22 22:11:27 +00001970 // Emit up to MAX_LOADS_IN_LDM loads, then a TokenFactor barrier, then the
1971 // same number of stores. The loads and stores will get combined into
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001972 // ldm/stm later on.
Evan Cheng4102eb52007-10-22 22:11:27 +00001973 while (EmittedNumMemOps < NumMemOps) {
1974 for (i = 0;
1975 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00001976 Loads[i] = DAG.getLoad(VT, dl, Chain,
Owen Anderson825b72b2009-08-11 20:47:22 +00001977 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
1978 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001979 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00001980 TFOps[i] = Loads[i].getValue(1);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001981 SrcOff += VTSize;
1982 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001983 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001984
Evan Cheng4102eb52007-10-22 22:11:27 +00001985 for (i = 0;
1986 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00001987 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Owen Anderson825b72b2009-08-11 20:47:22 +00001988 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
1989 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001990 DstSV, DstSVOff + DstOff);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001991 DstOff += VTSize;
1992 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001993 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00001994
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001995 EmittedNumMemOps += i;
1996 }
1997
Bob Wilson2dc4f542009-03-20 22:42:55 +00001998 if (BytesLeft == 0)
Evan Cheng4102eb52007-10-22 22:11:27 +00001999 return Chain;
2000
2001 // Issue loads / stores for the trailing (1 - 3) bytes.
2002 unsigned BytesLeftSave = BytesLeft;
2003 i = 0;
2004 while (BytesLeft) {
2005 if (BytesLeft >= 2) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002006 VT = MVT::i16;
Evan Cheng4102eb52007-10-22 22:11:27 +00002007 VTSize = 2;
2008 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00002009 VT = MVT::i8;
Evan Cheng4102eb52007-10-22 22:11:27 +00002010 VTSize = 1;
2011 }
2012
Dale Johannesen0f502f62009-02-03 22:26:09 +00002013 Loads[i] = DAG.getLoad(VT, dl, Chain,
Owen Anderson825b72b2009-08-11 20:47:22 +00002014 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
2015 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00002016 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00002017 TFOps[i] = Loads[i].getValue(1);
2018 ++i;
2019 SrcOff += VTSize;
2020 BytesLeft -= VTSize;
2021 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002022 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00002023
2024 i = 0;
2025 BytesLeft = BytesLeftSave;
2026 while (BytesLeft) {
2027 if (BytesLeft >= 2) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002028 VT = MVT::i16;
Evan Cheng4102eb52007-10-22 22:11:27 +00002029 VTSize = 2;
2030 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00002031 VT = MVT::i8;
Evan Cheng4102eb52007-10-22 22:11:27 +00002032 VTSize = 1;
2033 }
2034
Dale Johannesen0f502f62009-02-03 22:26:09 +00002035 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Owen Anderson825b72b2009-08-11 20:47:22 +00002036 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
2037 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00002038 DstSV, DstSVOff + DstOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00002039 ++i;
2040 DstOff += VTSize;
2041 BytesLeft -= VTSize;
2042 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002043 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002044}
2045
Duncan Sands1607f052008-12-01 11:39:25 +00002046static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00002047 SDValue Op = N->getOperand(0);
Dale Johannesende064702009-02-06 21:50:26 +00002048 DebugLoc dl = N->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00002049 if (N->getValueType(0) == MVT::f64) {
Evan Chengc7c77292008-11-04 19:57:48 +00002050 // Turn i64->f64 into FMDRR.
Owen Anderson825b72b2009-08-11 20:47:22 +00002051 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2052 DAG.getConstant(0, MVT::i32));
2053 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2054 DAG.getConstant(1, MVT::i32));
2055 return DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
Evan Chengc7c77292008-11-04 19:57:48 +00002056 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002057
Evan Chengc7c77292008-11-04 19:57:48 +00002058 // Turn f64->i64 into FMRRD.
Bob Wilson2dc4f542009-03-20 22:42:55 +00002059 SDValue Cvt = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00002060 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002061
Chris Lattner27a6c732007-11-24 07:07:01 +00002062 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002063 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
Chris Lattner27a6c732007-11-24 07:07:01 +00002064}
2065
Bob Wilson5bafff32009-06-22 23:27:02 +00002066/// getZeroVector - Returns a vector of specified type with all zero elements.
2067///
Owen Andersone50ed302009-08-10 22:56:29 +00002068static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002069 assert(VT.isVector() && "Expected a vector type");
2070
2071 // Zero vectors are used to represent vector negation and in those cases
2072 // will be implemented with the NEON VNEG instruction. However, VNEG does
2073 // not support i64 elements, so sometimes the zero vectors will need to be
2074 // explicitly constructed. For those cases, and potentially other uses in
2075 // the future, always build zero vectors as <4 x i32> or <2 x i32> bitcasted
2076 // to their dest type. This ensures they get CSE'd.
2077 SDValue Vec;
Owen Anderson825b72b2009-08-11 20:47:22 +00002078 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002079 if (VT.getSizeInBits() == 64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002080 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Bob Wilson5bafff32009-06-22 23:27:02 +00002081 else
Owen Anderson825b72b2009-08-11 20:47:22 +00002082 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Bob Wilson5bafff32009-06-22 23:27:02 +00002083
2084 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2085}
2086
2087/// getOnesVector - Returns a vector of specified type with all bits set.
2088///
Owen Andersone50ed302009-08-10 22:56:29 +00002089static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002090 assert(VT.isVector() && "Expected a vector type");
2091
2092 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2093 // type. This ensures they get CSE'd.
2094 SDValue Vec;
Owen Anderson825b72b2009-08-11 20:47:22 +00002095 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002096 if (VT.getSizeInBits() == 64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002097 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Bob Wilson5bafff32009-06-22 23:27:02 +00002098 else
Owen Anderson825b72b2009-08-11 20:47:22 +00002099 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Bob Wilson5bafff32009-06-22 23:27:02 +00002100
2101 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2102}
2103
2104static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2105 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00002106 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002107 DebugLoc dl = N->getDebugLoc();
2108
2109 // Lower vector shifts on NEON to use VSHL.
2110 if (VT.isVector()) {
2111 assert(ST->hasNEON() && "unexpected vector shift");
2112
2113 // Left shifts translate directly to the vshiftu intrinsic.
2114 if (N->getOpcode() == ISD::SHL)
2115 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002116 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002117 N->getOperand(0), N->getOperand(1));
2118
2119 assert((N->getOpcode() == ISD::SRA ||
2120 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2121
2122 // NEON uses the same intrinsics for both left and right shifts. For
2123 // right shifts, the shift amounts are negative, so negate the vector of
2124 // shift amounts.
Owen Andersone50ed302009-08-10 22:56:29 +00002125 EVT ShiftVT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002126 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2127 getZeroVector(ShiftVT, DAG, dl),
2128 N->getOperand(1));
2129 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2130 Intrinsic::arm_neon_vshifts :
2131 Intrinsic::arm_neon_vshiftu);
2132 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002133 DAG.getConstant(vshiftInt, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002134 N->getOperand(0), NegatedCount);
2135 }
2136
Eli Friedmance392eb2009-08-22 03:13:10 +00002137 // We can get here for a node like i32 = ISD::SHL i32, i64
2138 if (VT != MVT::i64)
2139 return SDValue();
2140
2141 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00002142 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00002143
Chris Lattner27a6c732007-11-24 07:07:01 +00002144 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2145 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002146 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00002147 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002148
Chris Lattner27a6c732007-11-24 07:07:01 +00002149 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00002150 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002151
Chris Lattner27a6c732007-11-24 07:07:01 +00002152 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00002153 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2154 DAG.getConstant(0, MVT::i32));
2155 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2156 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002157
Chris Lattner27a6c732007-11-24 07:07:01 +00002158 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2159 // captures the result into a carry flag.
2160 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Owen Anderson825b72b2009-08-11 20:47:22 +00002161 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002162
Chris Lattner27a6c732007-11-24 07:07:01 +00002163 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00002164 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002165
Chris Lattner27a6c732007-11-24 07:07:01 +00002166 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002167 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00002168}
2169
Bob Wilson5bafff32009-06-22 23:27:02 +00002170static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2171 SDValue TmpOp0, TmpOp1;
2172 bool Invert = false;
2173 bool Swap = false;
2174 unsigned Opc = 0;
2175
2176 SDValue Op0 = Op.getOperand(0);
2177 SDValue Op1 = Op.getOperand(1);
2178 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00002179 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002180 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2181 DebugLoc dl = Op.getDebugLoc();
2182
2183 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2184 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002185 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002186 case ISD::SETUNE:
2187 case ISD::SETNE: Invert = true; // Fallthrough
2188 case ISD::SETOEQ:
2189 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2190 case ISD::SETOLT:
2191 case ISD::SETLT: Swap = true; // Fallthrough
2192 case ISD::SETOGT:
2193 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2194 case ISD::SETOLE:
2195 case ISD::SETLE: Swap = true; // Fallthrough
2196 case ISD::SETOGE:
2197 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2198 case ISD::SETUGE: Swap = true; // Fallthrough
2199 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2200 case ISD::SETUGT: Swap = true; // Fallthrough
2201 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2202 case ISD::SETUEQ: Invert = true; // Fallthrough
2203 case ISD::SETONE:
2204 // Expand this to (OLT | OGT).
2205 TmpOp0 = Op0;
2206 TmpOp1 = Op1;
2207 Opc = ISD::OR;
2208 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2209 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2210 break;
2211 case ISD::SETUO: Invert = true; // Fallthrough
2212 case ISD::SETO:
2213 // Expand this to (OLT | OGE).
2214 TmpOp0 = Op0;
2215 TmpOp1 = Op1;
2216 Opc = ISD::OR;
2217 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2218 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2219 break;
2220 }
2221 } else {
2222 // Integer comparisons.
2223 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002224 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002225 case ISD::SETNE: Invert = true;
2226 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2227 case ISD::SETLT: Swap = true;
2228 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2229 case ISD::SETLE: Swap = true;
2230 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2231 case ISD::SETULT: Swap = true;
2232 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2233 case ISD::SETULE: Swap = true;
2234 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2235 }
2236
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002237 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00002238 if (Opc == ARMISD::VCEQ) {
2239
2240 SDValue AndOp;
2241 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2242 AndOp = Op0;
2243 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2244 AndOp = Op1;
2245
2246 // Ignore bitconvert.
2247 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2248 AndOp = AndOp.getOperand(0);
2249
2250 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2251 Opc = ARMISD::VTST;
2252 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2253 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2254 Invert = !Invert;
2255 }
2256 }
2257 }
2258
2259 if (Swap)
2260 std::swap(Op0, Op1);
2261
2262 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
2263
2264 if (Invert)
2265 Result = DAG.getNOT(dl, Result, VT);
2266
2267 return Result;
2268}
2269
2270/// isVMOVSplat - Check if the specified splat value corresponds to an immediate
2271/// VMOV instruction, and if so, return the constant being splatted.
2272static SDValue isVMOVSplat(uint64_t SplatBits, uint64_t SplatUndef,
2273 unsigned SplatBitSize, SelectionDAG &DAG) {
2274 switch (SplatBitSize) {
2275 case 8:
2276 // Any 1-byte value is OK.
2277 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Owen Anderson825b72b2009-08-11 20:47:22 +00002278 return DAG.getTargetConstant(SplatBits, MVT::i8);
Bob Wilson5bafff32009-06-22 23:27:02 +00002279
2280 case 16:
2281 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
2282 if ((SplatBits & ~0xff) == 0 ||
2283 (SplatBits & ~0xff00) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002284 return DAG.getTargetConstant(SplatBits, MVT::i16);
Bob Wilson5bafff32009-06-22 23:27:02 +00002285 break;
2286
2287 case 32:
2288 // NEON's 32-bit VMOV supports splat values where:
2289 // * only one byte is nonzero, or
2290 // * the least significant byte is 0xff and the second byte is nonzero, or
2291 // * the least significant 2 bytes are 0xff and the third is nonzero.
2292 if ((SplatBits & ~0xff) == 0 ||
2293 (SplatBits & ~0xff00) == 0 ||
2294 (SplatBits & ~0xff0000) == 0 ||
2295 (SplatBits & ~0xff000000) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002296 return DAG.getTargetConstant(SplatBits, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002297
2298 if ((SplatBits & ~0xffff) == 0 &&
2299 ((SplatBits | SplatUndef) & 0xff) == 0xff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002300 return DAG.getTargetConstant(SplatBits | 0xff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002301
2302 if ((SplatBits & ~0xffffff) == 0 &&
2303 ((SplatBits | SplatUndef) & 0xffff) == 0xffff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002304 return DAG.getTargetConstant(SplatBits | 0xffff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002305
2306 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
2307 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
2308 // VMOV.I32. A (very) minor optimization would be to replicate the value
2309 // and fall through here to test for a valid 64-bit splat. But, then the
2310 // caller would also need to check and handle the change in size.
2311 break;
2312
2313 case 64: {
2314 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
2315 uint64_t BitMask = 0xff;
2316 uint64_t Val = 0;
2317 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
2318 if (((SplatBits | SplatUndef) & BitMask) == BitMask)
2319 Val |= BitMask;
2320 else if ((SplatBits & BitMask) != 0)
2321 return SDValue();
2322 BitMask <<= 8;
2323 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002324 return DAG.getTargetConstant(Val, MVT::i64);
Bob Wilson5bafff32009-06-22 23:27:02 +00002325 }
2326
2327 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002328 llvm_unreachable("unexpected size for isVMOVSplat");
Bob Wilson5bafff32009-06-22 23:27:02 +00002329 break;
2330 }
2331
2332 return SDValue();
2333}
2334
2335/// getVMOVImm - If this is a build_vector of constants which can be
2336/// formed by using a VMOV instruction of the specified element size,
2337/// return the constant being splatted. The ByteSize field indicates the
2338/// number of bytes of each element [1248].
2339SDValue ARM::getVMOVImm(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
2340 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
2341 APInt SplatBits, SplatUndef;
2342 unsigned SplatBitSize;
2343 bool HasAnyUndefs;
2344 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
2345 HasAnyUndefs, ByteSize * 8))
2346 return SDValue();
2347
2348 if (SplatBitSize > ByteSize * 8)
2349 return SDValue();
2350
2351 return isVMOVSplat(SplatBits.getZExtValue(), SplatUndef.getZExtValue(),
2352 SplatBitSize, DAG);
2353}
2354
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002355static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
2356 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002357 unsigned NumElts = VT.getVectorNumElements();
2358 ReverseVEXT = false;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002359 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002360
2361 // If this is a VEXT shuffle, the immediate value is the index of the first
2362 // element. The other shuffle indices must be the successive elements after
2363 // the first one.
2364 unsigned ExpectedElt = Imm;
2365 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002366 // Increment the expected index. If it wraps around, it may still be
2367 // a VEXT but the source vectors must be swapped.
2368 ExpectedElt += 1;
2369 if (ExpectedElt == NumElts * 2) {
2370 ExpectedElt = 0;
2371 ReverseVEXT = true;
2372 }
2373
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002374 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002375 return false;
2376 }
2377
2378 // Adjust the index value if the source operands will be swapped.
2379 if (ReverseVEXT)
2380 Imm -= NumElts;
2381
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002382 return true;
2383}
2384
Bob Wilson8bb9e482009-07-26 00:39:34 +00002385/// isVREVMask - Check if a vector shuffle corresponds to a VREV
2386/// instruction with the specified blocksize. (The order of the elements
2387/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002388static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
2389 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00002390 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
2391 "Only possible block sizes for VREV are: 16, 32, 64");
2392
Bob Wilson8bb9e482009-07-26 00:39:34 +00002393 unsigned NumElts = VT.getVectorNumElements();
2394 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002395 unsigned BlockElts = M[0] + 1;
Bob Wilson8bb9e482009-07-26 00:39:34 +00002396
2397 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
2398 return false;
2399
2400 for (unsigned i = 0; i < NumElts; ++i) {
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002401 if ((unsigned) M[i] !=
Bob Wilson8bb9e482009-07-26 00:39:34 +00002402 (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
2403 return false;
2404 }
2405
2406 return true;
2407}
2408
Bob Wilsonc692cb72009-08-21 20:54:19 +00002409static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
2410 unsigned &WhichResult) {
2411 unsigned NumElts = VT.getVectorNumElements();
2412 WhichResult = (M[0] == 0 ? 0 : 1);
2413 for (unsigned i = 0; i < NumElts; i += 2) {
2414 if ((unsigned) M[i] != i + WhichResult ||
2415 (unsigned) M[i+1] != i + NumElts + WhichResult)
2416 return false;
2417 }
2418 return true;
2419}
2420
2421static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
2422 unsigned &WhichResult) {
2423 unsigned NumElts = VT.getVectorNumElements();
2424 WhichResult = (M[0] == 0 ? 0 : 1);
2425 for (unsigned i = 0; i != NumElts; ++i) {
2426 if ((unsigned) M[i] != 2 * i + WhichResult)
2427 return false;
2428 }
2429
2430 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2431 if (VT.is64BitVector() && VT.getVectorElementType().getSizeInBits() == 32)
2432 return false;
2433
2434 return true;
2435}
2436
2437static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
2438 unsigned &WhichResult) {
2439 unsigned NumElts = VT.getVectorNumElements();
2440 WhichResult = (M[0] == 0 ? 0 : 1);
2441 unsigned Idx = WhichResult * NumElts / 2;
2442 for (unsigned i = 0; i != NumElts; i += 2) {
2443 if ((unsigned) M[i] != Idx ||
2444 (unsigned) M[i+1] != Idx + NumElts)
2445 return false;
2446 Idx += 1;
2447 }
2448
2449 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2450 if (VT.is64BitVector() && VT.getVectorElementType().getSizeInBits() == 32)
2451 return false;
2452
2453 return true;
2454}
2455
Owen Andersone50ed302009-08-10 22:56:29 +00002456static SDValue BuildSplat(SDValue Val, EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002457 // Canonicalize all-zeros and all-ones vectors.
Bob Wilsond06791f2009-08-13 01:57:47 +00002458 ConstantSDNode *ConstVal = cast<ConstantSDNode>(Val.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002459 if (ConstVal->isNullValue())
2460 return getZeroVector(VT, DAG, dl);
2461 if (ConstVal->isAllOnesValue())
2462 return getOnesVector(VT, DAG, dl);
2463
Owen Andersone50ed302009-08-10 22:56:29 +00002464 EVT CanonicalVT;
Bob Wilson5bafff32009-06-22 23:27:02 +00002465 if (VT.is64BitVector()) {
2466 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002467 case 8: CanonicalVT = MVT::v8i8; break;
2468 case 16: CanonicalVT = MVT::v4i16; break;
2469 case 32: CanonicalVT = MVT::v2i32; break;
2470 case 64: CanonicalVT = MVT::v1i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002471 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002472 }
2473 } else {
2474 assert(VT.is128BitVector() && "unknown splat vector size");
2475 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002476 case 8: CanonicalVT = MVT::v16i8; break;
2477 case 16: CanonicalVT = MVT::v8i16; break;
2478 case 32: CanonicalVT = MVT::v4i32; break;
2479 case 64: CanonicalVT = MVT::v2i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002480 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002481 }
2482 }
2483
2484 // Build a canonical splat for this value.
2485 SmallVector<SDValue, 8> Ops;
2486 Ops.assign(CanonicalVT.getVectorNumElements(), Val);
2487 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, &Ops[0],
2488 Ops.size());
2489 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Res);
2490}
2491
2492// If this is a case we can't handle, return null and let the default
2493// expansion code take care of it.
2494static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bob Wilsond06791f2009-08-13 01:57:47 +00002495 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002496 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002497 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002498
2499 APInt SplatBits, SplatUndef;
2500 unsigned SplatBitSize;
2501 bool HasAnyUndefs;
2502 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
2503 SDValue Val = isVMOVSplat(SplatBits.getZExtValue(),
2504 SplatUndef.getZExtValue(), SplatBitSize, DAG);
2505 if (Val.getNode())
Bob Wilsoncf661e22009-07-30 00:31:25 +00002506 return BuildSplat(Val, VT, DAG, dl);
2507 }
2508
2509 // If there are only 2 elements in a 128-bit vector, insert them into an
2510 // undef vector. This handles the common case for 128-bit vector argument
2511 // passing, where the insertions should be translated to subreg accesses
2512 // with no real instructions.
2513 if (VT.is128BitVector() && Op.getNumOperands() == 2) {
2514 SDValue Val = DAG.getUNDEF(VT);
2515 SDValue Op0 = Op.getOperand(0);
2516 SDValue Op1 = Op.getOperand(1);
2517 if (Op0.getOpcode() != ISD::UNDEF)
2518 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op0,
2519 DAG.getIntPtrConstant(0));
2520 if (Op1.getOpcode() != ISD::UNDEF)
2521 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op1,
2522 DAG.getIntPtrConstant(1));
2523 return Val;
Bob Wilson5bafff32009-06-22 23:27:02 +00002524 }
2525
2526 return SDValue();
2527}
2528
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002529/// isShuffleMaskLegal - Targets can use this to indicate that they only
2530/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
2531/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
2532/// are assumed to be legal.
2533bool
2534ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
2535 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002536 if (VT.getVectorNumElements() == 4 &&
2537 (VT.is128BitVector() || VT.is64BitVector())) {
2538 unsigned PFIndexes[4];
2539 for (unsigned i = 0; i != 4; ++i) {
2540 if (M[i] < 0)
2541 PFIndexes[i] = 8;
2542 else
2543 PFIndexes[i] = M[i];
2544 }
2545
2546 // Compute the index in the perfect shuffle table.
2547 unsigned PFTableIndex =
2548 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2549 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2550 unsigned Cost = (PFEntry >> 30);
2551
2552 if (Cost <= 4)
2553 return true;
2554 }
2555
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002556 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00002557 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002558
2559 return (ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
2560 isVREVMask(M, VT, 64) ||
2561 isVREVMask(M, VT, 32) ||
2562 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00002563 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
2564 isVTRNMask(M, VT, WhichResult) ||
2565 isVUZPMask(M, VT, WhichResult) ||
2566 isVZIPMask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002567}
2568
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002569/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
2570/// the specified operations to build the shuffle.
2571static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
2572 SDValue RHS, SelectionDAG &DAG,
2573 DebugLoc dl) {
2574 unsigned OpNum = (PFEntry >> 26) & 0x0F;
2575 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
2576 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
2577
2578 enum {
2579 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
2580 OP_VREV,
2581 OP_VDUP0,
2582 OP_VDUP1,
2583 OP_VDUP2,
2584 OP_VDUP3,
2585 OP_VEXT1,
2586 OP_VEXT2,
2587 OP_VEXT3,
2588 OP_VUZPL, // VUZP, left result
2589 OP_VUZPR, // VUZP, right result
2590 OP_VZIPL, // VZIP, left result
2591 OP_VZIPR, // VZIP, right result
2592 OP_VTRNL, // VTRN, left result
2593 OP_VTRNR // VTRN, right result
2594 };
2595
2596 if (OpNum == OP_COPY) {
2597 if (LHSID == (1*9+2)*9+3) return LHS;
2598 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
2599 return RHS;
2600 }
2601
2602 SDValue OpLHS, OpRHS;
2603 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
2604 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
2605 EVT VT = OpLHS.getValueType();
2606
2607 switch (OpNum) {
2608 default: llvm_unreachable("Unknown shuffle opcode!");
2609 case OP_VREV:
2610 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
2611 case OP_VDUP0:
2612 case OP_VDUP1:
2613 case OP_VDUP2:
2614 case OP_VDUP3:
2615 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002616 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002617 case OP_VEXT1:
2618 case OP_VEXT2:
2619 case OP_VEXT3:
2620 return DAG.getNode(ARMISD::VEXT, dl, VT,
2621 OpLHS, OpRHS,
2622 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
2623 case OP_VUZPL:
2624 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002625 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002626 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
2627 case OP_VZIPL:
2628 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002629 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002630 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
2631 case OP_VTRNL:
2632 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002633 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2634 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002635 }
2636}
2637
Bob Wilson5bafff32009-06-22 23:27:02 +00002638static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002639 SDValue V1 = Op.getOperand(0);
2640 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00002641 DebugLoc dl = Op.getDebugLoc();
2642 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002643 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002644 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00002645
Bob Wilson28865062009-08-13 02:13:04 +00002646 // Convert shuffles that are directly supported on NEON to target-specific
2647 // DAG nodes, instead of keeping them as shuffles and matching them again
2648 // during code selection. This is more efficient and avoids the possibility
2649 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00002650 // FIXME: floating-point vectors should be canonicalized to integer vectors
2651 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002652 SVN->getMask(ShuffleMask);
2653
2654 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
Bob Wilson0ce37102009-08-14 05:08:32 +00002655 int Lane = SVN->getSplatIndex();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002656 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
2657 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00002658 }
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002659 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002660 DAG.getConstant(Lane, MVT::i32));
Bob Wilson0ce37102009-08-14 05:08:32 +00002661 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002662
2663 bool ReverseVEXT;
2664 unsigned Imm;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002665 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002666 if (ReverseVEXT)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002667 std::swap(V1, V2);
2668 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002669 DAG.getConstant(Imm, MVT::i32));
2670 }
2671
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002672 if (isVREVMask(ShuffleMask, VT, 64))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002673 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002674 if (isVREVMask(ShuffleMask, VT, 32))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002675 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002676 if (isVREVMask(ShuffleMask, VT, 16))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002677 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
2678
Bob Wilsonc692cb72009-08-21 20:54:19 +00002679 // Check for Neon shuffles that modify both input vectors in place.
2680 // If both results are used, i.e., if there are two shuffles with the same
2681 // source operands and with masks corresponding to both results of one of
2682 // these operations, DAG memoization will ensure that a single node is
2683 // used for both shuffles.
2684 unsigned WhichResult;
2685 if (isVTRNMask(ShuffleMask, VT, WhichResult))
2686 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2687 V1, V2).getValue(WhichResult);
2688 if (isVUZPMask(ShuffleMask, VT, WhichResult))
2689 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
2690 V1, V2).getValue(WhichResult);
2691 if (isVZIPMask(ShuffleMask, VT, WhichResult))
2692 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
2693 V1, V2).getValue(WhichResult);
2694
2695 // If the shuffle is not directly supported and it has 4 elements, use
2696 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002697 if (VT.getVectorNumElements() == 4 &&
2698 (VT.is128BitVector() || VT.is64BitVector())) {
2699 unsigned PFIndexes[4];
2700 for (unsigned i = 0; i != 4; ++i) {
2701 if (ShuffleMask[i] < 0)
2702 PFIndexes[i] = 8;
2703 else
2704 PFIndexes[i] = ShuffleMask[i];
2705 }
2706
2707 // Compute the index in the perfect shuffle table.
2708 unsigned PFTableIndex =
2709 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2710
2711 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2712 unsigned Cost = (PFEntry >> 30);
2713
2714 if (Cost <= 4)
2715 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
2716 }
Bob Wilsond8e17572009-08-12 22:31:50 +00002717
Bob Wilson22cac0d2009-08-14 05:16:33 +00002718 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00002719}
2720
2721static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
2722 return Op;
2723}
2724
2725static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002726 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002727 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00002728 assert((VT == MVT::i8 || VT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00002729 "unexpected type for custom-lowering vector extract");
2730 SDValue Vec = Op.getOperand(0);
2731 SDValue Lane = Op.getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002732 Op = DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
2733 Op = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Op, DAG.getValueType(VT));
Bob Wilson5bafff32009-06-22 23:27:02 +00002734 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
2735}
2736
Bob Wilsona6d65862009-08-03 20:36:38 +00002737static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
2738 // The only time a CONCAT_VECTORS operation can have legal types is when
2739 // two 64-bit vectors are concatenated to a 128-bit vector.
2740 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
2741 "unexpected CONCAT_VECTORS");
2742 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00002743 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00002744 SDValue Op0 = Op.getOperand(0);
2745 SDValue Op1 = Op.getOperand(1);
2746 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00002747 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
2748 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00002749 DAG.getIntPtrConstant(0));
2750 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00002751 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
2752 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00002753 DAG.getIntPtrConstant(1));
2754 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00002755}
2756
Dan Gohman475871a2008-07-27 21:46:04 +00002757SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00002758 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002759 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00002760 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002761 case ISD::GlobalAddress:
2762 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
2763 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002764 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002765 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, Subtarget);
2766 case ISD::BR_CC: return LowerBR_CC(Op, DAG, Subtarget);
2767 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Evan Cheng86198642009-08-07 00:34:42 +00002768 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002769 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
2770 case ISD::SINT_TO_FP:
2771 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
2772 case ISD::FP_TO_SINT:
2773 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
2774 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00002775 case ISD::RETURNADDR: break;
Jim Grosbach0e0da732009-05-12 23:59:14 +00002776 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002777 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Bob Wilsonb36ec862009-08-06 18:47:44 +00002778 case ISD::INTRINSIC_VOID:
Bob Wilsona599bff2009-08-04 00:36:16 +00002779 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002780 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00002781 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00002782 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00002783 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00002784 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
2785 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
2786 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2787 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
2788 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
2789 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00002790 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002791 }
Dan Gohman475871a2008-07-27 21:46:04 +00002792 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00002793}
2794
Duncan Sands1607f052008-12-01 11:39:25 +00002795/// ReplaceNodeResults - Replace the results of node with an illegal result
2796/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00002797void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
2798 SmallVectorImpl<SDValue>&Results,
2799 SelectionDAG &DAG) {
Chris Lattner27a6c732007-11-24 07:07:01 +00002800 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00002801 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002802 llvm_unreachable("Don't know how to custom expand this!");
Duncan Sands1607f052008-12-01 11:39:25 +00002803 return;
2804 case ISD::BIT_CONVERT:
2805 Results.push_back(ExpandBIT_CONVERT(N, DAG));
2806 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00002807 case ISD::SRL:
Duncan Sands1607f052008-12-01 11:39:25 +00002808 case ISD::SRA: {
Bob Wilson5bafff32009-06-22 23:27:02 +00002809 SDValue Res = LowerShift(N, DAG, Subtarget);
Duncan Sands1607f052008-12-01 11:39:25 +00002810 if (Res.getNode())
2811 Results.push_back(Res);
2812 return;
2813 }
Chris Lattner27a6c732007-11-24 07:07:01 +00002814 }
2815}
Chris Lattner27a6c732007-11-24 07:07:01 +00002816
Evan Chenga8e29892007-01-19 07:51:42 +00002817//===----------------------------------------------------------------------===//
2818// ARM Scheduler Hooks
2819//===----------------------------------------------------------------------===//
2820
2821MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00002822ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00002823 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002824 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00002825 DebugLoc dl = MI->getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002826 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00002827 default:
2828 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng007ea272009-08-12 05:17:19 +00002829 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00002830 // To "insert" a SELECT_CC instruction, we actually have to insert the
2831 // diamond control-flow pattern. The incoming instruction knows the
2832 // destination vreg to set, the condition code register to branch on, the
2833 // true/false values to select between, and a branch opcode to use.
2834 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002835 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00002836 ++It;
2837
2838 // thisMBB:
2839 // ...
2840 // TrueVal = ...
2841 // cmpTY ccX, r1, r2
2842 // bCC copy1MBB
2843 // fallthrough --> copy0MBB
2844 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002845 MachineFunction *F = BB->getParent();
2846 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
2847 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenb6728402009-02-13 02:25:56 +00002848 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00002849 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002850 F->insert(It, copy0MBB);
2851 F->insert(It, sinkMBB);
Evan Chenga8e29892007-01-19 07:51:42 +00002852 // Update machine-CFG edges by first adding all successors of the current
2853 // block to the new block which will contain the Phi node for the select.
2854 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
2855 e = BB->succ_end(); i != e; ++i)
2856 sinkMBB->addSuccessor(*i);
2857 // Next, remove all successors of the current block, and add the true
2858 // and fallthrough blocks as its successors.
2859 while(!BB->succ_empty())
2860 BB->removeSuccessor(BB->succ_begin());
2861 BB->addSuccessor(copy0MBB);
2862 BB->addSuccessor(sinkMBB);
2863
2864 // copy0MBB:
2865 // %FalseValue = ...
2866 // # fallthrough to sinkMBB
2867 BB = copy0MBB;
2868
2869 // Update machine-CFG edges
2870 BB->addSuccessor(sinkMBB);
2871
2872 // sinkMBB:
2873 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2874 // ...
2875 BB = sinkMBB;
Dale Johannesenb6728402009-02-13 02:25:56 +00002876 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00002877 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
2878 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
2879
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002880 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00002881 return BB;
2882 }
Evan Cheng86198642009-08-07 00:34:42 +00002883
2884 case ARM::tANDsp:
2885 case ARM::tADDspr_:
2886 case ARM::tSUBspi_:
2887 case ARM::t2SUBrSPi_:
2888 case ARM::t2SUBrSPi12_:
2889 case ARM::t2SUBrSPs_: {
2890 MachineFunction *MF = BB->getParent();
2891 unsigned DstReg = MI->getOperand(0).getReg();
2892 unsigned SrcReg = MI->getOperand(1).getReg();
2893 bool DstIsDead = MI->getOperand(0).isDead();
2894 bool SrcIsKill = MI->getOperand(1).isKill();
2895
2896 if (SrcReg != ARM::SP) {
2897 // Copy the source to SP from virtual register.
2898 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(SrcReg);
2899 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
2900 ? ARM::tMOVtgpr2gpr : ARM::tMOVgpr2gpr;
2901 BuildMI(BB, dl, TII->get(CopyOpc), ARM::SP)
2902 .addReg(SrcReg, getKillRegState(SrcIsKill));
2903 }
2904
2905 unsigned OpOpc = 0;
2906 bool NeedPred = false, NeedCC = false, NeedOp3 = false;
2907 switch (MI->getOpcode()) {
2908 default:
2909 llvm_unreachable("Unexpected pseudo instruction!");
2910 case ARM::tANDsp:
2911 OpOpc = ARM::tAND;
2912 NeedPred = true;
2913 break;
2914 case ARM::tADDspr_:
2915 OpOpc = ARM::tADDspr;
2916 break;
2917 case ARM::tSUBspi_:
2918 OpOpc = ARM::tSUBspi;
2919 break;
2920 case ARM::t2SUBrSPi_:
2921 OpOpc = ARM::t2SUBrSPi;
2922 NeedPred = true; NeedCC = true;
2923 break;
2924 case ARM::t2SUBrSPi12_:
2925 OpOpc = ARM::t2SUBrSPi12;
2926 NeedPred = true;
2927 break;
2928 case ARM::t2SUBrSPs_:
2929 OpOpc = ARM::t2SUBrSPs;
2930 NeedPred = true; NeedCC = true; NeedOp3 = true;
2931 break;
2932 }
2933 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(OpOpc), ARM::SP);
2934 if (OpOpc == ARM::tAND)
2935 AddDefaultT1CC(MIB);
2936 MIB.addReg(ARM::SP);
2937 MIB.addOperand(MI->getOperand(2));
2938 if (NeedOp3)
2939 MIB.addOperand(MI->getOperand(3));
2940 if (NeedPred)
2941 AddDefaultPred(MIB);
2942 if (NeedCC)
2943 AddDefaultCC(MIB);
2944
2945 // Copy the result from SP to virtual register.
2946 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(DstReg);
2947 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
2948 ? ARM::tMOVgpr2tgpr : ARM::tMOVgpr2gpr;
2949 BuildMI(BB, dl, TII->get(CopyOpc))
2950 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
2951 .addReg(ARM::SP);
2952 MF->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
2953 return BB;
2954 }
Evan Chenga8e29892007-01-19 07:51:42 +00002955 }
2956}
2957
2958//===----------------------------------------------------------------------===//
2959// ARM Optimization Hooks
2960//===----------------------------------------------------------------------===//
2961
Chris Lattnerd1980a52009-03-12 06:52:53 +00002962static
2963SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
2964 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00002965 SelectionDAG &DAG = DCI.DAG;
2966 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00002967 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00002968 unsigned Opc = N->getOpcode();
2969 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
2970 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
2971 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
2972 ISD::CondCode CC = ISD::SETCC_INVALID;
2973
2974 if (isSlctCC) {
2975 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
2976 } else {
2977 SDValue CCOp = Slct.getOperand(0);
2978 if (CCOp.getOpcode() == ISD::SETCC)
2979 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
2980 }
2981
2982 bool DoXform = false;
2983 bool InvCC = false;
2984 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
2985 "Bad input!");
2986
2987 if (LHS.getOpcode() == ISD::Constant &&
2988 cast<ConstantSDNode>(LHS)->isNullValue()) {
2989 DoXform = true;
2990 } else if (CC != ISD::SETCC_INVALID &&
2991 RHS.getOpcode() == ISD::Constant &&
2992 cast<ConstantSDNode>(RHS)->isNullValue()) {
2993 std::swap(LHS, RHS);
2994 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00002995 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00002996 Op0.getOperand(0).getValueType();
2997 bool isInt = OpVT.isInteger();
2998 CC = ISD::getSetCCInverse(CC, isInt);
2999
3000 if (!TLI.isCondCodeLegal(CC, OpVT))
3001 return SDValue(); // Inverse operator isn't legal.
3002
3003 DoXform = true;
3004 InvCC = true;
3005 }
3006
3007 if (DoXform) {
3008 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
3009 if (isSlctCC)
3010 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
3011 Slct.getOperand(0), Slct.getOperand(1), CC);
3012 SDValue CCOp = Slct.getOperand(0);
3013 if (InvCC)
3014 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
3015 CCOp.getOperand(0), CCOp.getOperand(1), CC);
3016 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
3017 CCOp, OtherOp, Result);
3018 }
3019 return SDValue();
3020}
3021
3022/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
3023static SDValue PerformADDCombine(SDNode *N,
3024 TargetLowering::DAGCombinerInfo &DCI) {
3025 // added by evan in r37685 with no testcase.
3026 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003027
Chris Lattnerd1980a52009-03-12 06:52:53 +00003028 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
3029 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
3030 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
3031 if (Result.getNode()) return Result;
3032 }
3033 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3034 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3035 if (Result.getNode()) return Result;
3036 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003037
Chris Lattnerd1980a52009-03-12 06:52:53 +00003038 return SDValue();
3039}
3040
3041/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
3042static SDValue PerformSUBCombine(SDNode *N,
3043 TargetLowering::DAGCombinerInfo &DCI) {
3044 // added by evan in r37685 with no testcase.
3045 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003046
Chris Lattnerd1980a52009-03-12 06:52:53 +00003047 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
3048 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3049 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3050 if (Result.getNode()) return Result;
3051 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003052
Chris Lattnerd1980a52009-03-12 06:52:53 +00003053 return SDValue();
3054}
3055
3056
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003057/// PerformFMRRDCombine - Target-specific dag combine xforms for ARMISD::FMRRD.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003058static SDValue PerformFMRRDCombine(SDNode *N,
3059 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003060 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00003061 SDValue InDouble = N->getOperand(0);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003062 if (InDouble.getOpcode() == ARMISD::FMDRR)
3063 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003064 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003065}
3066
Bob Wilson5bafff32009-06-22 23:27:02 +00003067/// getVShiftImm - Check if this is a valid build_vector for the immediate
3068/// operand of a vector shift operation, where all the elements of the
3069/// build_vector must have the same constant integer value.
3070static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
3071 // Ignore bit_converts.
3072 while (Op.getOpcode() == ISD::BIT_CONVERT)
3073 Op = Op.getOperand(0);
3074 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
3075 APInt SplatBits, SplatUndef;
3076 unsigned SplatBitSize;
3077 bool HasAnyUndefs;
3078 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
3079 HasAnyUndefs, ElementBits) ||
3080 SplatBitSize > ElementBits)
3081 return false;
3082 Cnt = SplatBits.getSExtValue();
3083 return true;
3084}
3085
3086/// isVShiftLImm - Check if this is a valid build_vector for the immediate
3087/// operand of a vector shift left operation. That value must be in the range:
3088/// 0 <= Value < ElementBits for a left shift; or
3089/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003090static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003091 assert(VT.isVector() && "vector shift count is not a vector type");
3092 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3093 if (! getVShiftImm(Op, ElementBits, Cnt))
3094 return false;
3095 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
3096}
3097
3098/// isVShiftRImm - Check if this is a valid build_vector for the immediate
3099/// operand of a vector shift right operation. For a shift opcode, the value
3100/// is positive, but for an intrinsic the value count must be negative. The
3101/// absolute value must be in the range:
3102/// 1 <= |Value| <= ElementBits for a right shift; or
3103/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003104static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00003105 int64_t &Cnt) {
3106 assert(VT.isVector() && "vector shift count is not a vector type");
3107 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3108 if (! getVShiftImm(Op, ElementBits, Cnt))
3109 return false;
3110 if (isIntrinsic)
3111 Cnt = -Cnt;
3112 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
3113}
3114
3115/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
3116static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
3117 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
3118 switch (IntNo) {
3119 default:
3120 // Don't do anything for most intrinsics.
3121 break;
3122
3123 // Vector shifts: check for immediate versions and lower them.
3124 // Note: This is done during DAG combining instead of DAG legalizing because
3125 // the build_vectors for 64-bit vector element shift counts are generally
3126 // not legal, and it is hard to see their values after they get legalized to
3127 // loads from a constant pool.
3128 case Intrinsic::arm_neon_vshifts:
3129 case Intrinsic::arm_neon_vshiftu:
3130 case Intrinsic::arm_neon_vshiftls:
3131 case Intrinsic::arm_neon_vshiftlu:
3132 case Intrinsic::arm_neon_vshiftn:
3133 case Intrinsic::arm_neon_vrshifts:
3134 case Intrinsic::arm_neon_vrshiftu:
3135 case Intrinsic::arm_neon_vrshiftn:
3136 case Intrinsic::arm_neon_vqshifts:
3137 case Intrinsic::arm_neon_vqshiftu:
3138 case Intrinsic::arm_neon_vqshiftsu:
3139 case Intrinsic::arm_neon_vqshiftns:
3140 case Intrinsic::arm_neon_vqshiftnu:
3141 case Intrinsic::arm_neon_vqshiftnsu:
3142 case Intrinsic::arm_neon_vqrshiftns:
3143 case Intrinsic::arm_neon_vqrshiftnu:
3144 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00003145 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003146 int64_t Cnt;
3147 unsigned VShiftOpc = 0;
3148
3149 switch (IntNo) {
3150 case Intrinsic::arm_neon_vshifts:
3151 case Intrinsic::arm_neon_vshiftu:
3152 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
3153 VShiftOpc = ARMISD::VSHL;
3154 break;
3155 }
3156 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
3157 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
3158 ARMISD::VSHRs : ARMISD::VSHRu);
3159 break;
3160 }
3161 return SDValue();
3162
3163 case Intrinsic::arm_neon_vshiftls:
3164 case Intrinsic::arm_neon_vshiftlu:
3165 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
3166 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003167 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003168
3169 case Intrinsic::arm_neon_vrshifts:
3170 case Intrinsic::arm_neon_vrshiftu:
3171 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
3172 break;
3173 return SDValue();
3174
3175 case Intrinsic::arm_neon_vqshifts:
3176 case Intrinsic::arm_neon_vqshiftu:
3177 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3178 break;
3179 return SDValue();
3180
3181 case Intrinsic::arm_neon_vqshiftsu:
3182 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3183 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003184 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003185
3186 case Intrinsic::arm_neon_vshiftn:
3187 case Intrinsic::arm_neon_vrshiftn:
3188 case Intrinsic::arm_neon_vqshiftns:
3189 case Intrinsic::arm_neon_vqshiftnu:
3190 case Intrinsic::arm_neon_vqshiftnsu:
3191 case Intrinsic::arm_neon_vqrshiftns:
3192 case Intrinsic::arm_neon_vqrshiftnu:
3193 case Intrinsic::arm_neon_vqrshiftnsu:
3194 // Narrowing shifts require an immediate right shift.
3195 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
3196 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003197 llvm_unreachable("invalid shift count for narrowing vector shift intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003198
3199 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003200 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003201 }
3202
3203 switch (IntNo) {
3204 case Intrinsic::arm_neon_vshifts:
3205 case Intrinsic::arm_neon_vshiftu:
3206 // Opcode already set above.
3207 break;
3208 case Intrinsic::arm_neon_vshiftls:
3209 case Intrinsic::arm_neon_vshiftlu:
3210 if (Cnt == VT.getVectorElementType().getSizeInBits())
3211 VShiftOpc = ARMISD::VSHLLi;
3212 else
3213 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
3214 ARMISD::VSHLLs : ARMISD::VSHLLu);
3215 break;
3216 case Intrinsic::arm_neon_vshiftn:
3217 VShiftOpc = ARMISD::VSHRN; break;
3218 case Intrinsic::arm_neon_vrshifts:
3219 VShiftOpc = ARMISD::VRSHRs; break;
3220 case Intrinsic::arm_neon_vrshiftu:
3221 VShiftOpc = ARMISD::VRSHRu; break;
3222 case Intrinsic::arm_neon_vrshiftn:
3223 VShiftOpc = ARMISD::VRSHRN; break;
3224 case Intrinsic::arm_neon_vqshifts:
3225 VShiftOpc = ARMISD::VQSHLs; break;
3226 case Intrinsic::arm_neon_vqshiftu:
3227 VShiftOpc = ARMISD::VQSHLu; break;
3228 case Intrinsic::arm_neon_vqshiftsu:
3229 VShiftOpc = ARMISD::VQSHLsu; break;
3230 case Intrinsic::arm_neon_vqshiftns:
3231 VShiftOpc = ARMISD::VQSHRNs; break;
3232 case Intrinsic::arm_neon_vqshiftnu:
3233 VShiftOpc = ARMISD::VQSHRNu; break;
3234 case Intrinsic::arm_neon_vqshiftnsu:
3235 VShiftOpc = ARMISD::VQSHRNsu; break;
3236 case Intrinsic::arm_neon_vqrshiftns:
3237 VShiftOpc = ARMISD::VQRSHRNs; break;
3238 case Intrinsic::arm_neon_vqrshiftnu:
3239 VShiftOpc = ARMISD::VQRSHRNu; break;
3240 case Intrinsic::arm_neon_vqrshiftnsu:
3241 VShiftOpc = ARMISD::VQRSHRNsu; break;
3242 }
3243
3244 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003245 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003246 }
3247
3248 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00003249 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003250 int64_t Cnt;
3251 unsigned VShiftOpc = 0;
3252
3253 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
3254 VShiftOpc = ARMISD::VSLI;
3255 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
3256 VShiftOpc = ARMISD::VSRI;
3257 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00003258 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003259 }
3260
3261 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
3262 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00003263 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003264 }
3265
3266 case Intrinsic::arm_neon_vqrshifts:
3267 case Intrinsic::arm_neon_vqrshiftu:
3268 // No immediate versions of these to check for.
3269 break;
3270 }
3271
3272 return SDValue();
3273}
3274
3275/// PerformShiftCombine - Checks for immediate versions of vector shifts and
3276/// lowers them. As with the vector shift intrinsics, this is done during DAG
3277/// combining instead of DAG legalizing because the build_vectors for 64-bit
3278/// vector element shift counts are generally not legal, and it is hard to see
3279/// their values after they get legalized to loads from a constant pool.
3280static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
3281 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003282 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003283
3284 // Nothing to be done for scalar shifts.
3285 if (! VT.isVector())
3286 return SDValue();
3287
3288 assert(ST->hasNEON() && "unexpected vector shift");
3289 int64_t Cnt;
3290
3291 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003292 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003293
3294 case ISD::SHL:
3295 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
3296 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003297 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003298 break;
3299
3300 case ISD::SRA:
3301 case ISD::SRL:
3302 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
3303 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
3304 ARMISD::VSHRs : ARMISD::VSHRu);
3305 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003306 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003307 }
3308 }
3309 return SDValue();
3310}
3311
3312/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
3313/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
3314static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
3315 const ARMSubtarget *ST) {
3316 SDValue N0 = N->getOperand(0);
3317
3318 // Check for sign- and zero-extensions of vector extract operations of 8-
3319 // and 16-bit vector elements. NEON supports these directly. They are
3320 // handled during DAG combining because type legalization will promote them
3321 // to 32-bit types and it is messy to recognize the operations after that.
3322 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
3323 SDValue Vec = N0.getOperand(0);
3324 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00003325 EVT VT = N->getValueType(0);
3326 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003327 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3328
Owen Anderson825b72b2009-08-11 20:47:22 +00003329 if (VT == MVT::i32 &&
3330 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00003331 TLI.isTypeLegal(Vec.getValueType())) {
3332
3333 unsigned Opc = 0;
3334 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003335 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003336 case ISD::SIGN_EXTEND:
3337 Opc = ARMISD::VGETLANEs;
3338 break;
3339 case ISD::ZERO_EXTEND:
3340 case ISD::ANY_EXTEND:
3341 Opc = ARMISD::VGETLANEu;
3342 break;
3343 }
3344 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
3345 }
3346 }
3347
3348 return SDValue();
3349}
3350
Dan Gohman475871a2008-07-27 21:46:04 +00003351SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003352 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003353 switch (N->getOpcode()) {
3354 default: break;
Chris Lattnerd1980a52009-03-12 06:52:53 +00003355 case ISD::ADD: return PerformADDCombine(N, DCI);
3356 case ISD::SUB: return PerformSUBCombine(N, DCI);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003357 case ARMISD::FMRRD: return PerformFMRRDCombine(N, DCI);
Bob Wilson5bafff32009-06-22 23:27:02 +00003358 case ISD::INTRINSIC_WO_CHAIN:
3359 return PerformIntrinsicCombine(N, DCI.DAG);
3360 case ISD::SHL:
3361 case ISD::SRA:
3362 case ISD::SRL:
3363 return PerformShiftCombine(N, DCI.DAG, Subtarget);
3364 case ISD::SIGN_EXTEND:
3365 case ISD::ZERO_EXTEND:
3366 case ISD::ANY_EXTEND:
3367 return PerformExtendCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003368 }
Dan Gohman475871a2008-07-27 21:46:04 +00003369 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003370}
3371
Bill Wendlingaf566342009-08-15 21:21:19 +00003372bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
3373 if (!Subtarget->hasV6Ops())
3374 // Pre-v6 does not support unaligned mem access.
3375 return false;
3376 else if (!Subtarget->hasV6Ops()) {
3377 // v6 may or may not support unaligned mem access.
3378 if (!Subtarget->isTargetDarwin())
3379 return false;
3380 }
3381
3382 switch (VT.getSimpleVT().SimpleTy) {
3383 default:
3384 return false;
3385 case MVT::i8:
3386 case MVT::i16:
3387 case MVT::i32:
3388 return true;
3389 // FIXME: VLD1 etc with standard alignment is legal.
3390 }
3391}
3392
Evan Chenge6c835f2009-08-14 20:09:37 +00003393static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
3394 if (V < 0)
3395 return false;
3396
3397 unsigned Scale = 1;
3398 switch (VT.getSimpleVT().SimpleTy) {
3399 default: return false;
3400 case MVT::i1:
3401 case MVT::i8:
3402 // Scale == 1;
3403 break;
3404 case MVT::i16:
3405 // Scale == 2;
3406 Scale = 2;
3407 break;
3408 case MVT::i32:
3409 // Scale == 4;
3410 Scale = 4;
3411 break;
3412 }
3413
3414 if ((V & (Scale - 1)) != 0)
3415 return false;
3416 V /= Scale;
3417 return V == (V & ((1LL << 5) - 1));
3418}
3419
3420static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
3421 const ARMSubtarget *Subtarget) {
3422 bool isNeg = false;
3423 if (V < 0) {
3424 isNeg = true;
3425 V = - V;
3426 }
3427
3428 switch (VT.getSimpleVT().SimpleTy) {
3429 default: return false;
3430 case MVT::i1:
3431 case MVT::i8:
3432 case MVT::i16:
3433 case MVT::i32:
3434 // + imm12 or - imm8
3435 if (isNeg)
3436 return V == (V & ((1LL << 8) - 1));
3437 return V == (V & ((1LL << 12) - 1));
3438 case MVT::f32:
3439 case MVT::f64:
3440 // Same as ARM mode. FIXME: NEON?
3441 if (!Subtarget->hasVFP2())
3442 return false;
3443 if ((V & 3) != 0)
3444 return false;
3445 V >>= 2;
3446 return V == (V & ((1LL << 8) - 1));
3447 }
3448}
3449
Evan Chengb01fad62007-03-12 23:30:29 +00003450/// isLegalAddressImmediate - Return true if the integer value can be used
3451/// as the offset of the target addressing mode for load / store of the
3452/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00003453static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00003454 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00003455 if (V == 0)
3456 return true;
3457
Evan Cheng65011532009-03-09 19:15:00 +00003458 if (!VT.isSimple())
3459 return false;
3460
Evan Chenge6c835f2009-08-14 20:09:37 +00003461 if (Subtarget->isThumb1Only())
3462 return isLegalT1AddressImmediate(V, VT);
3463 else if (Subtarget->isThumb2())
3464 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00003465
Evan Chenge6c835f2009-08-14 20:09:37 +00003466 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00003467 if (V < 0)
3468 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00003469 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00003470 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00003471 case MVT::i1:
3472 case MVT::i8:
3473 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00003474 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003475 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003476 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00003477 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003478 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003479 case MVT::f32:
3480 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00003481 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00003482 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00003483 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00003484 return false;
3485 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003486 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00003487 }
Evan Chenga8e29892007-01-19 07:51:42 +00003488}
3489
Evan Chenge6c835f2009-08-14 20:09:37 +00003490bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
3491 EVT VT) const {
3492 int Scale = AM.Scale;
3493 if (Scale < 0)
3494 return false;
3495
3496 switch (VT.getSimpleVT().SimpleTy) {
3497 default: return false;
3498 case MVT::i1:
3499 case MVT::i8:
3500 case MVT::i16:
3501 case MVT::i32:
3502 if (Scale == 1)
3503 return true;
3504 // r + r << imm
3505 Scale = Scale & ~1;
3506 return Scale == 2 || Scale == 4 || Scale == 8;
3507 case MVT::i64:
3508 // r + r
3509 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
3510 return true;
3511 return false;
3512 case MVT::isVoid:
3513 // Note, we allow "void" uses (basically, uses that aren't loads or
3514 // stores), because arm allows folding a scale into many arithmetic
3515 // operations. This should be made more precise and revisited later.
3516
3517 // Allow r << imm, but the imm has to be a multiple of two.
3518 if (Scale & 1) return false;
3519 return isPowerOf2_32(Scale);
3520 }
3521}
3522
Chris Lattner37caf8c2007-04-09 23:33:39 +00003523/// isLegalAddressingMode - Return true if the addressing mode represented
3524/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003525bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00003526 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00003527 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00003528 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00003529 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003530
Chris Lattner37caf8c2007-04-09 23:33:39 +00003531 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003532 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003533 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003534
Chris Lattner37caf8c2007-04-09 23:33:39 +00003535 switch (AM.Scale) {
3536 case 0: // no scale reg, must be "r+i" or "r", or "i".
3537 break;
3538 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00003539 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00003540 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00003541 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00003542 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00003543 // ARM doesn't support any R+R*scale+imm addr modes.
3544 if (AM.BaseOffs)
3545 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003546
Bob Wilson2c7dab12009-04-08 17:55:28 +00003547 if (!VT.isSimple())
3548 return false;
3549
Evan Chenge6c835f2009-08-14 20:09:37 +00003550 if (Subtarget->isThumb2())
3551 return isLegalT2ScaledAddressingMode(AM, VT);
3552
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003553 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00003554 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00003555 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00003556 case MVT::i1:
3557 case MVT::i8:
3558 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003559 if (Scale < 0) Scale = -Scale;
3560 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003561 return true;
3562 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00003563 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003564 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00003565 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00003566 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003567 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003568 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00003569 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003570
Owen Anderson825b72b2009-08-11 20:47:22 +00003571 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00003572 // Note, we allow "void" uses (basically, uses that aren't loads or
3573 // stores), because arm allows folding a scale into many arithmetic
3574 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003575
Chris Lattner37caf8c2007-04-09 23:33:39 +00003576 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00003577 if (Scale & 1) return false;
3578 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00003579 }
3580 break;
Evan Chengb01fad62007-03-12 23:30:29 +00003581 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00003582 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00003583}
3584
Owen Andersone50ed302009-08-10 22:56:29 +00003585static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00003586 bool isSEXTLoad, SDValue &Base,
3587 SDValue &Offset, bool &isInc,
3588 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00003589 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
3590 return false;
3591
Owen Anderson825b72b2009-08-11 20:47:22 +00003592 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00003593 // AddressingMode 3
3594 Base = Ptr->getOperand(0);
3595 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003596 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003597 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003598 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00003599 isInc = false;
3600 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3601 return true;
3602 }
3603 }
3604 isInc = (Ptr->getOpcode() == ISD::ADD);
3605 Offset = Ptr->getOperand(1);
3606 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00003607 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00003608 // AddressingMode 2
3609 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003610 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003611 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003612 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00003613 isInc = false;
3614 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3615 Base = Ptr->getOperand(0);
3616 return true;
3617 }
3618 }
3619
3620 if (Ptr->getOpcode() == ISD::ADD) {
3621 isInc = true;
3622 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
3623 if (ShOpcVal != ARM_AM::no_shift) {
3624 Base = Ptr->getOperand(1);
3625 Offset = Ptr->getOperand(0);
3626 } else {
3627 Base = Ptr->getOperand(0);
3628 Offset = Ptr->getOperand(1);
3629 }
3630 return true;
3631 }
3632
3633 isInc = (Ptr->getOpcode() == ISD::ADD);
3634 Base = Ptr->getOperand(0);
3635 Offset = Ptr->getOperand(1);
3636 return true;
3637 }
3638
3639 // FIXME: Use FLDM / FSTM to emulate indexed FP load / store.
3640 return false;
3641}
3642
Owen Andersone50ed302009-08-10 22:56:29 +00003643static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00003644 bool isSEXTLoad, SDValue &Base,
3645 SDValue &Offset, bool &isInc,
3646 SelectionDAG &DAG) {
3647 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
3648 return false;
3649
3650 Base = Ptr->getOperand(0);
3651 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
3652 int RHSC = (int)RHS->getZExtValue();
3653 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
3654 assert(Ptr->getOpcode() == ISD::ADD);
3655 isInc = false;
3656 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3657 return true;
3658 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
3659 isInc = Ptr->getOpcode() == ISD::ADD;
3660 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
3661 return true;
3662 }
3663 }
3664
3665 return false;
3666}
3667
Evan Chenga8e29892007-01-19 07:51:42 +00003668/// getPreIndexedAddressParts - returns true by value, base pointer and
3669/// offset pointer and addressing mode by reference if the node's address
3670/// can be legally represented as pre-indexed load / store address.
3671bool
Dan Gohman475871a2008-07-27 21:46:04 +00003672ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
3673 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003674 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00003675 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003676 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00003677 return false;
3678
Owen Andersone50ed302009-08-10 22:56:29 +00003679 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00003680 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00003681 bool isSEXTLoad = false;
3682 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
3683 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003684 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003685 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
3686 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
3687 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003688 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003689 } else
3690 return false;
3691
3692 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00003693 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00003694 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00003695 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
3696 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00003697 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00003698 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00003699 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00003700 if (!isLegal)
3701 return false;
3702
3703 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
3704 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00003705}
3706
3707/// getPostIndexedAddressParts - returns true by value, base pointer and
3708/// offset pointer and addressing mode by reference if this node can be
3709/// combined with a load / store to form a post-indexed load / store.
3710bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00003711 SDValue &Base,
3712 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003713 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00003714 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003715 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00003716 return false;
3717
Owen Andersone50ed302009-08-10 22:56:29 +00003718 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00003719 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00003720 bool isSEXTLoad = false;
3721 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003722 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003723 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
3724 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003725 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003726 } else
3727 return false;
3728
3729 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00003730 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00003731 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00003732 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003733 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00003734 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00003735 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
3736 isInc, DAG);
3737 if (!isLegal)
3738 return false;
3739
3740 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
3741 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00003742}
3743
Dan Gohman475871a2008-07-27 21:46:04 +00003744void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00003745 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003746 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003747 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00003748 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00003749 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003750 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00003751 switch (Op.getOpcode()) {
3752 default: break;
3753 case ARMISD::CMOV: {
3754 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00003755 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00003756 if (KnownZero == 0 && KnownOne == 0) return;
3757
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003758 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00003759 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
3760 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00003761 KnownZero &= KnownZeroRHS;
3762 KnownOne &= KnownOneRHS;
3763 return;
3764 }
3765 }
3766}
3767
3768//===----------------------------------------------------------------------===//
3769// ARM Inline Assembly Support
3770//===----------------------------------------------------------------------===//
3771
3772/// getConstraintType - Given a constraint letter, return the type of
3773/// constraint it is for this target.
3774ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00003775ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
3776 if (Constraint.size() == 1) {
3777 switch (Constraint[0]) {
3778 default: break;
3779 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003780 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00003781 }
Evan Chenga8e29892007-01-19 07:51:42 +00003782 }
Chris Lattner4234f572007-03-25 02:14:49 +00003783 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00003784}
3785
Bob Wilson2dc4f542009-03-20 22:42:55 +00003786std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00003787ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00003788 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003789 if (Constraint.size() == 1) {
3790 // GCC RS6000 Constraint Letters
3791 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003792 case 'l':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003793 if (Subtarget->isThumb1Only())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00003794 return std::make_pair(0U, ARM::tGPRRegisterClass);
3795 else
3796 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003797 case 'r':
3798 return std::make_pair(0U, ARM::GPRRegisterClass);
3799 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00003800 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003801 return std::make_pair(0U, ARM::SPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00003802 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003803 return std::make_pair(0U, ARM::DPRRegisterClass);
3804 break;
Evan Chenga8e29892007-01-19 07:51:42 +00003805 }
3806 }
3807 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3808}
3809
3810std::vector<unsigned> ARMTargetLowering::
3811getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00003812 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003813 if (Constraint.size() != 1)
3814 return std::vector<unsigned>();
3815
3816 switch (Constraint[0]) { // GCC ARM Constraint Letters
3817 default: break;
3818 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00003819 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
3820 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
3821 0);
Evan Chenga8e29892007-01-19 07:51:42 +00003822 case 'r':
3823 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
3824 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
3825 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
3826 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003827 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00003828 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003829 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
3830 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
3831 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
3832 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
3833 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
3834 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
3835 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
3836 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00003837 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003838 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
3839 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
3840 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
3841 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
3842 break;
Evan Chenga8e29892007-01-19 07:51:42 +00003843 }
3844
3845 return std::vector<unsigned>();
3846}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003847
3848/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3849/// vector. If it is invalid, don't add anything to Ops.
3850void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3851 char Constraint,
3852 bool hasMemory,
3853 std::vector<SDValue>&Ops,
3854 SelectionDAG &DAG) const {
3855 SDValue Result(0, 0);
3856
3857 switch (Constraint) {
3858 default: break;
3859 case 'I': case 'J': case 'K': case 'L':
3860 case 'M': case 'N': case 'O':
3861 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
3862 if (!C)
3863 return;
3864
3865 int64_t CVal64 = C->getSExtValue();
3866 int CVal = (int) CVal64;
3867 // None of these constraints allow values larger than 32 bits. Check
3868 // that the value fits in an int.
3869 if (CVal != CVal64)
3870 return;
3871
3872 switch (Constraint) {
3873 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003874 if (Subtarget->isThumb1Only()) {
3875 // This must be a constant between 0 and 255, for ADD
3876 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003877 if (CVal >= 0 && CVal <= 255)
3878 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00003879 } else if (Subtarget->isThumb2()) {
3880 // A constant that can be used as an immediate value in a
3881 // data-processing instruction.
3882 if (ARM_AM::getT2SOImmVal(CVal) != -1)
3883 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003884 } else {
3885 // A constant that can be used as an immediate value in a
3886 // data-processing instruction.
3887 if (ARM_AM::getSOImmVal(CVal) != -1)
3888 break;
3889 }
3890 return;
3891
3892 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003893 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003894 // This must be a constant between -255 and -1, for negated ADD
3895 // immediates. This can be used in GCC with an "n" modifier that
3896 // prints the negated value, for use with SUB instructions. It is
3897 // not useful otherwise but is implemented for compatibility.
3898 if (CVal >= -255 && CVal <= -1)
3899 break;
3900 } else {
3901 // This must be a constant between -4095 and 4095. It is not clear
3902 // what this constraint is intended for. Implemented for
3903 // compatibility with GCC.
3904 if (CVal >= -4095 && CVal <= 4095)
3905 break;
3906 }
3907 return;
3908
3909 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003910 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003911 // A 32-bit value where only one byte has a nonzero value. Exclude
3912 // zero to match GCC. This constraint is used by GCC internally for
3913 // constants that can be loaded with a move/shift combination.
3914 // It is not useful otherwise but is implemented for compatibility.
3915 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
3916 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00003917 } else if (Subtarget->isThumb2()) {
3918 // A constant whose bitwise inverse can be used as an immediate
3919 // value in a data-processing instruction. This can be used in GCC
3920 // with a "B" modifier that prints the inverted value, for use with
3921 // BIC and MVN instructions. It is not useful otherwise but is
3922 // implemented for compatibility.
3923 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
3924 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003925 } else {
3926 // A constant whose bitwise inverse can be used as an immediate
3927 // value in a data-processing instruction. This can be used in GCC
3928 // with a "B" modifier that prints the inverted value, for use with
3929 // BIC and MVN instructions. It is not useful otherwise but is
3930 // implemented for compatibility.
3931 if (ARM_AM::getSOImmVal(~CVal) != -1)
3932 break;
3933 }
3934 return;
3935
3936 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003937 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003938 // This must be a constant between -7 and 7,
3939 // for 3-operand ADD/SUB immediate instructions.
3940 if (CVal >= -7 && CVal < 7)
3941 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00003942 } else if (Subtarget->isThumb2()) {
3943 // A constant whose negation can be used as an immediate value in a
3944 // data-processing instruction. This can be used in GCC with an "n"
3945 // modifier that prints the negated value, for use with SUB
3946 // instructions. It is not useful otherwise but is implemented for
3947 // compatibility.
3948 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
3949 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003950 } else {
3951 // A constant whose negation can be used as an immediate value in a
3952 // data-processing instruction. This can be used in GCC with an "n"
3953 // modifier that prints the negated value, for use with SUB
3954 // instructions. It is not useful otherwise but is implemented for
3955 // compatibility.
3956 if (ARM_AM::getSOImmVal(-CVal) != -1)
3957 break;
3958 }
3959 return;
3960
3961 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003962 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003963 // This must be a multiple of 4 between 0 and 1020, for
3964 // ADD sp + immediate.
3965 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
3966 break;
3967 } else {
3968 // A power of two or a constant between 0 and 32. This is used in
3969 // GCC for the shift amount on shifted register operands, but it is
3970 // useful in general for any shift amounts.
3971 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
3972 break;
3973 }
3974 return;
3975
3976 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003977 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003978 // This must be a constant between 0 and 31, for shift amounts.
3979 if (CVal >= 0 && CVal <= 31)
3980 break;
3981 }
3982 return;
3983
3984 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003985 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003986 // This must be a multiple of 4 between -508 and 508, for
3987 // ADD/SUB sp = sp + immediate.
3988 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
3989 break;
3990 }
3991 return;
3992 }
3993 Result = DAG.getTargetConstant(CVal, Op.getValueType());
3994 break;
3995 }
3996
3997 if (Result.getNode()) {
3998 Ops.push_back(Result);
3999 return;
4000 }
4001 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
4002 Ops, DAG);
4003}