blob: 5a6721b70acb256842a700ce5ff8fb0f8fc1b53b [file] [log] [blame]
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001//===-- HexagonISelLowering.cpp - Hexagon DAG Lowering Implementation -----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the interfaces that Hexagon uses to lower LLVM code
11// into a selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "HexagonISelLowering.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000016#include "HexagonMachineFunctionInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000017#include "HexagonSubtarget.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "HexagonTargetMachine.h"
19#include "HexagonTargetObjectFile.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000020#include "llvm/CodeGen/CallingConvLower.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/MachineFunction.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
Craig Topperb25fda92012-03-17 18:46:09 +000024#include "llvm/CodeGen/MachineJumpTableInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000025#include "llvm/CodeGen/MachineRegisterInfo.h"
26#include "llvm/CodeGen/SelectionDAGISel.h"
27#include "llvm/CodeGen/ValueTypes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/CallingConv.h"
29#include "llvm/IR/DerivedTypes.h"
30#include "llvm/IR/Function.h"
31#include "llvm/IR/GlobalAlias.h"
32#include "llvm/IR/GlobalVariable.h"
33#include "llvm/IR/InlineAsm.h"
34#include "llvm/IR/Intrinsics.h"
NAKAMURA Takumi54eed762012-04-21 15:31:36 +000035#include "llvm/Support/CommandLine.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000036#include "llvm/Support/Debug.h"
37#include "llvm/Support/ErrorHandling.h"
NAKAMURA Takumie30303f2012-04-21 15:31:45 +000038#include "llvm/Support/raw_ostream.h"
NAKAMURA Takumi54eed762012-04-21 15:31:36 +000039
Craig Topperb25fda92012-03-17 18:46:09 +000040using namespace llvm;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000041
Chandler Carruthe96dd892014-04-21 22:55:11 +000042#define DEBUG_TYPE "hexagon-lowering"
43
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +000044static cl::opt<bool> EmitJumpTables("hexagon-emit-jump-tables",
45 cl::init(true), cl::Hidden,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +000046 cl::desc("Control jump table emission on Hexagon target"));
47
48static cl::opt<bool> EnableHexSDNodeSched("enable-hexagon-sdnode-sched",
49 cl::Hidden, cl::ZeroOrMore, cl::init(false),
50 cl::desc("Enable Hexagon SDNode scheduling"));
51
52static cl::opt<bool> EnableFastMath("ffast-math",
53 cl::Hidden, cl::ZeroOrMore, cl::init(false),
54 cl::desc("Enable Fast Math processing"));
55
56static cl::opt<int> MinimumJumpTables("minimum-jump-tables",
57 cl::Hidden, cl::ZeroOrMore, cl::init(5),
58 cl::desc("Set minimum jump tables"));
59
60static cl::opt<int> MaxStoresPerMemcpyCL("max-store-memcpy",
61 cl::Hidden, cl::ZeroOrMore, cl::init(6),
62 cl::desc("Max #stores to inline memcpy"));
63
64static cl::opt<int> MaxStoresPerMemcpyOptSizeCL("max-store-memcpy-Os",
65 cl::Hidden, cl::ZeroOrMore, cl::init(4),
66 cl::desc("Max #stores to inline memcpy"));
67
68static cl::opt<int> MaxStoresPerMemmoveCL("max-store-memmove",
69 cl::Hidden, cl::ZeroOrMore, cl::init(6),
70 cl::desc("Max #stores to inline memmove"));
71
72static cl::opt<int> MaxStoresPerMemmoveOptSizeCL("max-store-memmove-Os",
73 cl::Hidden, cl::ZeroOrMore, cl::init(4),
74 cl::desc("Max #stores to inline memmove"));
75
76static cl::opt<int> MaxStoresPerMemsetCL("max-store-memset",
77 cl::Hidden, cl::ZeroOrMore, cl::init(8),
78 cl::desc("Max #stores to inline memset"));
79
80static cl::opt<int> MaxStoresPerMemsetOptSizeCL("max-store-memset-Os",
81 cl::Hidden, cl::ZeroOrMore, cl::init(4),
82 cl::desc("Max #stores to inline memset"));
83
Tony Linthicum1213a7a2011-12-12 21:14:40 +000084
Benjamin Kramer602bb4a2013-10-27 11:16:09 +000085namespace {
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +000086 class HexagonCCState : public CCState {
87 unsigned NumNamedVarArgParams;
Benjamin Kramer602bb4a2013-10-27 11:16:09 +000088
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +000089 public:
90 HexagonCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
91 SmallVectorImpl<CCValAssign> &locs, LLVMContext &C,
92 int NumNamedVarArgParams)
93 : CCState(CC, isVarArg, MF, locs, C),
94 NumNamedVarArgParams(NumNamedVarArgParams) {}
Benjamin Kramer602bb4a2013-10-27 11:16:09 +000095
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +000096 unsigned getNumNamedVarArgParams() const { return NumNamedVarArgParams; }
97 };
98
99 enum StridedLoadKind {
100 Even = 0,
101 Odd,
102 NoPattern
103 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000104}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000105
106// Implement calling convention for Hexagon.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000107
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000108static bool isHvxVectorType(MVT ty);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000109
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000110static bool
111CC_Hexagon(unsigned ValNo, MVT ValVT,
112 MVT LocVT, CCValAssign::LocInfo LocInfo,
113 ISD::ArgFlagsTy ArgFlags, CCState &State);
114
115static bool
116CC_Hexagon32(unsigned ValNo, MVT ValVT,
117 MVT LocVT, CCValAssign::LocInfo LocInfo,
118 ISD::ArgFlagsTy ArgFlags, CCState &State);
119
120static bool
121CC_Hexagon64(unsigned ValNo, MVT ValVT,
122 MVT LocVT, CCValAssign::LocInfo LocInfo,
123 ISD::ArgFlagsTy ArgFlags, CCState &State);
124
125static bool
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000126CC_HexagonVector(unsigned ValNo, MVT ValVT,
127 MVT LocVT, CCValAssign::LocInfo LocInfo,
128 ISD::ArgFlagsTy ArgFlags, CCState &State);
129
130static bool
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000131RetCC_Hexagon(unsigned ValNo, MVT ValVT,
132 MVT LocVT, CCValAssign::LocInfo LocInfo,
133 ISD::ArgFlagsTy ArgFlags, CCState &State);
134
135static bool
136RetCC_Hexagon32(unsigned ValNo, MVT ValVT,
137 MVT LocVT, CCValAssign::LocInfo LocInfo,
138 ISD::ArgFlagsTy ArgFlags, CCState &State);
139
140static bool
141RetCC_Hexagon64(unsigned ValNo, MVT ValVT,
142 MVT LocVT, CCValAssign::LocInfo LocInfo,
143 ISD::ArgFlagsTy ArgFlags, CCState &State);
144
145static bool
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000146RetCC_HexagonVector(unsigned ValNo, MVT ValVT,
147 MVT LocVT, CCValAssign::LocInfo LocInfo,
148 ISD::ArgFlagsTy ArgFlags, CCState &State);
149
150static bool
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000151CC_Hexagon_VarArg (unsigned ValNo, MVT ValVT,
152 MVT LocVT, CCValAssign::LocInfo LocInfo,
153 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000154 HexagonCCState &HState = static_cast<HexagonCCState &>(State);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000155
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000156 if (ValNo < HState.getNumNamedVarArgParams()) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000157 // Deal with named arguments.
158 return CC_Hexagon(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State);
159 }
160
161 // Deal with un-named arguments.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000162 unsigned Offset;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000163 if (ArgFlags.isByVal()) {
164 // If pass-by-value, the size allocated on stack is decided
165 // by ArgFlags.getByValSize(), not by the size of LocVT.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000166 Offset = State.AllocateStack(ArgFlags.getByValSize(),
167 ArgFlags.getByValAlign());
168 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000169 return false;
170 }
Jyotsna Vermac7dcc2f2013-03-07 20:28:34 +0000171 if (LocVT == MVT::i1 || LocVT == MVT::i8 || LocVT == MVT::i16) {
172 LocVT = MVT::i32;
173 ValVT = MVT::i32;
174 if (ArgFlags.isSExt())
175 LocInfo = CCValAssign::SExt;
176 else if (ArgFlags.isZExt())
177 LocInfo = CCValAssign::ZExt;
178 else
179 LocInfo = CCValAssign::AExt;
180 }
Sirish Pande69295b82012-05-10 20:20:25 +0000181 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000182 Offset = State.AllocateStack(4, 4);
183 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000184 return false;
185 }
Sirish Pande69295b82012-05-10 20:20:25 +0000186 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000187 Offset = State.AllocateStack(8, 8);
188 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000189 return false;
190 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000191 if (LocVT == MVT::v2i64 || LocVT == MVT::v4i32 || LocVT == MVT::v8i16 ||
192 LocVT == MVT::v16i8) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000193 Offset = State.AllocateStack(16, 16);
194 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000195 return false;
196 }
197 if (LocVT == MVT::v4i64 || LocVT == MVT::v8i32 || LocVT == MVT::v16i16 ||
198 LocVT == MVT::v32i8) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000199 Offset = State.AllocateStack(32, 32);
200 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000201 return false;
202 }
203 if (LocVT == MVT::v8i64 || LocVT == MVT::v16i32 || LocVT == MVT::v32i16 ||
204 LocVT == MVT::v64i8 || LocVT == MVT::v512i1) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000205 Offset = State.AllocateStack(64, 64);
206 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000207 return false;
208 }
209 if (LocVT == MVT::v16i64 || LocVT == MVT::v32i32 || LocVT == MVT::v64i16 ||
210 LocVT == MVT::v128i8 || LocVT == MVT::v1024i1) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000211 Offset = State.AllocateStack(128, 128);
212 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000213 return false;
214 }
215 if (LocVT == MVT::v32i64 || LocVT == MVT::v64i32 || LocVT == MVT::v128i16 ||
216 LocVT == MVT::v256i8) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000217 Offset = State.AllocateStack(256, 256);
218 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000219 return false;
220 }
221
Craig Toppere73658d2014-04-28 04:05:08 +0000222 llvm_unreachable(nullptr);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000223}
224
225
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000226static bool CC_Hexagon (unsigned ValNo, MVT ValVT, MVT LocVT,
227 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, CCState &State) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000228 if (ArgFlags.isByVal()) {
229 // Passed on stack.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000230 unsigned Offset = State.AllocateStack(ArgFlags.getByValSize(),
231 ArgFlags.getByValAlign());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000232 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
233 return false;
234 }
235
236 if (LocVT == MVT::i1 || LocVT == MVT::i8 || LocVT == MVT::i16) {
237 LocVT = MVT::i32;
238 ValVT = MVT::i32;
239 if (ArgFlags.isSExt())
240 LocInfo = CCValAssign::SExt;
241 else if (ArgFlags.isZExt())
242 LocInfo = CCValAssign::ZExt;
243 else
244 LocInfo = CCValAssign::AExt;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000245 } else if (LocVT == MVT::v4i8 || LocVT == MVT::v2i16) {
246 LocVT = MVT::i32;
247 LocInfo = CCValAssign::BCvt;
248 } else if (LocVT == MVT::v8i8 || LocVT == MVT::v4i16 || LocVT == MVT::v2i32) {
249 LocVT = MVT::i64;
250 LocInfo = CCValAssign::BCvt;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000251 }
252
Sirish Pande69295b82012-05-10 20:20:25 +0000253 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000254 if (!CC_Hexagon32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
255 return false;
256 }
257
Sirish Pande69295b82012-05-10 20:20:25 +0000258 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000259 if (!CC_Hexagon64(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
260 return false;
261 }
262
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000263 if (LocVT == MVT::v8i32 || LocVT == MVT::v16i16 || LocVT == MVT::v32i8) {
264 unsigned Offset = State.AllocateStack(ArgFlags.getByValSize(), 32);
265 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
266 return false;
267 }
268
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000269 if (isHvxVectorType(LocVT)) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000270 if (!CC_HexagonVector(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
271 return false;
272 }
273
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000274 return true; // CC didn't match.
275}
276
277
278static bool CC_Hexagon32(unsigned ValNo, MVT ValVT,
279 MVT LocVT, CCValAssign::LocInfo LocInfo,
280 ISD::ArgFlagsTy ArgFlags, CCState &State) {
281
Craig Topper840beec2014-04-04 05:16:06 +0000282 static const MCPhysReg RegList[] = {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000283 Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3, Hexagon::R4,
284 Hexagon::R5
285 };
Tim Northover3b6b7ca2015-02-21 02:11:17 +0000286 if (unsigned Reg = State.AllocateReg(RegList)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000287 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
288 return false;
289 }
290
291 unsigned Offset = State.AllocateStack(4, 4);
292 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
293 return false;
294}
295
296static bool CC_Hexagon64(unsigned ValNo, MVT ValVT,
297 MVT LocVT, CCValAssign::LocInfo LocInfo,
298 ISD::ArgFlagsTy ArgFlags, CCState &State) {
299
300 if (unsigned Reg = State.AllocateReg(Hexagon::D0)) {
301 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
302 return false;
303 }
304
Craig Topper840beec2014-04-04 05:16:06 +0000305 static const MCPhysReg RegList1[] = {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000306 Hexagon::D1, Hexagon::D2
307 };
Craig Topper840beec2014-04-04 05:16:06 +0000308 static const MCPhysReg RegList2[] = {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000309 Hexagon::R1, Hexagon::R3
310 };
Tim Northover3b6b7ca2015-02-21 02:11:17 +0000311 if (unsigned Reg = State.AllocateReg(RegList1, RegList2)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000312 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
313 return false;
314 }
315
316 unsigned Offset = State.AllocateStack(8, 8, Hexagon::D2);
317 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
318 return false;
319}
320
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000321static bool CC_HexagonVector(unsigned ValNo, MVT ValVT,
322 MVT LocVT, CCValAssign::LocInfo LocInfo,
323 ISD::ArgFlagsTy ArgFlags, CCState &State) {
324
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000325 static const MCPhysReg VecLstS[] = {
326 Hexagon::V0, Hexagon::V1, Hexagon::V2, Hexagon::V3, Hexagon::V4,
327 Hexagon::V5, Hexagon::V6, Hexagon::V7, Hexagon::V8, Hexagon::V9,
328 Hexagon::V10, Hexagon::V11, Hexagon::V12, Hexagon::V13, Hexagon::V14,
329 Hexagon::V15
330 };
331 static const MCPhysReg VecLstD[] = {
332 Hexagon::W0, Hexagon::W1, Hexagon::W2, Hexagon::W3, Hexagon::W4,
333 Hexagon::W5, Hexagon::W6, Hexagon::W7
334 };
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000335 auto &MF = State.getMachineFunction();
336 auto &HST = MF.getSubtarget<HexagonSubtarget>();
337 bool UseHVX = HST.useHVXOps();
338 bool UseHVXDbl = HST.useHVXDblOps();
339
340 if ((UseHVX && !UseHVXDbl) &&
341 (LocVT == MVT::v8i64 || LocVT == MVT::v16i32 || LocVT == MVT::v32i16 ||
342 LocVT == MVT::v64i8 || LocVT == MVT::v512i1)) {
343 if (unsigned Reg = State.AllocateReg(VecLstS)) {
344 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
345 return false;
346 }
347 unsigned Offset = State.AllocateStack(64, 64);
348 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
349 return false;
350 }
351 if ((UseHVX && !UseHVXDbl) &&
352 (LocVT == MVT::v16i64 || LocVT == MVT::v32i32 || LocVT == MVT::v64i16 ||
353 LocVT == MVT::v128i8)) {
354 if (unsigned Reg = State.AllocateReg(VecLstD)) {
355 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
356 return false;
357 }
358 unsigned Offset = State.AllocateStack(128, 128);
359 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
360 return false;
361 }
362 // 128B Mode
363 if ((UseHVX && UseHVXDbl) &&
364 (LocVT == MVT::v32i64 || LocVT == MVT::v64i32 || LocVT == MVT::v128i16 ||
365 LocVT == MVT::v256i8)) {
366 if (unsigned Reg = State.AllocateReg(VecLstD)) {
367 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
368 return false;
369 }
370 unsigned Offset = State.AllocateStack(256, 256);
371 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
372 return false;
373 }
374 if ((UseHVX && UseHVXDbl) &&
375 (LocVT == MVT::v16i64 || LocVT == MVT::v32i32 || LocVT == MVT::v64i16 ||
376 LocVT == MVT::v128i8 || LocVT == MVT::v1024i1)) {
377 if (unsigned Reg = State.AllocateReg(VecLstS)) {
378 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
379 return false;
380 }
381 unsigned Offset = State.AllocateStack(128, 128);
382 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
383 return false;
384 }
385 return true;
386}
387
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000388static bool RetCC_Hexagon(unsigned ValNo, MVT ValVT,
389 MVT LocVT, CCValAssign::LocInfo LocInfo,
390 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000391 auto &MF = State.getMachineFunction();
392 auto &HST = MF.getSubtarget<HexagonSubtarget>();
393 bool UseHVX = HST.useHVXOps();
394 bool UseHVXDbl = HST.useHVXDblOps();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000395
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000396 if (LocVT == MVT::i1) {
397 // Return values of type MVT::i1 still need to be assigned to R0, but
398 // the value type needs to remain i1. LowerCallResult will deal with it,
399 // but it needs to recognize i1 as the value type.
400 LocVT = MVT::i32;
401 } else if (LocVT == MVT::i8 || LocVT == MVT::i16) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000402 LocVT = MVT::i32;
403 ValVT = MVT::i32;
404 if (ArgFlags.isSExt())
405 LocInfo = CCValAssign::SExt;
406 else if (ArgFlags.isZExt())
407 LocInfo = CCValAssign::ZExt;
408 else
409 LocInfo = CCValAssign::AExt;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000410 } else if (LocVT == MVT::v4i8 || LocVT == MVT::v2i16) {
411 LocVT = MVT::i32;
412 LocInfo = CCValAssign::BCvt;
413 } else if (LocVT == MVT::v8i8 || LocVT == MVT::v4i16 || LocVT == MVT::v2i32) {
414 LocVT = MVT::i64;
415 LocInfo = CCValAssign::BCvt;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000416 } else if (LocVT == MVT::v64i8 || LocVT == MVT::v32i16 ||
417 LocVT == MVT::v16i32 || LocVT == MVT::v8i64 ||
418 LocVT == MVT::v512i1) {
419 LocVT = MVT::v16i32;
420 ValVT = MVT::v16i32;
421 LocInfo = CCValAssign::Full;
422 } else if (LocVT == MVT::v128i8 || LocVT == MVT::v64i16 ||
423 LocVT == MVT::v32i32 || LocVT == MVT::v16i64 ||
424 (LocVT == MVT::v1024i1 && UseHVX && UseHVXDbl)) {
425 LocVT = MVT::v32i32;
426 ValVT = MVT::v32i32;
427 LocInfo = CCValAssign::Full;
428 } else if (LocVT == MVT::v256i8 || LocVT == MVT::v128i16 ||
429 LocVT == MVT::v64i32 || LocVT == MVT::v32i64) {
430 LocVT = MVT::v64i32;
431 ValVT = MVT::v64i32;
432 LocInfo = CCValAssign::Full;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000433 }
Sirish Pande69295b82012-05-10 20:20:25 +0000434 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000435 if (!RetCC_Hexagon32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000436 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000437 }
438
Sirish Pande69295b82012-05-10 20:20:25 +0000439 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000440 if (!RetCC_Hexagon64(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000441 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000442 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000443 if (LocVT == MVT::v16i32 || LocVT == MVT::v32i32 || LocVT == MVT::v64i32) {
444 if (!RetCC_HexagonVector(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000445 return false;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000446 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000447 return true; // CC didn't match.
448}
449
450static bool RetCC_Hexagon32(unsigned ValNo, MVT ValVT,
451 MVT LocVT, CCValAssign::LocInfo LocInfo,
452 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Sirish Pande69295b82012-05-10 20:20:25 +0000453 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Krzysztof Parzyszek14412ef2016-07-18 17:36:46 +0000454 // Note that use of registers beyond R1 is not ABI compliant. However there
455 // are (experimental) IR passes which generate internal functions that
456 // return structs using these additional registers.
457 static const uint16_t RegList[] = { Hexagon::R0, Hexagon::R1,
458 Hexagon::R2, Hexagon::R3,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000459 Hexagon::R4, Hexagon::R5 };
Krzysztof Parzyszek14412ef2016-07-18 17:36:46 +0000460 if (unsigned Reg = State.AllocateReg(RegList)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000461 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
462 return false;
463 }
464 }
465
466 unsigned Offset = State.AllocateStack(4, 4);
467 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
468 return false;
469}
470
471static bool RetCC_Hexagon64(unsigned ValNo, MVT ValVT,
472 MVT LocVT, CCValAssign::LocInfo LocInfo,
473 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Sirish Pande69295b82012-05-10 20:20:25 +0000474 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000475 if (unsigned Reg = State.AllocateReg(Hexagon::D0)) {
476 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
477 return false;
478 }
479 }
480
481 unsigned Offset = State.AllocateStack(8, 8);
482 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
483 return false;
484}
485
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000486static bool RetCC_HexagonVector(unsigned ValNo, MVT ValVT,
487 MVT LocVT, CCValAssign::LocInfo LocInfo,
488 ISD::ArgFlagsTy ArgFlags, CCState &State) {
489 auto &MF = State.getMachineFunction();
490 auto &HST = MF.getSubtarget<HexagonSubtarget>();
491 bool UseHVX = HST.useHVXOps();
492 bool UseHVXDbl = HST.useHVXDblOps();
493
494 unsigned OffSiz = 64;
495 if (LocVT == MVT::v16i32) {
496 if (unsigned Reg = State.AllocateReg(Hexagon::V0)) {
497 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
498 return false;
499 }
500 } else if (LocVT == MVT::v32i32) {
501 unsigned Req = (UseHVX && UseHVXDbl) ? Hexagon::V0 : Hexagon::W0;
502 if (unsigned Reg = State.AllocateReg(Req)) {
503 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
504 return false;
505 }
506 OffSiz = 128;
507 } else if (LocVT == MVT::v64i32) {
508 if (unsigned Reg = State.AllocateReg(Hexagon::W0)) {
509 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
510 return false;
511 }
512 OffSiz = 256;
513 }
514
515 unsigned Offset = State.AllocateStack(OffSiz, OffSiz);
516 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
517 return false;
518}
519
Craig Topper18e69f42016-04-15 06:20:21 +0000520void HexagonTargetLowering::promoteLdStType(MVT VT, MVT PromotedLdStVT) {
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000521 if (VT != PromotedLdStVT) {
Craig Topper18e69f42016-04-15 06:20:21 +0000522 setOperationAction(ISD::LOAD, VT, Promote);
523 AddPromotedToType(ISD::LOAD, VT, PromotedLdStVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000524
Craig Topper18e69f42016-04-15 06:20:21 +0000525 setOperationAction(ISD::STORE, VT, Promote);
526 AddPromotedToType(ISD::STORE, VT, PromotedLdStVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000527 }
528}
529
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000530SDValue
531HexagonTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG)
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000532 const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000533 return SDValue();
534}
535
536/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
537/// by "Src" to address "Dst" of size "Size". Alignment information is
538/// specified by the specific parameter attribute. The copy will be passed as
539/// a byval function parameter. Sometimes what we are copying is the end of a
540/// larger object, the part that does not fit in registers.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000541static SDValue CreateCopyOfByValArgument(SDValue Src, SDValue Dst,
542 SDValue Chain, ISD::ArgFlagsTy Flags,
543 SelectionDAG &DAG, const SDLoc &dl) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000544 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000545 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
546 /*isVolatile=*/false, /*AlwaysInline=*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +0000547 /*isTailCall=*/false,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000548 MachinePointerInfo(), MachinePointerInfo());
549}
550
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000551static bool isHvxVectorType(MVT Ty) {
552 switch (Ty.SimpleTy) {
553 case MVT::v8i64:
554 case MVT::v16i32:
555 case MVT::v32i16:
556 case MVT::v64i8:
557 case MVT::v16i64:
558 case MVT::v32i32:
559 case MVT::v64i16:
560 case MVT::v128i8:
561 case MVT::v32i64:
562 case MVT::v64i32:
563 case MVT::v128i16:
564 case MVT::v256i8:
565 case MVT::v512i1:
566 case MVT::v1024i1:
567 return true;
568 default:
569 return false;
570 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000571}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000572
573// LowerReturn - Lower ISD::RET. If a struct is larger than 8 bytes and is
574// passed by value, the function prototype is modified to return void and
575// the value is stored in memory pointed by a pointer passed by caller.
576SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000577HexagonTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
578 bool isVarArg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000579 const SmallVectorImpl<ISD::OutputArg> &Outs,
580 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000581 const SDLoc &dl, SelectionDAG &DAG) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000582
583 // CCValAssign - represent the assignment of the return value to locations.
584 SmallVector<CCValAssign, 16> RVLocs;
585
586 // CCState - Info about the registers and stack slot.
Eric Christopherb5217502014-08-06 18:45:26 +0000587 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
588 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000589
590 // Analyze return values of ISD::RET
591 CCInfo.AnalyzeReturn(Outs, RetCC_Hexagon);
592
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000593 SDValue Flag;
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000594 SmallVector<SDValue, 4> RetOps(1, Chain);
595
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000596 // Copy the result values into the output registers.
597 for (unsigned i = 0; i != RVLocs.size(); ++i) {
598 CCValAssign &VA = RVLocs[i];
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000599
600 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), OutVals[i], Flag);
601
602 // Guarantee that all emitted copies are stuck together with flags.
603 Flag = Chain.getValue(1);
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000604 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000605 }
606
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000607 RetOps[0] = Chain; // Update chain.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000608
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000609 // Add the flag if we have it.
610 if (Flag.getNode())
611 RetOps.push_back(Flag);
612
Craig Topper48d114b2014-04-26 18:35:24 +0000613 return DAG.getNode(HexagonISD::RET_FLAG, dl, MVT::Other, RetOps);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000614}
615
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000616bool HexagonTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
617 // If either no tail call or told not to tail call at all, don't.
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000618 auto Attr =
619 CI->getParent()->getParent()->getFnAttribute("disable-tail-calls");
620 if (!CI->isTailCall() || Attr.getValueAsString() == "true")
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000621 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000622
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000623 return true;
624}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000625
626/// LowerCallResult - Lower the result values of an ISD::CALL into the
627/// appropriate copies out of appropriate physical registers. This assumes that
628/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
629/// being lowered. Returns a SDNode with the same number of values as the
630/// ISD::CALL.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000631SDValue HexagonTargetLowering::LowerCallResult(
632 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool isVarArg,
633 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
634 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals,
635 const SmallVectorImpl<SDValue> &OutVals, SDValue Callee) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000636 // Assign locations to each value returned by this call.
637 SmallVector<CCValAssign, 16> RVLocs;
638
Eric Christopherb5217502014-08-06 18:45:26 +0000639 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
640 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000641
642 CCInfo.AnalyzeCallResult(Ins, RetCC_Hexagon);
643
644 // Copy all of the result registers out of their specified physreg.
645 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000646 SDValue RetVal;
647 if (RVLocs[i].getValVT() == MVT::i1) {
648 // Return values of type MVT::i1 require special handling. The reason
649 // is that MVT::i1 is associated with the PredRegs register class, but
650 // values of that type are still returned in R0. Generate an explicit
651 // copy into a predicate register from R0, and treat the value of the
652 // predicate register as the call result.
653 auto &MRI = DAG.getMachineFunction().getRegInfo();
654 SDValue FR0 = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
655 MVT::i32, InFlag);
656 // FR0 = (Value, Chain, Glue)
657 unsigned PredR = MRI.createVirtualRegister(&Hexagon::PredRegsRegClass);
658 SDValue TPR = DAG.getCopyToReg(FR0.getValue(1), dl, PredR,
659 FR0.getValue(0), FR0.getValue(2));
660 // TPR = (Chain, Glue)
661 RetVal = DAG.getCopyFromReg(TPR.getValue(0), dl, PredR, MVT::i1,
662 TPR.getValue(1));
663 } else {
664 RetVal = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
665 RVLocs[i].getValVT(), InFlag);
666 }
667 InVals.push_back(RetVal.getValue(0));
668 Chain = RetVal.getValue(1);
669 InFlag = RetVal.getValue(2);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000670 }
671
672 return Chain;
673}
674
675/// LowerCall - Functions arguments are copied from virtual regs to
676/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
677SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +0000678HexagonTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000679 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +0000680 SelectionDAG &DAG = CLI.DAG;
Craig Topperb94011f2013-07-14 04:42:23 +0000681 SDLoc &dl = CLI.DL;
682 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
683 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
684 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000685 SDValue Chain = CLI.Chain;
686 SDValue Callee = CLI.Callee;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000687 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000688 CallingConv::ID CallConv = CLI.CallConv;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000689 bool IsVarArg = CLI.IsVarArg;
690 bool DoesNotReturn = CLI.DoesNotReturn;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000691
692 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000693 MachineFunction &MF = DAG.getMachineFunction();
Mehdi Amini44ede332015-07-09 02:09:04 +0000694 auto PtrVT = getPointerTy(MF.getDataLayout());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000695
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000696 // Check for varargs.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000697 unsigned NumNamedVarArgParams = -1U;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000698 if (GlobalAddressSDNode *GAN = dyn_cast<GlobalAddressSDNode>(Callee)) {
699 const GlobalValue *GV = GAN->getGlobal();
700 Callee = DAG.getTargetGlobalAddress(GV, dl, MVT::i32);
701 if (const Function* F = dyn_cast<Function>(GV)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000702 // If a function has zero args and is a vararg function, that's
703 // disallowed so it must be an undeclared function. Do not assume
704 // varargs if the callee is undefined.
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000705 if (F->isVarArg() && F->getFunctionType()->getNumParams() != 0)
706 NumNamedVarArgParams = F->getFunctionType()->getNumParams();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000707 }
708 }
709
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000710 // Analyze operands of the call, assigning locations to each operand.
711 SmallVector<CCValAssign, 16> ArgLocs;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000712 HexagonCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
Eric Christopherb5217502014-08-06 18:45:26 +0000713 *DAG.getContext(), NumNamedVarArgParams);
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000714
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000715 if (IsVarArg)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000716 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon_VarArg);
717 else
718 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon);
719
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000720 auto Attr = MF.getFunction()->getFnAttribute("disable-tail-calls");
721 if (Attr.getValueAsString() == "true")
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000722 IsTailCall = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000723
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000724 if (IsTailCall) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000725 bool StructAttrFlag = MF.getFunction()->hasStructRetAttr();
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000726 IsTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
727 IsVarArg, IsStructRet,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000728 StructAttrFlag,
729 Outs, OutVals, Ins, DAG);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000730 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000731 CCValAssign &VA = ArgLocs[i];
732 if (VA.isMemLoc()) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000733 IsTailCall = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000734 break;
735 }
736 }
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000737 DEBUG(dbgs() << (IsTailCall ? "Eligible for Tail Call\n"
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000738 : "Argument must be passed on stack. "
739 "Not eligible for Tail Call\n"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000740 }
741 // Get a count of how many bytes are to be pushed on the stack.
742 unsigned NumBytes = CCInfo.getNextStackOffset();
743 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
744 SmallVector<SDValue, 8> MemOpChains;
745
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000746 auto &HRI = *Subtarget.getRegisterInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +0000747 SDValue StackPtr =
748 DAG.getCopyFromReg(Chain, dl, HRI.getStackRegister(), PtrVT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000749
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000750 bool NeedsArgAlign = false;
751 unsigned LargestAlignSeen = 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000752 // Walk the register/memloc assignments, inserting copies/loads.
753 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
754 CCValAssign &VA = ArgLocs[i];
755 SDValue Arg = OutVals[i];
756 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000757 // Record if we need > 8 byte alignment on an argument.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000758 bool ArgAlign = isHvxVectorType(VA.getValVT());
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000759 NeedsArgAlign |= ArgAlign;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000760
761 // Promote the value if needed.
762 switch (VA.getLocInfo()) {
763 default:
764 // Loc info must be one of Full, SExt, ZExt, or AExt.
Craig Toppere55c5562012-02-07 02:50:20 +0000765 llvm_unreachable("Unknown loc info!");
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000766 case CCValAssign::BCvt:
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000767 case CCValAssign::Full:
768 break;
769 case CCValAssign::SExt:
770 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
771 break;
772 case CCValAssign::ZExt:
773 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
774 break;
775 case CCValAssign::AExt:
776 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
777 break;
778 }
779
780 if (VA.isMemLoc()) {
781 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000782 SDValue MemAddr = DAG.getConstant(LocMemOffset, dl,
783 StackPtr.getValueType());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000784 MemAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, MemAddr);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000785 if (ArgAlign)
786 LargestAlignSeen = std::max(LargestAlignSeen,
787 VA.getLocVT().getStoreSizeInBits() >> 3);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000788 if (Flags.isByVal()) {
789 // The argument is a struct passed by value. According to LLVM, "Arg"
790 // is is pointer.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000791 MemOpChains.push_back(CreateCopyOfByValArgument(Arg, MemAddr, Chain,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000792 Flags, DAG, dl));
793 } else {
Alex Lorenze40c8a22015-08-11 23:09:45 +0000794 MachinePointerInfo LocPI = MachinePointerInfo::getStack(
795 DAG.getMachineFunction(), LocMemOffset);
Justin Lebar9c375812016-07-15 18:27:10 +0000796 SDValue S = DAG.getStore(Chain, dl, Arg, MemAddr, LocPI);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000797 MemOpChains.push_back(S);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000798 }
799 continue;
800 }
801
802 // Arguments that can be passed on register must be kept at RegsToPass
803 // vector.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000804 if (VA.isRegLoc())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000805 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000806 }
807
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000808 if (NeedsArgAlign && Subtarget.hasV60TOps()) {
809 DEBUG(dbgs() << "Function needs byte stack align due to call args\n");
Matthias Braun941a7052016-07-28 18:40:00 +0000810 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000811 // V6 vectors passed by value have 64 or 128 byte alignment depending
812 // on whether we are 64 byte vector mode or 128 byte.
813 bool UseHVXDbl = Subtarget.useHVXDblOps();
814 assert(Subtarget.useHVXOps());
815 const unsigned ObjAlign = UseHVXDbl ? 128 : 64;
816 LargestAlignSeen = std::max(LargestAlignSeen, ObjAlign);
Matthias Braun941a7052016-07-28 18:40:00 +0000817 MFI.ensureMaxAlignment(LargestAlignSeen);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000818 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000819 // Transform all store nodes into one single node because all store
820 // nodes are independent of each other.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000821 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000822 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000823
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000824 if (!IsTailCall) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000825 SDValue C = DAG.getConstant(NumBytes, dl, PtrVT, true);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000826 Chain = DAG.getCALLSEQ_START(Chain, C, dl);
827 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000828
829 // Build a sequence of copy-to-reg nodes chained together with token
830 // chain and flag operands which copy the outgoing args into registers.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000831 // The Glue is necessary since all emitted instructions must be
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000832 // stuck together.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000833 SDValue Glue;
834 if (!IsTailCall) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000835 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
836 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000837 RegsToPass[i].second, Glue);
838 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000839 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000840 } else {
841 // For tail calls lower the arguments to the 'real' stack slot.
842 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000843 // Force all the incoming stack arguments to be loaded from the stack
844 // before any new outgoing arguments are stored to the stack, because the
845 // outgoing stack slots may alias the incoming argument stack slots, and
846 // the alias isn't otherwise explicit. This is slightly more conservative
847 // than necessary, because it means that each store effectively depends
848 // on every argument instead of just those arguments it would clobber.
849 //
Benjamin Kramerbde91762012-06-02 10:20:22 +0000850 // Do not flag preceding copytoreg stuff together with the following stuff.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000851 Glue = SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000852 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
853 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000854 RegsToPass[i].second, Glue);
855 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000856 }
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000857 Glue = SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000858 }
859
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000860 bool LongCalls = MF.getSubtarget<HexagonSubtarget>().useLongCalls();
861 unsigned Flags = LongCalls ? HexagonII::HMOTF_ConstExtended : 0;
862
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000863 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
864 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
865 // node so that legalize doesn't hack it.
Tobias Edler von Kochb51460c2015-12-16 17:29:37 +0000866 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000867 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, PtrVT, 0, Flags);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000868 } else if (ExternalSymbolSDNode *S =
869 dyn_cast<ExternalSymbolSDNode>(Callee)) {
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000870 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), PtrVT, Flags);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000871 }
872
873 // Returns a chain & a flag for retval copy to use.
874 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
875 SmallVector<SDValue, 8> Ops;
876 Ops.push_back(Chain);
877 Ops.push_back(Callee);
878
879 // Add argument registers to the end of the list so that they are
880 // known live into the call.
881 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
882 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
883 RegsToPass[i].second.getValueType()));
884 }
885
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000886 if (Glue.getNode())
887 Ops.push_back(Glue);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000888
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000889 if (IsTailCall) {
Matthias Braun941a7052016-07-28 18:40:00 +0000890 MF.getFrameInfo().setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +0000891 return DAG.getNode(HexagonISD::TC_RETURN, dl, NodeTys, Ops);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +0000892 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000893
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +0000894 unsigned OpCode = DoesNotReturn ? HexagonISD::CALLnr : HexagonISD::CALL;
Colin LeMahieu2e3a26d2015-01-16 17:05:27 +0000895 Chain = DAG.getNode(OpCode, dl, NodeTys, Ops);
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000896 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000897
898 // Create the CALLSEQ_END node.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000899 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000900 DAG.getIntPtrConstant(0, dl, true), Glue, dl);
901 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000902
903 // Handle result values, copying them out of physregs into vregs that we
904 // return.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000905 return LowerCallResult(Chain, Glue, CallConv, IsVarArg, Ins, dl, DAG,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000906 InVals, OutVals, Callee);
907}
908
909static bool getIndexedAddressParts(SDNode *Ptr, EVT VT,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000910 SDValue &Base, SDValue &Offset,
911 bool &IsInc, SelectionDAG &DAG) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000912 if (Ptr->getOpcode() != ISD::ADD)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000913 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000914
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000915 auto &HST = static_cast<const HexagonSubtarget&>(DAG.getSubtarget());
916 bool UseHVX = HST.useHVXOps();
917 bool UseHVXDbl = HST.useHVXDblOps();
918
919 bool ValidHVXDblType =
920 (UseHVX && UseHVXDbl) && (VT == MVT::v32i32 || VT == MVT::v16i64 ||
921 VT == MVT::v64i16 || VT == MVT::v128i8);
922 bool ValidHVXType =
923 UseHVX && !UseHVXDbl && (VT == MVT::v16i32 || VT == MVT::v8i64 ||
924 VT == MVT::v32i16 || VT == MVT::v64i8);
925
926 if (ValidHVXDblType || ValidHVXType ||
927 VT == MVT::i64 || VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000928 IsInc = (Ptr->getOpcode() == ISD::ADD);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000929 Base = Ptr->getOperand(0);
930 Offset = Ptr->getOperand(1);
931 // Ensure that Offset is a constant.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000932 return isa<ConstantSDNode>(Offset);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000933 }
934
935 return false;
936}
937
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000938/// getPostIndexedAddressParts - returns true by value, base pointer and
939/// offset pointer and addressing mode by reference if this node can be
940/// combined with a load / store to form a post-indexed load / store.
941bool HexagonTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
942 SDValue &Base,
943 SDValue &Offset,
944 ISD::MemIndexedMode &AM,
945 SelectionDAG &DAG) const
946{
947 EVT VT;
948 SDValue Ptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000949
950 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
951 VT = LD->getMemoryVT();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000952 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
953 VT = ST->getMemoryVT();
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000954 if (ST->getValue().getValueType() == MVT::i64 && ST->isTruncatingStore())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000955 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000956 } else {
957 return false;
958 }
959
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000960 bool IsInc = false;
961 bool isLegal = getIndexedAddressParts(Op, VT, Base, Offset, IsInc, DAG);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000962 if (isLegal) {
963 auto &HII = *Subtarget.getInstrInfo();
964 int32_t OffsetVal = cast<ConstantSDNode>(Offset.getNode())->getSExtValue();
965 if (HII.isValidAutoIncImm(VT, OffsetVal)) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000966 AM = IsInc ? ISD::POST_INC : ISD::POST_DEC;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000967 return true;
968 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000969 }
970
971 return false;
972}
973
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000974SDValue
975HexagonTargetLowering::LowerINLINEASM(SDValue Op, SelectionDAG &DAG) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000976 SDNode *Node = Op.getNode();
977 MachineFunction &MF = DAG.getMachineFunction();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000978 auto &FuncInfo = *MF.getInfo<HexagonMachineFunctionInfo>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000979 switch (Node->getOpcode()) {
980 case ISD::INLINEASM: {
981 unsigned NumOps = Node->getNumOperands();
982 if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue)
983 --NumOps; // Ignore the flag operand.
984
985 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000986 if (FuncInfo.hasClobberLR())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000987 break;
988 unsigned Flags =
989 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
990 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
991 ++i; // Skip the ID value.
992
993 switch (InlineAsm::getKind(Flags)) {
994 default: llvm_unreachable("Bad flags!");
995 case InlineAsm::Kind_RegDef:
996 case InlineAsm::Kind_RegUse:
997 case InlineAsm::Kind_Imm:
998 case InlineAsm::Kind_Clobber:
999 case InlineAsm::Kind_Mem: {
1000 for (; NumVals; --NumVals, ++i) {}
1001 break;
1002 }
1003 case InlineAsm::Kind_RegDefEarlyClobber: {
1004 for (; NumVals; --NumVals, ++i) {
1005 unsigned Reg =
1006 cast<RegisterSDNode>(Node->getOperand(i))->getReg();
1007
1008 // Check it to be lr
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001009 const HexagonRegisterInfo *QRI = Subtarget.getRegisterInfo();
Eric Christopherdbe1cb02014-06-27 00:13:52 +00001010 if (Reg == QRI->getRARegister()) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001011 FuncInfo.setHasClobberLR(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001012 break;
1013 }
1014 }
1015 break;
1016 }
1017 }
1018 }
1019 }
1020 } // Node->getOpcode
1021 return Op;
1022}
1023
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00001024// Need to transform ISD::PREFETCH into something that doesn't inherit
1025// all of the properties of ISD::PREFETCH, specifically SDNPMayLoad and
1026// SDNPMayStore.
1027SDValue HexagonTargetLowering::LowerPREFETCH(SDValue Op,
1028 SelectionDAG &DAG) const {
1029 SDValue Chain = Op.getOperand(0);
1030 SDValue Addr = Op.getOperand(1);
1031 // Lower it to DCFETCH($reg, #0). A "pat" will try to merge the offset in,
1032 // if the "reg" is fed by an "add".
1033 SDLoc DL(Op);
1034 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
1035 return DAG.getNode(HexagonISD::DCFETCH, DL, MVT::Other, Chain, Addr, Zero);
1036}
1037
1038SDValue HexagonTargetLowering::LowerINTRINSIC_VOID(SDValue Op,
1039 SelectionDAG &DAG) const {
1040 SDValue Chain = Op.getOperand(0);
1041 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1042 // Lower the hexagon_prefetch builtin to DCFETCH, as above.
1043 if (IntNo == Intrinsic::hexagon_prefetch) {
1044 SDValue Addr = Op.getOperand(2);
1045 SDLoc DL(Op);
1046 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
1047 return DAG.getNode(HexagonISD::DCFETCH, DL, MVT::Other, Chain, Addr, Zero);
1048 }
1049 return SDValue();
1050}
1051
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001052SDValue
1053HexagonTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
1054 SelectionDAG &DAG) const {
1055 SDValue Chain = Op.getOperand(0);
1056 SDValue Size = Op.getOperand(1);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001057 SDValue Align = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001058 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001059
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001060 ConstantSDNode *AlignConst = dyn_cast<ConstantSDNode>(Align);
1061 assert(AlignConst && "Non-constant Align in LowerDYNAMIC_STACKALLOC");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001062
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001063 unsigned A = AlignConst->getSExtValue();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001064 auto &HFI = *Subtarget.getFrameLowering();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001065 // "Zero" means natural stack alignment.
1066 if (A == 0)
1067 A = HFI.getStackAlignment();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001068
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001069 DEBUG({
Krzysztof Parzyszek9ee04e42015-04-22 17:19:44 +00001070 dbgs () << LLVM_FUNCTION_NAME << " Align: " << A << " Size: ";
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001071 Size.getNode()->dump(&DAG);
1072 dbgs() << "\n";
1073 });
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001074
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001075 SDValue AC = DAG.getConstant(A, dl, MVT::i32);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001076 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00001077 SDValue AA = DAG.getNode(HexagonISD::ALLOCA, dl, VTs, Chain, Size, AC);
Nirav Davebfdb4832016-06-23 17:52:57 +00001078
1079 DAG.ReplaceAllUsesOfValueWith(Op, AA);
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00001080 return AA;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001081}
1082
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001083SDValue HexagonTargetLowering::LowerFormalArguments(
1084 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
1085 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
1086 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001087
1088 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00001089 MachineFrameInfo &MFI = MF.getFrameInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001090 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001091 auto &FuncInfo = *MF.getInfo<HexagonMachineFunctionInfo>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001092
1093 // Assign locations to all of the incoming arguments.
1094 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001095 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1096 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001097
1098 CCInfo.AnalyzeFormalArguments(Ins, CC_Hexagon);
1099
1100 // For LLVM, in the case when returning a struct by value (>8byte),
1101 // the first argument is a pointer that points to the location on caller's
1102 // stack where the return value will be stored. For Hexagon, the location on
1103 // caller's stack is passed only when the struct size is smaller than (and
1104 // equal to) 8 bytes. If not, no address will be passed into callee and
1105 // callee return the result direclty through R0/R1.
1106
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001107 SmallVector<SDValue, 8> MemOps;
1108 bool UseHVX = Subtarget.useHVXOps(), UseHVXDbl = Subtarget.useHVXDblOps();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001109
1110 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1111 CCValAssign &VA = ArgLocs[i];
1112 ISD::ArgFlagsTy Flags = Ins[i].Flags;
1113 unsigned ObjSize;
1114 unsigned StackLocation;
1115 int FI;
1116
1117 if ( (VA.isRegLoc() && !Flags.isByVal())
1118 || (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() > 8)) {
1119 // Arguments passed in registers
1120 // 1. int, long long, ptr args that get allocated in register.
1121 // 2. Large struct that gets an register to put its address in.
1122 EVT RegVT = VA.getLocVT();
Sirish Pande69295b82012-05-10 20:20:25 +00001123 if (RegVT == MVT::i8 || RegVT == MVT::i16 ||
1124 RegVT == MVT::i32 || RegVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001125 unsigned VReg =
Craig Topperc7242e02012-04-20 07:30:17 +00001126 RegInfo.createVirtualRegister(&Hexagon::IntRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001127 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1128 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Colin LeMahieu4379d102015-01-28 22:08:16 +00001129 } else if (RegVT == MVT::i64 || RegVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001130 unsigned VReg =
Craig Topperc7242e02012-04-20 07:30:17 +00001131 RegInfo.createVirtualRegister(&Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001132 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1133 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001134
1135 // Single Vector
1136 } else if ((RegVT == MVT::v8i64 || RegVT == MVT::v16i32 ||
1137 RegVT == MVT::v32i16 || RegVT == MVT::v64i8)) {
1138 unsigned VReg =
1139 RegInfo.createVirtualRegister(&Hexagon::VectorRegsRegClass);
1140 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1141 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1142 } else if (UseHVX && UseHVXDbl &&
1143 ((RegVT == MVT::v16i64 || RegVT == MVT::v32i32 ||
1144 RegVT == MVT::v64i16 || RegVT == MVT::v128i8))) {
1145 unsigned VReg =
1146 RegInfo.createVirtualRegister(&Hexagon::VectorRegs128BRegClass);
1147 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1148 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1149
1150 // Double Vector
1151 } else if ((RegVT == MVT::v16i64 || RegVT == MVT::v32i32 ||
1152 RegVT == MVT::v64i16 || RegVT == MVT::v128i8)) {
1153 unsigned VReg =
1154 RegInfo.createVirtualRegister(&Hexagon::VecDblRegsRegClass);
1155 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1156 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1157 } else if (UseHVX && UseHVXDbl &&
1158 ((RegVT == MVT::v32i64 || RegVT == MVT::v64i32 ||
1159 RegVT == MVT::v128i16 || RegVT == MVT::v256i8))) {
1160 unsigned VReg =
1161 RegInfo.createVirtualRegister(&Hexagon::VecDblRegs128BRegClass);
1162 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1163 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1164 } else if (RegVT == MVT::v512i1 || RegVT == MVT::v1024i1) {
1165 assert(0 && "need to support VecPred regs");
1166 unsigned VReg =
1167 RegInfo.createVirtualRegister(&Hexagon::VecPredRegsRegClass);
1168 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1169 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001170 } else {
1171 assert (0);
1172 }
1173 } else if (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() <= 8) {
1174 assert (0 && "ByValSize must be bigger than 8 bytes");
1175 } else {
1176 // Sanity check.
1177 assert(VA.isMemLoc());
1178
1179 if (Flags.isByVal()) {
1180 // If it's a byval parameter, then we need to compute the
1181 // "real" size, not the size of the pointer.
1182 ObjSize = Flags.getByValSize();
1183 } else {
1184 ObjSize = VA.getLocVT().getStoreSizeInBits() >> 3;
1185 }
1186
1187 StackLocation = HEXAGON_LRFP_SIZE + VA.getLocMemOffset();
1188 // Create the frame index object for this incoming parameter...
Matthias Braun941a7052016-07-28 18:40:00 +00001189 FI = MFI.CreateFixedObject(ObjSize, StackLocation, true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001190
1191 // Create the SelectionDAG nodes cordl, responding to a load
1192 // from this parameter.
1193 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
1194
1195 if (Flags.isByVal()) {
1196 // If it's a pass-by-value aggregate, then do not dereference the stack
1197 // location. Instead, we should generate a reference to the stack
1198 // location.
1199 InVals.push_back(FIN);
1200 } else {
Justin Lebar9c375812016-07-15 18:27:10 +00001201 InVals.push_back(
1202 DAG.getLoad(VA.getLocVT(), dl, Chain, FIN, MachinePointerInfo()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001203 }
1204 }
1205 }
1206
1207 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001208 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001209
1210 if (isVarArg) {
1211 // This will point to the next argument passed via stack.
Matthias Braun941a7052016-07-28 18:40:00 +00001212 int FrameIndex = MFI.CreateFixedObject(Hexagon_PointerSize,
1213 HEXAGON_LRFP_SIZE +
1214 CCInfo.getNextStackOffset(),
1215 true);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001216 FuncInfo.setVarArgsFrameIndex(FrameIndex);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001217 }
1218
1219 return Chain;
1220}
1221
1222SDValue
1223HexagonTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
1224 // VASTART stores the address of the VarArgsFrameIndex slot into the
1225 // memory location argument.
1226 MachineFunction &MF = DAG.getMachineFunction();
1227 HexagonMachineFunctionInfo *QFI = MF.getInfo<HexagonMachineFunctionInfo>();
1228 SDValue Addr = DAG.getFrameIndex(QFI->getVarArgsFrameIndex(), MVT::i32);
1229 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Justin Lebar9c375812016-07-15 18:27:10 +00001230 return DAG.getStore(Op.getOperand(0), SDLoc(Op), Addr, Op.getOperand(1),
1231 MachinePointerInfo(SV));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001232}
1233
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001234// Creates a SPLAT instruction for a constant value VAL.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001235static SDValue createSplat(SelectionDAG &DAG, const SDLoc &dl, EVT VT,
1236 SDValue Val) {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001237 if (VT.getSimpleVT() == MVT::v4i8)
1238 return DAG.getNode(HexagonISD::VSPLATB, dl, VT, Val);
1239
1240 if (VT.getSimpleVT() == MVT::v4i16)
1241 return DAG.getNode(HexagonISD::VSPLATH, dl, VT, Val);
1242
1243 return SDValue();
1244}
1245
1246static bool isSExtFree(SDValue N) {
1247 // A sign-extend of a truncate of a sign-extend is free.
1248 if (N.getOpcode() == ISD::TRUNCATE &&
1249 N.getOperand(0).getOpcode() == ISD::AssertSext)
1250 return true;
1251 // We have sign-extended loads.
1252 if (N.getOpcode() == ISD::LOAD)
1253 return true;
1254 return false;
1255}
1256
1257SDValue HexagonTargetLowering::LowerCTPOP(SDValue Op, SelectionDAG &DAG) const {
1258 SDLoc dl(Op);
1259 SDValue InpVal = Op.getOperand(0);
1260 if (isa<ConstantSDNode>(InpVal)) {
1261 uint64_t V = cast<ConstantSDNode>(InpVal)->getZExtValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001262 return DAG.getTargetConstant(countPopulation(V), dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001263 }
1264 SDValue PopOut = DAG.getNode(HexagonISD::POPCOUNT, dl, MVT::i32, InpVal);
1265 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, PopOut);
1266}
1267
1268SDValue HexagonTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1269 SDLoc dl(Op);
1270
1271 SDValue LHS = Op.getOperand(0);
1272 SDValue RHS = Op.getOperand(1);
1273 SDValue Cmp = Op.getOperand(2);
1274 ISD::CondCode CC = cast<CondCodeSDNode>(Cmp)->get();
1275
1276 EVT VT = Op.getValueType();
1277 EVT LHSVT = LHS.getValueType();
1278 EVT RHSVT = RHS.getValueType();
1279
1280 if (LHSVT == MVT::v2i16) {
1281 assert(ISD::isSignedIntSetCC(CC) || ISD::isUnsignedIntSetCC(CC));
1282 unsigned ExtOpc = ISD::isSignedIntSetCC(CC) ? ISD::SIGN_EXTEND
1283 : ISD::ZERO_EXTEND;
1284 SDValue LX = DAG.getNode(ExtOpc, dl, MVT::v2i32, LHS);
1285 SDValue RX = DAG.getNode(ExtOpc, dl, MVT::v2i32, RHS);
1286 SDValue SC = DAG.getNode(ISD::SETCC, dl, MVT::v2i1, LX, RX, Cmp);
1287 return SC;
1288 }
1289
1290 // Treat all other vector types as legal.
1291 if (VT.isVector())
1292 return Op;
1293
1294 // Equals and not equals should use sign-extend, not zero-extend, since
1295 // we can represent small negative values in the compare instructions.
1296 // The LLVM default is to use zero-extend arbitrarily in these cases.
1297 if ((CC == ISD::SETEQ || CC == ISD::SETNE) &&
1298 (RHSVT == MVT::i8 || RHSVT == MVT::i16) &&
1299 (LHSVT == MVT::i8 || LHSVT == MVT::i16)) {
1300 ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS);
1301 if (C && C->getAPIntValue().isNegative()) {
1302 LHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, LHS);
1303 RHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, RHS);
1304 return DAG.getNode(ISD::SETCC, dl, Op.getValueType(),
1305 LHS, RHS, Op.getOperand(2));
1306 }
1307 if (isSExtFree(LHS) || isSExtFree(RHS)) {
1308 LHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, LHS);
1309 RHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, RHS);
1310 return DAG.getNode(ISD::SETCC, dl, Op.getValueType(),
1311 LHS, RHS, Op.getOperand(2));
1312 }
1313 }
1314 return SDValue();
1315}
1316
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001317SDValue
1318HexagonTargetLowering::LowerVSELECT(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001319 SDValue PredOp = Op.getOperand(0);
1320 SDValue Op1 = Op.getOperand(1), Op2 = Op.getOperand(2);
1321 EVT OpVT = Op1.getValueType();
1322 SDLoc DL(Op);
1323
1324 if (OpVT == MVT::v2i16) {
1325 SDValue X1 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op1);
1326 SDValue X2 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op2);
1327 SDValue SL = DAG.getNode(ISD::VSELECT, DL, MVT::v2i32, PredOp, X1, X2);
1328 SDValue TR = DAG.getNode(ISD::TRUNCATE, DL, MVT::v2i16, SL);
1329 return TR;
1330 }
1331
1332 return SDValue();
1333}
1334
1335// Handle only specific vector loads.
1336SDValue HexagonTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1337 EVT VT = Op.getValueType();
1338 SDLoc DL(Op);
1339 LoadSDNode *LoadNode = cast<LoadSDNode>(Op);
1340 SDValue Chain = LoadNode->getChain();
1341 SDValue Ptr = Op.getOperand(1);
1342 SDValue LoweredLoad;
1343 SDValue Result;
1344 SDValue Base = LoadNode->getBasePtr();
1345 ISD::LoadExtType Ext = LoadNode->getExtensionType();
1346 unsigned Alignment = LoadNode->getAlignment();
1347 SDValue LoadChain;
1348
1349 if(Ext == ISD::NON_EXTLOAD)
1350 Ext = ISD::ZEXTLOAD;
1351
1352 if (VT == MVT::v4i16) {
1353 if (Alignment == 2) {
1354 SDValue Loads[4];
1355 // Base load.
1356 Loads[0] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Base,
Justin Lebar9c375812016-07-15 18:27:10 +00001357 LoadNode->getPointerInfo(), MVT::i16, Alignment,
1358 LoadNode->getMemOperand()->getFlags());
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001359 // Base+2 load.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001360 SDValue Increment = DAG.getConstant(2, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001361 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1362 Loads[1] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
Justin Lebar9c375812016-07-15 18:27:10 +00001363 LoadNode->getPointerInfo(), MVT::i16, Alignment,
1364 LoadNode->getMemOperand()->getFlags());
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001365 // SHL 16, then OR base and base+2.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001366 SDValue ShiftAmount = DAG.getConstant(16, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001367 SDValue Tmp1 = DAG.getNode(ISD::SHL, DL, MVT::i32, Loads[1], ShiftAmount);
1368 SDValue Tmp2 = DAG.getNode(ISD::OR, DL, MVT::i32, Tmp1, Loads[0]);
1369 // Base + 4.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001370 Increment = DAG.getConstant(4, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001371 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1372 Loads[2] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
Justin Lebar9c375812016-07-15 18:27:10 +00001373 LoadNode->getPointerInfo(), MVT::i16, Alignment,
1374 LoadNode->getMemOperand()->getFlags());
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001375 // Base + 6.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001376 Increment = DAG.getConstant(6, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001377 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1378 Loads[3] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
Justin Lebar9c375812016-07-15 18:27:10 +00001379 LoadNode->getPointerInfo(), MVT::i16, Alignment,
1380 LoadNode->getMemOperand()->getFlags());
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001381 // SHL 16, then OR base+4 and base+6.
1382 Tmp1 = DAG.getNode(ISD::SHL, DL, MVT::i32, Loads[3], ShiftAmount);
1383 SDValue Tmp4 = DAG.getNode(ISD::OR, DL, MVT::i32, Tmp1, Loads[2]);
1384 // Combine to i64. This could be optimised out later if we can
1385 // affect reg allocation of this code.
1386 Result = DAG.getNode(HexagonISD::COMBINE, DL, MVT::i64, Tmp4, Tmp2);
1387 LoadChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
1388 Loads[0].getValue(1), Loads[1].getValue(1),
1389 Loads[2].getValue(1), Loads[3].getValue(1));
1390 } else {
1391 // Perform default type expansion.
1392 Result = DAG.getLoad(MVT::i64, DL, Chain, Ptr, LoadNode->getPointerInfo(),
Justin Lebar9c375812016-07-15 18:27:10 +00001393 LoadNode->getAlignment(),
1394 LoadNode->getMemOperand()->getFlags());
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001395 LoadChain = Result.getValue(1);
1396 }
1397 } else
1398 llvm_unreachable("Custom lowering unsupported load");
1399
1400 Result = DAG.getNode(ISD::BITCAST, DL, VT, Result);
1401 // Since we pretend to lower a load, we need the original chain
1402 // info attached to the result.
1403 SDValue Ops[] = { Result, LoadChain };
1404
1405 return DAG.getMergeValues(Ops, DL);
1406}
1407
1408
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001409SDValue
Sirish Pande69295b82012-05-10 20:20:25 +00001410HexagonTargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
1411 EVT ValTy = Op.getValueType();
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001412 ConstantPoolSDNode *CPN = cast<ConstantPoolSDNode>(Op);
1413 unsigned Align = CPN->getAlignment();
Rafael Espindola405e25a2016-06-26 22:24:01 +00001414 bool IsPositionIndependent = isPositionIndependent();
1415 unsigned char TF = IsPositionIndependent ? HexagonII::MO_PCREL : 0;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001416
Ron Lieberman822ee882016-08-13 23:41:11 +00001417 unsigned Offset = 0;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001418 SDValue T;
1419 if (CPN->isMachineConstantPoolEntry())
Ron Lieberman822ee882016-08-13 23:41:11 +00001420 T = DAG.getTargetConstantPool(CPN->getMachineCPVal(), ValTy, Align, Offset,
1421 TF);
Sirish Pande69295b82012-05-10 20:20:25 +00001422 else
Ron Lieberman822ee882016-08-13 23:41:11 +00001423 T = DAG.getTargetConstantPool(CPN->getConstVal(), ValTy, Align, Offset,
1424 TF);
1425
1426 assert(cast<ConstantPoolSDNode>(T)->getTargetFlags() == TF &&
1427 "Inconsistent target flag encountered");
1428
Rafael Espindola405e25a2016-06-26 22:24:01 +00001429 if (IsPositionIndependent)
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001430 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), ValTy, T);
1431 return DAG.getNode(HexagonISD::CP, SDLoc(Op), ValTy, T);
1432}
1433
1434SDValue
1435HexagonTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
1436 EVT VT = Op.getValueType();
1437 int Idx = cast<JumpTableSDNode>(Op)->getIndex();
Rafael Espindola405e25a2016-06-26 22:24:01 +00001438 if (isPositionIndependent()) {
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001439 SDValue T = DAG.getTargetJumpTable(Idx, VT, HexagonII::MO_PCREL);
1440 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), VT, T);
1441 }
1442
1443 SDValue T = DAG.getTargetJumpTable(Idx, VT);
1444 return DAG.getNode(HexagonISD::JT, SDLoc(Op), VT, T);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001445}
1446
1447SDValue
1448HexagonTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001449 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001450 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00001451 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001452 MFI.setReturnAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001453
Bill Wendling908bf812014-01-06 00:43:20 +00001454 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001455 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001456
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001457 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001458 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001459 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1460 if (Depth) {
1461 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001462 SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001463 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
1464 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Justin Lebar9c375812016-07-15 18:27:10 +00001465 MachinePointerInfo());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001466 }
1467
1468 // Return LR, which contains the return address. Mark it an implicit live-in.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001469 unsigned Reg = MF.addLiveIn(HRI.getRARegister(), getRegClassFor(MVT::i32));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001470 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
1471}
1472
1473SDValue
1474HexagonTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001475 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Matthias Braun941a7052016-07-28 18:40:00 +00001476 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001477 MFI.setFrameAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001478
1479 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001480 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001481 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1482 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001483 HRI.getFrameRegister(), VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001484 while (Depth--)
1485 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00001486 MachinePointerInfo());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001487 return FrameAddr;
1488}
1489
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001490SDValue
1491HexagonTargetLowering::LowerATOMIC_FENCE(SDValue Op, SelectionDAG& DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001492 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001493 return DAG.getNode(HexagonISD::BARRIER, dl, MVT::Other, Op.getOperand(0));
1494}
1495
1496
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001497SDValue
1498HexagonTargetLowering::LowerGLOBALADDRESS(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001499 SDLoc dl(Op);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001500 auto *GAN = cast<GlobalAddressSDNode>(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00001501 auto PtrVT = getPointerTy(DAG.getDataLayout());
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001502 auto *GV = GAN->getGlobal();
1503 int64_t Offset = GAN->getOffset();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001504
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001505 auto &HLOF = *HTM.getObjFileLowering();
1506 Reloc::Model RM = HTM.getRelocationModel();
1507
1508 if (RM == Reloc::Static) {
1509 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset);
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +00001510 if (HLOF.isGlobalInSmallSection(GV, HTM))
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001511 return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, GA);
1512 return DAG.getNode(HexagonISD::CONST32, dl, PtrVT, GA);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001513 }
1514
Rafael Espindola3beef8d2016-06-27 23:15:57 +00001515 bool UsePCRel = getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001516 if (UsePCRel) {
1517 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset,
1518 HexagonII::MO_PCREL);
1519 return DAG.getNode(HexagonISD::AT_PCREL, dl, PtrVT, GA);
1520 }
1521
1522 // Use GOT index.
1523 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
1524 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, HexagonII::MO_GOT);
1525 SDValue Off = DAG.getConstant(Offset, dl, MVT::i32);
1526 return DAG.getNode(HexagonISD::AT_GOT, dl, PtrVT, GOT, GA, Off);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001527}
1528
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001529// Specifies that for loads and stores VT can be promoted to PromotedLdStVT.
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001530SDValue
1531HexagonTargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
1532 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001533 SDLoc dl(Op);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001534 EVT PtrVT = getPointerTy(DAG.getDataLayout());
1535
1536 Reloc::Model RM = HTM.getRelocationModel();
1537 if (RM == Reloc::Static) {
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001538 SDValue A = DAG.getTargetBlockAddress(BA, PtrVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001539 return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, A);
1540 }
1541
1542 SDValue A = DAG.getTargetBlockAddress(BA, PtrVT, 0, HexagonII::MO_PCREL);
1543 return DAG.getNode(HexagonISD::AT_PCREL, dl, PtrVT, A);
1544}
1545
1546SDValue
1547HexagonTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op, SelectionDAG &DAG)
1548 const {
1549 EVT PtrVT = getPointerTy(DAG.getDataLayout());
1550 SDValue GOTSym = DAG.getTargetExternalSymbol(HEXAGON_GOT_SYM_NAME, PtrVT,
1551 HexagonII::MO_PCREL);
1552 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), PtrVT, GOTSym);
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001553}
1554
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001555SDValue
1556HexagonTargetLowering::GetDynamicTLSAddr(SelectionDAG &DAG, SDValue Chain,
1557 GlobalAddressSDNode *GA, SDValue *InFlag, EVT PtrVT, unsigned ReturnReg,
1558 unsigned char OperandFlags) const {
Matthias Braun941a7052016-07-28 18:40:00 +00001559 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001560 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1561 SDLoc dl(GA);
1562 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
1563 GA->getValueType(0),
1564 GA->getOffset(),
1565 OperandFlags);
1566 // Create Operands for the call.The Operands should have the following:
1567 // 1. Chain SDValue
1568 // 2. Callee which in this case is the Global address value.
1569 // 3. Registers live into the call.In this case its R0, as we
1570 // have just one argument to be passed.
1571 // 4. InFlag if there is any.
1572 // Note: The order is important.
1573
1574 if (InFlag) {
1575 SDValue Ops[] = { Chain, TGA,
1576 DAG.getRegister(Hexagon::R0, PtrVT), *InFlag };
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001577 Chain = DAG.getNode(HexagonISD::CALL, dl, NodeTys, Ops);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001578 } else {
1579 SDValue Ops[] = { Chain, TGA, DAG.getRegister(Hexagon::R0, PtrVT)};
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001580 Chain = DAG.getNode(HexagonISD::CALL, dl, NodeTys, Ops);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001581 }
1582
1583 // Inform MFI that function has calls.
Matthias Braun941a7052016-07-28 18:40:00 +00001584 MFI.setAdjustsStack(true);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001585
1586 SDValue Flag = Chain.getValue(1);
1587 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
1588}
1589
1590//
1591// Lower using the intial executable model for TLS addresses
1592//
1593SDValue
1594HexagonTargetLowering::LowerToTLSInitialExecModel(GlobalAddressSDNode *GA,
1595 SelectionDAG &DAG) const {
1596 SDLoc dl(GA);
1597 int64_t Offset = GA->getOffset();
1598 auto PtrVT = getPointerTy(DAG.getDataLayout());
1599
1600 // Get the thread pointer.
1601 SDValue TP = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Hexagon::UGP, PtrVT);
1602
Rafael Espindola405e25a2016-06-26 22:24:01 +00001603 bool IsPositionIndependent = isPositionIndependent();
1604 unsigned char TF =
1605 IsPositionIndependent ? HexagonII::MO_IEGOT : HexagonII::MO_IE;
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001606
1607 // First generate the TLS symbol address
1608 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT,
1609 Offset, TF);
1610
1611 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1612
Rafael Espindola405e25a2016-06-26 22:24:01 +00001613 if (IsPositionIndependent) {
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001614 // Generate the GOT pointer in case of position independent code
1615 SDValue GOT = LowerGLOBAL_OFFSET_TABLE(Sym, DAG);
1616
1617 // Add the TLS Symbol address to GOT pointer.This gives
1618 // GOT relative relocation for the symbol.
1619 Sym = DAG.getNode(ISD::ADD, dl, PtrVT, GOT, Sym);
1620 }
1621
1622 // Load the offset value for TLS symbol.This offset is relative to
1623 // thread pointer.
Justin Lebar9c375812016-07-15 18:27:10 +00001624 SDValue LoadOffset =
1625 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Sym, MachinePointerInfo());
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001626
1627 // Address of the thread local variable is the add of thread
1628 // pointer and the offset of the variable.
1629 return DAG.getNode(ISD::ADD, dl, PtrVT, TP, LoadOffset);
1630}
1631
1632//
1633// Lower using the local executable model for TLS addresses
1634//
1635SDValue
1636HexagonTargetLowering::LowerToTLSLocalExecModel(GlobalAddressSDNode *GA,
1637 SelectionDAG &DAG) const {
1638 SDLoc dl(GA);
1639 int64_t Offset = GA->getOffset();
1640 auto PtrVT = getPointerTy(DAG.getDataLayout());
1641
1642 // Get the thread pointer.
1643 SDValue TP = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Hexagon::UGP, PtrVT);
1644 // Generate the TLS symbol address
1645 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT, Offset,
1646 HexagonII::MO_TPREL);
1647 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1648
1649 // Address of the thread local variable is the add of thread
1650 // pointer and the offset of the variable.
1651 return DAG.getNode(ISD::ADD, dl, PtrVT, TP, Sym);
1652}
1653
1654//
1655// Lower using the general dynamic model for TLS addresses
1656//
1657SDValue
1658HexagonTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
1659 SelectionDAG &DAG) const {
1660 SDLoc dl(GA);
1661 int64_t Offset = GA->getOffset();
1662 auto PtrVT = getPointerTy(DAG.getDataLayout());
1663
1664 // First generate the TLS symbol address
1665 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT, Offset,
1666 HexagonII::MO_GDGOT);
1667
1668 // Then, generate the GOT pointer
1669 SDValue GOT = LowerGLOBAL_OFFSET_TABLE(TGA, DAG);
1670
1671 // Add the TLS symbol and the GOT pointer
1672 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1673 SDValue Chain = DAG.getNode(ISD::ADD, dl, PtrVT, GOT, Sym);
1674
1675 // Copy over the argument to R0
1676 SDValue InFlag;
1677 Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, Hexagon::R0, Chain, InFlag);
1678 InFlag = Chain.getValue(1);
1679
1680 return GetDynamicTLSAddr(DAG, Chain, GA, &InFlag, PtrVT,
1681 Hexagon::R0, HexagonII::MO_GDPLT);
1682}
1683
1684//
1685// Lower TLS addresses.
1686//
1687// For now for dynamic models, we only support the general dynamic model.
1688//
1689SDValue
1690HexagonTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1691 SelectionDAG &DAG) const {
1692 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1693
1694 switch (HTM.getTLSModel(GA->getGlobal())) {
1695 case TLSModel::GeneralDynamic:
1696 case TLSModel::LocalDynamic:
1697 return LowerToTLSGeneralDynamicModel(GA, DAG);
1698 case TLSModel::InitialExec:
1699 return LowerToTLSInitialExecModel(GA, DAG);
1700 case TLSModel::LocalExec:
1701 return LowerToTLSLocalExecModel(GA, DAG);
1702 }
1703 llvm_unreachable("Bogus TLS model");
1704}
1705
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001706//===----------------------------------------------------------------------===//
1707// TargetLowering Implementation
1708//===----------------------------------------------------------------------===//
1709
Eric Christopherd737b762015-02-02 22:11:36 +00001710HexagonTargetLowering::HexagonTargetLowering(const TargetMachine &TM,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001711 const HexagonSubtarget &ST)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001712 : TargetLowering(TM), HTM(static_cast<const HexagonTargetMachine&>(TM)),
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001713 Subtarget(ST) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001714 bool IsV4 = !Subtarget.hasV5TOps();
1715 auto &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00001716 bool UseHVX = Subtarget.useHVXOps();
1717 bool UseHVXSgl = Subtarget.useHVXSglOps();
1718 bool UseHVXDbl = Subtarget.useHVXDblOps();
Sirish Pande69295b82012-05-10 20:20:25 +00001719
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001720 setPrefLoopAlignment(4);
1721 setPrefFunctionAlignment(4);
1722 setMinFunctionAlignment(2);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001723 setStackPointerRegisterToSaveRestore(HRI.getStackRegister());
1724
Krzysztof Parzyszekf228c952016-06-22 16:07:10 +00001725 setMaxAtomicSizeInBitsSupported(64);
1726 setMinCmpXchgSizeInBits(32);
1727
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001728 if (EnableHexSDNodeSched)
1729 setSchedulingPreference(Sched::VLIW);
1730 else
1731 setSchedulingPreference(Sched::Source);
1732
1733 // Limits for inline expansion of memcpy/memmove
1734 MaxStoresPerMemcpy = MaxStoresPerMemcpyCL;
1735 MaxStoresPerMemcpyOptSize = MaxStoresPerMemcpyOptSizeCL;
1736 MaxStoresPerMemmove = MaxStoresPerMemmoveCL;
1737 MaxStoresPerMemmoveOptSize = MaxStoresPerMemmoveOptSizeCL;
1738 MaxStoresPerMemset = MaxStoresPerMemsetCL;
1739 MaxStoresPerMemsetOptSize = MaxStoresPerMemsetOptSizeCL;
1740
1741 //
1742 // Set up register classes.
1743 //
1744
1745 addRegisterClass(MVT::i1, &Hexagon::PredRegsRegClass);
1746 addRegisterClass(MVT::v2i1, &Hexagon::PredRegsRegClass); // bbbbaaaa
1747 addRegisterClass(MVT::v4i1, &Hexagon::PredRegsRegClass); // ddccbbaa
1748 addRegisterClass(MVT::v8i1, &Hexagon::PredRegsRegClass); // hgfedcba
1749 addRegisterClass(MVT::i32, &Hexagon::IntRegsRegClass);
1750 addRegisterClass(MVT::v4i8, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001751 addRegisterClass(MVT::v2i16, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001752 addRegisterClass(MVT::i64, &Hexagon::DoubleRegsRegClass);
1753 addRegisterClass(MVT::v8i8, &Hexagon::DoubleRegsRegClass);
1754 addRegisterClass(MVT::v4i16, &Hexagon::DoubleRegsRegClass);
1755 addRegisterClass(MVT::v2i32, &Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001756
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001757 if (Subtarget.hasV5TOps()) {
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001758 addRegisterClass(MVT::f32, &Hexagon::IntRegsRegClass);
1759 addRegisterClass(MVT::f64, &Hexagon::DoubleRegsRegClass);
1760 }
Sirish Pande69295b82012-05-10 20:20:25 +00001761
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001762 if (Subtarget.hasV60TOps()) {
1763 if (Subtarget.useHVXSglOps()) {
1764 addRegisterClass(MVT::v64i8, &Hexagon::VectorRegsRegClass);
1765 addRegisterClass(MVT::v32i16, &Hexagon::VectorRegsRegClass);
1766 addRegisterClass(MVT::v16i32, &Hexagon::VectorRegsRegClass);
1767 addRegisterClass(MVT::v8i64, &Hexagon::VectorRegsRegClass);
1768 addRegisterClass(MVT::v128i8, &Hexagon::VecDblRegsRegClass);
1769 addRegisterClass(MVT::v64i16, &Hexagon::VecDblRegsRegClass);
1770 addRegisterClass(MVT::v32i32, &Hexagon::VecDblRegsRegClass);
1771 addRegisterClass(MVT::v16i64, &Hexagon::VecDblRegsRegClass);
1772 addRegisterClass(MVT::v512i1, &Hexagon::VecPredRegsRegClass);
1773 } else if (Subtarget.useHVXDblOps()) {
1774 addRegisterClass(MVT::v128i8, &Hexagon::VectorRegs128BRegClass);
1775 addRegisterClass(MVT::v64i16, &Hexagon::VectorRegs128BRegClass);
1776 addRegisterClass(MVT::v32i32, &Hexagon::VectorRegs128BRegClass);
1777 addRegisterClass(MVT::v16i64, &Hexagon::VectorRegs128BRegClass);
1778 addRegisterClass(MVT::v256i8, &Hexagon::VecDblRegs128BRegClass);
1779 addRegisterClass(MVT::v128i16, &Hexagon::VecDblRegs128BRegClass);
1780 addRegisterClass(MVT::v64i32, &Hexagon::VecDblRegs128BRegClass);
1781 addRegisterClass(MVT::v32i64, &Hexagon::VecDblRegs128BRegClass);
1782 addRegisterClass(MVT::v1024i1, &Hexagon::VecPredRegs128BRegClass);
1783 }
1784
1785 }
1786
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001787 //
1788 // Handling of scalar operations.
1789 //
1790 // All operations default to "legal", except:
1791 // - indexed loads and stores (pre-/post-incremented),
1792 // - ANY_EXTEND_VECTOR_INREG, ATOMIC_CMP_SWAP_WITH_SUCCESS, CONCAT_VECTORS,
1793 // ConstantFP, DEBUGTRAP, FCEIL, FCOPYSIGN, FEXP, FEXP2, FFLOOR, FGETSIGN,
1794 // FLOG, FLOG2, FLOG10, FMAXNUM, FMINNUM, FNEARBYINT, FRINT, FROUND, TRAP,
1795 // FTRUNC, PREFETCH, SIGN_EXTEND_VECTOR_INREG, ZERO_EXTEND_VECTOR_INREG,
1796 // which default to "expand" for at least one type.
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001797
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001798 // Misc operations.
1799 setOperationAction(ISD::ConstantFP, MVT::f32, Legal); // Default: expand
1800 setOperationAction(ISD::ConstantFP, MVT::f64, Legal); // Default: expand
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001801
1802 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001803 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001804 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001805 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
1806 setOperationAction(ISD::INLINEASM, MVT::Other, Custom);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00001807 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
1808 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001809 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001810 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001811 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001812 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001813
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001814 // Custom legalize GlobalAddress nodes into CONST32.
1815 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001816 setOperationAction(ISD::GlobalAddress, MVT::i8, Custom);
1817 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001818
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001819 // Hexagon needs to optimize cases with negative constants.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001820 setOperationAction(ISD::SETCC, MVT::i8, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001821 setOperationAction(ISD::SETCC, MVT::i16, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001822
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001823 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
1824 setOperationAction(ISD::VASTART, MVT::Other, Custom);
1825 setOperationAction(ISD::VAEND, MVT::Other, Expand);
1826 setOperationAction(ISD::VAARG, MVT::Other, Expand);
1827
1828 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
1829 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
1830 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
1831
1832 if (EmitJumpTables)
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001833 setMinimumJumpTableEntries(MinimumJumpTables);
Krzysztof Parzyszeka61f7da2016-01-13 21:43:13 +00001834 else
1835 setMinimumJumpTableEntries(INT_MAX);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001836 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001837
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001838 // Hexagon has instructions for add/sub with carry. The problem with
1839 // modeling these instructions is that they produce 2 results: Rdd and Px.
1840 // To model the update of Px, we will have to use Defs[p0..p3] which will
1841 // cause any predicate live range to spill. So, we pretend we dont't have
1842 // these instructions.
1843 setOperationAction(ISD::ADDE, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001844 setOperationAction(ISD::ADDE, MVT::i16, Expand);
1845 setOperationAction(ISD::ADDE, MVT::i32, Expand);
1846 setOperationAction(ISD::ADDE, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001847 setOperationAction(ISD::SUBE, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001848 setOperationAction(ISD::SUBE, MVT::i16, Expand);
1849 setOperationAction(ISD::SUBE, MVT::i32, Expand);
1850 setOperationAction(ISD::SUBE, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001851 setOperationAction(ISD::ADDC, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001852 setOperationAction(ISD::ADDC, MVT::i16, Expand);
1853 setOperationAction(ISD::ADDC, MVT::i32, Expand);
1854 setOperationAction(ISD::ADDC, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001855 setOperationAction(ISD::SUBC, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001856 setOperationAction(ISD::SUBC, MVT::i16, Expand);
1857 setOperationAction(ISD::SUBC, MVT::i32, Expand);
1858 setOperationAction(ISD::SUBC, MVT::i64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001859
Krzysztof Parzyszek2c4487d2015-04-13 20:37:01 +00001860 // Only add and sub that detect overflow are the saturating ones.
1861 for (MVT VT : MVT::integer_valuetypes()) {
1862 setOperationAction(ISD::UADDO, VT, Expand);
1863 setOperationAction(ISD::SADDO, VT, Expand);
1864 setOperationAction(ISD::USUBO, VT, Expand);
1865 setOperationAction(ISD::SSUBO, VT, Expand);
1866 }
1867
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001868 setOperationAction(ISD::CTLZ, MVT::i8, Promote);
1869 setOperationAction(ISD::CTLZ, MVT::i16, Promote);
1870 setOperationAction(ISD::CTTZ, MVT::i8, Promote);
1871 setOperationAction(ISD::CTTZ, MVT::i16, Promote);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001872
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001873 // In V5, popcount can count # of 1s in i64 but returns i32.
1874 // On V4 it will be expanded (set later).
1875 setOperationAction(ISD::CTPOP, MVT::i8, Promote);
1876 setOperationAction(ISD::CTPOP, MVT::i16, Promote);
1877 setOperationAction(ISD::CTPOP, MVT::i32, Promote);
1878 setOperationAction(ISD::CTPOP, MVT::i64, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001879
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001880 // We custom lower i64 to i64 mul, so that it is not considered as a legal
1881 // operation. There is a pattern that will match i64 mul and transform it
1882 // to a series of instructions.
1883 setOperationAction(ISD::MUL, MVT::i64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001884
Benjamin Kramer62460692015-04-25 14:46:53 +00001885 for (unsigned IntExpOp :
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001886 { ISD::SDIV, ISD::UDIV, ISD::SREM, ISD::UREM,
1887 ISD::SDIVREM, ISD::UDIVREM, ISD::ROTL, ISD::ROTR,
1888 ISD::BSWAP, ISD::SHL_PARTS, ISD::SRA_PARTS, ISD::SRL_PARTS,
1889 ISD::SMUL_LOHI, ISD::UMUL_LOHI }) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001890 setOperationAction(IntExpOp, MVT::i32, Expand);
1891 setOperationAction(IntExpOp, MVT::i64, Expand);
1892 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001893
Benjamin Kramer62460692015-04-25 14:46:53 +00001894 for (unsigned FPExpOp :
1895 {ISD::FDIV, ISD::FREM, ISD::FSQRT, ISD::FSIN, ISD::FCOS, ISD::FSINCOS,
1896 ISD::FPOW, ISD::FCOPYSIGN}) {
1897 setOperationAction(FPExpOp, MVT::f32, Expand);
1898 setOperationAction(FPExpOp, MVT::f64, Expand);
1899 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001900
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001901 // No extending loads from i32.
1902 for (MVT VT : MVT::integer_valuetypes()) {
1903 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
1904 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
1905 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
1906 }
1907 // Turn FP truncstore into trunc + store.
1908 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Michael Kuperstein2bc3d4d2016-08-18 20:08:15 +00001909 // Turn FP extload into load/fpextend.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001910 for (MVT VT : MVT::fp_valuetypes())
1911 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001912
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001913 // Expand BR_CC and SELECT_CC for all integer and fp types.
1914 for (MVT VT : MVT::integer_valuetypes()) {
1915 setOperationAction(ISD::BR_CC, VT, Expand);
1916 setOperationAction(ISD::SELECT_CC, VT, Expand);
1917 }
1918 for (MVT VT : MVT::fp_valuetypes()) {
1919 setOperationAction(ISD::BR_CC, VT, Expand);
1920 setOperationAction(ISD::SELECT_CC, VT, Expand);
1921 }
1922 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001923
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001924 //
1925 // Handling of vector operations.
1926 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001927
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001928 // Custom lower v4i16 load only. Let v4i16 store to be
1929 // promoted for now.
1930 promoteLdStType(MVT::v4i8, MVT::i32);
1931 promoteLdStType(MVT::v2i16, MVT::i32);
1932 promoteLdStType(MVT::v8i8, MVT::i64);
1933 promoteLdStType(MVT::v2i32, MVT::i64);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001934
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001935 setOperationAction(ISD::LOAD, MVT::v4i16, Custom);
1936 setOperationAction(ISD::STORE, MVT::v4i16, Promote);
1937 AddPromotedToType(ISD::LOAD, MVT::v4i16, MVT::i64);
1938 AddPromotedToType(ISD::STORE, MVT::v4i16, MVT::i64);
1939
1940 // Set the action for vector operations to "expand", then override it with
1941 // either "custom" or "legal" for specific cases.
Craig Topper26260942015-10-18 05:15:34 +00001942 static const unsigned VectExpOps[] = {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001943 // Integer arithmetic:
1944 ISD::ADD, ISD::SUB, ISD::MUL, ISD::SDIV, ISD::UDIV,
1945 ISD::SREM, ISD::UREM, ISD::SDIVREM, ISD::UDIVREM, ISD::ADDC,
1946 ISD::SUBC, ISD::SADDO, ISD::UADDO, ISD::SSUBO, ISD::USUBO,
1947 ISD::SMUL_LOHI, ISD::UMUL_LOHI,
1948 // Logical/bit:
1949 ISD::AND, ISD::OR, ISD::XOR, ISD::ROTL, ISD::ROTR,
Craig Topper33772c52016-04-28 03:34:31 +00001950 ISD::CTPOP, ISD::CTLZ, ISD::CTTZ,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001951 // Floating point arithmetic/math functions:
1952 ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FMA, ISD::FDIV,
1953 ISD::FREM, ISD::FNEG, ISD::FABS, ISD::FSQRT, ISD::FSIN,
1954 ISD::FCOS, ISD::FPOWI, ISD::FPOW, ISD::FLOG, ISD::FLOG2,
1955 ISD::FLOG10, ISD::FEXP, ISD::FEXP2, ISD::FCEIL, ISD::FTRUNC,
1956 ISD::FRINT, ISD::FNEARBYINT, ISD::FROUND, ISD::FFLOOR,
1957 ISD::FMINNUM, ISD::FMAXNUM, ISD::FSINCOS,
1958 // Misc:
1959 ISD::SELECT, ISD::ConstantPool,
1960 // Vector:
1961 ISD::BUILD_VECTOR, ISD::SCALAR_TO_VECTOR,
1962 ISD::EXTRACT_VECTOR_ELT, ISD::INSERT_VECTOR_ELT,
1963 ISD::EXTRACT_SUBVECTOR, ISD::INSERT_SUBVECTOR,
1964 ISD::CONCAT_VECTORS, ISD::VECTOR_SHUFFLE
1965 };
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001966
1967 for (MVT VT : MVT::vector_valuetypes()) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001968 for (unsigned VectExpOp : VectExpOps)
1969 setOperationAction(VectExpOp, VT, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001970
Krzysztof Parzyszeka696b1b2016-09-08 17:42:14 +00001971 // Expand all extending loads and truncating stores:
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001972 for (MVT TargetVT : MVT::vector_valuetypes()) {
Krzysztof Parzyszeka696b1b2016-09-08 17:42:14 +00001973 if (TargetVT == VT)
1974 continue;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001975 setLoadExtAction(ISD::EXTLOAD, TargetVT, VT, Expand);
Krzysztof Parzyszeka696b1b2016-09-08 17:42:14 +00001976 setLoadExtAction(ISD::ZEXTLOAD, TargetVT, VT, Expand);
1977 setLoadExtAction(ISD::SEXTLOAD, TargetVT, VT, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001978 setTruncStoreAction(VT, TargetVT, Expand);
1979 }
1980
1981 setOperationAction(ISD::SRA, VT, Custom);
1982 setOperationAction(ISD::SHL, VT, Custom);
1983 setOperationAction(ISD::SRL, VT, Custom);
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00001984
1985 setOperationAction(ISD::BR_CC, VT, Expand);
1986 setOperationAction(ISD::SELECT_CC, VT, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001987 }
1988
1989 // Types natively supported:
Benjamin Kramer62460692015-04-25 14:46:53 +00001990 for (MVT NativeVT : {MVT::v2i1, MVT::v4i1, MVT::v8i1, MVT::v32i1, MVT::v64i1,
1991 MVT::v4i8, MVT::v8i8, MVT::v2i16, MVT::v4i16, MVT::v1i32,
1992 MVT::v2i32, MVT::v1i64}) {
1993 setOperationAction(ISD::BUILD_VECTOR, NativeVT, Custom);
1994 setOperationAction(ISD::EXTRACT_VECTOR_ELT, NativeVT, Custom);
1995 setOperationAction(ISD::INSERT_VECTOR_ELT, NativeVT, Custom);
1996 setOperationAction(ISD::EXTRACT_SUBVECTOR, NativeVT, Custom);
1997 setOperationAction(ISD::INSERT_SUBVECTOR, NativeVT, Custom);
1998 setOperationAction(ISD::CONCAT_VECTORS, NativeVT, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001999
Benjamin Kramer62460692015-04-25 14:46:53 +00002000 setOperationAction(ISD::ADD, NativeVT, Legal);
2001 setOperationAction(ISD::SUB, NativeVT, Legal);
2002 setOperationAction(ISD::MUL, NativeVT, Legal);
2003 setOperationAction(ISD::AND, NativeVT, Legal);
2004 setOperationAction(ISD::OR, NativeVT, Legal);
2005 setOperationAction(ISD::XOR, NativeVT, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002006 }
2007
2008 setOperationAction(ISD::SETCC, MVT::v2i16, Custom);
2009 setOperationAction(ISD::VSELECT, MVT::v2i16, Custom);
2010 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
2011 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00002012
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002013 if (UseHVX) {
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002014 if (UseHVXSgl) {
2015 setOperationAction(ISD::CONCAT_VECTORS, MVT::v128i8, Custom);
2016 setOperationAction(ISD::CONCAT_VECTORS, MVT::v64i16, Custom);
2017 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i32, Custom);
2018 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i64, Custom);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002019 // We try to generate the vpack{e/o} instructions. If we fail
2020 // we fall back upon ExpandOp.
2021 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v64i8, Custom);
2022 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v32i16, Custom);
2023 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v64i8, Custom);
2024 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v32i16, Custom);
2025 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v16i32, Custom);
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002026 } else if (UseHVXDbl) {
2027 setOperationAction(ISD::CONCAT_VECTORS, MVT::v256i8, Custom);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002028 setOperationAction(ISD::CONCAT_VECTORS, MVT::v128i16, Custom);
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002029 setOperationAction(ISD::CONCAT_VECTORS, MVT::v64i32, Custom);
2030 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i64, Custom);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002031 // We try to generate the vpack{e/o} instructions. If we fail
2032 // we fall back upon ExpandOp.
2033 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v128i8, Custom);
2034 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v64i16, Custom);
2035 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v4i32, Custom);
2036 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v128i8, Custom);
2037 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v64i16, Custom);
2038 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v32i32, Custom);
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002039 } else {
2040 llvm_unreachable("Unrecognized HVX mode");
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002041 }
2042 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002043 // Subtarget-specific operation actions.
2044 //
2045 if (Subtarget.hasV5TOps()) {
2046 setOperationAction(ISD::FMA, MVT::f64, Expand);
2047 setOperationAction(ISD::FADD, MVT::f64, Expand);
2048 setOperationAction(ISD::FSUB, MVT::f64, Expand);
2049 setOperationAction(ISD::FMUL, MVT::f64, Expand);
2050
Krzysztof Parzyszekbd8ef4b2016-08-19 13:34:31 +00002051 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
2052 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
2053
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002054 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Promote);
2055 setOperationAction(ISD::FP_TO_UINT, MVT::i8, Promote);
2056 setOperationAction(ISD::FP_TO_UINT, MVT::i16, Promote);
2057 setOperationAction(ISD::FP_TO_SINT, MVT::i1, Promote);
2058 setOperationAction(ISD::FP_TO_SINT, MVT::i8, Promote);
2059 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
2060 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote);
2061 setOperationAction(ISD::UINT_TO_FP, MVT::i8, Promote);
2062 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote);
2063 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote);
2064 setOperationAction(ISD::SINT_TO_FP, MVT::i8, Promote);
2065 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
2066
2067 } else { // V4
2068 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
2069 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Expand);
2070 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
2071 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
2072 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Expand);
2073 setOperationAction(ISD::FP_TO_SINT, MVT::f32, Expand);
2074 setOperationAction(ISD::FP_EXTEND, MVT::f32, Expand);
2075 setOperationAction(ISD::FP_ROUND, MVT::f64, Expand);
2076 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
2077
2078 setOperationAction(ISD::CTPOP, MVT::i8, Expand);
2079 setOperationAction(ISD::CTPOP, MVT::i16, Expand);
2080 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
2081 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
2082
2083 // Expand these operations for both f32 and f64:
Benjamin Kramer62460692015-04-25 14:46:53 +00002084 for (unsigned FPExpOpV4 :
2085 {ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FABS, ISD::FNEG, ISD::FMA}) {
2086 setOperationAction(FPExpOpV4, MVT::f32, Expand);
2087 setOperationAction(FPExpOpV4, MVT::f64, Expand);
2088 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002089
Benjamin Kramer62460692015-04-25 14:46:53 +00002090 for (ISD::CondCode FPExpCCV4 :
2091 {ISD::SETOEQ, ISD::SETOGT, ISD::SETOLT, ISD::SETOGE, ISD::SETOLE,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002092 ISD::SETUO, ISD::SETO}) {
Benjamin Kramer62460692015-04-25 14:46:53 +00002093 setCondCodeAction(FPExpCCV4, MVT::f32, Expand);
2094 setCondCodeAction(FPExpCCV4, MVT::f64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002095 }
2096 }
2097
2098 // Handling of indexed loads/stores: default is "expand".
2099 //
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +00002100 for (MVT VT : {MVT::i8, MVT::i16, MVT::i32, MVT::i64}) {
2101 setIndexedLoadAction(ISD::POST_INC, VT, Legal);
2102 setIndexedStoreAction(ISD::POST_INC, VT, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002103 }
2104
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +00002105 if (UseHVXSgl) {
2106 for (MVT VT : {MVT::v64i8, MVT::v32i16, MVT::v16i32, MVT::v8i64,
2107 MVT::v128i8, MVT::v64i16, MVT::v32i32, MVT::v16i64}) {
2108 setIndexedLoadAction(ISD::POST_INC, VT, Legal);
2109 setIndexedStoreAction(ISD::POST_INC, VT, Legal);
2110 }
2111 } else if (UseHVXDbl) {
2112 for (MVT VT : {MVT::v128i8, MVT::v64i16, MVT::v32i32, MVT::v16i64,
2113 MVT::v256i8, MVT::v128i16, MVT::v64i32, MVT::v32i64}) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002114 setIndexedLoadAction(ISD::POST_INC, VT, Legal);
2115 setIndexedStoreAction(ISD::POST_INC, VT, Legal);
2116 }
2117 }
2118
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002119 computeRegisterProperties(&HRI);
2120
2121 //
2122 // Library calls for unsupported operations
2123 //
2124 bool FastMath = EnableFastMath;
2125
Benjamin Kramera37c8092015-04-25 14:46:46 +00002126 setLibcallName(RTLIB::SDIV_I32, "__hexagon_divsi3");
2127 setLibcallName(RTLIB::SDIV_I64, "__hexagon_divdi3");
2128 setLibcallName(RTLIB::UDIV_I32, "__hexagon_udivsi3");
2129 setLibcallName(RTLIB::UDIV_I64, "__hexagon_udivdi3");
2130 setLibcallName(RTLIB::SREM_I32, "__hexagon_modsi3");
2131 setLibcallName(RTLIB::SREM_I64, "__hexagon_moddi3");
2132 setLibcallName(RTLIB::UREM_I32, "__hexagon_umodsi3");
2133 setLibcallName(RTLIB::UREM_I64, "__hexagon_umoddi3");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002134
Benjamin Kramera37c8092015-04-25 14:46:46 +00002135 setLibcallName(RTLIB::SINTTOFP_I128_F64, "__hexagon_floattidf");
2136 setLibcallName(RTLIB::SINTTOFP_I128_F32, "__hexagon_floattisf");
2137 setLibcallName(RTLIB::FPTOUINT_F32_I128, "__hexagon_fixunssfti");
2138 setLibcallName(RTLIB::FPTOUINT_F64_I128, "__hexagon_fixunsdfti");
2139 setLibcallName(RTLIB::FPTOSINT_F32_I128, "__hexagon_fixsfti");
2140 setLibcallName(RTLIB::FPTOSINT_F64_I128, "__hexagon_fixdfti");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002141
2142 if (IsV4) {
2143 // Handle single-precision floating point operations on V4.
Benjamin Kramera37c8092015-04-25 14:46:46 +00002144 if (FastMath) {
2145 setLibcallName(RTLIB::ADD_F32, "__hexagon_fast_addsf3");
2146 setLibcallName(RTLIB::SUB_F32, "__hexagon_fast_subsf3");
2147 setLibcallName(RTLIB::MUL_F32, "__hexagon_fast_mulsf3");
2148 setLibcallName(RTLIB::OGT_F32, "__hexagon_fast_gtsf2");
2149 setLibcallName(RTLIB::OLT_F32, "__hexagon_fast_ltsf2");
2150 // Double-precision compares.
2151 setLibcallName(RTLIB::OGT_F64, "__hexagon_fast_gtdf2");
2152 setLibcallName(RTLIB::OLT_F64, "__hexagon_fast_ltdf2");
2153 } else {
2154 setLibcallName(RTLIB::ADD_F32, "__hexagon_addsf3");
2155 setLibcallName(RTLIB::SUB_F32, "__hexagon_subsf3");
2156 setLibcallName(RTLIB::MUL_F32, "__hexagon_mulsf3");
2157 setLibcallName(RTLIB::OGT_F32, "__hexagon_gtsf2");
2158 setLibcallName(RTLIB::OLT_F32, "__hexagon_ltsf2");
2159 // Double-precision compares.
2160 setLibcallName(RTLIB::OGT_F64, "__hexagon_gtdf2");
2161 setLibcallName(RTLIB::OLT_F64, "__hexagon_ltdf2");
2162 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002163 }
2164
2165 // This is the only fast library function for sqrtd.
2166 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00002167 setLibcallName(RTLIB::SQRT_F64, "__hexagon_fast2_sqrtdf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002168
Benjamin Kramera37c8092015-04-25 14:46:46 +00002169 // Prefix is: nothing for "slow-math",
2170 // "fast2_" for V4 fast-math and V5+ fast-math double-precision
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002171 // (actually, keep fast-math and fast-math2 separate for now)
Benjamin Kramera37c8092015-04-25 14:46:46 +00002172 if (FastMath) {
2173 setLibcallName(RTLIB::ADD_F64, "__hexagon_fast_adddf3");
2174 setLibcallName(RTLIB::SUB_F64, "__hexagon_fast_subdf3");
2175 setLibcallName(RTLIB::MUL_F64, "__hexagon_fast_muldf3");
2176 setLibcallName(RTLIB::DIV_F64, "__hexagon_fast_divdf3");
2177 // Calling __hexagon_fast2_divsf3 with fast-math on V5 (ok).
2178 setLibcallName(RTLIB::DIV_F32, "__hexagon_fast_divsf3");
2179 } else {
2180 setLibcallName(RTLIB::ADD_F64, "__hexagon_adddf3");
2181 setLibcallName(RTLIB::SUB_F64, "__hexagon_subdf3");
2182 setLibcallName(RTLIB::MUL_F64, "__hexagon_muldf3");
2183 setLibcallName(RTLIB::DIV_F64, "__hexagon_divdf3");
2184 setLibcallName(RTLIB::DIV_F32, "__hexagon_divsf3");
2185 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002186
2187 if (Subtarget.hasV5TOps()) {
2188 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00002189 setLibcallName(RTLIB::SQRT_F32, "__hexagon_fast2_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002190 else
Benjamin Kramera37c8092015-04-25 14:46:46 +00002191 setLibcallName(RTLIB::SQRT_F32, "__hexagon_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002192 } else {
2193 // V4
Benjamin Kramera37c8092015-04-25 14:46:46 +00002194 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__hexagon_floatsisf");
2195 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__hexagon_floatsidf");
2196 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__hexagon_floatdisf");
2197 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__hexagon_floatdidf");
2198 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__hexagon_floatunsisf");
2199 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__hexagon_floatunsidf");
2200 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__hexagon_floatundisf");
2201 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__hexagon_floatundidf");
2202 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__hexagon_fixunssfsi");
2203 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__hexagon_fixunssfdi");
2204 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__hexagon_fixunsdfsi");
2205 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__hexagon_fixunsdfdi");
2206 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__hexagon_fixsfsi");
2207 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__hexagon_fixsfdi");
2208 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__hexagon_fixdfsi");
2209 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__hexagon_fixdfdi");
2210 setLibcallName(RTLIB::FPEXT_F32_F64, "__hexagon_extendsfdf2");
2211 setLibcallName(RTLIB::FPROUND_F64_F32, "__hexagon_truncdfsf2");
2212 setLibcallName(RTLIB::OEQ_F32, "__hexagon_eqsf2");
2213 setLibcallName(RTLIB::OEQ_F64, "__hexagon_eqdf2");
2214 setLibcallName(RTLIB::OGE_F32, "__hexagon_gesf2");
2215 setLibcallName(RTLIB::OGE_F64, "__hexagon_gedf2");
2216 setLibcallName(RTLIB::OLE_F32, "__hexagon_lesf2");
2217 setLibcallName(RTLIB::OLE_F64, "__hexagon_ledf2");
2218 setLibcallName(RTLIB::UNE_F32, "__hexagon_nesf2");
2219 setLibcallName(RTLIB::UNE_F64, "__hexagon_nedf2");
2220 setLibcallName(RTLIB::UO_F32, "__hexagon_unordsf2");
2221 setLibcallName(RTLIB::UO_F64, "__hexagon_unorddf2");
2222 setLibcallName(RTLIB::O_F32, "__hexagon_unordsf2");
2223 setLibcallName(RTLIB::O_F64, "__hexagon_unorddf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002224 }
2225
2226 // These cause problems when the shift amount is non-constant.
2227 setLibcallName(RTLIB::SHL_I128, nullptr);
2228 setLibcallName(RTLIB::SRL_I128, nullptr);
2229 setLibcallName(RTLIB::SRA_I128, nullptr);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002230}
2231
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002232
2233const char* HexagonTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00002234 switch ((HexagonISD::NodeType)Opcode) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002235 case HexagonISD::ALLOCA: return "HexagonISD::ALLOCA";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002236 case HexagonISD::AT_GOT: return "HexagonISD::AT_GOT";
2237 case HexagonISD::AT_PCREL: return "HexagonISD::AT_PCREL";
2238 case HexagonISD::BARRIER: return "HexagonISD::BARRIER";
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002239 case HexagonISD::CALL: return "HexagonISD::CALL";
2240 case HexagonISD::CALLnr: return "HexagonISD::CALLnr";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002241 case HexagonISD::CALLR: return "HexagonISD::CALLR";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002242 case HexagonISD::COMBINE: return "HexagonISD::COMBINE";
2243 case HexagonISD::CONST32_GP: return "HexagonISD::CONST32_GP";
2244 case HexagonISD::CONST32: return "HexagonISD::CONST32";
2245 case HexagonISD::CP: return "HexagonISD::CP";
2246 case HexagonISD::DCFETCH: return "HexagonISD::DCFETCH";
2247 case HexagonISD::EH_RETURN: return "HexagonISD::EH_RETURN";
2248 case HexagonISD::EXTRACTU: return "HexagonISD::EXTRACTU";
2249 case HexagonISD::EXTRACTURP: return "HexagonISD::EXTRACTURP";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002250 case HexagonISD::INSERT: return "HexagonISD::INSERT";
2251 case HexagonISD::INSERTRP: return "HexagonISD::INSERTRP";
2252 case HexagonISD::JT: return "HexagonISD::JT";
2253 case HexagonISD::PACKHL: return "HexagonISD::PACKHL";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002254 case HexagonISD::POPCOUNT: return "HexagonISD::POPCOUNT";
2255 case HexagonISD::RET_FLAG: return "HexagonISD::RET_FLAG";
2256 case HexagonISD::SHUFFEB: return "HexagonISD::SHUFFEB";
2257 case HexagonISD::SHUFFEH: return "HexagonISD::SHUFFEH";
2258 case HexagonISD::SHUFFOB: return "HexagonISD::SHUFFOB";
2259 case HexagonISD::SHUFFOH: return "HexagonISD::SHUFFOH";
2260 case HexagonISD::TC_RETURN: return "HexagonISD::TC_RETURN";
2261 case HexagonISD::VCMPBEQ: return "HexagonISD::VCMPBEQ";
2262 case HexagonISD::VCMPBGT: return "HexagonISD::VCMPBGT";
2263 case HexagonISD::VCMPBGTU: return "HexagonISD::VCMPBGTU";
2264 case HexagonISD::VCMPHEQ: return "HexagonISD::VCMPHEQ";
2265 case HexagonISD::VCMPHGT: return "HexagonISD::VCMPHGT";
2266 case HexagonISD::VCMPHGTU: return "HexagonISD::VCMPHGTU";
2267 case HexagonISD::VCMPWEQ: return "HexagonISD::VCMPWEQ";
2268 case HexagonISD::VCMPWGT: return "HexagonISD::VCMPWGT";
2269 case HexagonISD::VCMPWGTU: return "HexagonISD::VCMPWGTU";
Krzysztof Parzyszekc168c012015-12-03 16:47:20 +00002270 case HexagonISD::VCOMBINE: return "HexagonISD::VCOMBINE";
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002271 case HexagonISD::VPACK: return "HexagonISD::VPACK";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002272 case HexagonISD::VSHLH: return "HexagonISD::VSHLH";
2273 case HexagonISD::VSHLW: return "HexagonISD::VSHLW";
2274 case HexagonISD::VSPLATB: return "HexagonISD::VSPLTB";
2275 case HexagonISD::VSPLATH: return "HexagonISD::VSPLATH";
2276 case HexagonISD::VSRAH: return "HexagonISD::VSRAH";
2277 case HexagonISD::VSRAW: return "HexagonISD::VSRAW";
2278 case HexagonISD::VSRLH: return "HexagonISD::VSRLH";
2279 case HexagonISD::VSRLW: return "HexagonISD::VSRLW";
2280 case HexagonISD::VSXTBH: return "HexagonISD::VSXTBH";
2281 case HexagonISD::VSXTBW: return "HexagonISD::VSXTBW";
Matthias Braund04893f2015-05-07 21:33:59 +00002282 case HexagonISD::OP_END: break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002283 }
Matthias Braund04893f2015-05-07 21:33:59 +00002284 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002285}
2286
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002287bool HexagonTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002288 EVT MTy1 = EVT::getEVT(Ty1);
2289 EVT MTy2 = EVT::getEVT(Ty2);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002290 if (!MTy1.isSimple() || !MTy2.isSimple())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002291 return false;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002292 return (MTy1.getSimpleVT() == MVT::i64) && (MTy2.getSimpleVT() == MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002293}
2294
2295bool HexagonTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002296 if (!VT1.isSimple() || !VT2.isSimple())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002297 return false;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002298 return (VT1.getSimpleVT() == MVT::i64) && (VT2.getSimpleVT() == MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002299}
2300
Krzysztof Parzyszekbd8ef4b2016-08-19 13:34:31 +00002301bool HexagonTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
2302 return isOperationLegalOrCustom(ISD::FMA, VT);
2303}
2304
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002305// Should we expand the build vector with shuffles?
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00002306bool HexagonTargetLowering::shouldExpandBuildVectorWithShuffles(EVT VT,
2307 unsigned DefinedValues) const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002308 // Hexagon vector shuffle operates on element sizes of bytes or halfwords
2309 EVT EltVT = VT.getVectorElementType();
2310 int EltBits = EltVT.getSizeInBits();
2311 if ((EltBits != 8) && (EltBits != 16))
2312 return false;
2313
2314 return TargetLowering::shouldExpandBuildVectorWithShuffles(VT, DefinedValues);
2315}
2316
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00002317static StridedLoadKind isStridedLoad(const ArrayRef<int> &Mask) {
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002318 int even_start = -2;
2319 int odd_start = -1;
2320 size_t mask_len = Mask.size();
2321 for (auto idx : Mask) {
2322 if ((idx - even_start) == 2)
2323 even_start = idx;
2324 else
2325 break;
2326 }
2327 if (even_start == (int)(mask_len * 2) - 2)
2328 return StridedLoadKind::Even;
2329 for (auto idx : Mask) {
2330 if ((idx - odd_start) == 2)
2331 odd_start = idx;
2332 else
2333 break;
2334 }
2335 if (odd_start == (int)(mask_len * 2) - 1)
2336 return StridedLoadKind::Odd;
2337
2338 return StridedLoadKind::NoPattern;
2339}
2340
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00002341bool HexagonTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &Mask,
2342 EVT VT) const {
2343 if (Subtarget.useHVXOps())
2344 return isStridedLoad(Mask) != StridedLoadKind::NoPattern;
2345 return true;
2346}
2347
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002348// Lower a vector shuffle (V1, V2, V3). V1 and V2 are the two vectors
2349// to select data from, V3 is the permutation.
2350SDValue
2351HexagonTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG)
2352 const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002353 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
2354 SDValue V1 = Op.getOperand(0);
2355 SDValue V2 = Op.getOperand(1);
2356 SDLoc dl(Op);
2357 EVT VT = Op.getValueType();
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002358 bool UseHVX = Subtarget.useHVXOps();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002359
Sanjay Patel57195842016-03-14 17:28:46 +00002360 if (V2.isUndef())
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002361 V2 = V1;
2362
2363 if (SVN->isSplat()) {
2364 int Lane = SVN->getSplatIndex();
2365 if (Lane == -1) Lane = 0;
2366
2367 // Test if V1 is a SCALAR_TO_VECTOR.
2368 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR)
2369 return createSplat(DAG, dl, VT, V1.getOperand(0));
2370
2371 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
2372 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
2373 // reaches it).
2374 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
2375 !isa<ConstantSDNode>(V1.getOperand(0))) {
2376 bool IsScalarToVector = true;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00002377 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i) {
Sanjay Patel75068522016-03-14 18:09:43 +00002378 if (!V1.getOperand(i).isUndef()) {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002379 IsScalarToVector = false;
2380 break;
2381 }
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00002382 }
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002383 if (IsScalarToVector)
2384 return createSplat(DAG, dl, VT, V1.getOperand(0));
2385 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002386 return createSplat(DAG, dl, VT, DAG.getConstant(Lane, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002387 }
2388
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002389 if (UseHVX) {
2390 ArrayRef<int> Mask = SVN->getMask();
2391 size_t MaskLen = Mask.size();
Sanjay Patel1ed771f2016-09-14 16:37:15 +00002392 int ElemSizeInBits = VT.getScalarSizeInBits();
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002393 if ((Subtarget.useHVXSglOps() && (ElemSizeInBits * MaskLen) == 64 * 8) ||
2394 (Subtarget.useHVXDblOps() && (ElemSizeInBits * MaskLen) == 128 * 8)) {
2395 // Return 1 for odd and 2 of even
2396 StridedLoadKind Pattern = isStridedLoad(Mask);
2397
2398 if (Pattern == StridedLoadKind::NoPattern)
2399 return SDValue();
2400
2401 SDValue Vec0 = Op.getOperand(0);
2402 SDValue Vec1 = Op.getOperand(1);
2403 SDValue StridePattern = DAG.getConstant(Pattern, dl, MVT::i32);
2404 SDValue Ops[] = { Vec1, Vec0, StridePattern };
2405 return DAG.getNode(HexagonISD::VPACK, dl, VT, Ops);
2406 }
2407 // We used to assert in the "else" part here, but that is bad for Halide
2408 // Halide creates intermediate double registers by interleaving two
2409 // concatenated vector registers. The interleaving requires vector_shuffle
2410 // nodes and we shouldn't barf on a double register result of a
2411 // vector_shuffle because it is most likely an intermediate result.
2412 }
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002413 // FIXME: We need to support more general vector shuffles. See
2414 // below the comment from the ARM backend that deals in the general
2415 // case with the vector shuffles. For now, let expand handle these.
2416 return SDValue();
2417
2418 // If the shuffle is not directly supported and it has 4 elements, use
2419 // the PerfectShuffle-generated table to synthesize it from other shuffles.
2420}
2421
2422// If BUILD_VECTOR has same base element repeated several times,
2423// report true.
2424static bool isCommonSplatElement(BuildVectorSDNode *BVN) {
2425 unsigned NElts = BVN->getNumOperands();
2426 SDValue V0 = BVN->getOperand(0);
2427
2428 for (unsigned i = 1, e = NElts; i != e; ++i) {
2429 if (BVN->getOperand(i) != V0)
2430 return false;
2431 }
2432 return true;
2433}
2434
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002435// Lower a vector shift. Try to convert
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002436// <VT> = SHL/SRA/SRL <VT> by <VT> to Hexagon specific
2437// <VT> = SHL/SRA/SRL <VT> by <IT/i32>.
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002438SDValue
2439HexagonTargetLowering::LowerVECTOR_SHIFT(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002440 BuildVectorSDNode *BVN = 0;
2441 SDValue V1 = Op.getOperand(0);
2442 SDValue V2 = Op.getOperand(1);
2443 SDValue V3;
2444 SDLoc dl(Op);
2445 EVT VT = Op.getValueType();
2446
2447 if ((BVN = dyn_cast<BuildVectorSDNode>(V1.getNode())) &&
2448 isCommonSplatElement(BVN))
2449 V3 = V2;
2450 else if ((BVN = dyn_cast<BuildVectorSDNode>(V2.getNode())) &&
2451 isCommonSplatElement(BVN))
2452 V3 = V1;
2453 else
2454 return SDValue();
2455
2456 SDValue CommonSplat = BVN->getOperand(0);
2457 SDValue Result;
2458
2459 if (VT.getSimpleVT() == MVT::v4i16) {
2460 switch (Op.getOpcode()) {
2461 case ISD::SRA:
2462 Result = DAG.getNode(HexagonISD::VSRAH, dl, VT, V3, CommonSplat);
2463 break;
2464 case ISD::SHL:
2465 Result = DAG.getNode(HexagonISD::VSHLH, dl, VT, V3, CommonSplat);
2466 break;
2467 case ISD::SRL:
2468 Result = DAG.getNode(HexagonISD::VSRLH, dl, VT, V3, CommonSplat);
2469 break;
2470 default:
2471 return SDValue();
2472 }
2473 } else if (VT.getSimpleVT() == MVT::v2i32) {
2474 switch (Op.getOpcode()) {
2475 case ISD::SRA:
2476 Result = DAG.getNode(HexagonISD::VSRAW, dl, VT, V3, CommonSplat);
2477 break;
2478 case ISD::SHL:
2479 Result = DAG.getNode(HexagonISD::VSHLW, dl, VT, V3, CommonSplat);
2480 break;
2481 case ISD::SRL:
2482 Result = DAG.getNode(HexagonISD::VSRLW, dl, VT, V3, CommonSplat);
2483 break;
2484 default:
2485 return SDValue();
2486 }
2487 } else {
2488 return SDValue();
2489 }
2490
2491 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
2492}
2493
2494SDValue
2495HexagonTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
2496 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
2497 SDLoc dl(Op);
2498 EVT VT = Op.getValueType();
2499
2500 unsigned Size = VT.getSizeInBits();
2501
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002502 // Only handle vectors of 64 bits or shorter.
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002503 if (Size > 64)
2504 return SDValue();
2505
2506 APInt APSplatBits, APSplatUndef;
2507 unsigned SplatBitSize;
2508 bool HasAnyUndefs;
2509 unsigned NElts = BVN->getNumOperands();
2510
2511 // Try to generate a SPLAT instruction.
2512 if ((VT.getSimpleVT() == MVT::v4i8 || VT.getSimpleVT() == MVT::v4i16) &&
2513 (BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
2514 HasAnyUndefs, 0, true) && SplatBitSize <= 16)) {
2515 unsigned SplatBits = APSplatBits.getZExtValue();
2516 int32_t SextVal = ((int32_t) (SplatBits << (32 - SplatBitSize)) >>
2517 (32 - SplatBitSize));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002518 return createSplat(DAG, dl, VT, DAG.getConstant(SextVal, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002519 }
2520
2521 // Try to generate COMBINE to build v2i32 vectors.
2522 if (VT.getSimpleVT() == MVT::v2i32) {
2523 SDValue V0 = BVN->getOperand(0);
2524 SDValue V1 = BVN->getOperand(1);
2525
Sanjay Patel57195842016-03-14 17:28:46 +00002526 if (V0.isUndef())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002527 V0 = DAG.getConstant(0, dl, MVT::i32);
Sanjay Patel57195842016-03-14 17:28:46 +00002528 if (V1.isUndef())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002529 V1 = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002530
2531 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(V0);
2532 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(V1);
2533 // If the element isn't a constant, it is in a register:
2534 // generate a COMBINE Register Register instruction.
2535 if (!C0 || !C1)
2536 return DAG.getNode(HexagonISD::COMBINE, dl, VT, V1, V0);
2537
2538 // If one of the operands is an 8 bit integer constant, generate
2539 // a COMBINE Immediate Immediate instruction.
2540 if (isInt<8>(C0->getSExtValue()) ||
2541 isInt<8>(C1->getSExtValue()))
2542 return DAG.getNode(HexagonISD::COMBINE, dl, VT, V1, V0);
2543 }
2544
2545 // Try to generate a S2_packhl to build v2i16 vectors.
2546 if (VT.getSimpleVT() == MVT::v2i16) {
2547 for (unsigned i = 0, e = NElts; i != e; ++i) {
Sanjay Patel57195842016-03-14 17:28:46 +00002548 if (BVN->getOperand(i).isUndef())
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002549 continue;
2550 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(BVN->getOperand(i));
2551 // If the element isn't a constant, it is in a register:
2552 // generate a S2_packhl instruction.
2553 if (!Cst) {
2554 SDValue pack = DAG.getNode(HexagonISD::PACKHL, dl, MVT::v4i16,
2555 BVN->getOperand(1), BVN->getOperand(0));
2556
2557 return DAG.getTargetExtractSubreg(Hexagon::subreg_loreg, dl, MVT::v2i16,
2558 pack);
2559 }
2560 }
2561 }
2562
2563 // In the general case, generate a CONST32 or a CONST64 for constant vectors,
2564 // and insert_vector_elt for all the other cases.
2565 uint64_t Res = 0;
2566 unsigned EltSize = Size / NElts;
2567 SDValue ConstVal;
2568 uint64_t Mask = ~uint64_t(0ULL) >> (64 - EltSize);
2569 bool HasNonConstantElements = false;
2570
2571 for (unsigned i = 0, e = NElts; i != e; ++i) {
2572 // LLVM's BUILD_VECTOR operands are in Little Endian mode, whereas Hexagon's
2573 // combine, const64, etc. are Big Endian.
2574 unsigned OpIdx = NElts - i - 1;
2575 SDValue Operand = BVN->getOperand(OpIdx);
Sanjay Patel57195842016-03-14 17:28:46 +00002576 if (Operand.isUndef())
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002577 continue;
2578
2579 int64_t Val = 0;
2580 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Operand))
2581 Val = Cst->getSExtValue();
2582 else
2583 HasNonConstantElements = true;
2584
2585 Val &= Mask;
2586 Res = (Res << EltSize) | Val;
2587 }
2588
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002589 if (Size > 64)
2590 return SDValue();
2591
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002592 if (Size == 64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002593 ConstVal = DAG.getConstant(Res, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002594 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002595 ConstVal = DAG.getConstant(Res, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002596
2597 // When there are non constant operands, add them with INSERT_VECTOR_ELT to
2598 // ConstVal, the constant part of the vector.
2599 if (HasNonConstantElements) {
2600 EVT EltVT = VT.getVectorElementType();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002601 SDValue Width = DAG.getConstant(EltVT.getSizeInBits(), dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002602 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002603 DAG.getConstant(32, dl, MVT::i64));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002604
2605 for (unsigned i = 0, e = NElts; i != e; ++i) {
2606 // LLVM's BUILD_VECTOR operands are in Little Endian mode, whereas Hexagon
2607 // is Big Endian.
2608 unsigned OpIdx = NElts - i - 1;
2609 SDValue Operand = BVN->getOperand(OpIdx);
Benjamin Kramer619c4e52015-04-10 11:24:51 +00002610 if (isa<ConstantSDNode>(Operand))
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002611 // This operand is already in ConstVal.
2612 continue;
2613
2614 if (VT.getSizeInBits() == 64 &&
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00002615 Operand.getValueSizeInBits() == 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002616 SDValue C = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002617 Operand = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, Operand);
2618 }
2619
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002620 SDValue Idx = DAG.getConstant(OpIdx, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002621 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i64, Idx, Width);
2622 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2623 const SDValue Ops[] = {ConstVal, Operand, Combined};
2624
2625 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002626 ConstVal = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002627 else
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002628 ConstVal = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002629 }
2630 }
2631
2632 return DAG.getNode(ISD::BITCAST, dl, VT, ConstVal);
2633}
2634
2635SDValue
2636HexagonTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
2637 SelectionDAG &DAG) const {
2638 SDLoc dl(Op);
Krzysztof Parzyszekc168c012015-12-03 16:47:20 +00002639 bool UseHVX = Subtarget.useHVXOps();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002640 EVT VT = Op.getValueType();
2641 unsigned NElts = Op.getNumOperands();
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002642 SDValue Vec0 = Op.getOperand(0);
2643 EVT VecVT = Vec0.getValueType();
2644 unsigned Width = VecVT.getSizeInBits();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002645
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002646 if (NElts == 2) {
2647 MVT ST = VecVT.getSimpleVT();
2648 // We are trying to concat two v2i16 to a single v4i16, or two v4i8
2649 // into a single v8i8.
2650 if (ST == MVT::v2i16 || ST == MVT::v4i8)
2651 return DAG.getNode(HexagonISD::COMBINE, dl, VT, Op.getOperand(1), Vec0);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002652
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002653 if (UseHVX) {
2654 assert((Width == 64*8 && Subtarget.useHVXSglOps()) ||
2655 (Width == 128*8 && Subtarget.useHVXDblOps()));
2656 SDValue Vec1 = Op.getOperand(1);
2657 MVT OpTy = Subtarget.useHVXSglOps() ? MVT::v16i32 : MVT::v32i32;
2658 MVT ReTy = Subtarget.useHVXSglOps() ? MVT::v32i32 : MVT::v64i32;
2659 SDValue B0 = DAG.getNode(ISD::BITCAST, dl, OpTy, Vec0);
2660 SDValue B1 = DAG.getNode(ISD::BITCAST, dl, OpTy, Vec1);
2661 SDValue VC = DAG.getNode(HexagonISD::VCOMBINE, dl, ReTy, B1, B0);
2662 return DAG.getNode(ISD::BITCAST, dl, VT, VC);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002663 }
2664 }
2665
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002666 if (VT.getSizeInBits() != 32 && VT.getSizeInBits() != 64)
2667 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002668
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002669 SDValue C0 = DAG.getConstant(0, dl, MVT::i64);
2670 SDValue C32 = DAG.getConstant(32, dl, MVT::i64);
2671 SDValue W = DAG.getConstant(Width, dl, MVT::i64);
2672 // Create the "width" part of the argument to insert_rp/insertp_rp.
2673 SDValue S = DAG.getNode(ISD::SHL, dl, MVT::i64, W, C32);
2674 SDValue V = C0;
2675
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002676 for (unsigned i = 0, e = NElts; i != e; ++i) {
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002677 unsigned N = NElts-i-1;
2678 SDValue OpN = Op.getOperand(N);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002679
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00002680 if (VT.getSizeInBits() == 64 && OpN.getValueSizeInBits() == 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002681 SDValue C = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002682 OpN = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, OpN);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002683 }
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002684 SDValue Idx = DAG.getConstant(N, dl, MVT::i64);
2685 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i64, Idx, W);
2686 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, S, Offset);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002687 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002688 V = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, {V, OpN, Or});
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002689 else if (VT.getSizeInBits() == 64)
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002690 V = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, {V, OpN, Or});
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002691 else
2692 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002693 }
2694
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002695 return DAG.getNode(ISD::BITCAST, dl, VT, V);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002696}
2697
2698SDValue
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002699HexagonTargetLowering::LowerEXTRACT_SUBVECTOR_HVX(SDValue Op,
2700 SelectionDAG &DAG) const {
2701 EVT VT = Op.getOperand(0).getValueType();
2702 SDLoc dl(Op);
2703 bool UseHVX = Subtarget.useHVXOps();
2704 bool UseHVXSgl = Subtarget.useHVXSglOps();
2705 // Just in case...
2706
2707 if (!VT.isVector() || !UseHVX)
2708 return SDValue();
2709
2710 EVT ResVT = Op.getValueType();
2711 unsigned ResSize = ResVT.getSizeInBits();
2712 unsigned VectorSizeInBits = UseHVXSgl ? (64 * 8) : (128 * 8);
2713 unsigned OpSize = VT.getSizeInBits();
2714
2715 // We deal only with cases where the result is the vector size
2716 // and the vector operand is a double register.
2717 if (!(ResVT.isByteSized() && ResSize == VectorSizeInBits) ||
2718 !(VT.isByteSized() && OpSize == 2 * VectorSizeInBits))
2719 return SDValue();
2720
2721 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Op.getOperand(1));
2722 if (!Cst)
2723 return SDValue();
2724 unsigned Val = Cst->getZExtValue();
2725
2726 // These two will get lowered to an appropriate EXTRACT_SUBREG in ISel.
2727 if (Val == 0) {
2728 SDValue Vec = Op.getOperand(0);
2729 unsigned Subreg = Hexagon::subreg_loreg;
2730 return DAG.getTargetExtractSubreg(Subreg, dl, ResVT, Vec);
2731 }
2732
2733 if (ResVT.getVectorNumElements() == Val) {
2734 SDValue Vec = Op.getOperand(0);
2735 unsigned Subreg = Hexagon::subreg_hireg;
2736 return DAG.getTargetExtractSubreg(Subreg, dl, ResVT, Vec);
2737 }
2738
2739 return SDValue();
2740}
2741
2742SDValue
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002743HexagonTargetLowering::LowerEXTRACT_VECTOR(SDValue Op,
2744 SelectionDAG &DAG) const {
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002745 // If we are dealing with EXTRACT_SUBVECTOR on a HVX type, we may
2746 // be able to simplify it to an EXTRACT_SUBREG.
2747 if (Op.getOpcode() == ISD::EXTRACT_SUBVECTOR && Subtarget.useHVXOps() &&
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00002748 isHvxVectorType(Op.getValueType().getSimpleVT()))
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002749 return LowerEXTRACT_SUBVECTOR_HVX(Op, DAG);
2750
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002751 EVT VT = Op.getValueType();
2752 int VTN = VT.isVector() ? VT.getVectorNumElements() : 1;
2753 SDLoc dl(Op);
2754 SDValue Idx = Op.getOperand(1);
2755 SDValue Vec = Op.getOperand(0);
2756 EVT VecVT = Vec.getValueType();
2757 EVT EltVT = VecVT.getVectorElementType();
2758 int EltSize = EltVT.getSizeInBits();
2759 SDValue Width = DAG.getConstant(Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT ?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002760 EltSize : VTN * EltSize, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002761
2762 // Constant element number.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002763 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Idx)) {
2764 uint64_t X = CI->getZExtValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002765 SDValue Offset = DAG.getConstant(X * EltSize, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002766 const SDValue Ops[] = {Vec, Width, Offset};
2767
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002768 ConstantSDNode *CW = dyn_cast<ConstantSDNode>(Width);
2769 assert(CW && "Non constant width in LowerEXTRACT_VECTOR");
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002770
2771 SDValue N;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002772 MVT SVT = VecVT.getSimpleVT();
2773 uint64_t W = CW->getZExtValue();
2774
2775 if (W == 32) {
2776 // Translate this node into EXTRACT_SUBREG.
2777 unsigned Subreg = (X == 0) ? Hexagon::subreg_loreg : 0;
2778
2779 if (X == 0)
2780 Subreg = Hexagon::subreg_loreg;
2781 else if (SVT == MVT::v2i32 && X == 1)
2782 Subreg = Hexagon::subreg_hireg;
2783 else if (SVT == MVT::v4i16 && X == 2)
2784 Subreg = Hexagon::subreg_hireg;
2785 else if (SVT == MVT::v8i8 && X == 4)
2786 Subreg = Hexagon::subreg_hireg;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002787 else
2788 llvm_unreachable("Bad offset");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002789 N = DAG.getTargetExtractSubreg(Subreg, dl, MVT::i32, Vec);
2790
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002791 } else if (SVT.getSizeInBits() == 32) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002792 N = DAG.getNode(HexagonISD::EXTRACTU, dl, MVT::i32, Ops);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002793 } else if (SVT.getSizeInBits() == 64) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002794 N = DAG.getNode(HexagonISD::EXTRACTU, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002795 if (VT.getSizeInBits() == 32)
2796 N = DAG.getTargetExtractSubreg(Hexagon::subreg_loreg, dl, MVT::i32, N);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002797 } else
2798 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002799
2800 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2801 }
2802
2803 // Variable element number.
2804 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i32, Idx,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002805 DAG.getConstant(EltSize, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002806 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002807 DAG.getConstant(32, dl, MVT::i64));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002808 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2809
2810 const SDValue Ops[] = {Vec, Combined};
2811
2812 SDValue N;
2813 if (VecVT.getSizeInBits() == 32) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002814 N = DAG.getNode(HexagonISD::EXTRACTURP, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002815 } else {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002816 N = DAG.getNode(HexagonISD::EXTRACTURP, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002817 if (VT.getSizeInBits() == 32)
2818 N = DAG.getTargetExtractSubreg(Hexagon::subreg_loreg, dl, MVT::i32, N);
2819 }
2820 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2821}
2822
2823SDValue
2824HexagonTargetLowering::LowerINSERT_VECTOR(SDValue Op,
2825 SelectionDAG &DAG) const {
2826 EVT VT = Op.getValueType();
2827 int VTN = VT.isVector() ? VT.getVectorNumElements() : 1;
2828 SDLoc dl(Op);
2829 SDValue Vec = Op.getOperand(0);
2830 SDValue Val = Op.getOperand(1);
2831 SDValue Idx = Op.getOperand(2);
2832 EVT VecVT = Vec.getValueType();
2833 EVT EltVT = VecVT.getVectorElementType();
2834 int EltSize = EltVT.getSizeInBits();
2835 SDValue Width = DAG.getConstant(Op.getOpcode() == ISD::INSERT_VECTOR_ELT ?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002836 EltSize : VTN * EltSize, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002837
2838 if (ConstantSDNode *C = cast<ConstantSDNode>(Idx)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002839 SDValue Offset = DAG.getConstant(C->getSExtValue() * EltSize, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002840 const SDValue Ops[] = {Vec, Val, Width, Offset};
2841
2842 SDValue N;
2843 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002844 N = DAG.getNode(HexagonISD::INSERT, dl, MVT::i32, Ops);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002845 else if (VT.getSizeInBits() == 64)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002846 N = DAG.getNode(HexagonISD::INSERT, dl, MVT::i64, Ops);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002847 else
2848 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002849
2850 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2851 }
2852
2853 // Variable element number.
2854 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i32, Idx,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002855 DAG.getConstant(EltSize, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002856 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002857 DAG.getConstant(32, dl, MVT::i64));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002858 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2859
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00002860 if (VT.getSizeInBits() == 64 && Val.getValueSizeInBits() == 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002861 SDValue C = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002862 Val = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, Val);
2863 }
2864
2865 const SDValue Ops[] = {Vec, Val, Combined};
2866
2867 SDValue N;
2868 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002869 N = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, Ops);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002870 else if (VT.getSizeInBits() == 64)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002871 N = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, Ops);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002872 else
2873 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002874
2875 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2876}
2877
Tim Northovera4415852013-08-06 09:12:35 +00002878bool
2879HexagonTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
2880 // Assuming the caller does not have either a signext or zeroext modifier, and
2881 // only one value is accepted, any reasonable truncation is allowed.
2882 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
2883 return false;
2884
2885 // FIXME: in principle up to 64-bit could be made safe, but it would be very
2886 // fragile at the moment: any support for multiple value returns would be
2887 // liable to disallow tail calls involving i64 -> iN truncation in many cases.
2888 return Ty1->getPrimitiveSizeInBits() <= 32;
2889}
2890
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002891SDValue
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002892HexagonTargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
2893 SDValue Chain = Op.getOperand(0);
2894 SDValue Offset = Op.getOperand(1);
2895 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002896 SDLoc dl(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002897 auto PtrVT = getPointerTy(DAG.getDataLayout());
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002898
2899 // Mark function as containing a call to EH_RETURN.
2900 HexagonMachineFunctionInfo *FuncInfo =
2901 DAG.getMachineFunction().getInfo<HexagonMachineFunctionInfo>();
2902 FuncInfo->setHasEHReturn();
2903
2904 unsigned OffsetReg = Hexagon::R28;
2905
Mehdi Amini44ede332015-07-09 02:09:04 +00002906 SDValue StoreAddr =
2907 DAG.getNode(ISD::ADD, dl, PtrVT, DAG.getRegister(Hexagon::R30, PtrVT),
2908 DAG.getIntPtrConstant(4, dl));
Justin Lebar9c375812016-07-15 18:27:10 +00002909 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo());
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002910 Chain = DAG.getCopyToReg(Chain, dl, OffsetReg, Offset);
2911
2912 // Not needed we already use it as explict input to EH_RETURN.
2913 // MF.getRegInfo().addLiveOut(OffsetReg);
2914
2915 return DAG.getNode(HexagonISD::EH_RETURN, dl, MVT::Other, Chain);
2916}
2917
2918SDValue
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002919HexagonTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002920 unsigned Opc = Op.getOpcode();
2921 switch (Opc) {
2922 default:
2923#ifndef NDEBUG
2924 Op.getNode()->dumpr(&DAG);
2925 if (Opc > HexagonISD::OP_BEGIN && Opc < HexagonISD::OP_END)
2926 errs() << "Check for a non-legal type in this operation\n";
2927#endif
2928 llvm_unreachable("Should not custom lower this!");
2929 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
2930 case ISD::INSERT_SUBVECTOR: return LowerINSERT_VECTOR(Op, DAG);
2931 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR(Op, DAG);
2932 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_VECTOR(Op, DAG);
2933 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR(Op, DAG);
2934 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2935 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002936 case ISD::SRA:
2937 case ISD::SHL:
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002938 case ISD::SRL: return LowerVECTOR_SHIFT(Op, DAG);
2939 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002940 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002941 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
2942 // Frame & Return address. Currently unimplemented.
2943 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
2944 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00002945 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002946 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
2947 case ISD::GlobalAddress: return LowerGLOBALADDRESS(Op, DAG);
2948 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002949 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002950 case ISD::VASTART: return LowerVASTART(Op, DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002951 // Custom lower some vector loads.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002952 case ISD::LOAD: return LowerLOAD(Op, DAG);
2953 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
2954 case ISD::SETCC: return LowerSETCC(Op, DAG);
2955 case ISD::VSELECT: return LowerVSELECT(Op, DAG);
2956 case ISD::CTPOP: return LowerCTPOP(Op, DAG);
2957 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00002958 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002959 case ISD::INLINEASM: return LowerINLINEASM(Op, DAG);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00002960 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002961 }
2962}
2963
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002964/// Returns relocation base for the given PIC jumptable.
2965SDValue
2966HexagonTargetLowering::getPICJumpTableRelocBase(SDValue Table,
2967 SelectionDAG &DAG) const {
2968 int Idx = cast<JumpTableSDNode>(Table)->getIndex();
2969 EVT VT = Table.getValueType();
2970 SDValue T = DAG.getTargetJumpTable(Idx, VT, HexagonII::MO_PCREL);
2971 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Table), VT, T);
2972}
2973
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002974//===----------------------------------------------------------------------===//
2975// Inline Assembly Support
2976//===----------------------------------------------------------------------===//
2977
Krzysztof Parzyszekca3b5322016-05-18 14:34:51 +00002978TargetLowering::ConstraintType
2979HexagonTargetLowering::getConstraintType(StringRef Constraint) const {
2980 if (Constraint.size() == 1) {
2981 switch (Constraint[0]) {
2982 case 'q':
2983 case 'v':
2984 if (Subtarget.useHVXOps())
2985 return C_Register;
2986 break;
2987 }
2988 }
2989 return TargetLowering::getConstraintType(Constraint);
2990}
2991
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002992std::pair<unsigned, const TargetRegisterClass*>
Eric Christopher11e4df72015-02-26 22:38:43 +00002993HexagonTargetLowering::getRegForInlineAsmConstraint(
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00002994 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002995 bool UseHVX = Subtarget.useHVXOps(), UseHVXDbl = Subtarget.useHVXDblOps();
2996
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002997 if (Constraint.size() == 1) {
2998 switch (Constraint[0]) {
2999 case 'r': // R0-R31
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003000 switch (VT.SimpleTy) {
3001 default:
3002 llvm_unreachable("getRegForInlineAsmConstraint Unhandled data type");
3003 case MVT::i1:
3004 case MVT::i8:
3005 case MVT::i16:
3006 case MVT::i32:
3007 case MVT::f32:
3008 return std::make_pair(0U, &Hexagon::IntRegsRegClass);
3009 case MVT::i64:
3010 case MVT::f64:
3011 return std::make_pair(0U, &Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003012 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003013 case 'q': // q0-q3
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003014 switch (VT.SimpleTy) {
3015 default:
3016 llvm_unreachable("getRegForInlineAsmConstraint Unhandled data type");
3017 case MVT::v1024i1:
3018 case MVT::v512i1:
3019 case MVT::v32i16:
3020 case MVT::v16i32:
3021 case MVT::v64i8:
3022 case MVT::v8i64:
3023 return std::make_pair(0U, &Hexagon::VecPredRegsRegClass);
3024 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003025 case 'v': // V0-V31
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003026 switch (VT.SimpleTy) {
3027 default:
3028 llvm_unreachable("getRegForInlineAsmConstraint Unhandled data type");
3029 case MVT::v16i32:
3030 case MVT::v32i16:
3031 case MVT::v64i8:
3032 case MVT::v8i64:
3033 return std::make_pair(0U, &Hexagon::VectorRegsRegClass);
3034 case MVT::v32i32:
3035 case MVT::v64i16:
3036 case MVT::v16i64:
3037 case MVT::v128i8:
3038 if (Subtarget.hasV60TOps() && UseHVX && UseHVXDbl)
3039 return std::make_pair(0U, &Hexagon::VectorRegs128BRegClass);
3040 return std::make_pair(0U, &Hexagon::VecDblRegsRegClass);
3041 case MVT::v256i8:
3042 case MVT::v128i16:
3043 case MVT::v64i32:
3044 case MVT::v32i64:
3045 return std::make_pair(0U, &Hexagon::VecDblRegs128BRegClass);
3046 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003047
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003048 default:
Craig Toppere55c5562012-02-07 02:50:20 +00003049 llvm_unreachable("Unknown asm register class");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003050 }
3051 }
3052
Eric Christopher11e4df72015-02-26 22:38:43 +00003053 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003054}
3055
Sirish Pande69295b82012-05-10 20:20:25 +00003056/// isFPImmLegal - Returns true if the target can instruction select the
3057/// specified FP immediate natively. If false, the legalizer will
3058/// materialize the FP immediate as a load from a constant pool.
3059bool HexagonTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00003060 return Subtarget.hasV5TOps();
Sirish Pande69295b82012-05-10 20:20:25 +00003061}
3062
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003063/// isLegalAddressingMode - Return true if the addressing mode represented by
3064/// AM is legal for this target, for a load/store of the specified type.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +00003065bool HexagonTargetLowering::isLegalAddressingMode(const DataLayout &DL,
3066 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +00003067 unsigned AS) const {
Krzysztof Parzyszeked4e7822016-08-03 15:06:18 +00003068 if (Ty->isSized()) {
3069 // When LSR detects uses of the same base address to access different
3070 // types (e.g. unions), it will assume a conservative type for these
3071 // uses:
3072 // LSR Use: Kind=Address of void in addrspace(4294967295), ...
3073 // The type Ty passed here would then be "void". Skip the alignment
3074 // checks, but do not return false right away, since that confuses
3075 // LSR into crashing.
3076 unsigned A = DL.getABITypeAlignment(Ty);
3077 // The base offset must be a multiple of the alignment.
3078 if ((AM.BaseOffs % A) != 0)
3079 return false;
3080 // The shifted offset must fit in 11 bits.
3081 if (!isInt<11>(AM.BaseOffs >> Log2_32(A)))
3082 return false;
3083 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003084
3085 // No global is ever allowed as a base.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00003086 if (AM.BaseGV)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003087 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003088
3089 int Scale = AM.Scale;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00003090 if (Scale < 0)
3091 Scale = -Scale;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003092 switch (Scale) {
3093 case 0: // No scale reg, "r+i", "r", or just "i".
3094 break;
3095 default: // No scaled addressing mode.
3096 return false;
3097 }
3098 return true;
3099}
3100
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00003101/// Return true if folding a constant offset with the given GlobalAddress is
3102/// legal. It is frequently not legal in PIC relocation models.
3103bool HexagonTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA)
3104 const {
3105 return HTM.getRelocationModel() == Reloc::Static;
3106}
3107
3108
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003109/// isLegalICmpImmediate - Return true if the specified immediate is legal
3110/// icmp immediate, that is the target has icmp instructions which can compare
3111/// a register against the immediate without having to materialize the
3112/// immediate into a register.
3113bool HexagonTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
3114 return Imm >= -512 && Imm <= 511;
3115}
3116
3117/// IsEligibleForTailCallOptimization - Check whether the call is eligible
3118/// for tail call optimization. Targets which want to do tail call
3119/// optimization should implement this function.
3120bool HexagonTargetLowering::IsEligibleForTailCallOptimization(
3121 SDValue Callee,
3122 CallingConv::ID CalleeCC,
3123 bool isVarArg,
3124 bool isCalleeStructRet,
3125 bool isCallerStructRet,
3126 const SmallVectorImpl<ISD::OutputArg> &Outs,
3127 const SmallVectorImpl<SDValue> &OutVals,
3128 const SmallVectorImpl<ISD::InputArg> &Ins,
3129 SelectionDAG& DAG) const {
3130 const Function *CallerF = DAG.getMachineFunction().getFunction();
3131 CallingConv::ID CallerCC = CallerF->getCallingConv();
3132 bool CCMatch = CallerCC == CalleeCC;
3133
3134 // ***************************************************************************
3135 // Look for obvious safe cases to perform tail call optimization that do not
3136 // require ABI changes.
3137 // ***************************************************************************
3138
3139 // If this is a tail call via a function pointer, then don't do it!
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00003140 if (!isa<GlobalAddressSDNode>(Callee) &&
3141 !isa<ExternalSymbolSDNode>(Callee)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003142 return false;
3143 }
3144
Krzysztof Parzyszek0ba97542016-08-19 15:02:18 +00003145 // Do not optimize if the calling conventions do not match and the conventions
3146 // used are not C or Fast.
3147 if (!CCMatch) {
3148 bool R = (CallerCC == CallingConv::C || CallerCC == CallingConv::Fast);
3149 bool E = (CalleeCC == CallingConv::C || CalleeCC == CallingConv::Fast);
3150 // If R & E, then ok.
3151 if (!R || !E)
3152 return false;
3153 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003154
3155 // Do not tail call optimize vararg calls.
3156 if (isVarArg)
3157 return false;
3158
3159 // Also avoid tail call optimization if either caller or callee uses struct
3160 // return semantics.
3161 if (isCalleeStructRet || isCallerStructRet)
3162 return false;
3163
3164 // In addition to the cases above, we also disable Tail Call Optimization if
3165 // the calling convention code that at least one outgoing argument needs to
3166 // go on the stack. We cannot check that here because at this point that
3167 // information is not available.
3168 return true;
3169}
Colin LeMahieu025f8602014-12-08 21:19:18 +00003170
Krzysztof Parzyszek3e409e12016-08-02 18:34:31 +00003171/// Returns the target specific optimal type for load and store operations as
3172/// a result of memset, memcpy, and memmove lowering.
3173///
3174/// If DstAlign is zero that means it's safe to destination alignment can
3175/// satisfy any constraint. Similarly if SrcAlign is zero it means there isn't
3176/// a need to check it against alignment requirement, probably because the
3177/// source does not need to be loaded. If 'IsMemset' is true, that means it's
3178/// expanding a memset. If 'ZeroMemset' is true, that means it's a memset of
3179/// zero. 'MemcpyStrSrc' indicates whether the memcpy source is constant so it
3180/// does not need to be loaded. It returns EVT::Other if the type should be
3181/// determined using generic target-independent logic.
3182EVT HexagonTargetLowering::getOptimalMemOpType(uint64_t Size,
3183 unsigned DstAlign, unsigned SrcAlign, bool IsMemset, bool ZeroMemset,
3184 bool MemcpyStrSrc, MachineFunction &MF) const {
3185
3186 auto Aligned = [](unsigned GivenA, unsigned MinA) -> bool {
3187 return (GivenA % MinA) == 0;
3188 };
3189
3190 if (Size >= 8 && Aligned(DstAlign, 8) && (IsMemset || Aligned(SrcAlign, 8)))
3191 return MVT::i64;
3192 if (Size >= 4 && Aligned(DstAlign, 4) && (IsMemset || Aligned(SrcAlign, 4)))
3193 return MVT::i32;
3194 if (Size >= 2 && Aligned(DstAlign, 2) && (IsMemset || Aligned(SrcAlign, 2)))
3195 return MVT::i16;
3196
3197 return MVT::Other;
3198}
3199
Colin LeMahieu025f8602014-12-08 21:19:18 +00003200// Return true when the given node fits in a positive half word.
3201bool llvm::isPositiveHalfWord(SDNode *N) {
3202 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N);
3203 if (CN && CN->getSExtValue() > 0 && isInt<16>(CN->getSExtValue()))
3204 return true;
3205
3206 switch (N->getOpcode()) {
3207 default:
3208 return false;
3209 case ISD::SIGN_EXTEND_INREG:
3210 return true;
3211 }
3212}
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003213
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +00003214bool HexagonTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
3215 unsigned AS, unsigned Align, bool *Fast) const {
3216 if (Fast)
3217 *Fast = false;
3218
3219 switch (VT.getSimpleVT().SimpleTy) {
3220 default:
3221 return false;
3222 case MVT::v64i8:
3223 case MVT::v128i8:
3224 case MVT::v256i8:
3225 case MVT::v32i16:
3226 case MVT::v64i16:
3227 case MVT::v128i16:
3228 case MVT::v16i32:
3229 case MVT::v32i32:
3230 case MVT::v64i32:
3231 case MVT::v8i64:
3232 case MVT::v16i64:
3233 case MVT::v32i64:
3234 return true;
3235 }
3236 return false;
3237}
3238
Krzysztof Parzyszek3e137e32016-07-29 17:50:47 +00003239
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003240std::pair<const TargetRegisterClass*, uint8_t>
3241HexagonTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
3242 MVT VT) const {
3243 const TargetRegisterClass *RRC = nullptr;
3244
3245 uint8_t Cost = 1;
3246 switch (VT.SimpleTy) {
3247 default:
3248 return TargetLowering::findRepresentativeClass(TRI, VT);
3249 case MVT::v64i8:
3250 case MVT::v32i16:
3251 case MVT::v16i32:
3252 case MVT::v8i64:
3253 RRC = &Hexagon::VectorRegsRegClass;
3254 break;
3255 case MVT::v128i8:
3256 case MVT::v64i16:
3257 case MVT::v32i32:
3258 case MVT::v16i64:
3259 if (Subtarget.hasV60TOps() && Subtarget.useHVXOps() &&
3260 Subtarget.useHVXDblOps())
3261 RRC = &Hexagon::VectorRegs128BRegClass;
3262 else
3263 RRC = &Hexagon::VecDblRegsRegClass;
3264 break;
3265 case MVT::v256i8:
3266 case MVT::v128i16:
3267 case MVT::v64i32:
3268 case MVT::v32i64:
3269 RRC = &Hexagon::VecDblRegs128BRegClass;
3270 break;
3271 }
3272 return std::make_pair(RRC, Cost);
3273}
3274
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003275Value *HexagonTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
3276 AtomicOrdering Ord) const {
3277 BasicBlock *BB = Builder.GetInsertBlock();
3278 Module *M = BB->getParent()->getParent();
3279 Type *Ty = cast<PointerType>(Addr->getType())->getElementType();
3280 unsigned SZ = Ty->getPrimitiveSizeInBits();
3281 assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic loads supported");
3282 Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_L2_loadw_locked
3283 : Intrinsic::hexagon_L4_loadd_locked;
3284 Value *Fn = Intrinsic::getDeclaration(M, IntID);
3285 return Builder.CreateCall(Fn, Addr, "larx");
3286}
3287
3288/// Perform a store-conditional operation to Addr. Return the status of the
3289/// store. This should be 0 if the store succeeded, non-zero otherwise.
3290Value *HexagonTargetLowering::emitStoreConditional(IRBuilder<> &Builder,
3291 Value *Val, Value *Addr, AtomicOrdering Ord) const {
3292 BasicBlock *BB = Builder.GetInsertBlock();
3293 Module *M = BB->getParent()->getParent();
3294 Type *Ty = Val->getType();
3295 unsigned SZ = Ty->getPrimitiveSizeInBits();
3296 assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic stores supported");
3297 Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_S2_storew_locked
3298 : Intrinsic::hexagon_S4_stored_locked;
3299 Value *Fn = Intrinsic::getDeclaration(M, IntID);
3300 Value *Call = Builder.CreateCall(Fn, {Addr, Val}, "stcx");
3301 Value *Cmp = Builder.CreateICmpEQ(Call, Builder.getInt32(0), "");
3302 Value *Ext = Builder.CreateZExt(Cmp, Type::getInt32Ty(M->getContext()));
3303 return Ext;
3304}
3305
Ahmed Bougacha52468672015-09-11 17:08:28 +00003306TargetLowering::AtomicExpansionKind
3307HexagonTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003308 // Do not expand loads and stores that don't exceed 64 bits.
Ahmed Bougacha52468672015-09-11 17:08:28 +00003309 return LI->getType()->getPrimitiveSizeInBits() > 64
Tim Northoverf520eff2015-12-02 18:12:57 +00003310 ? AtomicExpansionKind::LLOnly
Ahmed Bougacha52468672015-09-11 17:08:28 +00003311 : AtomicExpansionKind::None;
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003312}
3313
3314bool HexagonTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
3315 // Do not expand loads and stores that don't exceed 64 bits.
3316 return SI->getValueOperand()->getType()->getPrimitiveSizeInBits() > 64;
3317}
Krzysztof Parzyszekf228c952016-06-22 16:07:10 +00003318
3319bool HexagonTargetLowering::shouldExpandAtomicCmpXchgInIR(
3320 AtomicCmpXchgInst *AI) const {
3321 const DataLayout &DL = AI->getModule()->getDataLayout();
3322 unsigned Size = DL.getTypeStoreSize(AI->getCompareOperand()->getType());
3323 return Size >= 4 && Size <= 8;
3324}