blob: 10a39a71678146b3455131a5d9c6e6c8e4df0d6d [file] [log] [blame]
Evan Chenga8e29892007-01-19 07:51:42 +00001//===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chenga8e29892007-01-19 07:51:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that ARM uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "ARM.h"
16#include "ARMAddressingModes.h"
17#include "ARMConstantPoolValue.h"
18#include "ARMISelLowering.h"
19#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +000020#include "ARMPerfectShuffle.h"
Evan Chenga8e29892007-01-19 07:51:42 +000021#include "ARMRegisterInfo.h"
22#include "ARMSubtarget.h"
23#include "ARMTargetMachine.h"
Chris Lattner80ec2792009-08-02 00:34:36 +000024#include "ARMTargetObjectFile.h"
Evan Chenga8e29892007-01-19 07:51:42 +000025#include "llvm/CallingConv.h"
26#include "llvm/Constants.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000027#include "llvm/Function.h"
Evan Cheng27707472007-03-16 08:43:56 +000028#include "llvm/Instruction.h"
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +000029#include "llvm/Intrinsics.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000030#include "llvm/GlobalValue.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000031#include "llvm/CodeGen/CallingConvLower.h"
Evan Chenga8e29892007-01-19 07:51:42 +000032#include "llvm/CodeGen/MachineBasicBlock.h"
33#include "llvm/CodeGen/MachineFrameInfo.h"
34#include "llvm/CodeGen/MachineFunction.h"
35#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000036#include "llvm/CodeGen/MachineRegisterInfo.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000037#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chenga8e29892007-01-19 07:51:42 +000038#include "llvm/CodeGen/SelectionDAG.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000039#include "llvm/Target/TargetOptions.h"
Evan Chenga8e29892007-01-19 07:51:42 +000040#include "llvm/ADT/VectorExtras.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000041#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000042#include "llvm/Support/MathExtras.h"
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000043#include <sstream>
Evan Chenga8e29892007-01-19 07:51:42 +000044using namespace llvm;
45
Owen Andersone50ed302009-08-10 22:56:29 +000046static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000047 CCValAssign::LocInfo &LocInfo,
48 ISD::ArgFlagsTy &ArgFlags,
49 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000050static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000051 CCValAssign::LocInfo &LocInfo,
52 ISD::ArgFlagsTy &ArgFlags,
53 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000054static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000055 CCValAssign::LocInfo &LocInfo,
56 ISD::ArgFlagsTy &ArgFlags,
57 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000058static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000059 CCValAssign::LocInfo &LocInfo,
60 ISD::ArgFlagsTy &ArgFlags,
61 CCState &State);
62
Owen Andersone50ed302009-08-10 22:56:29 +000063void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
64 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000065 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000066 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000067 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
68 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000069
Owen Anderson70671842009-08-10 20:18:46 +000070 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +000071 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +000072 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000073 }
74
Owen Andersone50ed302009-08-10 22:56:29 +000075 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +000076 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Owen Anderson70671842009-08-10 20:18:46 +000077 setOperationAction(ISD::VSETCC, VT.getSimpleVT(), Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +000078 if (ElemTy == MVT::i8 || ElemTy == MVT::i16)
Owen Anderson70671842009-08-10 20:18:46 +000079 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
80 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
81 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Owen Anderson70671842009-08-10 20:18:46 +000082 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Custom);
Anton Korobeynikov8e6c2b92009-08-21 12:40:35 +000083 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +000084 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +000085 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
86 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
87 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +000088 }
89
90 // Promote all bit-wise operations.
91 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +000092 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000093 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
94 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +000095 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +000096 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +000097 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +000098 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +000099 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000100 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000101 }
102}
103
Owen Andersone50ed302009-08-10 22:56:29 +0000104void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000105 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000106 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000107}
108
Owen Andersone50ed302009-08-10 22:56:29 +0000109void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000110 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000111 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000112}
113
Chris Lattnerf0144122009-07-28 03:13:23 +0000114static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
115 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000116 return new TargetLoweringObjectFileMachO();
Chris Lattner80ec2792009-08-02 00:34:36 +0000117 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000118}
119
Evan Chenga8e29892007-01-19 07:51:42 +0000120ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000121 : TargetLowering(TM, createTLOF(TM)), ARMPCLabelIndex(0) {
Evan Chenga8e29892007-01-19 07:51:42 +0000122 Subtarget = &TM.getSubtarget<ARMSubtarget>();
123
Evan Chengb1df8f22007-04-27 08:15:43 +0000124 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000125 // Uses VFP for Thumb libfuncs if available.
126 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
127 // Single-precision floating-point arithmetic.
128 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
129 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
130 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
131 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000132
Evan Chengb1df8f22007-04-27 08:15:43 +0000133 // Double-precision floating-point arithmetic.
134 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
135 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
136 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
137 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000138
Evan Chengb1df8f22007-04-27 08:15:43 +0000139 // Single-precision comparisons.
140 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
141 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
142 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
143 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
144 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
145 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
146 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
147 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000148
Evan Chengb1df8f22007-04-27 08:15:43 +0000149 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
150 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
151 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
152 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
153 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
154 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
155 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
156 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000157
Evan Chengb1df8f22007-04-27 08:15:43 +0000158 // Double-precision comparisons.
159 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
160 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
161 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
162 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
163 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
164 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
165 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
166 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000167
Evan Chengb1df8f22007-04-27 08:15:43 +0000168 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
169 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
170 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
171 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
172 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
173 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
174 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
175 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000176
Evan Chengb1df8f22007-04-27 08:15:43 +0000177 // Floating-point to integer conversions.
178 // i64 conversions are done via library routines even when generating VFP
179 // instructions, so use the same ones.
180 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
181 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
182 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
183 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000184
Evan Chengb1df8f22007-04-27 08:15:43 +0000185 // Conversions between floating types.
186 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
187 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
188
189 // Integer to floating-point conversions.
190 // i64 conversions are done via library routines even when generating VFP
191 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000192 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
193 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000194 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
195 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
196 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
197 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
198 }
Evan Chenga8e29892007-01-19 07:51:42 +0000199 }
200
Bob Wilson2f954612009-05-22 17:38:41 +0000201 // These libcalls are not available in 32-bit.
202 setLibcallName(RTLIB::SHL_I128, 0);
203 setLibcallName(RTLIB::SRL_I128, 0);
204 setLibcallName(RTLIB::SRA_I128, 0);
205
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000206 // Libcalls should use the AAPCS base standard ABI, even if hard float
207 // is in effect, as per the ARM RTABI specification, section 4.1.2.
208 if (Subtarget->isAAPCS_ABI()) {
209 for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i) {
210 setLibcallCallingConv(static_cast<RTLIB::Libcall>(i),
211 CallingConv::ARM_AAPCS);
212 }
213 }
214
David Goodwinf1daf7d2009-07-08 23:10:31 +0000215 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000216 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000217 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000218 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000219 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000220 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
221 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000222
Owen Anderson825b72b2009-08-11 20:47:22 +0000223 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000224 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000225
226 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000227 addDRTypeForNEON(MVT::v2f32);
228 addDRTypeForNEON(MVT::v8i8);
229 addDRTypeForNEON(MVT::v4i16);
230 addDRTypeForNEON(MVT::v2i32);
231 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000232
Owen Anderson825b72b2009-08-11 20:47:22 +0000233 addQRTypeForNEON(MVT::v4f32);
234 addQRTypeForNEON(MVT::v2f64);
235 addQRTypeForNEON(MVT::v16i8);
236 addQRTypeForNEON(MVT::v8i16);
237 addQRTypeForNEON(MVT::v4i32);
238 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000239
240 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
241 setTargetDAGCombine(ISD::SHL);
242 setTargetDAGCombine(ISD::SRL);
243 setTargetDAGCombine(ISD::SRA);
244 setTargetDAGCombine(ISD::SIGN_EXTEND);
245 setTargetDAGCombine(ISD::ZERO_EXTEND);
246 setTargetDAGCombine(ISD::ANY_EXTEND);
247 }
248
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000249 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000250
251 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000252 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000253
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000254 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000255 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000256
Evan Chenga8e29892007-01-19 07:51:42 +0000257 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000258 if (!Subtarget->isThumb1Only()) {
259 for (unsigned im = (unsigned)ISD::PRE_INC;
260 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000261 setIndexedLoadAction(im, MVT::i1, Legal);
262 setIndexedLoadAction(im, MVT::i8, Legal);
263 setIndexedLoadAction(im, MVT::i16, Legal);
264 setIndexedLoadAction(im, MVT::i32, Legal);
265 setIndexedStoreAction(im, MVT::i1, Legal);
266 setIndexedStoreAction(im, MVT::i8, Legal);
267 setIndexedStoreAction(im, MVT::i16, Legal);
268 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000269 }
Evan Chenga8e29892007-01-19 07:51:42 +0000270 }
271
272 // i64 operation support.
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000273 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000274 setOperationAction(ISD::MUL, MVT::i64, Expand);
275 setOperationAction(ISD::MULHU, MVT::i32, Expand);
276 setOperationAction(ISD::MULHS, MVT::i32, Expand);
277 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
278 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000279 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000280 setOperationAction(ISD::MUL, MVT::i64, Expand);
281 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb6207242009-08-01 00:16:10 +0000282 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000283 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000284 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000285 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
286 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
287 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
288 setOperationAction(ISD::SRL, MVT::i64, Custom);
289 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000290
291 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000292 setOperationAction(ISD::ROTL, MVT::i32, Expand);
293 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
294 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000295 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000296 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000297
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000298 // Only ARMv6 has BSWAP.
299 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000300 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000301
Evan Chenga8e29892007-01-19 07:51:42 +0000302 // These are expanded into libcalls.
Owen Anderson825b72b2009-08-11 20:47:22 +0000303 setOperationAction(ISD::SDIV, MVT::i32, Expand);
304 setOperationAction(ISD::UDIV, MVT::i32, Expand);
305 setOperationAction(ISD::SREM, MVT::i32, Expand);
306 setOperationAction(ISD::UREM, MVT::i32, Expand);
307 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
308 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000309
Evan Chenga8e29892007-01-19 07:51:42 +0000310 // Support label based line numbers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000311 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
312 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000313
Owen Anderson825b72b2009-08-11 20:47:22 +0000314 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
315 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
316 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
317 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000318
Evan Chenga8e29892007-01-19 07:51:42 +0000319 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000320 setOperationAction(ISD::VASTART, MVT::Other, Custom);
321 setOperationAction(ISD::VAARG, MVT::Other, Expand);
322 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
323 setOperationAction(ISD::VAEND, MVT::Other, Expand);
324 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
325 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000326 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
327 // FIXME: Shouldn't need this, since no register is used, but the legalizer
328 // doesn't yet know how to not do that for SjLj.
329 setExceptionSelectorRegister(ARM::R0);
Evan Cheng86198642009-08-07 00:34:42 +0000330 if (Subtarget->isThumb())
Owen Anderson825b72b2009-08-11 20:47:22 +0000331 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Evan Cheng86198642009-08-07 00:34:42 +0000332 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000333 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
334 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000335
Evan Chengd27c9fc2009-07-03 01:43:10 +0000336 if (!Subtarget->hasV6Ops() && !Subtarget->isThumb2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000337 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
338 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000339 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000340 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000341
David Goodwinf1daf7d2009-07-08 23:10:31 +0000342 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only())
Evan Chengc7c77292008-11-04 19:57:48 +0000343 // Turn f64->i64 into FMRRD, i64 -> f64 to FMDRR iff target supports vfp2.
Owen Anderson825b72b2009-08-11 20:47:22 +0000344 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000345
346 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000347 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
348 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
349 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000350
Owen Anderson825b72b2009-08-11 20:47:22 +0000351 setOperationAction(ISD::SETCC, MVT::i32, Expand);
352 setOperationAction(ISD::SETCC, MVT::f32, Expand);
353 setOperationAction(ISD::SETCC, MVT::f64, Expand);
354 setOperationAction(ISD::SELECT, MVT::i32, Expand);
355 setOperationAction(ISD::SELECT, MVT::f32, Expand);
356 setOperationAction(ISD::SELECT, MVT::f64, Expand);
357 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
358 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
359 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000360
Owen Anderson825b72b2009-08-11 20:47:22 +0000361 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
362 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
363 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
364 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
365 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000366
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000367 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000368 setOperationAction(ISD::FSIN, MVT::f64, Expand);
369 setOperationAction(ISD::FSIN, MVT::f32, Expand);
370 setOperationAction(ISD::FCOS, MVT::f32, Expand);
371 setOperationAction(ISD::FCOS, MVT::f64, Expand);
372 setOperationAction(ISD::FREM, MVT::f64, Expand);
373 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000374 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000375 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
376 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000377 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000378 setOperationAction(ISD::FPOW, MVT::f64, Expand);
379 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000380
Evan Chenga8e29892007-01-19 07:51:42 +0000381 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
David Goodwinf1daf7d2009-07-08 23:10:31 +0000382 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000383 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
384 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
385 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
386 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000387 }
Evan Chenga8e29892007-01-19 07:51:42 +0000388
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000389 // We have target-specific dag combine patterns for the following nodes:
390 // ARMISD::FMRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000391 setTargetDAGCombine(ISD::ADD);
392 setTargetDAGCombine(ISD::SUB);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000393
Evan Chenga8e29892007-01-19 07:51:42 +0000394 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Chenga8e29892007-01-19 07:51:42 +0000395 setSchedulingPreference(SchedulingForRegPressure);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000396
Evan Chengbc9b7542009-08-15 07:59:10 +0000397 // FIXME: If-converter should use instruction latency to determine
398 // profitability rather than relying on fixed limits.
399 if (Subtarget->getCPUString() == "generic") {
400 // Generic (and overly aggressive) if-conversion limits.
401 setIfCvtBlockSizeLimit(10);
402 setIfCvtDupBlockSizeLimit(2);
403 } else if (Subtarget->hasV6Ops()) {
404 setIfCvtBlockSizeLimit(2);
405 setIfCvtDupBlockSizeLimit(1);
406 } else {
407 setIfCvtBlockSizeLimit(3);
408 setIfCvtDupBlockSizeLimit(2);
Evan Cheng8557c2b2009-06-19 01:51:50 +0000409 }
410
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000411 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Bob Wilsone6abdff2009-05-18 20:55:32 +0000412 // Do not enable CodePlacementOpt for now: it currently runs after the
413 // ARMConstantIslandPass and messes up branch relaxation and placement
414 // of constant islands.
415 // benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000416}
417
Evan Chenga8e29892007-01-19 07:51:42 +0000418const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
419 switch (Opcode) {
420 default: return 0;
421 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000422 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
423 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000424 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000425 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
426 case ARMISD::tCALL: return "ARMISD::tCALL";
427 case ARMISD::BRCOND: return "ARMISD::BRCOND";
428 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000429 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000430 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
431 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
432 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000433 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000434 case ARMISD::CMPFP: return "ARMISD::CMPFP";
435 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
436 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
437 case ARMISD::CMOV: return "ARMISD::CMOV";
438 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000439
Evan Chenga8e29892007-01-19 07:51:42 +0000440 case ARMISD::FTOSI: return "ARMISD::FTOSI";
441 case ARMISD::FTOUI: return "ARMISD::FTOUI";
442 case ARMISD::SITOF: return "ARMISD::SITOF";
443 case ARMISD::UITOF: return "ARMISD::UITOF";
Evan Chenga8e29892007-01-19 07:51:42 +0000444
445 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
446 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
447 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000448
Evan Chenga8e29892007-01-19 07:51:42 +0000449 case ARMISD::FMRRD: return "ARMISD::FMRRD";
450 case ARMISD::FMDRR: return "ARMISD::FMDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000451
452 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000453
Evan Cheng86198642009-08-07 00:34:42 +0000454 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
455
Bob Wilson5bafff32009-06-22 23:27:02 +0000456 case ARMISD::VCEQ: return "ARMISD::VCEQ";
457 case ARMISD::VCGE: return "ARMISD::VCGE";
458 case ARMISD::VCGEU: return "ARMISD::VCGEU";
459 case ARMISD::VCGT: return "ARMISD::VCGT";
460 case ARMISD::VCGTU: return "ARMISD::VCGTU";
461 case ARMISD::VTST: return "ARMISD::VTST";
462
463 case ARMISD::VSHL: return "ARMISD::VSHL";
464 case ARMISD::VSHRs: return "ARMISD::VSHRs";
465 case ARMISD::VSHRu: return "ARMISD::VSHRu";
466 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
467 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
468 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
469 case ARMISD::VSHRN: return "ARMISD::VSHRN";
470 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
471 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
472 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
473 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
474 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
475 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
476 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
477 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
478 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
479 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
480 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
481 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
482 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
483 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000484 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000485 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000486 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000487 case ARMISD::VREV64: return "ARMISD::VREV64";
488 case ARMISD::VREV32: return "ARMISD::VREV32";
489 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000490 case ARMISD::VZIP: return "ARMISD::VZIP";
491 case ARMISD::VUZP: return "ARMISD::VUZP";
492 case ARMISD::VTRN: return "ARMISD::VTRN";
Evan Chenga8e29892007-01-19 07:51:42 +0000493 }
494}
495
Bill Wendlingb4202b82009-07-01 18:50:55 +0000496/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000497unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
498 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 1 : 2;
499}
500
Evan Chenga8e29892007-01-19 07:51:42 +0000501//===----------------------------------------------------------------------===//
502// Lowering Code
503//===----------------------------------------------------------------------===//
504
Evan Chenga8e29892007-01-19 07:51:42 +0000505/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
506static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
507 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000508 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000509 case ISD::SETNE: return ARMCC::NE;
510 case ISD::SETEQ: return ARMCC::EQ;
511 case ISD::SETGT: return ARMCC::GT;
512 case ISD::SETGE: return ARMCC::GE;
513 case ISD::SETLT: return ARMCC::LT;
514 case ISD::SETLE: return ARMCC::LE;
515 case ISD::SETUGT: return ARMCC::HI;
516 case ISD::SETUGE: return ARMCC::HS;
517 case ISD::SETULT: return ARMCC::LO;
518 case ISD::SETULE: return ARMCC::LS;
519 }
520}
521
522/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC. It
523/// returns true if the operands should be inverted to form the proper
524/// comparison.
525static bool FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
526 ARMCC::CondCodes &CondCode2) {
527 bool Invert = false;
528 CondCode2 = ARMCC::AL;
529 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000530 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000531 case ISD::SETEQ:
532 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
533 case ISD::SETGT:
534 case ISD::SETOGT: CondCode = ARMCC::GT; break;
535 case ISD::SETGE:
536 case ISD::SETOGE: CondCode = ARMCC::GE; break;
537 case ISD::SETOLT: CondCode = ARMCC::MI; break;
538 case ISD::SETOLE: CondCode = ARMCC::GT; Invert = true; break;
539 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
540 case ISD::SETO: CondCode = ARMCC::VC; break;
541 case ISD::SETUO: CondCode = ARMCC::VS; break;
542 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
543 case ISD::SETUGT: CondCode = ARMCC::HI; break;
544 case ISD::SETUGE: CondCode = ARMCC::PL; break;
545 case ISD::SETLT:
546 case ISD::SETULT: CondCode = ARMCC::LT; break;
547 case ISD::SETLE:
548 case ISD::SETULE: CondCode = ARMCC::LE; break;
549 case ISD::SETNE:
550 case ISD::SETUNE: CondCode = ARMCC::NE; break;
551 }
552 return Invert;
553}
554
Bob Wilson1f595bb2009-04-17 19:07:39 +0000555//===----------------------------------------------------------------------===//
556// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +0000557//===----------------------------------------------------------------------===//
558
559#include "ARMGenCallingConv.inc"
560
561// APCS f64 is in register pairs, possibly split to stack
Owen Andersone50ed302009-08-10 22:56:29 +0000562static bool f64AssignAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000563 CCValAssign::LocInfo &LocInfo,
564 CCState &State, bool CanFail) {
565 static const unsigned RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
566
567 // Try to get the first register.
568 if (unsigned Reg = State.AllocateReg(RegList, 4))
569 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
570 else {
571 // For the 2nd half of a v2f64, do not fail.
572 if (CanFail)
573 return false;
574
575 // Put the whole thing on the stack.
576 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
577 State.AllocateStack(8, 4),
578 LocVT, LocInfo));
579 return true;
580 }
581
582 // Try to get the second register.
583 if (unsigned Reg = State.AllocateReg(RegList, 4))
584 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
585 else
586 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
587 State.AllocateStack(4, 4),
588 LocVT, LocInfo));
589 return true;
590}
591
Owen Andersone50ed302009-08-10 22:56:29 +0000592static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000593 CCValAssign::LocInfo &LocInfo,
594 ISD::ArgFlagsTy &ArgFlags,
595 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000596 if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
597 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000598 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000599 !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
600 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000601 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000602}
603
604// AAPCS f64 is in aligned register pairs
Owen Andersone50ed302009-08-10 22:56:29 +0000605static bool f64AssignAAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000606 CCValAssign::LocInfo &LocInfo,
607 CCState &State, bool CanFail) {
608 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
609 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
610
611 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
612 if (Reg == 0) {
613 // For the 2nd half of a v2f64, do not just fail.
614 if (CanFail)
615 return false;
616
617 // Put the whole thing on the stack.
618 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
619 State.AllocateStack(8, 8),
620 LocVT, LocInfo));
621 return true;
622 }
623
624 unsigned i;
625 for (i = 0; i < 2; ++i)
626 if (HiRegList[i] == Reg)
627 break;
628
629 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
630 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
631 LocVT, LocInfo));
632 return true;
633}
634
Owen Andersone50ed302009-08-10 22:56:29 +0000635static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000636 CCValAssign::LocInfo &LocInfo,
637 ISD::ArgFlagsTy &ArgFlags,
638 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000639 if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
640 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000641 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000642 !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
643 return false;
644 return true; // we handled it
645}
646
Owen Andersone50ed302009-08-10 22:56:29 +0000647static bool f64RetAssign(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000648 CCValAssign::LocInfo &LocInfo, CCState &State) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000649 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
650 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
651
Bob Wilsone65586b2009-04-17 20:40:45 +0000652 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
653 if (Reg == 0)
654 return false; // we didn't handle it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000655
Bob Wilsone65586b2009-04-17 20:40:45 +0000656 unsigned i;
657 for (i = 0; i < 2; ++i)
658 if (HiRegList[i] == Reg)
659 break;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000660
Bob Wilson5bafff32009-06-22 23:27:02 +0000661 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bob Wilsone65586b2009-04-17 20:40:45 +0000662 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000663 LocVT, LocInfo));
664 return true;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000665}
666
Owen Andersone50ed302009-08-10 22:56:29 +0000667static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000668 CCValAssign::LocInfo &LocInfo,
669 ISD::ArgFlagsTy &ArgFlags,
670 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000671 if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
672 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000673 if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
Bob Wilson5bafff32009-06-22 23:27:02 +0000674 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000675 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000676}
677
Owen Andersone50ed302009-08-10 22:56:29 +0000678static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000679 CCValAssign::LocInfo &LocInfo,
680 ISD::ArgFlagsTy &ArgFlags,
681 CCState &State) {
682 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
683 State);
684}
685
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000686/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
687/// given CallingConvention value.
688CCAssignFn *ARMTargetLowering::CCAssignFnForNode(unsigned CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000689 bool Return,
690 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000691 switch (CC) {
692 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000693 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000694 case CallingConv::C:
695 case CallingConv::Fast:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000696 // Use target triple & subtarget features to do actual dispatch.
697 if (Subtarget->isAAPCS_ABI()) {
698 if (Subtarget->hasVFP2() &&
699 FloatABIType == FloatABI::Hard && !isVarArg)
700 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
701 else
702 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
703 } else
704 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000705 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000706 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000707 case CallingConv::ARM_AAPCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000708 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000709 case CallingConv::ARM_APCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000710 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000711 }
712}
713
Dan Gohman98ca4f22009-08-05 01:29:28 +0000714/// LowerCallResult - Lower the result values of a call into the
715/// appropriate copies out of appropriate physical registers.
716SDValue
717ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
718 unsigned CallConv, bool isVarArg,
719 const SmallVectorImpl<ISD::InputArg> &Ins,
720 DebugLoc dl, SelectionDAG &DAG,
721 SmallVectorImpl<SDValue> &InVals) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000722
Bob Wilson1f595bb2009-04-17 19:07:39 +0000723 // Assign locations to each value returned by this call.
724 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000725 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000726 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000727 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000728 CCAssignFnForNode(CallConv, /* Return*/ true,
729 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000730
731 // Copy all of the result registers out of their specified physreg.
732 for (unsigned i = 0; i != RVLocs.size(); ++i) {
733 CCValAssign VA = RVLocs[i];
734
Bob Wilson80915242009-04-25 00:33:20 +0000735 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000736 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000737 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000738 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000739 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000740 Chain = Lo.getValue(1);
741 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000742 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000743 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000744 InFlag);
745 Chain = Hi.getValue(1);
746 InFlag = Hi.getValue(2);
Owen Anderson825b72b2009-08-11 20:47:22 +0000747 Val = DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +0000748
Owen Anderson825b72b2009-08-11 20:47:22 +0000749 if (VA.getLocVT() == MVT::v2f64) {
750 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
751 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
752 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000753
754 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000755 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000756 Chain = Lo.getValue(1);
757 InFlag = Lo.getValue(2);
758 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000759 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000760 Chain = Hi.getValue(1);
761 InFlag = Hi.getValue(2);
Owen Anderson825b72b2009-08-11 20:47:22 +0000762 Val = DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
763 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
764 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000765 }
Bob Wilson1f595bb2009-04-17 19:07:39 +0000766 } else {
Bob Wilson80915242009-04-25 00:33:20 +0000767 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
768 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000769 Chain = Val.getValue(1);
770 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000771 }
Bob Wilson80915242009-04-25 00:33:20 +0000772
773 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000774 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +0000775 case CCValAssign::Full: break;
776 case CCValAssign::BCvt:
777 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
778 break;
779 }
780
Dan Gohman98ca4f22009-08-05 01:29:28 +0000781 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000782 }
783
Dan Gohman98ca4f22009-08-05 01:29:28 +0000784 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000785}
786
787/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
788/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +0000789/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +0000790/// a byval function parameter.
791/// Sometimes what we are copying is the end of a larger object, the part that
792/// does not fit in registers.
793static SDValue
794CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
795 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
796 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000797 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000798 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
799 /*AlwaysInline=*/false, NULL, 0, NULL, 0);
800}
801
Bob Wilsondee46d72009-04-17 20:35:10 +0000802/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000803SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +0000804ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
805 SDValue StackPtr, SDValue Arg,
806 DebugLoc dl, SelectionDAG &DAG,
807 const CCValAssign &VA,
808 ISD::ArgFlagsTy Flags) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000809 unsigned LocMemOffset = VA.getLocMemOffset();
810 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
811 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
812 if (Flags.isByVal()) {
813 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
814 }
815 return DAG.getStore(Chain, dl, Arg, PtrOff,
816 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chenga8e29892007-01-19 07:51:42 +0000817}
818
Dan Gohman98ca4f22009-08-05 01:29:28 +0000819void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +0000820 SDValue Chain, SDValue &Arg,
821 RegsToPassVector &RegsToPass,
822 CCValAssign &VA, CCValAssign &NextVA,
823 SDValue &StackPtr,
824 SmallVector<SDValue, 8> &MemOpChains,
825 ISD::ArgFlagsTy Flags) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000826
827 SDValue fmrrd = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +0000828 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +0000829 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
830
831 if (NextVA.isRegLoc())
832 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
833 else {
834 assert(NextVA.isMemLoc());
835 if (StackPtr.getNode() == 0)
836 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
837
Dan Gohman98ca4f22009-08-05 01:29:28 +0000838 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
839 dl, DAG, NextVA,
840 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000841 }
842}
843
Dan Gohman98ca4f22009-08-05 01:29:28 +0000844/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +0000845/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
846/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000847SDValue
848ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
849 unsigned CallConv, bool isVarArg,
850 bool isTailCall,
851 const SmallVectorImpl<ISD::OutputArg> &Outs,
852 const SmallVectorImpl<ISD::InputArg> &Ins,
853 DebugLoc dl, SelectionDAG &DAG,
854 SmallVectorImpl<SDValue> &InVals) {
Evan Chenga8e29892007-01-19 07:51:42 +0000855
Bob Wilson1f595bb2009-04-17 19:07:39 +0000856 // Analyze operands of the call, assigning locations to each operand.
857 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000858 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
859 *DAG.getContext());
860 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000861 CCAssignFnForNode(CallConv, /* Return*/ false,
862 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +0000863
Bob Wilson1f595bb2009-04-17 19:07:39 +0000864 // Get a count of how many bytes are to be pushed on the stack.
865 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +0000866
867 // Adjust the stack pointer for the new arguments...
868 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattnere563bbc2008-10-11 22:08:30 +0000869 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +0000870
Owen Anderson825b72b2009-08-11 20:47:22 +0000871 SDValue StackPtr = DAG.getRegister(ARM::SP, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000872
Bob Wilson5bafff32009-06-22 23:27:02 +0000873 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000874 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +0000875
Bob Wilson1f595bb2009-04-17 19:07:39 +0000876 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +0000877 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000878 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
879 i != e;
880 ++i, ++realArgIdx) {
881 CCValAssign &VA = ArgLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +0000882 SDValue Arg = Outs[realArgIdx].Val;
883 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +0000884
Bob Wilson1f595bb2009-04-17 19:07:39 +0000885 // Promote the value if needed.
886 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000887 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +0000888 case CCValAssign::Full: break;
889 case CCValAssign::SExt:
890 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
891 break;
892 case CCValAssign::ZExt:
893 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
894 break;
895 case CCValAssign::AExt:
896 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
897 break;
898 case CCValAssign::BCvt:
899 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
900 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000901 }
902
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000903 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +0000904 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000905 if (VA.getLocVT() == MVT::v2f64) {
906 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
907 DAG.getConstant(0, MVT::i32));
908 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
909 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000910
Dan Gohman98ca4f22009-08-05 01:29:28 +0000911 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +0000912 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
913
914 VA = ArgLocs[++i]; // skip ahead to next loc
915 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000916 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +0000917 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
918 } else {
919 assert(VA.isMemLoc());
920 if (StackPtr.getNode() == 0)
921 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
922
Dan Gohman98ca4f22009-08-05 01:29:28 +0000923 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
924 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000925 }
926 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000927 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000928 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000929 }
930 } else if (VA.isRegLoc()) {
931 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
932 } else {
933 assert(VA.isMemLoc());
934 if (StackPtr.getNode() == 0)
935 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
936
Dan Gohman98ca4f22009-08-05 01:29:28 +0000937 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
938 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000939 }
Evan Chenga8e29892007-01-19 07:51:42 +0000940 }
941
942 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000943 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +0000944 &MemOpChains[0], MemOpChains.size());
945
946 // Build a sequence of copy-to-reg nodes chained together with token chain
947 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +0000948 SDValue InFlag;
Evan Chenga8e29892007-01-19 07:51:42 +0000949 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Bob Wilson2dc4f542009-03-20 22:42:55 +0000950 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000951 RegsToPass[i].second, InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +0000952 InFlag = Chain.getValue(1);
953 }
954
Bill Wendling056292f2008-09-16 21:48:12 +0000955 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
956 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
957 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +0000958 bool isDirect = false;
959 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +0000960 bool isLocalARMFunc = false;
Evan Chenga8e29892007-01-19 07:51:42 +0000961 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
962 GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +0000963 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +0000964 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +0000965 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +0000966 getTargetMachine().getRelocationModel() != Reloc::Static;
967 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +0000968 // ARM call to a local ARM function is predicable.
969 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
Evan Chengc60e76d2007-01-30 20:37:08 +0000970 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +0000971 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge4e4ed32009-08-28 23:18:09 +0000972 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +0000973 ARMPCLabelIndex,
974 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000975 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +0000976 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000977 Callee = DAG.getLoad(getPointerTy(), dl,
978 DAG.getEntryNode(), CPAddr, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000979 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000980 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000981 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +0000982 } else
983 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +0000984 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000985 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +0000986 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +0000987 getTargetMachine().getRelocationModel() != Reloc::Static;
988 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +0000989 // tBX takes a register source operand.
990 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +0000991 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000992 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +0000993 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000994 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +0000995 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000996 Callee = DAG.getLoad(getPointerTy(), dl,
Bob Wilson2dc4f542009-03-20 22:42:55 +0000997 DAG.getEntryNode(), CPAddr, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000998 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000999 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001000 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001001 } else
Bill Wendling056292f2008-09-16 21:48:12 +00001002 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001003 }
1004
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001005 // FIXME: handle tail calls differently.
1006 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001007 if (Subtarget->isThumb()) {
1008 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001009 CallOpc = ARMISD::CALL_NOLINK;
1010 else
1011 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1012 } else {
1013 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001014 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1015 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001016 }
David Goodwinf1daf7d2009-07-08 23:10:31 +00001017 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb1Only()) {
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +00001018 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Owen Anderson825b72b2009-08-11 20:47:22 +00001019 Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001020 InFlag = Chain.getValue(1);
1021 }
1022
Dan Gohman475871a2008-07-27 21:46:04 +00001023 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001024 Ops.push_back(Chain);
1025 Ops.push_back(Callee);
1026
1027 // Add argument registers to the end of the list so that they are known live
1028 // into the call.
1029 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1030 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1031 RegsToPass[i].second.getValueType()));
1032
Gabor Greifba36cb52008-08-28 21:40:38 +00001033 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001034 Ops.push_back(InFlag);
Duncan Sands4bdcb612008-07-02 17:40:58 +00001035 // Returns a chain and a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00001036 Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Flag),
Duncan Sands4bdcb612008-07-02 17:40:58 +00001037 &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001038 InFlag = Chain.getValue(1);
1039
Chris Lattnere563bbc2008-10-11 22:08:30 +00001040 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1041 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001042 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001043 InFlag = Chain.getValue(1);
1044
Bob Wilson1f595bb2009-04-17 19:07:39 +00001045 // Handle result values, copying them out of physregs into vregs that we
1046 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001047 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1048 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001049}
1050
Dan Gohman98ca4f22009-08-05 01:29:28 +00001051SDValue
1052ARMTargetLowering::LowerReturn(SDValue Chain,
1053 unsigned CallConv, bool isVarArg,
1054 const SmallVectorImpl<ISD::OutputArg> &Outs,
1055 DebugLoc dl, SelectionDAG &DAG) {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001056
Bob Wilsondee46d72009-04-17 20:35:10 +00001057 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001058 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001059
Bob Wilsondee46d72009-04-17 20:35:10 +00001060 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001061 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1062 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001063
Dan Gohman98ca4f22009-08-05 01:29:28 +00001064 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001065 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1066 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001067
1068 // If this is the first return lowered for this function, add
1069 // the regs to the liveout set for the function.
1070 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1071 for (unsigned i = 0; i != RVLocs.size(); ++i)
1072 if (RVLocs[i].isRegLoc())
1073 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001074 }
1075
Bob Wilson1f595bb2009-04-17 19:07:39 +00001076 SDValue Flag;
1077
1078 // Copy the result values into the output registers.
1079 for (unsigned i = 0, realRVLocIdx = 0;
1080 i != RVLocs.size();
1081 ++i, ++realRVLocIdx) {
1082 CCValAssign &VA = RVLocs[i];
1083 assert(VA.isRegLoc() && "Can only return in registers!");
1084
Dan Gohman98ca4f22009-08-05 01:29:28 +00001085 SDValue Arg = Outs[realRVLocIdx].Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001086
1087 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001088 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001089 case CCValAssign::Full: break;
1090 case CCValAssign::BCvt:
1091 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1092 break;
1093 }
1094
Bob Wilson1f595bb2009-04-17 19:07:39 +00001095 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001096 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001097 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001098 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1099 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001100 SDValue HalfGPRs = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001101 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001102
1103 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1104 Flag = Chain.getValue(1);
1105 VA = RVLocs[++i]; // skip ahead to next loc
1106 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1107 HalfGPRs.getValue(1), Flag);
1108 Flag = Chain.getValue(1);
1109 VA = RVLocs[++i]; // skip ahead to next loc
1110
1111 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001112 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1113 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001114 }
1115 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1116 // available.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001117 SDValue fmrrd = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001118 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001119 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001120 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001121 VA = RVLocs[++i]; // skip ahead to next loc
1122 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1123 Flag);
1124 } else
1125 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1126
Bob Wilsondee46d72009-04-17 20:35:10 +00001127 // Guarantee that all emitted copies are
1128 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001129 Flag = Chain.getValue(1);
1130 }
1131
1132 SDValue result;
1133 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001134 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001135 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001136 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001137
1138 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001139}
1140
Bob Wilson2dc4f542009-03-20 22:42:55 +00001141// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
Bob Wilsond2559bf2009-07-13 18:11:36 +00001142// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
Bill Wendling056292f2008-09-16 21:48:12 +00001143// one of the above mentioned nodes. It has to be wrapped because otherwise
1144// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1145// be used to form addressing mode. These wrapped nodes will be selected
1146// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001147static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001148 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001149 // FIXME there is no actual debug info here
1150 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001151 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001152 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001153 if (CP->isMachineConstantPoolEntry())
1154 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1155 CP->getAlignment());
1156 else
1157 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1158 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001159 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001160}
1161
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001162// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001163SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001164ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
1165 SelectionDAG &DAG) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001166 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001167 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001168 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1169 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001170 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001171 ARMCP::CPValue, PCAdj, "tlsgd", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001172 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001173 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001174 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001175 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001176
Owen Anderson825b72b2009-08-11 20:47:22 +00001177 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001178 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001179
1180 // call __tls_get_addr.
1181 ArgListTy Args;
1182 ArgListEntry Entry;
1183 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001184 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001185 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001186 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001187 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001188 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1189 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001190 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001191 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001192 return CallResult.first;
1193}
1194
1195// Lower ISD::GlobalTLSAddress using the "initial exec" or
1196// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001197SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001198ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001199 SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001200 GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001201 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001202 SDValue Offset;
1203 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001204 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001205 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001206 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001207
Chris Lattner4fb63d02009-07-15 04:12:33 +00001208 if (GV->isDeclaration()) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001209 // initial exec model
1210 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1211 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001212 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001213 ARMCP::CPValue, PCAdj, "gottpoff", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001214 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001215 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001216 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001217 Chain = Offset.getValue(1);
1218
Owen Anderson825b72b2009-08-11 20:47:22 +00001219 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001220 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001221
Dale Johannesen33c960f2009-02-04 20:06:27 +00001222 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001223 } else {
1224 // local exec model
Evan Chenge4e4ed32009-08-28 23:18:09 +00001225 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, "tpoff");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001226 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001227 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
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 }
1230
1231 // The address of the thread local variable is the add of the thread
1232 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001233 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001234}
1235
Dan Gohman475871a2008-07-27 21:46:04 +00001236SDValue
1237ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001238 // TODO: implement the "local dynamic" model
1239 assert(Subtarget->isTargetELF() &&
1240 "TLS not implemented for non-ELF targets");
1241 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1242 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1243 // otherwise use the "Local Exec" TLS Model
1244 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1245 return LowerToTLSGeneralDynamicModel(GA, DAG);
1246 else
1247 return LowerToTLSExecModels(GA, DAG);
1248}
1249
Dan Gohman475871a2008-07-27 21:46:04 +00001250SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001251 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001252 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001253 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001254 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1255 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1256 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001257 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001258 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001259 new ARMConstantPoolValue(GV, UseGOTOFF ? "GOTOFF" : "GOT");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001260 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001261 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001262 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Dale Johannesen33c960f2009-02-04 20:06:27 +00001263 CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001264 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001265 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001266 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001267 if (!UseGOTOFF)
Dale Johannesen33c960f2009-02-04 20:06:27 +00001268 Result = DAG.getLoad(PtrVT, dl, Chain, Result, NULL, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001269 return Result;
1270 } else {
Evan Cheng1606e8e2009-03-13 07:51:59 +00001271 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001272 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001273 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001274 }
1275}
1276
Dan Gohman475871a2008-07-27 21:46:04 +00001277SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001278 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001279 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001280 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001281 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1282 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Dan Gohman475871a2008-07-27 21:46:04 +00001283 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +00001284 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001285 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001286 else {
Evan Chenge4e4ed32009-08-28 23:18:09 +00001287 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
1288 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001289 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001290 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001291 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001292 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001293
Dale Johannesen33c960f2009-02-04 20:06:27 +00001294 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001295 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001296
1297 if (RelocM == Reloc::PIC_) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001298 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001299 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001300 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00001301
1302 if (Subtarget->GVIsIndirectSymbol(GV, RelocM == Reloc::Static))
Dale Johannesen33c960f2009-02-04 20:06:27 +00001303 Result = DAG.getLoad(PtrVT, dl, Chain, Result, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001304
1305 return Result;
1306}
1307
Dan Gohman475871a2008-07-27 21:46:04 +00001308SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001309 SelectionDAG &DAG){
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001310 assert(Subtarget->isTargetELF() &&
1311 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Owen Andersone50ed302009-08-10 22:56:29 +00001312 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001313 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001314 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00001315 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1316 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00001317 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001318 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001319 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001320 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001321 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001322 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001323}
1324
Bob Wilsona599bff2009-08-04 00:36:16 +00001325static SDValue LowerNeonVLDIntrinsic(SDValue Op, SelectionDAG &DAG,
Bob Wilson31fb12f2009-08-26 17:39:53 +00001326 unsigned NumVecs) {
Bob Wilsona599bff2009-08-04 00:36:16 +00001327 SDNode *Node = Op.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001328 EVT VT = Node->getValueType(0);
Bob Wilsona599bff2009-08-04 00:36:16 +00001329
Bob Wilson31fb12f2009-08-26 17:39:53 +00001330 // No expansion needed for 64-bit vectors.
1331 if (VT.is64BitVector())
1332 return SDValue();
Bob Wilsona599bff2009-08-04 00:36:16 +00001333
Bob Wilson31fb12f2009-08-26 17:39:53 +00001334 // FIXME: We need to expand VLD3 and VLD4 of 128-bit vectors into separate
1335 // operations to load the even and odd registers.
1336 return SDValue();
Bob Wilsona599bff2009-08-04 00:36:16 +00001337}
1338
Bob Wilsonb36ec862009-08-06 18:47:44 +00001339static SDValue LowerNeonVSTIntrinsic(SDValue Op, SelectionDAG &DAG,
Bob Wilson31fb12f2009-08-26 17:39:53 +00001340 unsigned NumVecs) {
Bob Wilsonb36ec862009-08-06 18:47:44 +00001341 SDNode *Node = Op.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001342 EVT VT = Node->getOperand(3).getValueType();
Bob Wilsonb36ec862009-08-06 18:47:44 +00001343
Bob Wilson31fb12f2009-08-26 17:39:53 +00001344 // No expansion needed for 64-bit vectors.
1345 if (VT.is64BitVector())
1346 return SDValue();
Bob Wilsonb36ec862009-08-06 18:47:44 +00001347
Bob Wilson31fb12f2009-08-26 17:39:53 +00001348 // FIXME: We need to expand VST3 and VST4 of 128-bit vectors into separate
1349 // operations to store the even and odd registers.
1350 return SDValue();
Bob Wilsonb36ec862009-08-06 18:47:44 +00001351}
1352
Bob Wilsona599bff2009-08-04 00:36:16 +00001353SDValue
1354ARMTargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) {
1355 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1356 switch (IntNo) {
Bob Wilsonb0abb4d2009-08-11 05:39:44 +00001357 case Intrinsic::arm_neon_vld3:
Bob Wilson31fb12f2009-08-26 17:39:53 +00001358 return LowerNeonVLDIntrinsic(Op, DAG, 3);
Bob Wilsonb0abb4d2009-08-11 05:39:44 +00001359 case Intrinsic::arm_neon_vld4:
Bob Wilson31fb12f2009-08-26 17:39:53 +00001360 return LowerNeonVLDIntrinsic(Op, DAG, 4);
Bob Wilsonb0abb4d2009-08-11 05:39:44 +00001361 case Intrinsic::arm_neon_vst3:
Bob Wilson31fb12f2009-08-26 17:39:53 +00001362 return LowerNeonVSTIntrinsic(Op, DAG, 3);
Bob Wilsonb0abb4d2009-08-11 05:39:44 +00001363 case Intrinsic::arm_neon_vst4:
Bob Wilson31fb12f2009-08-26 17:39:53 +00001364 return LowerNeonVSTIntrinsic(Op, DAG, 4);
Bob Wilsona599bff2009-08-04 00:36:16 +00001365 default: return SDValue(); // Don't custom lower most intrinsics.
1366 }
1367}
1368
Jim Grosbach0e0da732009-05-12 23:59:14 +00001369SDValue
1370ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001371 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001372 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001373 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001374 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00001375 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00001376 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00001377 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1378 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001379 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001380 MachineFunction &MF = DAG.getMachineFunction();
1381 EVT PtrVT = getPointerTy();
1382 DebugLoc dl = Op.getDebugLoc();
1383 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1384 SDValue CPAddr;
1385 unsigned PCAdj = (RelocM != Reloc::PIC_)
1386 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001387 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001388 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
1389 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001390 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001391 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001392 SDValue Result =
1393 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
1394 SDValue Chain = Result.getValue(1);
1395
1396 if (RelocM == Reloc::PIC_) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001397 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001398 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1399 }
1400 return Result;
1401 }
Jim Grosbachf9570122009-05-14 00:46:35 +00001402 case Intrinsic::eh_sjlj_setjmp:
Owen Anderson825b72b2009-08-11 20:47:22 +00001403 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(1));
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001404 }
1405}
1406
Dan Gohman475871a2008-07-27 21:46:04 +00001407static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001408 unsigned VarArgsFrameIndex) {
Evan Chenga8e29892007-01-19 07:51:42 +00001409 // vastart just stores the address of the VarArgsFrameIndex slot into the
1410 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001411 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001412 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001413 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001414 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001415 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001416}
1417
Dan Gohman475871a2008-07-27 21:46:04 +00001418SDValue
Evan Cheng86198642009-08-07 00:34:42 +00001419ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) {
1420 SDNode *Node = Op.getNode();
1421 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001422 EVT VT = Node->getValueType(0);
Evan Cheng86198642009-08-07 00:34:42 +00001423 SDValue Chain = Op.getOperand(0);
1424 SDValue Size = Op.getOperand(1);
1425 SDValue Align = Op.getOperand(2);
1426
1427 // Chain the dynamic stack allocation so that it doesn't modify the stack
1428 // pointer when other instructions are using the stack.
1429 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1430
1431 unsigned AlignVal = cast<ConstantSDNode>(Align)->getZExtValue();
1432 unsigned StackAlign = getTargetMachine().getFrameInfo()->getStackAlignment();
1433 if (AlignVal > StackAlign)
1434 // Do this now since selection pass cannot introduce new target
1435 // independent node.
1436 Align = DAG.getConstant(-(uint64_t)AlignVal, VT);
1437
1438 // In Thumb1 mode, there isn't a "sub r, sp, r" instruction, we will end up
1439 // using a "add r, sp, r" instead. Negate the size now so we don't have to
1440 // do even more horrible hack later.
1441 MachineFunction &MF = DAG.getMachineFunction();
1442 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1443 if (AFI->isThumb1OnlyFunction()) {
1444 bool Negate = true;
1445 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Size);
1446 if (C) {
1447 uint32_t Val = C->getZExtValue();
1448 if (Val <= 508 && ((Val & 3) == 0))
1449 Negate = false;
1450 }
1451 if (Negate)
1452 Size = DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(0, VT), Size);
1453 }
1454
Owen Anderson825b72b2009-08-11 20:47:22 +00001455 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
Evan Cheng86198642009-08-07 00:34:42 +00001456 SDValue Ops1[] = { Chain, Size, Align };
1457 SDValue Res = DAG.getNode(ARMISD::DYN_ALLOC, dl, VTList, Ops1, 3);
1458 Chain = Res.getValue(1);
1459 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1460 DAG.getIntPtrConstant(0, true), SDValue());
1461 SDValue Ops2[] = { Res, Chain };
1462 return DAG.getMergeValues(Ops2, 2, dl);
1463}
1464
1465SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00001466ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
1467 SDValue &Root, SelectionDAG &DAG,
1468 DebugLoc dl) {
1469 MachineFunction &MF = DAG.getMachineFunction();
1470 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1471
1472 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001473 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00001474 RC = ARM::tGPRRegisterClass;
1475 else
1476 RC = ARM::GPRRegisterClass;
1477
1478 // Transform the arguments stored in physical registers into virtual ones.
1479 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001480 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001481
1482 SDValue ArgValue2;
1483 if (NextVA.isMemLoc()) {
1484 unsigned ArgSize = NextVA.getLocVT().getSizeInBits()/8;
1485 MachineFrameInfo *MFI = MF.getFrameInfo();
1486 int FI = MFI->CreateFixedObject(ArgSize, NextVA.getLocMemOffset());
1487
1488 // Create load node to retrieve arguments from the stack.
1489 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00001490 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN, NULL, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00001491 } else {
1492 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001493 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001494 }
1495
Owen Anderson825b72b2009-08-11 20:47:22 +00001496 return DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00001497}
1498
1499SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001500ARMTargetLowering::LowerFormalArguments(SDValue Chain,
1501 unsigned CallConv, bool isVarArg,
1502 const SmallVectorImpl<ISD::InputArg>
1503 &Ins,
1504 DebugLoc dl, SelectionDAG &DAG,
1505 SmallVectorImpl<SDValue> &InVals) {
1506
Bob Wilson1f595bb2009-04-17 19:07:39 +00001507 MachineFunction &MF = DAG.getMachineFunction();
1508 MachineFrameInfo *MFI = MF.getFrameInfo();
1509
Bob Wilson1f595bb2009-04-17 19:07:39 +00001510 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1511
1512 // Assign locations to all of the incoming arguments.
1513 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001514 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1515 *DAG.getContext());
1516 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001517 CCAssignFnForNode(CallConv, /* Return*/ false,
1518 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001519
1520 SmallVector<SDValue, 16> ArgValues;
1521
1522 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1523 CCValAssign &VA = ArgLocs[i];
1524
Bob Wilsondee46d72009-04-17 20:35:10 +00001525 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001526 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001527 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00001528
Bob Wilson5bafff32009-06-22 23:27:02 +00001529 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001530 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001531 // f64 and vector types are split up into multiple registers or
1532 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00001533 RegVT = MVT::i32;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001534
Owen Anderson825b72b2009-08-11 20:47:22 +00001535 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001536 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001537 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00001538 VA = ArgLocs[++i]; // skip ahead to next loc
1539 SDValue ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001540 Chain, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001541 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1542 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001543 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00001544 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001545 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
1546 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001547 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001548
Bob Wilson5bafff32009-06-22 23:27:02 +00001549 } else {
1550 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001551
Owen Anderson825b72b2009-08-11 20:47:22 +00001552 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00001553 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001554 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00001555 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001556 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001557 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001558 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001559 RC = (AFI->isThumb1OnlyFunction() ?
1560 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00001561 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001562 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00001563
1564 // Transform the arguments in physical registers into virtual ones.
1565 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001566 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001567 }
1568
1569 // If this is an 8 or 16-bit value, it is really passed promoted
1570 // to 32 bits. Insert an assert[sz]ext to capture this, then
1571 // truncate to the right size.
1572 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001573 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001574 case CCValAssign::Full: break;
1575 case CCValAssign::BCvt:
1576 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1577 break;
1578 case CCValAssign::SExt:
1579 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
1580 DAG.getValueType(VA.getValVT()));
1581 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1582 break;
1583 case CCValAssign::ZExt:
1584 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
1585 DAG.getValueType(VA.getValVT()));
1586 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1587 break;
1588 }
1589
Dan Gohman98ca4f22009-08-05 01:29:28 +00001590 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001591
1592 } else { // VA.isRegLoc()
1593
1594 // sanity check
1595 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00001596 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001597
1598 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
1599 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset());
1600
Bob Wilsondee46d72009-04-17 20:35:10 +00001601 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001602 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001603 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN, NULL, 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001604 }
1605 }
1606
1607 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00001608 if (isVarArg) {
1609 static const unsigned GPRArgRegs[] = {
1610 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1611 };
1612
Bob Wilsondee46d72009-04-17 20:35:10 +00001613 unsigned NumGPRs = CCInfo.getFirstUnallocated
1614 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001615
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001616 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1617 unsigned VARegSize = (4 - NumGPRs) * 4;
1618 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001619 unsigned ArgOffset = 0;
Evan Chenga8e29892007-01-19 07:51:42 +00001620 if (VARegSaveSize) {
1621 // If this function is vararg, store any remaining integer argument regs
1622 // to their spots on the stack so that they may be loaded by deferencing
1623 // the result of va_next.
1624 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001625 ArgOffset = CCInfo.getNextStackOffset();
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001626 VarArgsFrameIndex = MFI->CreateFixedObject(VARegSaveSize, ArgOffset +
1627 VARegSaveSize - VARegSize);
Dan Gohman475871a2008-07-27 21:46:04 +00001628 SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001629
Dan Gohman475871a2008-07-27 21:46:04 +00001630 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00001631 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001632 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001633 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00001634 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001635 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00001636 RC = ARM::GPRRegisterClass;
1637
Bob Wilson998e1252009-04-20 18:36:57 +00001638 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001639 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001640 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001641 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001642 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00001643 DAG.getConstant(4, getPointerTy()));
1644 }
1645 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001646 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001647 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001648 } else
1649 // This will point to the next argument passed via stack.
1650 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1651 }
1652
Dan Gohman98ca4f22009-08-05 01:29:28 +00001653 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00001654}
1655
1656/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00001657static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00001658 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001659 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00001660 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00001661 // Maybe this has already been legalized into the constant pool?
1662 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00001663 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001664 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
1665 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001666 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001667 }
1668 }
1669 return false;
1670}
1671
David Goodwinf1daf7d2009-07-08 23:10:31 +00001672static bool isLegalCmpImmediate(unsigned C, bool isThumb1Only) {
1673 return ( isThumb1Only && (C & ~255U) == 0) ||
1674 (!isThumb1Only && ARM_AM::getSOImmVal(C) != -1);
Evan Chenga8e29892007-01-19 07:51:42 +00001675}
1676
1677/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1678/// the given operands.
Dan Gohman475871a2008-07-27 21:46:04 +00001679static SDValue getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
David Goodwinf1daf7d2009-07-08 23:10:31 +00001680 SDValue &ARMCC, SelectionDAG &DAG, bool isThumb1Only,
Dale Johannesende064702009-02-06 21:50:26 +00001681 DebugLoc dl) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001682 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001683 unsigned C = RHSC->getZExtValue();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001684 if (!isLegalCmpImmediate(C, isThumb1Only)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001685 // Constant does not fit, try adjusting it by one?
1686 switch (CC) {
1687 default: break;
1688 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00001689 case ISD::SETGE:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001690 if (isLegalCmpImmediate(C-1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001691 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001692 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001693 }
1694 break;
1695 case ISD::SETULT:
1696 case ISD::SETUGE:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001697 if (C > 0 && isLegalCmpImmediate(C-1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001698 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001699 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001700 }
1701 break;
1702 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00001703 case ISD::SETGT:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001704 if (isLegalCmpImmediate(C+1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001705 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001706 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001707 }
1708 break;
1709 case ISD::SETULE:
1710 case ISD::SETUGT:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001711 if (C < 0xffffffff && isLegalCmpImmediate(C+1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001712 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001713 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001714 }
1715 break;
1716 }
1717 }
1718 }
1719
1720 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001721 ARMISD::NodeType CompareType;
1722 switch (CondCode) {
1723 default:
1724 CompareType = ARMISD::CMP;
1725 break;
1726 case ARMCC::EQ:
1727 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00001728 // Uses only Z Flag
1729 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001730 break;
1731 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001732 ARMCC = DAG.getConstant(CondCode, MVT::i32);
1733 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001734}
1735
1736/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001737static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Dale Johannesende064702009-02-06 21:50:26 +00001738 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001739 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00001740 if (!isFloatingPointZero(RHS))
Owen Anderson825b72b2009-08-11 20:47:22 +00001741 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001742 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001743 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
1744 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001745}
1746
Dan Gohman475871a2008-07-27 21:46:04 +00001747static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001748 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00001749 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001750 SDValue LHS = Op.getOperand(0);
1751 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001752 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001753 SDValue TrueVal = Op.getOperand(2);
1754 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00001755 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001756
Owen Anderson825b72b2009-08-11 20:47:22 +00001757 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001758 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001759 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
David Goodwinf1daf7d2009-07-08 23:10:31 +00001760 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb1Only(), dl);
Dale Johannesende064702009-02-06 21:50:26 +00001761 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001762 }
1763
1764 ARMCC::CondCodes CondCode, CondCode2;
1765 if (FPCCToARMCC(CC, CondCode, CondCode2))
1766 std::swap(TrueVal, FalseVal);
1767
Owen Anderson825b72b2009-08-11 20:47:22 +00001768 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1769 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001770 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
1771 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00001772 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001773 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001774 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001775 // FIXME: Needs another CMP because flag can have but one use.
Dale Johannesende064702009-02-06 21:50:26 +00001776 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001777 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Dale Johannesende064702009-02-06 21:50:26 +00001778 Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00001779 }
1780 return Result;
1781}
1782
Dan Gohman475871a2008-07-27 21:46:04 +00001783static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001784 const ARMSubtarget *ST) {
Dan Gohman475871a2008-07-27 21:46:04 +00001785 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001786 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001787 SDValue LHS = Op.getOperand(2);
1788 SDValue RHS = Op.getOperand(3);
1789 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00001790 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001791
Owen Anderson825b72b2009-08-11 20:47:22 +00001792 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001793 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001794 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
David Goodwinf1daf7d2009-07-08 23:10:31 +00001795 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb1Only(), dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001796 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Dale Johannesende064702009-02-06 21:50:26 +00001797 Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001798 }
1799
Owen Anderson825b72b2009-08-11 20:47:22 +00001800 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Chenga8e29892007-01-19 07:51:42 +00001801 ARMCC::CondCodes CondCode, CondCode2;
1802 if (FPCCToARMCC(CC, CondCode, CondCode2))
1803 // Swap the LHS/RHS of the comparison if needed.
1804 std::swap(LHS, RHS);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001805
Dale Johannesende064702009-02-06 21:50:26 +00001806 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001807 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1808 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1809 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00001810 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00001811 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001812 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001813 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00001814 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00001815 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001816 }
1817 return Res;
1818}
1819
Dan Gohman475871a2008-07-27 21:46:04 +00001820SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) {
1821 SDValue Chain = Op.getOperand(0);
1822 SDValue Table = Op.getOperand(1);
1823 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001824 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001825
Owen Andersone50ed302009-08-10 22:56:29 +00001826 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00001827 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
1828 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00001829 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00001830 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00001831 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00001832 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
1833 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00001834 if (Subtarget->isThumb2()) {
1835 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
1836 // which does another jump to the destination. This also makes it easier
1837 // to translate it to TBB / TBH later.
1838 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00001839 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00001840 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001841 }
Evan Cheng66ac5312009-07-25 00:33:29 +00001842 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001843 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr, NULL, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00001844 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001845 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00001846 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001847 } else {
1848 Addr = DAG.getLoad(PTy, dl, Chain, Addr, NULL, 0);
1849 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00001850 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001851 }
Evan Chenga8e29892007-01-19 07:51:42 +00001852}
1853
Dan Gohman475871a2008-07-27 21:46:04 +00001854static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Dale Johannesende064702009-02-06 21:50:26 +00001855 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001856 unsigned Opc =
1857 Op.getOpcode() == ISD::FP_TO_SINT ? ARMISD::FTOSI : ARMISD::FTOUI;
Owen Anderson825b72b2009-08-11 20:47:22 +00001858 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
1859 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001860}
1861
Dan Gohman475871a2008-07-27 21:46:04 +00001862static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001863 EVT VT = Op.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001864 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001865 unsigned Opc =
1866 Op.getOpcode() == ISD::SINT_TO_FP ? ARMISD::SITOF : ARMISD::UITOF;
1867
Owen Anderson825b72b2009-08-11 20:47:22 +00001868 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
Dale Johannesende064702009-02-06 21:50:26 +00001869 return DAG.getNode(Opc, dl, VT, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001870}
1871
Dan Gohman475871a2008-07-27 21:46:04 +00001872static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00001873 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00001874 SDValue Tmp0 = Op.getOperand(0);
1875 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00001876 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001877 EVT VT = Op.getValueType();
1878 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001879 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
1880 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001881 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
1882 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001883 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001884}
1885
Jim Grosbach0e0da732009-05-12 23:59:14 +00001886SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
1887 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1888 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00001889 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001890 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
1891 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00001892 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00001893 ? ARM::R7 : ARM::R11;
1894 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
1895 while (Depth--)
1896 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
1897 return FrameAddr;
1898}
1899
Dan Gohman475871a2008-07-27 21:46:04 +00001900SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00001901ARMTargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman475871a2008-07-27 21:46:04 +00001902 SDValue Chain,
1903 SDValue Dst, SDValue Src,
1904 SDValue Size, unsigned Align,
Dan Gohman707e0182008-04-12 04:36:06 +00001905 bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00001906 const Value *DstSV, uint64_t DstSVOff,
1907 const Value *SrcSV, uint64_t SrcSVOff){
Evan Cheng4102eb52007-10-22 22:11:27 +00001908 // Do repeated 4-byte loads and stores. To be improved.
Dan Gohman707e0182008-04-12 04:36:06 +00001909 // This requires 4-byte alignment.
1910 if ((Align & 3) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00001911 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001912 // This requires the copy size to be a constant, preferrably
1913 // within a subtarget-specific limit.
1914 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
1915 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00001916 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001917 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001918 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00001919 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001920
1921 unsigned BytesLeft = SizeVal & 3;
1922 unsigned NumMemOps = SizeVal >> 2;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001923 unsigned EmittedNumMemOps = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001924 EVT VT = MVT::i32;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001925 unsigned VTSize = 4;
Evan Cheng4102eb52007-10-22 22:11:27 +00001926 unsigned i = 0;
Evan Chenge5e7ce42007-05-18 01:19:57 +00001927 const unsigned MAX_LOADS_IN_LDM = 6;
Dan Gohman475871a2008-07-27 21:46:04 +00001928 SDValue TFOps[MAX_LOADS_IN_LDM];
1929 SDValue Loads[MAX_LOADS_IN_LDM];
Dan Gohman1f13c682008-04-28 17:15:20 +00001930 uint64_t SrcOff = 0, DstOff = 0;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001931
Evan Cheng4102eb52007-10-22 22:11:27 +00001932 // Emit up to MAX_LOADS_IN_LDM loads, then a TokenFactor barrier, then the
1933 // same number of stores. The loads and stores will get combined into
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001934 // ldm/stm later on.
Evan Cheng4102eb52007-10-22 22:11:27 +00001935 while (EmittedNumMemOps < NumMemOps) {
1936 for (i = 0;
1937 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00001938 Loads[i] = DAG.getLoad(VT, dl, Chain,
Owen Anderson825b72b2009-08-11 20:47:22 +00001939 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
1940 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001941 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00001942 TFOps[i] = Loads[i].getValue(1);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001943 SrcOff += VTSize;
1944 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001945 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001946
Evan Cheng4102eb52007-10-22 22:11:27 +00001947 for (i = 0;
1948 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00001949 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Owen Anderson825b72b2009-08-11 20:47:22 +00001950 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
1951 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001952 DstSV, DstSVOff + DstOff);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001953 DstOff += VTSize;
1954 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001955 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00001956
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001957 EmittedNumMemOps += i;
1958 }
1959
Bob Wilson2dc4f542009-03-20 22:42:55 +00001960 if (BytesLeft == 0)
Evan Cheng4102eb52007-10-22 22:11:27 +00001961 return Chain;
1962
1963 // Issue loads / stores for the trailing (1 - 3) bytes.
1964 unsigned BytesLeftSave = BytesLeft;
1965 i = 0;
1966 while (BytesLeft) {
1967 if (BytesLeft >= 2) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001968 VT = MVT::i16;
Evan Cheng4102eb52007-10-22 22:11:27 +00001969 VTSize = 2;
1970 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001971 VT = MVT::i8;
Evan Cheng4102eb52007-10-22 22:11:27 +00001972 VTSize = 1;
1973 }
1974
Dale Johannesen0f502f62009-02-03 22:26:09 +00001975 Loads[i] = DAG.getLoad(VT, dl, Chain,
Owen Anderson825b72b2009-08-11 20:47:22 +00001976 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
1977 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001978 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00001979 TFOps[i] = Loads[i].getValue(1);
1980 ++i;
1981 SrcOff += VTSize;
1982 BytesLeft -= VTSize;
1983 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001984 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00001985
1986 i = 0;
1987 BytesLeft = BytesLeftSave;
1988 while (BytesLeft) {
1989 if (BytesLeft >= 2) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001990 VT = MVT::i16;
Evan Cheng4102eb52007-10-22 22:11:27 +00001991 VTSize = 2;
1992 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001993 VT = MVT::i8;
Evan Cheng4102eb52007-10-22 22:11:27 +00001994 VTSize = 1;
1995 }
1996
Dale Johannesen0f502f62009-02-03 22:26:09 +00001997 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Owen Anderson825b72b2009-08-11 20:47:22 +00001998 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
1999 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00002000 DstSV, DstSVOff + DstOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00002001 ++i;
2002 DstOff += VTSize;
2003 BytesLeft -= VTSize;
2004 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002005 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002006}
2007
Duncan Sands1607f052008-12-01 11:39:25 +00002008static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00002009 SDValue Op = N->getOperand(0);
Dale Johannesende064702009-02-06 21:50:26 +00002010 DebugLoc dl = N->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00002011 if (N->getValueType(0) == MVT::f64) {
Evan Chengc7c77292008-11-04 19:57:48 +00002012 // Turn i64->f64 into FMDRR.
Owen Anderson825b72b2009-08-11 20:47:22 +00002013 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2014 DAG.getConstant(0, MVT::i32));
2015 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2016 DAG.getConstant(1, MVT::i32));
2017 return DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
Evan Chengc7c77292008-11-04 19:57:48 +00002018 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002019
Evan Chengc7c77292008-11-04 19:57:48 +00002020 // Turn f64->i64 into FMRRD.
Bob Wilson2dc4f542009-03-20 22:42:55 +00002021 SDValue Cvt = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00002022 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002023
Chris Lattner27a6c732007-11-24 07:07:01 +00002024 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002025 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
Chris Lattner27a6c732007-11-24 07:07:01 +00002026}
2027
Bob Wilson5bafff32009-06-22 23:27:02 +00002028/// getZeroVector - Returns a vector of specified type with all zero elements.
2029///
Owen Andersone50ed302009-08-10 22:56:29 +00002030static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002031 assert(VT.isVector() && "Expected a vector type");
2032
2033 // Zero vectors are used to represent vector negation and in those cases
2034 // will be implemented with the NEON VNEG instruction. However, VNEG does
2035 // not support i64 elements, so sometimes the zero vectors will need to be
2036 // explicitly constructed. For those cases, and potentially other uses in
2037 // the future, always build zero vectors as <4 x i32> or <2 x i32> bitcasted
2038 // to their dest type. This ensures they get CSE'd.
2039 SDValue Vec;
Owen Anderson825b72b2009-08-11 20:47:22 +00002040 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002041 if (VT.getSizeInBits() == 64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002042 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Bob Wilson5bafff32009-06-22 23:27:02 +00002043 else
Owen Anderson825b72b2009-08-11 20:47:22 +00002044 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Bob Wilson5bafff32009-06-22 23:27:02 +00002045
2046 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2047}
2048
2049/// getOnesVector - Returns a vector of specified type with all bits set.
2050///
Owen Andersone50ed302009-08-10 22:56:29 +00002051static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002052 assert(VT.isVector() && "Expected a vector type");
2053
2054 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2055 // type. This ensures they get CSE'd.
2056 SDValue Vec;
Owen Anderson825b72b2009-08-11 20:47:22 +00002057 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002058 if (VT.getSizeInBits() == 64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002059 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Bob Wilson5bafff32009-06-22 23:27:02 +00002060 else
Owen Anderson825b72b2009-08-11 20:47:22 +00002061 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Bob Wilson5bafff32009-06-22 23:27:02 +00002062
2063 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2064}
2065
2066static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2067 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00002068 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002069 DebugLoc dl = N->getDebugLoc();
2070
2071 // Lower vector shifts on NEON to use VSHL.
2072 if (VT.isVector()) {
2073 assert(ST->hasNEON() && "unexpected vector shift");
2074
2075 // Left shifts translate directly to the vshiftu intrinsic.
2076 if (N->getOpcode() == ISD::SHL)
2077 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002078 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002079 N->getOperand(0), N->getOperand(1));
2080
2081 assert((N->getOpcode() == ISD::SRA ||
2082 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2083
2084 // NEON uses the same intrinsics for both left and right shifts. For
2085 // right shifts, the shift amounts are negative, so negate the vector of
2086 // shift amounts.
Owen Andersone50ed302009-08-10 22:56:29 +00002087 EVT ShiftVT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002088 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2089 getZeroVector(ShiftVT, DAG, dl),
2090 N->getOperand(1));
2091 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2092 Intrinsic::arm_neon_vshifts :
2093 Intrinsic::arm_neon_vshiftu);
2094 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002095 DAG.getConstant(vshiftInt, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002096 N->getOperand(0), NegatedCount);
2097 }
2098
Eli Friedmance392eb2009-08-22 03:13:10 +00002099 // We can get here for a node like i32 = ISD::SHL i32, i64
2100 if (VT != MVT::i64)
2101 return SDValue();
2102
2103 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00002104 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00002105
Chris Lattner27a6c732007-11-24 07:07:01 +00002106 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2107 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002108 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00002109 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002110
Chris Lattner27a6c732007-11-24 07:07:01 +00002111 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00002112 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002113
Chris Lattner27a6c732007-11-24 07:07:01 +00002114 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00002115 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2116 DAG.getConstant(0, MVT::i32));
2117 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2118 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002119
Chris Lattner27a6c732007-11-24 07:07:01 +00002120 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2121 // captures the result into a carry flag.
2122 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Owen Anderson825b72b2009-08-11 20:47:22 +00002123 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002124
Chris Lattner27a6c732007-11-24 07:07:01 +00002125 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00002126 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002127
Chris Lattner27a6c732007-11-24 07:07:01 +00002128 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002129 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00002130}
2131
Bob Wilson5bafff32009-06-22 23:27:02 +00002132static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2133 SDValue TmpOp0, TmpOp1;
2134 bool Invert = false;
2135 bool Swap = false;
2136 unsigned Opc = 0;
2137
2138 SDValue Op0 = Op.getOperand(0);
2139 SDValue Op1 = Op.getOperand(1);
2140 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00002141 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002142 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2143 DebugLoc dl = Op.getDebugLoc();
2144
2145 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2146 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002147 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002148 case ISD::SETUNE:
2149 case ISD::SETNE: Invert = true; // Fallthrough
2150 case ISD::SETOEQ:
2151 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2152 case ISD::SETOLT:
2153 case ISD::SETLT: Swap = true; // Fallthrough
2154 case ISD::SETOGT:
2155 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2156 case ISD::SETOLE:
2157 case ISD::SETLE: Swap = true; // Fallthrough
2158 case ISD::SETOGE:
2159 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2160 case ISD::SETUGE: Swap = true; // Fallthrough
2161 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2162 case ISD::SETUGT: Swap = true; // Fallthrough
2163 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2164 case ISD::SETUEQ: Invert = true; // Fallthrough
2165 case ISD::SETONE:
2166 // Expand this to (OLT | OGT).
2167 TmpOp0 = Op0;
2168 TmpOp1 = Op1;
2169 Opc = ISD::OR;
2170 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2171 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2172 break;
2173 case ISD::SETUO: Invert = true; // Fallthrough
2174 case ISD::SETO:
2175 // Expand this to (OLT | OGE).
2176 TmpOp0 = Op0;
2177 TmpOp1 = Op1;
2178 Opc = ISD::OR;
2179 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2180 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2181 break;
2182 }
2183 } else {
2184 // Integer comparisons.
2185 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002186 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002187 case ISD::SETNE: Invert = true;
2188 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2189 case ISD::SETLT: Swap = true;
2190 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2191 case ISD::SETLE: Swap = true;
2192 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2193 case ISD::SETULT: Swap = true;
2194 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2195 case ISD::SETULE: Swap = true;
2196 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2197 }
2198
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002199 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00002200 if (Opc == ARMISD::VCEQ) {
2201
2202 SDValue AndOp;
2203 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2204 AndOp = Op0;
2205 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2206 AndOp = Op1;
2207
2208 // Ignore bitconvert.
2209 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2210 AndOp = AndOp.getOperand(0);
2211
2212 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2213 Opc = ARMISD::VTST;
2214 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2215 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2216 Invert = !Invert;
2217 }
2218 }
2219 }
2220
2221 if (Swap)
2222 std::swap(Op0, Op1);
2223
2224 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
2225
2226 if (Invert)
2227 Result = DAG.getNOT(dl, Result, VT);
2228
2229 return Result;
2230}
2231
2232/// isVMOVSplat - Check if the specified splat value corresponds to an immediate
2233/// VMOV instruction, and if so, return the constant being splatted.
2234static SDValue isVMOVSplat(uint64_t SplatBits, uint64_t SplatUndef,
2235 unsigned SplatBitSize, SelectionDAG &DAG) {
2236 switch (SplatBitSize) {
2237 case 8:
2238 // Any 1-byte value is OK.
2239 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Owen Anderson825b72b2009-08-11 20:47:22 +00002240 return DAG.getTargetConstant(SplatBits, MVT::i8);
Bob Wilson5bafff32009-06-22 23:27:02 +00002241
2242 case 16:
2243 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
2244 if ((SplatBits & ~0xff) == 0 ||
2245 (SplatBits & ~0xff00) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002246 return DAG.getTargetConstant(SplatBits, MVT::i16);
Bob Wilson5bafff32009-06-22 23:27:02 +00002247 break;
2248
2249 case 32:
2250 // NEON's 32-bit VMOV supports splat values where:
2251 // * only one byte is nonzero, or
2252 // * the least significant byte is 0xff and the second byte is nonzero, or
2253 // * the least significant 2 bytes are 0xff and the third is nonzero.
2254 if ((SplatBits & ~0xff) == 0 ||
2255 (SplatBits & ~0xff00) == 0 ||
2256 (SplatBits & ~0xff0000) == 0 ||
2257 (SplatBits & ~0xff000000) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002258 return DAG.getTargetConstant(SplatBits, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002259
2260 if ((SplatBits & ~0xffff) == 0 &&
2261 ((SplatBits | SplatUndef) & 0xff) == 0xff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002262 return DAG.getTargetConstant(SplatBits | 0xff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002263
2264 if ((SplatBits & ~0xffffff) == 0 &&
2265 ((SplatBits | SplatUndef) & 0xffff) == 0xffff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002266 return DAG.getTargetConstant(SplatBits | 0xffff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002267
2268 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
2269 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
2270 // VMOV.I32. A (very) minor optimization would be to replicate the value
2271 // and fall through here to test for a valid 64-bit splat. But, then the
2272 // caller would also need to check and handle the change in size.
2273 break;
2274
2275 case 64: {
2276 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
2277 uint64_t BitMask = 0xff;
2278 uint64_t Val = 0;
2279 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
2280 if (((SplatBits | SplatUndef) & BitMask) == BitMask)
2281 Val |= BitMask;
2282 else if ((SplatBits & BitMask) != 0)
2283 return SDValue();
2284 BitMask <<= 8;
2285 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002286 return DAG.getTargetConstant(Val, MVT::i64);
Bob Wilson5bafff32009-06-22 23:27:02 +00002287 }
2288
2289 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002290 llvm_unreachable("unexpected size for isVMOVSplat");
Bob Wilson5bafff32009-06-22 23:27:02 +00002291 break;
2292 }
2293
2294 return SDValue();
2295}
2296
2297/// getVMOVImm - If this is a build_vector of constants which can be
2298/// formed by using a VMOV instruction of the specified element size,
2299/// return the constant being splatted. The ByteSize field indicates the
2300/// number of bytes of each element [1248].
2301SDValue ARM::getVMOVImm(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
2302 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
2303 APInt SplatBits, SplatUndef;
2304 unsigned SplatBitSize;
2305 bool HasAnyUndefs;
2306 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
2307 HasAnyUndefs, ByteSize * 8))
2308 return SDValue();
2309
2310 if (SplatBitSize > ByteSize * 8)
2311 return SDValue();
2312
2313 return isVMOVSplat(SplatBits.getZExtValue(), SplatUndef.getZExtValue(),
2314 SplatBitSize, DAG);
2315}
2316
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002317static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
2318 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002319 unsigned NumElts = VT.getVectorNumElements();
2320 ReverseVEXT = false;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002321 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002322
2323 // If this is a VEXT shuffle, the immediate value is the index of the first
2324 // element. The other shuffle indices must be the successive elements after
2325 // the first one.
2326 unsigned ExpectedElt = Imm;
2327 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002328 // Increment the expected index. If it wraps around, it may still be
2329 // a VEXT but the source vectors must be swapped.
2330 ExpectedElt += 1;
2331 if (ExpectedElt == NumElts * 2) {
2332 ExpectedElt = 0;
2333 ReverseVEXT = true;
2334 }
2335
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002336 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002337 return false;
2338 }
2339
2340 // Adjust the index value if the source operands will be swapped.
2341 if (ReverseVEXT)
2342 Imm -= NumElts;
2343
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002344 return true;
2345}
2346
Bob Wilson8bb9e482009-07-26 00:39:34 +00002347/// isVREVMask - Check if a vector shuffle corresponds to a VREV
2348/// instruction with the specified blocksize. (The order of the elements
2349/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002350static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
2351 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00002352 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
2353 "Only possible block sizes for VREV are: 16, 32, 64");
2354
Bob Wilson8bb9e482009-07-26 00:39:34 +00002355 unsigned NumElts = VT.getVectorNumElements();
2356 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002357 unsigned BlockElts = M[0] + 1;
Bob Wilson8bb9e482009-07-26 00:39:34 +00002358
2359 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
2360 return false;
2361
2362 for (unsigned i = 0; i < NumElts; ++i) {
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002363 if ((unsigned) M[i] !=
Bob Wilson8bb9e482009-07-26 00:39:34 +00002364 (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
2365 return false;
2366 }
2367
2368 return true;
2369}
2370
Bob Wilsonc692cb72009-08-21 20:54:19 +00002371static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
2372 unsigned &WhichResult) {
2373 unsigned NumElts = VT.getVectorNumElements();
2374 WhichResult = (M[0] == 0 ? 0 : 1);
2375 for (unsigned i = 0; i < NumElts; i += 2) {
2376 if ((unsigned) M[i] != i + WhichResult ||
2377 (unsigned) M[i+1] != i + NumElts + WhichResult)
2378 return false;
2379 }
2380 return true;
2381}
2382
2383static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
2384 unsigned &WhichResult) {
2385 unsigned NumElts = VT.getVectorNumElements();
2386 WhichResult = (M[0] == 0 ? 0 : 1);
2387 for (unsigned i = 0; i != NumElts; ++i) {
2388 if ((unsigned) M[i] != 2 * i + WhichResult)
2389 return false;
2390 }
2391
2392 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2393 if (VT.is64BitVector() && VT.getVectorElementType().getSizeInBits() == 32)
2394 return false;
2395
2396 return true;
2397}
2398
2399static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
2400 unsigned &WhichResult) {
2401 unsigned NumElts = VT.getVectorNumElements();
2402 WhichResult = (M[0] == 0 ? 0 : 1);
2403 unsigned Idx = WhichResult * NumElts / 2;
2404 for (unsigned i = 0; i != NumElts; i += 2) {
2405 if ((unsigned) M[i] != Idx ||
2406 (unsigned) M[i+1] != Idx + NumElts)
2407 return false;
2408 Idx += 1;
2409 }
2410
2411 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2412 if (VT.is64BitVector() && VT.getVectorElementType().getSizeInBits() == 32)
2413 return false;
2414
2415 return true;
2416}
2417
Owen Andersone50ed302009-08-10 22:56:29 +00002418static SDValue BuildSplat(SDValue Val, EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002419 // Canonicalize all-zeros and all-ones vectors.
Bob Wilsond06791f2009-08-13 01:57:47 +00002420 ConstantSDNode *ConstVal = cast<ConstantSDNode>(Val.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002421 if (ConstVal->isNullValue())
2422 return getZeroVector(VT, DAG, dl);
2423 if (ConstVal->isAllOnesValue())
2424 return getOnesVector(VT, DAG, dl);
2425
Owen Andersone50ed302009-08-10 22:56:29 +00002426 EVT CanonicalVT;
Bob Wilson5bafff32009-06-22 23:27:02 +00002427 if (VT.is64BitVector()) {
2428 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002429 case 8: CanonicalVT = MVT::v8i8; break;
2430 case 16: CanonicalVT = MVT::v4i16; break;
2431 case 32: CanonicalVT = MVT::v2i32; break;
2432 case 64: CanonicalVT = MVT::v1i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002433 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002434 }
2435 } else {
2436 assert(VT.is128BitVector() && "unknown splat vector size");
2437 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002438 case 8: CanonicalVT = MVT::v16i8; break;
2439 case 16: CanonicalVT = MVT::v8i16; break;
2440 case 32: CanonicalVT = MVT::v4i32; break;
2441 case 64: CanonicalVT = MVT::v2i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002442 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002443 }
2444 }
2445
2446 // Build a canonical splat for this value.
2447 SmallVector<SDValue, 8> Ops;
2448 Ops.assign(CanonicalVT.getVectorNumElements(), Val);
2449 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, &Ops[0],
2450 Ops.size());
2451 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Res);
2452}
2453
2454// If this is a case we can't handle, return null and let the default
2455// expansion code take care of it.
2456static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bob Wilsond06791f2009-08-13 01:57:47 +00002457 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002458 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002459 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002460
2461 APInt SplatBits, SplatUndef;
2462 unsigned SplatBitSize;
2463 bool HasAnyUndefs;
2464 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00002465 if (SplatBitSize <= 64) {
2466 SDValue Val = isVMOVSplat(SplatBits.getZExtValue(),
2467 SplatUndef.getZExtValue(), SplatBitSize, DAG);
2468 if (Val.getNode())
2469 return BuildSplat(Val, VT, DAG, dl);
2470 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00002471 }
2472
2473 // If there are only 2 elements in a 128-bit vector, insert them into an
2474 // undef vector. This handles the common case for 128-bit vector argument
2475 // passing, where the insertions should be translated to subreg accesses
2476 // with no real instructions.
2477 if (VT.is128BitVector() && Op.getNumOperands() == 2) {
2478 SDValue Val = DAG.getUNDEF(VT);
2479 SDValue Op0 = Op.getOperand(0);
2480 SDValue Op1 = Op.getOperand(1);
2481 if (Op0.getOpcode() != ISD::UNDEF)
2482 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op0,
2483 DAG.getIntPtrConstant(0));
2484 if (Op1.getOpcode() != ISD::UNDEF)
2485 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op1,
2486 DAG.getIntPtrConstant(1));
2487 return Val;
Bob Wilson5bafff32009-06-22 23:27:02 +00002488 }
2489
2490 return SDValue();
2491}
2492
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002493/// isShuffleMaskLegal - Targets can use this to indicate that they only
2494/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
2495/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
2496/// are assumed to be legal.
2497bool
2498ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
2499 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002500 if (VT.getVectorNumElements() == 4 &&
2501 (VT.is128BitVector() || VT.is64BitVector())) {
2502 unsigned PFIndexes[4];
2503 for (unsigned i = 0; i != 4; ++i) {
2504 if (M[i] < 0)
2505 PFIndexes[i] = 8;
2506 else
2507 PFIndexes[i] = M[i];
2508 }
2509
2510 // Compute the index in the perfect shuffle table.
2511 unsigned PFTableIndex =
2512 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2513 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2514 unsigned Cost = (PFEntry >> 30);
2515
2516 if (Cost <= 4)
2517 return true;
2518 }
2519
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002520 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00002521 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002522
2523 return (ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
2524 isVREVMask(M, VT, 64) ||
2525 isVREVMask(M, VT, 32) ||
2526 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00002527 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
2528 isVTRNMask(M, VT, WhichResult) ||
2529 isVUZPMask(M, VT, WhichResult) ||
2530 isVZIPMask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002531}
2532
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002533/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
2534/// the specified operations to build the shuffle.
2535static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
2536 SDValue RHS, SelectionDAG &DAG,
2537 DebugLoc dl) {
2538 unsigned OpNum = (PFEntry >> 26) & 0x0F;
2539 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
2540 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
2541
2542 enum {
2543 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
2544 OP_VREV,
2545 OP_VDUP0,
2546 OP_VDUP1,
2547 OP_VDUP2,
2548 OP_VDUP3,
2549 OP_VEXT1,
2550 OP_VEXT2,
2551 OP_VEXT3,
2552 OP_VUZPL, // VUZP, left result
2553 OP_VUZPR, // VUZP, right result
2554 OP_VZIPL, // VZIP, left result
2555 OP_VZIPR, // VZIP, right result
2556 OP_VTRNL, // VTRN, left result
2557 OP_VTRNR // VTRN, right result
2558 };
2559
2560 if (OpNum == OP_COPY) {
2561 if (LHSID == (1*9+2)*9+3) return LHS;
2562 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
2563 return RHS;
2564 }
2565
2566 SDValue OpLHS, OpRHS;
2567 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
2568 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
2569 EVT VT = OpLHS.getValueType();
2570
2571 switch (OpNum) {
2572 default: llvm_unreachable("Unknown shuffle opcode!");
2573 case OP_VREV:
2574 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
2575 case OP_VDUP0:
2576 case OP_VDUP1:
2577 case OP_VDUP2:
2578 case OP_VDUP3:
2579 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002580 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002581 case OP_VEXT1:
2582 case OP_VEXT2:
2583 case OP_VEXT3:
2584 return DAG.getNode(ARMISD::VEXT, dl, VT,
2585 OpLHS, OpRHS,
2586 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
2587 case OP_VUZPL:
2588 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002589 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002590 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
2591 case OP_VZIPL:
2592 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002593 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002594 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
2595 case OP_VTRNL:
2596 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002597 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2598 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002599 }
2600}
2601
Bob Wilson5bafff32009-06-22 23:27:02 +00002602static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002603 SDValue V1 = Op.getOperand(0);
2604 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00002605 DebugLoc dl = Op.getDebugLoc();
2606 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002607 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002608 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00002609
Bob Wilson28865062009-08-13 02:13:04 +00002610 // Convert shuffles that are directly supported on NEON to target-specific
2611 // DAG nodes, instead of keeping them as shuffles and matching them again
2612 // during code selection. This is more efficient and avoids the possibility
2613 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00002614 // FIXME: floating-point vectors should be canonicalized to integer vectors
2615 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002616 SVN->getMask(ShuffleMask);
2617
2618 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
Bob Wilson0ce37102009-08-14 05:08:32 +00002619 int Lane = SVN->getSplatIndex();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002620 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
2621 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00002622 }
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002623 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002624 DAG.getConstant(Lane, MVT::i32));
Bob Wilson0ce37102009-08-14 05:08:32 +00002625 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002626
2627 bool ReverseVEXT;
2628 unsigned Imm;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002629 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002630 if (ReverseVEXT)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002631 std::swap(V1, V2);
2632 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002633 DAG.getConstant(Imm, MVT::i32));
2634 }
2635
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002636 if (isVREVMask(ShuffleMask, VT, 64))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002637 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002638 if (isVREVMask(ShuffleMask, VT, 32))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002639 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002640 if (isVREVMask(ShuffleMask, VT, 16))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002641 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
2642
Bob Wilsonc692cb72009-08-21 20:54:19 +00002643 // Check for Neon shuffles that modify both input vectors in place.
2644 // If both results are used, i.e., if there are two shuffles with the same
2645 // source operands and with masks corresponding to both results of one of
2646 // these operations, DAG memoization will ensure that a single node is
2647 // used for both shuffles.
2648 unsigned WhichResult;
2649 if (isVTRNMask(ShuffleMask, VT, WhichResult))
2650 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2651 V1, V2).getValue(WhichResult);
2652 if (isVUZPMask(ShuffleMask, VT, WhichResult))
2653 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
2654 V1, V2).getValue(WhichResult);
2655 if (isVZIPMask(ShuffleMask, VT, WhichResult))
2656 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
2657 V1, V2).getValue(WhichResult);
2658
2659 // If the shuffle is not directly supported and it has 4 elements, use
2660 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002661 if (VT.getVectorNumElements() == 4 &&
2662 (VT.is128BitVector() || VT.is64BitVector())) {
2663 unsigned PFIndexes[4];
2664 for (unsigned i = 0; i != 4; ++i) {
2665 if (ShuffleMask[i] < 0)
2666 PFIndexes[i] = 8;
2667 else
2668 PFIndexes[i] = ShuffleMask[i];
2669 }
2670
2671 // Compute the index in the perfect shuffle table.
2672 unsigned PFTableIndex =
2673 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2674
2675 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2676 unsigned Cost = (PFEntry >> 30);
2677
2678 if (Cost <= 4)
2679 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
2680 }
Bob Wilsond8e17572009-08-12 22:31:50 +00002681
Bob Wilson22cac0d2009-08-14 05:16:33 +00002682 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00002683}
2684
Bob Wilson5bafff32009-06-22 23:27:02 +00002685static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002686 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002687 DebugLoc dl = Op.getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00002688 SDValue Vec = Op.getOperand(0);
2689 SDValue Lane = Op.getOperand(1);
Anton Korobeynikovb00c03b2009-08-30 17:14:54 +00002690
2691 // FIXME: This is invalid for 8 and 16-bit elements - the information about
2692 // sign / zero extension is lost!
Owen Anderson825b72b2009-08-11 20:47:22 +00002693 Op = DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
2694 Op = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Op, DAG.getValueType(VT));
Anton Korobeynikovb00c03b2009-08-30 17:14:54 +00002695
2696 if (VT.bitsLT(MVT::i32))
2697 Op = DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
2698 else if (VT.bitsGT(MVT::i32))
2699 Op = DAG.getNode(ISD::ANY_EXTEND, dl, VT, Op);
2700
2701 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00002702}
2703
Bob Wilsona6d65862009-08-03 20:36:38 +00002704static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
2705 // The only time a CONCAT_VECTORS operation can have legal types is when
2706 // two 64-bit vectors are concatenated to a 128-bit vector.
2707 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
2708 "unexpected CONCAT_VECTORS");
2709 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00002710 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00002711 SDValue Op0 = Op.getOperand(0);
2712 SDValue Op1 = Op.getOperand(1);
2713 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00002714 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
2715 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00002716 DAG.getIntPtrConstant(0));
2717 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00002718 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
2719 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00002720 DAG.getIntPtrConstant(1));
2721 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00002722}
2723
Dan Gohman475871a2008-07-27 21:46:04 +00002724SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00002725 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002726 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00002727 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002728 case ISD::GlobalAddress:
2729 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
2730 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002731 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002732 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, Subtarget);
2733 case ISD::BR_CC: return LowerBR_CC(Op, DAG, Subtarget);
2734 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Evan Cheng86198642009-08-07 00:34:42 +00002735 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002736 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
2737 case ISD::SINT_TO_FP:
2738 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
2739 case ISD::FP_TO_SINT:
2740 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
2741 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00002742 case ISD::RETURNADDR: break;
Jim Grosbach0e0da732009-05-12 23:59:14 +00002743 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002744 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Bob Wilsonb36ec862009-08-06 18:47:44 +00002745 case ISD::INTRINSIC_VOID:
Bob Wilsona599bff2009-08-04 00:36:16 +00002746 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002747 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00002748 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00002749 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00002750 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00002751 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
2752 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
2753 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2754 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00002755 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00002756 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002757 }
Dan Gohman475871a2008-07-27 21:46:04 +00002758 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00002759}
2760
Duncan Sands1607f052008-12-01 11:39:25 +00002761/// ReplaceNodeResults - Replace the results of node with an illegal result
2762/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00002763void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
2764 SmallVectorImpl<SDValue>&Results,
2765 SelectionDAG &DAG) {
Chris Lattner27a6c732007-11-24 07:07:01 +00002766 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00002767 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002768 llvm_unreachable("Don't know how to custom expand this!");
Duncan Sands1607f052008-12-01 11:39:25 +00002769 return;
2770 case ISD::BIT_CONVERT:
2771 Results.push_back(ExpandBIT_CONVERT(N, DAG));
2772 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00002773 case ISD::SRL:
Duncan Sands1607f052008-12-01 11:39:25 +00002774 case ISD::SRA: {
Bob Wilson5bafff32009-06-22 23:27:02 +00002775 SDValue Res = LowerShift(N, DAG, Subtarget);
Duncan Sands1607f052008-12-01 11:39:25 +00002776 if (Res.getNode())
2777 Results.push_back(Res);
2778 return;
2779 }
Chris Lattner27a6c732007-11-24 07:07:01 +00002780 }
2781}
Chris Lattner27a6c732007-11-24 07:07:01 +00002782
Evan Chenga8e29892007-01-19 07:51:42 +00002783//===----------------------------------------------------------------------===//
2784// ARM Scheduler Hooks
2785//===----------------------------------------------------------------------===//
2786
2787MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00002788ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00002789 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002790 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00002791 DebugLoc dl = MI->getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002792 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00002793 default:
2794 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng007ea272009-08-12 05:17:19 +00002795 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00002796 // To "insert" a SELECT_CC instruction, we actually have to insert the
2797 // diamond control-flow pattern. The incoming instruction knows the
2798 // destination vreg to set, the condition code register to branch on, the
2799 // true/false values to select between, and a branch opcode to use.
2800 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002801 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00002802 ++It;
2803
2804 // thisMBB:
2805 // ...
2806 // TrueVal = ...
2807 // cmpTY ccX, r1, r2
2808 // bCC copy1MBB
2809 // fallthrough --> copy0MBB
2810 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002811 MachineFunction *F = BB->getParent();
2812 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
2813 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenb6728402009-02-13 02:25:56 +00002814 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00002815 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002816 F->insert(It, copy0MBB);
2817 F->insert(It, sinkMBB);
Evan Chenga8e29892007-01-19 07:51:42 +00002818 // Update machine-CFG edges by first adding all successors of the current
2819 // block to the new block which will contain the Phi node for the select.
2820 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
2821 e = BB->succ_end(); i != e; ++i)
2822 sinkMBB->addSuccessor(*i);
2823 // Next, remove all successors of the current block, and add the true
2824 // and fallthrough blocks as its successors.
2825 while(!BB->succ_empty())
2826 BB->removeSuccessor(BB->succ_begin());
2827 BB->addSuccessor(copy0MBB);
2828 BB->addSuccessor(sinkMBB);
2829
2830 // copy0MBB:
2831 // %FalseValue = ...
2832 // # fallthrough to sinkMBB
2833 BB = copy0MBB;
2834
2835 // Update machine-CFG edges
2836 BB->addSuccessor(sinkMBB);
2837
2838 // sinkMBB:
2839 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2840 // ...
2841 BB = sinkMBB;
Dale Johannesenb6728402009-02-13 02:25:56 +00002842 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00002843 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
2844 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
2845
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002846 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00002847 return BB;
2848 }
Evan Cheng86198642009-08-07 00:34:42 +00002849
2850 case ARM::tANDsp:
2851 case ARM::tADDspr_:
2852 case ARM::tSUBspi_:
2853 case ARM::t2SUBrSPi_:
2854 case ARM::t2SUBrSPi12_:
2855 case ARM::t2SUBrSPs_: {
2856 MachineFunction *MF = BB->getParent();
2857 unsigned DstReg = MI->getOperand(0).getReg();
2858 unsigned SrcReg = MI->getOperand(1).getReg();
2859 bool DstIsDead = MI->getOperand(0).isDead();
2860 bool SrcIsKill = MI->getOperand(1).isKill();
2861
2862 if (SrcReg != ARM::SP) {
2863 // Copy the source to SP from virtual register.
2864 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(SrcReg);
2865 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
2866 ? ARM::tMOVtgpr2gpr : ARM::tMOVgpr2gpr;
2867 BuildMI(BB, dl, TII->get(CopyOpc), ARM::SP)
2868 .addReg(SrcReg, getKillRegState(SrcIsKill));
2869 }
2870
2871 unsigned OpOpc = 0;
2872 bool NeedPred = false, NeedCC = false, NeedOp3 = false;
2873 switch (MI->getOpcode()) {
2874 default:
2875 llvm_unreachable("Unexpected pseudo instruction!");
2876 case ARM::tANDsp:
2877 OpOpc = ARM::tAND;
2878 NeedPred = true;
2879 break;
2880 case ARM::tADDspr_:
2881 OpOpc = ARM::tADDspr;
2882 break;
2883 case ARM::tSUBspi_:
2884 OpOpc = ARM::tSUBspi;
2885 break;
2886 case ARM::t2SUBrSPi_:
2887 OpOpc = ARM::t2SUBrSPi;
2888 NeedPred = true; NeedCC = true;
2889 break;
2890 case ARM::t2SUBrSPi12_:
2891 OpOpc = ARM::t2SUBrSPi12;
2892 NeedPred = true;
2893 break;
2894 case ARM::t2SUBrSPs_:
2895 OpOpc = ARM::t2SUBrSPs;
2896 NeedPred = true; NeedCC = true; NeedOp3 = true;
2897 break;
2898 }
2899 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(OpOpc), ARM::SP);
2900 if (OpOpc == ARM::tAND)
2901 AddDefaultT1CC(MIB);
2902 MIB.addReg(ARM::SP);
2903 MIB.addOperand(MI->getOperand(2));
2904 if (NeedOp3)
2905 MIB.addOperand(MI->getOperand(3));
2906 if (NeedPred)
2907 AddDefaultPred(MIB);
2908 if (NeedCC)
2909 AddDefaultCC(MIB);
2910
2911 // Copy the result from SP to virtual register.
2912 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(DstReg);
2913 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
2914 ? ARM::tMOVgpr2tgpr : ARM::tMOVgpr2gpr;
2915 BuildMI(BB, dl, TII->get(CopyOpc))
2916 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
2917 .addReg(ARM::SP);
2918 MF->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
2919 return BB;
2920 }
Evan Chenga8e29892007-01-19 07:51:42 +00002921 }
2922}
2923
2924//===----------------------------------------------------------------------===//
2925// ARM Optimization Hooks
2926//===----------------------------------------------------------------------===//
2927
Chris Lattnerd1980a52009-03-12 06:52:53 +00002928static
2929SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
2930 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00002931 SelectionDAG &DAG = DCI.DAG;
2932 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00002933 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00002934 unsigned Opc = N->getOpcode();
2935 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
2936 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
2937 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
2938 ISD::CondCode CC = ISD::SETCC_INVALID;
2939
2940 if (isSlctCC) {
2941 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
2942 } else {
2943 SDValue CCOp = Slct.getOperand(0);
2944 if (CCOp.getOpcode() == ISD::SETCC)
2945 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
2946 }
2947
2948 bool DoXform = false;
2949 bool InvCC = false;
2950 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
2951 "Bad input!");
2952
2953 if (LHS.getOpcode() == ISD::Constant &&
2954 cast<ConstantSDNode>(LHS)->isNullValue()) {
2955 DoXform = true;
2956 } else if (CC != ISD::SETCC_INVALID &&
2957 RHS.getOpcode() == ISD::Constant &&
2958 cast<ConstantSDNode>(RHS)->isNullValue()) {
2959 std::swap(LHS, RHS);
2960 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00002961 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00002962 Op0.getOperand(0).getValueType();
2963 bool isInt = OpVT.isInteger();
2964 CC = ISD::getSetCCInverse(CC, isInt);
2965
2966 if (!TLI.isCondCodeLegal(CC, OpVT))
2967 return SDValue(); // Inverse operator isn't legal.
2968
2969 DoXform = true;
2970 InvCC = true;
2971 }
2972
2973 if (DoXform) {
2974 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
2975 if (isSlctCC)
2976 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
2977 Slct.getOperand(0), Slct.getOperand(1), CC);
2978 SDValue CCOp = Slct.getOperand(0);
2979 if (InvCC)
2980 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
2981 CCOp.getOperand(0), CCOp.getOperand(1), CC);
2982 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
2983 CCOp, OtherOp, Result);
2984 }
2985 return SDValue();
2986}
2987
2988/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
2989static SDValue PerformADDCombine(SDNode *N,
2990 TargetLowering::DAGCombinerInfo &DCI) {
2991 // added by evan in r37685 with no testcase.
2992 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002993
Chris Lattnerd1980a52009-03-12 06:52:53 +00002994 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
2995 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
2996 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
2997 if (Result.getNode()) return Result;
2998 }
2999 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3000 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3001 if (Result.getNode()) return Result;
3002 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003003
Chris Lattnerd1980a52009-03-12 06:52:53 +00003004 return SDValue();
3005}
3006
3007/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
3008static SDValue PerformSUBCombine(SDNode *N,
3009 TargetLowering::DAGCombinerInfo &DCI) {
3010 // added by evan in r37685 with no testcase.
3011 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003012
Chris Lattnerd1980a52009-03-12 06:52:53 +00003013 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
3014 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3015 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3016 if (Result.getNode()) return Result;
3017 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003018
Chris Lattnerd1980a52009-03-12 06:52:53 +00003019 return SDValue();
3020}
3021
3022
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003023/// PerformFMRRDCombine - Target-specific dag combine xforms for ARMISD::FMRRD.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003024static SDValue PerformFMRRDCombine(SDNode *N,
3025 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003026 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00003027 SDValue InDouble = N->getOperand(0);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003028 if (InDouble.getOpcode() == ARMISD::FMDRR)
3029 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003030 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003031}
3032
Bob Wilson5bafff32009-06-22 23:27:02 +00003033/// getVShiftImm - Check if this is a valid build_vector for the immediate
3034/// operand of a vector shift operation, where all the elements of the
3035/// build_vector must have the same constant integer value.
3036static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
3037 // Ignore bit_converts.
3038 while (Op.getOpcode() == ISD::BIT_CONVERT)
3039 Op = Op.getOperand(0);
3040 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
3041 APInt SplatBits, SplatUndef;
3042 unsigned SplatBitSize;
3043 bool HasAnyUndefs;
3044 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
3045 HasAnyUndefs, ElementBits) ||
3046 SplatBitSize > ElementBits)
3047 return false;
3048 Cnt = SplatBits.getSExtValue();
3049 return true;
3050}
3051
3052/// isVShiftLImm - Check if this is a valid build_vector for the immediate
3053/// operand of a vector shift left operation. That value must be in the range:
3054/// 0 <= Value < ElementBits for a left shift; or
3055/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003056static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003057 assert(VT.isVector() && "vector shift count is not a vector type");
3058 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3059 if (! getVShiftImm(Op, ElementBits, Cnt))
3060 return false;
3061 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
3062}
3063
3064/// isVShiftRImm - Check if this is a valid build_vector for the immediate
3065/// operand of a vector shift right operation. For a shift opcode, the value
3066/// is positive, but for an intrinsic the value count must be negative. The
3067/// absolute value must be in the range:
3068/// 1 <= |Value| <= ElementBits for a right shift; or
3069/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003070static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00003071 int64_t &Cnt) {
3072 assert(VT.isVector() && "vector shift count is not a vector type");
3073 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3074 if (! getVShiftImm(Op, ElementBits, Cnt))
3075 return false;
3076 if (isIntrinsic)
3077 Cnt = -Cnt;
3078 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
3079}
3080
3081/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
3082static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
3083 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
3084 switch (IntNo) {
3085 default:
3086 // Don't do anything for most intrinsics.
3087 break;
3088
3089 // Vector shifts: check for immediate versions and lower them.
3090 // Note: This is done during DAG combining instead of DAG legalizing because
3091 // the build_vectors for 64-bit vector element shift counts are generally
3092 // not legal, and it is hard to see their values after they get legalized to
3093 // loads from a constant pool.
3094 case Intrinsic::arm_neon_vshifts:
3095 case Intrinsic::arm_neon_vshiftu:
3096 case Intrinsic::arm_neon_vshiftls:
3097 case Intrinsic::arm_neon_vshiftlu:
3098 case Intrinsic::arm_neon_vshiftn:
3099 case Intrinsic::arm_neon_vrshifts:
3100 case Intrinsic::arm_neon_vrshiftu:
3101 case Intrinsic::arm_neon_vrshiftn:
3102 case Intrinsic::arm_neon_vqshifts:
3103 case Intrinsic::arm_neon_vqshiftu:
3104 case Intrinsic::arm_neon_vqshiftsu:
3105 case Intrinsic::arm_neon_vqshiftns:
3106 case Intrinsic::arm_neon_vqshiftnu:
3107 case Intrinsic::arm_neon_vqshiftnsu:
3108 case Intrinsic::arm_neon_vqrshiftns:
3109 case Intrinsic::arm_neon_vqrshiftnu:
3110 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00003111 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003112 int64_t Cnt;
3113 unsigned VShiftOpc = 0;
3114
3115 switch (IntNo) {
3116 case Intrinsic::arm_neon_vshifts:
3117 case Intrinsic::arm_neon_vshiftu:
3118 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
3119 VShiftOpc = ARMISD::VSHL;
3120 break;
3121 }
3122 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
3123 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
3124 ARMISD::VSHRs : ARMISD::VSHRu);
3125 break;
3126 }
3127 return SDValue();
3128
3129 case Intrinsic::arm_neon_vshiftls:
3130 case Intrinsic::arm_neon_vshiftlu:
3131 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
3132 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003133 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003134
3135 case Intrinsic::arm_neon_vrshifts:
3136 case Intrinsic::arm_neon_vrshiftu:
3137 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
3138 break;
3139 return SDValue();
3140
3141 case Intrinsic::arm_neon_vqshifts:
3142 case Intrinsic::arm_neon_vqshiftu:
3143 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3144 break;
3145 return SDValue();
3146
3147 case Intrinsic::arm_neon_vqshiftsu:
3148 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3149 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003150 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003151
3152 case Intrinsic::arm_neon_vshiftn:
3153 case Intrinsic::arm_neon_vrshiftn:
3154 case Intrinsic::arm_neon_vqshiftns:
3155 case Intrinsic::arm_neon_vqshiftnu:
3156 case Intrinsic::arm_neon_vqshiftnsu:
3157 case Intrinsic::arm_neon_vqrshiftns:
3158 case Intrinsic::arm_neon_vqrshiftnu:
3159 case Intrinsic::arm_neon_vqrshiftnsu:
3160 // Narrowing shifts require an immediate right shift.
3161 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
3162 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003163 llvm_unreachable("invalid shift count for narrowing vector shift intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003164
3165 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003166 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003167 }
3168
3169 switch (IntNo) {
3170 case Intrinsic::arm_neon_vshifts:
3171 case Intrinsic::arm_neon_vshiftu:
3172 // Opcode already set above.
3173 break;
3174 case Intrinsic::arm_neon_vshiftls:
3175 case Intrinsic::arm_neon_vshiftlu:
3176 if (Cnt == VT.getVectorElementType().getSizeInBits())
3177 VShiftOpc = ARMISD::VSHLLi;
3178 else
3179 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
3180 ARMISD::VSHLLs : ARMISD::VSHLLu);
3181 break;
3182 case Intrinsic::arm_neon_vshiftn:
3183 VShiftOpc = ARMISD::VSHRN; break;
3184 case Intrinsic::arm_neon_vrshifts:
3185 VShiftOpc = ARMISD::VRSHRs; break;
3186 case Intrinsic::arm_neon_vrshiftu:
3187 VShiftOpc = ARMISD::VRSHRu; break;
3188 case Intrinsic::arm_neon_vrshiftn:
3189 VShiftOpc = ARMISD::VRSHRN; break;
3190 case Intrinsic::arm_neon_vqshifts:
3191 VShiftOpc = ARMISD::VQSHLs; break;
3192 case Intrinsic::arm_neon_vqshiftu:
3193 VShiftOpc = ARMISD::VQSHLu; break;
3194 case Intrinsic::arm_neon_vqshiftsu:
3195 VShiftOpc = ARMISD::VQSHLsu; break;
3196 case Intrinsic::arm_neon_vqshiftns:
3197 VShiftOpc = ARMISD::VQSHRNs; break;
3198 case Intrinsic::arm_neon_vqshiftnu:
3199 VShiftOpc = ARMISD::VQSHRNu; break;
3200 case Intrinsic::arm_neon_vqshiftnsu:
3201 VShiftOpc = ARMISD::VQSHRNsu; break;
3202 case Intrinsic::arm_neon_vqrshiftns:
3203 VShiftOpc = ARMISD::VQRSHRNs; break;
3204 case Intrinsic::arm_neon_vqrshiftnu:
3205 VShiftOpc = ARMISD::VQRSHRNu; break;
3206 case Intrinsic::arm_neon_vqrshiftnsu:
3207 VShiftOpc = ARMISD::VQRSHRNsu; break;
3208 }
3209
3210 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003211 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003212 }
3213
3214 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00003215 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003216 int64_t Cnt;
3217 unsigned VShiftOpc = 0;
3218
3219 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
3220 VShiftOpc = ARMISD::VSLI;
3221 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
3222 VShiftOpc = ARMISD::VSRI;
3223 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00003224 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003225 }
3226
3227 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
3228 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00003229 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003230 }
3231
3232 case Intrinsic::arm_neon_vqrshifts:
3233 case Intrinsic::arm_neon_vqrshiftu:
3234 // No immediate versions of these to check for.
3235 break;
3236 }
3237
3238 return SDValue();
3239}
3240
3241/// PerformShiftCombine - Checks for immediate versions of vector shifts and
3242/// lowers them. As with the vector shift intrinsics, this is done during DAG
3243/// combining instead of DAG legalizing because the build_vectors for 64-bit
3244/// vector element shift counts are generally not legal, and it is hard to see
3245/// their values after they get legalized to loads from a constant pool.
3246static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
3247 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003248 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003249
3250 // Nothing to be done for scalar shifts.
3251 if (! VT.isVector())
3252 return SDValue();
3253
3254 assert(ST->hasNEON() && "unexpected vector shift");
3255 int64_t Cnt;
3256
3257 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003258 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003259
3260 case ISD::SHL:
3261 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
3262 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003263 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003264 break;
3265
3266 case ISD::SRA:
3267 case ISD::SRL:
3268 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
3269 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
3270 ARMISD::VSHRs : ARMISD::VSHRu);
3271 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003272 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003273 }
3274 }
3275 return SDValue();
3276}
3277
3278/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
3279/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
3280static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
3281 const ARMSubtarget *ST) {
3282 SDValue N0 = N->getOperand(0);
3283
3284 // Check for sign- and zero-extensions of vector extract operations of 8-
3285 // and 16-bit vector elements. NEON supports these directly. They are
3286 // handled during DAG combining because type legalization will promote them
3287 // to 32-bit types and it is messy to recognize the operations after that.
3288 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
3289 SDValue Vec = N0.getOperand(0);
3290 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00003291 EVT VT = N->getValueType(0);
3292 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003293 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3294
Owen Anderson825b72b2009-08-11 20:47:22 +00003295 if (VT == MVT::i32 &&
3296 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00003297 TLI.isTypeLegal(Vec.getValueType())) {
3298
3299 unsigned Opc = 0;
3300 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003301 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003302 case ISD::SIGN_EXTEND:
3303 Opc = ARMISD::VGETLANEs;
3304 break;
3305 case ISD::ZERO_EXTEND:
3306 case ISD::ANY_EXTEND:
3307 Opc = ARMISD::VGETLANEu;
3308 break;
3309 }
3310 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
3311 }
3312 }
3313
3314 return SDValue();
3315}
3316
Dan Gohman475871a2008-07-27 21:46:04 +00003317SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003318 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003319 switch (N->getOpcode()) {
3320 default: break;
Chris Lattnerd1980a52009-03-12 06:52:53 +00003321 case ISD::ADD: return PerformADDCombine(N, DCI);
3322 case ISD::SUB: return PerformSUBCombine(N, DCI);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003323 case ARMISD::FMRRD: return PerformFMRRDCombine(N, DCI);
Bob Wilson5bafff32009-06-22 23:27:02 +00003324 case ISD::INTRINSIC_WO_CHAIN:
3325 return PerformIntrinsicCombine(N, DCI.DAG);
3326 case ISD::SHL:
3327 case ISD::SRA:
3328 case ISD::SRL:
3329 return PerformShiftCombine(N, DCI.DAG, Subtarget);
3330 case ISD::SIGN_EXTEND:
3331 case ISD::ZERO_EXTEND:
3332 case ISD::ANY_EXTEND:
3333 return PerformExtendCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003334 }
Dan Gohman475871a2008-07-27 21:46:04 +00003335 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003336}
3337
Bill Wendlingaf566342009-08-15 21:21:19 +00003338bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
3339 if (!Subtarget->hasV6Ops())
3340 // Pre-v6 does not support unaligned mem access.
3341 return false;
3342 else if (!Subtarget->hasV6Ops()) {
3343 // v6 may or may not support unaligned mem access.
3344 if (!Subtarget->isTargetDarwin())
3345 return false;
3346 }
3347
3348 switch (VT.getSimpleVT().SimpleTy) {
3349 default:
3350 return false;
3351 case MVT::i8:
3352 case MVT::i16:
3353 case MVT::i32:
3354 return true;
3355 // FIXME: VLD1 etc with standard alignment is legal.
3356 }
3357}
3358
Evan Chenge6c835f2009-08-14 20:09:37 +00003359static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
3360 if (V < 0)
3361 return false;
3362
3363 unsigned Scale = 1;
3364 switch (VT.getSimpleVT().SimpleTy) {
3365 default: return false;
3366 case MVT::i1:
3367 case MVT::i8:
3368 // Scale == 1;
3369 break;
3370 case MVT::i16:
3371 // Scale == 2;
3372 Scale = 2;
3373 break;
3374 case MVT::i32:
3375 // Scale == 4;
3376 Scale = 4;
3377 break;
3378 }
3379
3380 if ((V & (Scale - 1)) != 0)
3381 return false;
3382 V /= Scale;
3383 return V == (V & ((1LL << 5) - 1));
3384}
3385
3386static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
3387 const ARMSubtarget *Subtarget) {
3388 bool isNeg = false;
3389 if (V < 0) {
3390 isNeg = true;
3391 V = - V;
3392 }
3393
3394 switch (VT.getSimpleVT().SimpleTy) {
3395 default: return false;
3396 case MVT::i1:
3397 case MVT::i8:
3398 case MVT::i16:
3399 case MVT::i32:
3400 // + imm12 or - imm8
3401 if (isNeg)
3402 return V == (V & ((1LL << 8) - 1));
3403 return V == (V & ((1LL << 12) - 1));
3404 case MVT::f32:
3405 case MVT::f64:
3406 // Same as ARM mode. FIXME: NEON?
3407 if (!Subtarget->hasVFP2())
3408 return false;
3409 if ((V & 3) != 0)
3410 return false;
3411 V >>= 2;
3412 return V == (V & ((1LL << 8) - 1));
3413 }
3414}
3415
Evan Chengb01fad62007-03-12 23:30:29 +00003416/// isLegalAddressImmediate - Return true if the integer value can be used
3417/// as the offset of the target addressing mode for load / store of the
3418/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00003419static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00003420 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00003421 if (V == 0)
3422 return true;
3423
Evan Cheng65011532009-03-09 19:15:00 +00003424 if (!VT.isSimple())
3425 return false;
3426
Evan Chenge6c835f2009-08-14 20:09:37 +00003427 if (Subtarget->isThumb1Only())
3428 return isLegalT1AddressImmediate(V, VT);
3429 else if (Subtarget->isThumb2())
3430 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00003431
Evan Chenge6c835f2009-08-14 20:09:37 +00003432 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00003433 if (V < 0)
3434 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00003435 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00003436 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00003437 case MVT::i1:
3438 case MVT::i8:
3439 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00003440 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003441 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003442 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00003443 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003444 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003445 case MVT::f32:
3446 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00003447 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00003448 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00003449 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00003450 return false;
3451 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003452 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00003453 }
Evan Chenga8e29892007-01-19 07:51:42 +00003454}
3455
Evan Chenge6c835f2009-08-14 20:09:37 +00003456bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
3457 EVT VT) const {
3458 int Scale = AM.Scale;
3459 if (Scale < 0)
3460 return false;
3461
3462 switch (VT.getSimpleVT().SimpleTy) {
3463 default: return false;
3464 case MVT::i1:
3465 case MVT::i8:
3466 case MVT::i16:
3467 case MVT::i32:
3468 if (Scale == 1)
3469 return true;
3470 // r + r << imm
3471 Scale = Scale & ~1;
3472 return Scale == 2 || Scale == 4 || Scale == 8;
3473 case MVT::i64:
3474 // r + r
3475 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
3476 return true;
3477 return false;
3478 case MVT::isVoid:
3479 // Note, we allow "void" uses (basically, uses that aren't loads or
3480 // stores), because arm allows folding a scale into many arithmetic
3481 // operations. This should be made more precise and revisited later.
3482
3483 // Allow r << imm, but the imm has to be a multiple of two.
3484 if (Scale & 1) return false;
3485 return isPowerOf2_32(Scale);
3486 }
3487}
3488
Chris Lattner37caf8c2007-04-09 23:33:39 +00003489/// isLegalAddressingMode - Return true if the addressing mode represented
3490/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003491bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00003492 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00003493 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00003494 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00003495 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003496
Chris Lattner37caf8c2007-04-09 23:33:39 +00003497 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003498 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003499 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003500
Chris Lattner37caf8c2007-04-09 23:33:39 +00003501 switch (AM.Scale) {
3502 case 0: // no scale reg, must be "r+i" or "r", or "i".
3503 break;
3504 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00003505 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00003506 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00003507 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00003508 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00003509 // ARM doesn't support any R+R*scale+imm addr modes.
3510 if (AM.BaseOffs)
3511 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003512
Bob Wilson2c7dab12009-04-08 17:55:28 +00003513 if (!VT.isSimple())
3514 return false;
3515
Evan Chenge6c835f2009-08-14 20:09:37 +00003516 if (Subtarget->isThumb2())
3517 return isLegalT2ScaledAddressingMode(AM, VT);
3518
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003519 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00003520 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00003521 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00003522 case MVT::i1:
3523 case MVT::i8:
3524 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003525 if (Scale < 0) Scale = -Scale;
3526 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003527 return true;
3528 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00003529 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003530 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00003531 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00003532 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003533 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003534 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00003535 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003536
Owen Anderson825b72b2009-08-11 20:47:22 +00003537 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00003538 // Note, we allow "void" uses (basically, uses that aren't loads or
3539 // stores), because arm allows folding a scale into many arithmetic
3540 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003541
Chris Lattner37caf8c2007-04-09 23:33:39 +00003542 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00003543 if (Scale & 1) return false;
3544 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00003545 }
3546 break;
Evan Chengb01fad62007-03-12 23:30:29 +00003547 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00003548 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00003549}
3550
Owen Andersone50ed302009-08-10 22:56:29 +00003551static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00003552 bool isSEXTLoad, SDValue &Base,
3553 SDValue &Offset, bool &isInc,
3554 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00003555 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
3556 return false;
3557
Owen Anderson825b72b2009-08-11 20:47:22 +00003558 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00003559 // AddressingMode 3
3560 Base = Ptr->getOperand(0);
3561 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003562 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003563 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003564 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00003565 isInc = false;
3566 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3567 return true;
3568 }
3569 }
3570 isInc = (Ptr->getOpcode() == ISD::ADD);
3571 Offset = Ptr->getOperand(1);
3572 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00003573 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00003574 // AddressingMode 2
3575 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003576 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003577 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003578 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00003579 isInc = false;
3580 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3581 Base = Ptr->getOperand(0);
3582 return true;
3583 }
3584 }
3585
3586 if (Ptr->getOpcode() == ISD::ADD) {
3587 isInc = true;
3588 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
3589 if (ShOpcVal != ARM_AM::no_shift) {
3590 Base = Ptr->getOperand(1);
3591 Offset = Ptr->getOperand(0);
3592 } else {
3593 Base = Ptr->getOperand(0);
3594 Offset = Ptr->getOperand(1);
3595 }
3596 return true;
3597 }
3598
3599 isInc = (Ptr->getOpcode() == ISD::ADD);
3600 Base = Ptr->getOperand(0);
3601 Offset = Ptr->getOperand(1);
3602 return true;
3603 }
3604
3605 // FIXME: Use FLDM / FSTM to emulate indexed FP load / store.
3606 return false;
3607}
3608
Owen Andersone50ed302009-08-10 22:56:29 +00003609static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00003610 bool isSEXTLoad, SDValue &Base,
3611 SDValue &Offset, bool &isInc,
3612 SelectionDAG &DAG) {
3613 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
3614 return false;
3615
3616 Base = Ptr->getOperand(0);
3617 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
3618 int RHSC = (int)RHS->getZExtValue();
3619 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
3620 assert(Ptr->getOpcode() == ISD::ADD);
3621 isInc = false;
3622 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3623 return true;
3624 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
3625 isInc = Ptr->getOpcode() == ISD::ADD;
3626 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
3627 return true;
3628 }
3629 }
3630
3631 return false;
3632}
3633
Evan Chenga8e29892007-01-19 07:51:42 +00003634/// getPreIndexedAddressParts - returns true by value, base pointer and
3635/// offset pointer and addressing mode by reference if the node's address
3636/// can be legally represented as pre-indexed load / store address.
3637bool
Dan Gohman475871a2008-07-27 21:46:04 +00003638ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
3639 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003640 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00003641 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003642 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00003643 return false;
3644
Owen Andersone50ed302009-08-10 22:56:29 +00003645 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00003646 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00003647 bool isSEXTLoad = false;
3648 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
3649 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003650 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003651 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
3652 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
3653 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003654 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003655 } else
3656 return false;
3657
3658 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00003659 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00003660 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00003661 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
3662 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00003663 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00003664 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00003665 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00003666 if (!isLegal)
3667 return false;
3668
3669 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
3670 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00003671}
3672
3673/// getPostIndexedAddressParts - returns true by value, base pointer and
3674/// offset pointer and addressing mode by reference if this node can be
3675/// combined with a load / store to form a post-indexed load / store.
3676bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00003677 SDValue &Base,
3678 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003679 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00003680 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003681 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00003682 return false;
3683
Owen Andersone50ed302009-08-10 22:56:29 +00003684 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00003685 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00003686 bool isSEXTLoad = false;
3687 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003688 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003689 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
3690 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003691 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003692 } else
3693 return false;
3694
3695 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00003696 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00003697 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00003698 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003699 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00003700 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00003701 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
3702 isInc, DAG);
3703 if (!isLegal)
3704 return false;
3705
3706 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
3707 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00003708}
3709
Dan Gohman475871a2008-07-27 21:46:04 +00003710void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00003711 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003712 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003713 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00003714 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00003715 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003716 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00003717 switch (Op.getOpcode()) {
3718 default: break;
3719 case ARMISD::CMOV: {
3720 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00003721 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00003722 if (KnownZero == 0 && KnownOne == 0) return;
3723
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003724 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00003725 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
3726 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00003727 KnownZero &= KnownZeroRHS;
3728 KnownOne &= KnownOneRHS;
3729 return;
3730 }
3731 }
3732}
3733
3734//===----------------------------------------------------------------------===//
3735// ARM Inline Assembly Support
3736//===----------------------------------------------------------------------===//
3737
3738/// getConstraintType - Given a constraint letter, return the type of
3739/// constraint it is for this target.
3740ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00003741ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
3742 if (Constraint.size() == 1) {
3743 switch (Constraint[0]) {
3744 default: break;
3745 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003746 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00003747 }
Evan Chenga8e29892007-01-19 07:51:42 +00003748 }
Chris Lattner4234f572007-03-25 02:14:49 +00003749 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00003750}
3751
Bob Wilson2dc4f542009-03-20 22:42:55 +00003752std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00003753ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00003754 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003755 if (Constraint.size() == 1) {
3756 // GCC RS6000 Constraint Letters
3757 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003758 case 'l':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003759 if (Subtarget->isThumb1Only())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00003760 return std::make_pair(0U, ARM::tGPRRegisterClass);
3761 else
3762 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003763 case 'r':
3764 return std::make_pair(0U, ARM::GPRRegisterClass);
3765 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00003766 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003767 return std::make_pair(0U, ARM::SPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00003768 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003769 return std::make_pair(0U, ARM::DPRRegisterClass);
3770 break;
Evan Chenga8e29892007-01-19 07:51:42 +00003771 }
3772 }
3773 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3774}
3775
3776std::vector<unsigned> ARMTargetLowering::
3777getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00003778 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003779 if (Constraint.size() != 1)
3780 return std::vector<unsigned>();
3781
3782 switch (Constraint[0]) { // GCC ARM Constraint Letters
3783 default: break;
3784 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00003785 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
3786 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
3787 0);
Evan Chenga8e29892007-01-19 07:51:42 +00003788 case 'r':
3789 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
3790 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
3791 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
3792 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003793 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00003794 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003795 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
3796 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
3797 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
3798 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
3799 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
3800 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
3801 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
3802 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00003803 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003804 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
3805 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
3806 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
3807 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
3808 break;
Evan Chenga8e29892007-01-19 07:51:42 +00003809 }
3810
3811 return std::vector<unsigned>();
3812}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003813
3814/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3815/// vector. If it is invalid, don't add anything to Ops.
3816void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3817 char Constraint,
3818 bool hasMemory,
3819 std::vector<SDValue>&Ops,
3820 SelectionDAG &DAG) const {
3821 SDValue Result(0, 0);
3822
3823 switch (Constraint) {
3824 default: break;
3825 case 'I': case 'J': case 'K': case 'L':
3826 case 'M': case 'N': case 'O':
3827 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
3828 if (!C)
3829 return;
3830
3831 int64_t CVal64 = C->getSExtValue();
3832 int CVal = (int) CVal64;
3833 // None of these constraints allow values larger than 32 bits. Check
3834 // that the value fits in an int.
3835 if (CVal != CVal64)
3836 return;
3837
3838 switch (Constraint) {
3839 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003840 if (Subtarget->isThumb1Only()) {
3841 // This must be a constant between 0 and 255, for ADD
3842 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003843 if (CVal >= 0 && CVal <= 255)
3844 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00003845 } else if (Subtarget->isThumb2()) {
3846 // A constant that can be used as an immediate value in a
3847 // data-processing instruction.
3848 if (ARM_AM::getT2SOImmVal(CVal) != -1)
3849 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003850 } else {
3851 // A constant that can be used as an immediate value in a
3852 // data-processing instruction.
3853 if (ARM_AM::getSOImmVal(CVal) != -1)
3854 break;
3855 }
3856 return;
3857
3858 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003859 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003860 // This must be a constant between -255 and -1, for negated ADD
3861 // immediates. This can be used in GCC with an "n" modifier that
3862 // prints the negated value, for use with SUB instructions. It is
3863 // not useful otherwise but is implemented for compatibility.
3864 if (CVal >= -255 && CVal <= -1)
3865 break;
3866 } else {
3867 // This must be a constant between -4095 and 4095. It is not clear
3868 // what this constraint is intended for. Implemented for
3869 // compatibility with GCC.
3870 if (CVal >= -4095 && CVal <= 4095)
3871 break;
3872 }
3873 return;
3874
3875 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003876 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003877 // A 32-bit value where only one byte has a nonzero value. Exclude
3878 // zero to match GCC. This constraint is used by GCC internally for
3879 // constants that can be loaded with a move/shift combination.
3880 // It is not useful otherwise but is implemented for compatibility.
3881 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
3882 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00003883 } else if (Subtarget->isThumb2()) {
3884 // A constant whose bitwise inverse can be used as an immediate
3885 // value in a data-processing instruction. This can be used in GCC
3886 // with a "B" modifier that prints the inverted value, for use with
3887 // BIC and MVN instructions. It is not useful otherwise but is
3888 // implemented for compatibility.
3889 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
3890 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003891 } else {
3892 // A constant whose bitwise inverse can be used as an immediate
3893 // value in a data-processing instruction. This can be used in GCC
3894 // with a "B" modifier that prints the inverted value, for use with
3895 // BIC and MVN instructions. It is not useful otherwise but is
3896 // implemented for compatibility.
3897 if (ARM_AM::getSOImmVal(~CVal) != -1)
3898 break;
3899 }
3900 return;
3901
3902 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003903 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003904 // This must be a constant between -7 and 7,
3905 // for 3-operand ADD/SUB immediate instructions.
3906 if (CVal >= -7 && CVal < 7)
3907 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00003908 } else if (Subtarget->isThumb2()) {
3909 // A constant whose negation can be used as an immediate value in a
3910 // data-processing instruction. This can be used in GCC with an "n"
3911 // modifier that prints the negated value, for use with SUB
3912 // instructions. It is not useful otherwise but is implemented for
3913 // compatibility.
3914 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
3915 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003916 } else {
3917 // A constant whose negation can be used as an immediate value in a
3918 // data-processing instruction. This can be used in GCC with an "n"
3919 // modifier that prints the negated value, for use with SUB
3920 // instructions. It is not useful otherwise but is implemented for
3921 // compatibility.
3922 if (ARM_AM::getSOImmVal(-CVal) != -1)
3923 break;
3924 }
3925 return;
3926
3927 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003928 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003929 // This must be a multiple of 4 between 0 and 1020, for
3930 // ADD sp + immediate.
3931 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
3932 break;
3933 } else {
3934 // A power of two or a constant between 0 and 32. This is used in
3935 // GCC for the shift amount on shifted register operands, but it is
3936 // useful in general for any shift amounts.
3937 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
3938 break;
3939 }
3940 return;
3941
3942 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003943 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003944 // This must be a constant between 0 and 31, for shift amounts.
3945 if (CVal >= 0 && CVal <= 31)
3946 break;
3947 }
3948 return;
3949
3950 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003951 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003952 // This must be a multiple of 4 between -508 and 508, for
3953 // ADD/SUB sp = sp + immediate.
3954 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
3955 break;
3956 }
3957 return;
3958 }
3959 Result = DAG.getTargetConstant(CVal, Op.getValueType());
3960 break;
3961 }
3962
3963 if (Result.getNode()) {
3964 Ops.push_back(Result);
3965 return;
3966 }
3967 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
3968 Ops, DAG);
3969}