blob: cb106c83ec12837c014a6c1cacfbac0fa4f25d2a [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
108static bool IsHvxVectorType(MVT ty);
109
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.
162 unsigned ofst;
163 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 Parzyszek952d9512015-04-22 21:17:00 +0000166 ofst = State.AllocateStack(ArgFlags.getByValSize(),
167 ArgFlags.getByValAlign());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000168 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
169 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) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000182 ofst = State.AllocateStack(4, 4);
183 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
184 return false;
185 }
Sirish Pande69295b82012-05-10 20:20:25 +0000186 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000187 ofst = State.AllocateStack(8, 8);
188 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
189 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) {
193 ofst = State.AllocateStack(16, 16);
194 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
195 return false;
196 }
197 if (LocVT == MVT::v4i64 || LocVT == MVT::v8i32 || LocVT == MVT::v16i16 ||
198 LocVT == MVT::v32i8) {
199 ofst = State.AllocateStack(32, 32);
200 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
201 return false;
202 }
203 if (LocVT == MVT::v8i64 || LocVT == MVT::v16i32 || LocVT == MVT::v32i16 ||
204 LocVT == MVT::v64i8 || LocVT == MVT::v512i1) {
205 ofst = State.AllocateStack(64, 64);
206 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
207 return false;
208 }
209 if (LocVT == MVT::v16i64 || LocVT == MVT::v32i32 || LocVT == MVT::v64i16 ||
210 LocVT == MVT::v128i8 || LocVT == MVT::v1024i1) {
211 ofst = State.AllocateStack(128, 128);
212 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
213 return false;
214 }
215 if (LocVT == MVT::v32i64 || LocVT == MVT::v64i32 || LocVT == MVT::v128i16 ||
216 LocVT == MVT::v256i8) {
217 ofst = State.AllocateStack(256, 256);
218 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
219 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
269 if (IsHvxVectorType(LocVT)) {
270 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
Craig Toppere5e035a32015-12-05 07:13:35 +0000325 static const MCPhysReg VecLstS[] = { Hexagon::V0, Hexagon::V1,
326 Hexagon::V2, Hexagon::V3,
327 Hexagon::V4, Hexagon::V5,
328 Hexagon::V6, Hexagon::V7,
329 Hexagon::V8, Hexagon::V9,
330 Hexagon::V10, Hexagon::V11,
331 Hexagon::V12, Hexagon::V13,
332 Hexagon::V14, Hexagon::V15};
333 static const MCPhysReg VecLstD[] = { Hexagon::W0, Hexagon::W1,
334 Hexagon::W2, Hexagon::W3,
335 Hexagon::W4, Hexagon::W5,
336 Hexagon::W6, Hexagon::W7};
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000337 auto &MF = State.getMachineFunction();
338 auto &HST = MF.getSubtarget<HexagonSubtarget>();
339 bool UseHVX = HST.useHVXOps();
340 bool UseHVXDbl = HST.useHVXDblOps();
341
342 if ((UseHVX && !UseHVXDbl) &&
343 (LocVT == MVT::v8i64 || LocVT == MVT::v16i32 || LocVT == MVT::v32i16 ||
344 LocVT == MVT::v64i8 || LocVT == MVT::v512i1)) {
345 if (unsigned Reg = State.AllocateReg(VecLstS)) {
346 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
347 return false;
348 }
349 unsigned Offset = State.AllocateStack(64, 64);
350 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
351 return false;
352 }
353 if ((UseHVX && !UseHVXDbl) &&
354 (LocVT == MVT::v16i64 || LocVT == MVT::v32i32 || LocVT == MVT::v64i16 ||
355 LocVT == MVT::v128i8)) {
356 if (unsigned Reg = State.AllocateReg(VecLstD)) {
357 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
358 return false;
359 }
360 unsigned Offset = State.AllocateStack(128, 128);
361 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
362 return false;
363 }
364 // 128B Mode
365 if ((UseHVX && UseHVXDbl) &&
366 (LocVT == MVT::v32i64 || LocVT == MVT::v64i32 || LocVT == MVT::v128i16 ||
367 LocVT == MVT::v256i8)) {
368 if (unsigned Reg = State.AllocateReg(VecLstD)) {
369 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
370 return false;
371 }
372 unsigned Offset = State.AllocateStack(256, 256);
373 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
374 return false;
375 }
376 if ((UseHVX && UseHVXDbl) &&
377 (LocVT == MVT::v16i64 || LocVT == MVT::v32i32 || LocVT == MVT::v64i16 ||
378 LocVT == MVT::v128i8 || LocVT == MVT::v1024i1)) {
379 if (unsigned Reg = State.AllocateReg(VecLstS)) {
380 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
381 return false;
382 }
383 unsigned Offset = State.AllocateStack(128, 128);
384 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
385 return false;
386 }
387 return true;
388}
389
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000390static bool RetCC_Hexagon(unsigned ValNo, MVT ValVT,
391 MVT LocVT, CCValAssign::LocInfo LocInfo,
392 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000393 auto &MF = State.getMachineFunction();
394 auto &HST = MF.getSubtarget<HexagonSubtarget>();
395 bool UseHVX = HST.useHVXOps();
396 bool UseHVXDbl = HST.useHVXDblOps();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000397
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000398 if (LocVT == MVT::i1) {
399 // Return values of type MVT::i1 still need to be assigned to R0, but
400 // the value type needs to remain i1. LowerCallResult will deal with it,
401 // but it needs to recognize i1 as the value type.
402 LocVT = MVT::i32;
403 } else if (LocVT == MVT::i8 || LocVT == MVT::i16) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000404 LocVT = MVT::i32;
405 ValVT = MVT::i32;
406 if (ArgFlags.isSExt())
407 LocInfo = CCValAssign::SExt;
408 else if (ArgFlags.isZExt())
409 LocInfo = CCValAssign::ZExt;
410 else
411 LocInfo = CCValAssign::AExt;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000412 } else if (LocVT == MVT::v4i8 || LocVT == MVT::v2i16) {
413 LocVT = MVT::i32;
414 LocInfo = CCValAssign::BCvt;
415 } else if (LocVT == MVT::v8i8 || LocVT == MVT::v4i16 || LocVT == MVT::v2i32) {
416 LocVT = MVT::i64;
417 LocInfo = CCValAssign::BCvt;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000418 } else if (LocVT == MVT::v64i8 || LocVT == MVT::v32i16 ||
419 LocVT == MVT::v16i32 || LocVT == MVT::v8i64 ||
420 LocVT == MVT::v512i1) {
421 LocVT = MVT::v16i32;
422 ValVT = MVT::v16i32;
423 LocInfo = CCValAssign::Full;
424 } else if (LocVT == MVT::v128i8 || LocVT == MVT::v64i16 ||
425 LocVT == MVT::v32i32 || LocVT == MVT::v16i64 ||
426 (LocVT == MVT::v1024i1 && UseHVX && UseHVXDbl)) {
427 LocVT = MVT::v32i32;
428 ValVT = MVT::v32i32;
429 LocInfo = CCValAssign::Full;
430 } else if (LocVT == MVT::v256i8 || LocVT == MVT::v128i16 ||
431 LocVT == MVT::v64i32 || LocVT == MVT::v32i64) {
432 LocVT = MVT::v64i32;
433 ValVT = MVT::v64i32;
434 LocInfo = CCValAssign::Full;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000435 }
Sirish Pande69295b82012-05-10 20:20:25 +0000436 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000437 if (!RetCC_Hexagon32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
438 return false;
439 }
440
Sirish Pande69295b82012-05-10 20:20:25 +0000441 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000442 if (!RetCC_Hexagon64(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
443 return false;
444 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000445 if (LocVT == MVT::v16i32 || LocVT == MVT::v32i32 || LocVT == MVT::v64i32) {
446 if (!RetCC_HexagonVector(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
447 return false;
448 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000449 return true; // CC didn't match.
450}
451
452static bool RetCC_Hexagon32(unsigned ValNo, MVT ValVT,
453 MVT LocVT, CCValAssign::LocInfo LocInfo,
454 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Sirish Pande69295b82012-05-10 20:20:25 +0000455 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Krzysztof Parzyszek14412ef2016-07-18 17:36:46 +0000456 // Note that use of registers beyond R1 is not ABI compliant. However there
457 // are (experimental) IR passes which generate internal functions that
458 // return structs using these additional registers.
459 static const uint16_t RegList[] = { Hexagon::R0, Hexagon::R1,
460 Hexagon::R2, Hexagon::R3,
461 Hexagon::R4, Hexagon::R5};
462 if (unsigned Reg = State.AllocateReg(RegList)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000463 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
464 return false;
465 }
466 }
467
468 unsigned Offset = State.AllocateStack(4, 4);
469 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
470 return false;
471}
472
473static bool RetCC_Hexagon64(unsigned ValNo, MVT ValVT,
474 MVT LocVT, CCValAssign::LocInfo LocInfo,
475 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Sirish Pande69295b82012-05-10 20:20:25 +0000476 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000477 if (unsigned Reg = State.AllocateReg(Hexagon::D0)) {
478 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
479 return false;
480 }
481 }
482
483 unsigned Offset = State.AllocateStack(8, 8);
484 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
485 return false;
486}
487
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000488static bool RetCC_HexagonVector(unsigned ValNo, MVT ValVT,
489 MVT LocVT, CCValAssign::LocInfo LocInfo,
490 ISD::ArgFlagsTy ArgFlags, CCState &State) {
491 auto &MF = State.getMachineFunction();
492 auto &HST = MF.getSubtarget<HexagonSubtarget>();
493 bool UseHVX = HST.useHVXOps();
494 bool UseHVXDbl = HST.useHVXDblOps();
495
496 unsigned OffSiz = 64;
497 if (LocVT == MVT::v16i32) {
498 if (unsigned Reg = State.AllocateReg(Hexagon::V0)) {
499 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
500 return false;
501 }
502 } else if (LocVT == MVT::v32i32) {
503 unsigned Req = (UseHVX && UseHVXDbl) ? Hexagon::V0 : Hexagon::W0;
504 if (unsigned Reg = State.AllocateReg(Req)) {
505 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
506 return false;
507 }
508 OffSiz = 128;
509 } else if (LocVT == MVT::v64i32) {
510 if (unsigned Reg = State.AllocateReg(Hexagon::W0)) {
511 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
512 return false;
513 }
514 OffSiz = 256;
515 }
516
517 unsigned Offset = State.AllocateStack(OffSiz, OffSiz);
518 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
519 return false;
520}
521
Craig Topper18e69f42016-04-15 06:20:21 +0000522void HexagonTargetLowering::promoteLdStType(MVT VT, MVT PromotedLdStVT) {
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000523 if (VT != PromotedLdStVT) {
Craig Topper18e69f42016-04-15 06:20:21 +0000524 setOperationAction(ISD::LOAD, VT, Promote);
525 AddPromotedToType(ISD::LOAD, VT, PromotedLdStVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000526
Craig Topper18e69f42016-04-15 06:20:21 +0000527 setOperationAction(ISD::STORE, VT, Promote);
528 AddPromotedToType(ISD::STORE, VT, PromotedLdStVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000529 }
530}
531
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000532SDValue
533HexagonTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG)
534const {
535 return SDValue();
536}
537
538/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
539/// by "Src" to address "Dst" of size "Size". Alignment information is
540/// specified by the specific parameter attribute. The copy will be passed as
541/// a byval function parameter. Sometimes what we are copying is the end of a
542/// larger object, the part that does not fit in registers.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000543static SDValue CreateCopyOfByValArgument(SDValue Src, SDValue Dst,
544 SDValue Chain, ISD::ArgFlagsTy Flags,
545 SelectionDAG &DAG, const SDLoc &dl) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000546
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000547 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000548 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
549 /*isVolatile=*/false, /*AlwaysInline=*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +0000550 /*isTailCall=*/false,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000551 MachinePointerInfo(), MachinePointerInfo());
552}
553
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000554static bool IsHvxVectorType(MVT ty) {
555 return (ty == MVT::v8i64 || ty == MVT::v16i32 || ty == MVT::v32i16 ||
556 ty == MVT::v64i8 ||
557 ty == MVT::v16i64 || ty == MVT::v32i32 || ty == MVT::v64i16 ||
558 ty == MVT::v128i8 ||
559 ty == MVT::v32i64 || ty == MVT::v64i32 || ty == MVT::v128i16 ||
560 ty == MVT::v256i8 ||
561 ty == MVT::v512i1 || ty == MVT::v1024i1);
562}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000563
564// LowerReturn - Lower ISD::RET. If a struct is larger than 8 bytes and is
565// passed by value, the function prototype is modified to return void and
566// the value is stored in memory pointed by a pointer passed by caller.
567SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000568HexagonTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
569 bool isVarArg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000570 const SmallVectorImpl<ISD::OutputArg> &Outs,
571 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000572 const SDLoc &dl, SelectionDAG &DAG) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000573
574 // CCValAssign - represent the assignment of the return value to locations.
575 SmallVector<CCValAssign, 16> RVLocs;
576
577 // CCState - Info about the registers and stack slot.
Eric Christopherb5217502014-08-06 18:45:26 +0000578 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
579 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000580
581 // Analyze return values of ISD::RET
582 CCInfo.AnalyzeReturn(Outs, RetCC_Hexagon);
583
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000584 SDValue Flag;
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000585 SmallVector<SDValue, 4> RetOps(1, Chain);
586
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000587 // Copy the result values into the output registers.
588 for (unsigned i = 0; i != RVLocs.size(); ++i) {
589 CCValAssign &VA = RVLocs[i];
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000590
591 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), OutVals[i], Flag);
592
593 // Guarantee that all emitted copies are stuck together with flags.
594 Flag = Chain.getValue(1);
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000595 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000596 }
597
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000598 RetOps[0] = Chain; // Update chain.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000599
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000600 // Add the flag if we have it.
601 if (Flag.getNode())
602 RetOps.push_back(Flag);
603
Craig Topper48d114b2014-04-26 18:35:24 +0000604 return DAG.getNode(HexagonISD::RET_FLAG, dl, MVT::Other, RetOps);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000605}
606
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000607bool HexagonTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
608 // If either no tail call or told not to tail call at all, don't.
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000609 auto Attr =
610 CI->getParent()->getParent()->getFnAttribute("disable-tail-calls");
611 if (!CI->isTailCall() || Attr.getValueAsString() == "true")
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000612 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000613
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000614 return true;
615}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000616
617/// LowerCallResult - Lower the result values of an ISD::CALL into the
618/// appropriate copies out of appropriate physical registers. This assumes that
619/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
620/// being lowered. Returns a SDNode with the same number of values as the
621/// ISD::CALL.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000622SDValue HexagonTargetLowering::LowerCallResult(
623 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool isVarArg,
624 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
625 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals,
626 const SmallVectorImpl<SDValue> &OutVals, SDValue Callee) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000627 // Assign locations to each value returned by this call.
628 SmallVector<CCValAssign, 16> RVLocs;
629
Eric Christopherb5217502014-08-06 18:45:26 +0000630 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
631 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000632
633 CCInfo.AnalyzeCallResult(Ins, RetCC_Hexagon);
634
635 // Copy all of the result registers out of their specified physreg.
636 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000637 SDValue RetVal;
638 if (RVLocs[i].getValVT() == MVT::i1) {
639 // Return values of type MVT::i1 require special handling. The reason
640 // is that MVT::i1 is associated with the PredRegs register class, but
641 // values of that type are still returned in R0. Generate an explicit
642 // copy into a predicate register from R0, and treat the value of the
643 // predicate register as the call result.
644 auto &MRI = DAG.getMachineFunction().getRegInfo();
645 SDValue FR0 = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
646 MVT::i32, InFlag);
647 // FR0 = (Value, Chain, Glue)
648 unsigned PredR = MRI.createVirtualRegister(&Hexagon::PredRegsRegClass);
649 SDValue TPR = DAG.getCopyToReg(FR0.getValue(1), dl, PredR,
650 FR0.getValue(0), FR0.getValue(2));
651 // TPR = (Chain, Glue)
652 RetVal = DAG.getCopyFromReg(TPR.getValue(0), dl, PredR, MVT::i1,
653 TPR.getValue(1));
654 } else {
655 RetVal = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
656 RVLocs[i].getValVT(), InFlag);
657 }
658 InVals.push_back(RetVal.getValue(0));
659 Chain = RetVal.getValue(1);
660 InFlag = RetVal.getValue(2);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000661 }
662
663 return Chain;
664}
665
666/// LowerCall - Functions arguments are copied from virtual regs to
667/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
668SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +0000669HexagonTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000670 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +0000671 SelectionDAG &DAG = CLI.DAG;
Craig Topperb94011f2013-07-14 04:42:23 +0000672 SDLoc &dl = CLI.DL;
673 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
674 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
675 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000676 SDValue Chain = CLI.Chain;
677 SDValue Callee = CLI.Callee;
678 bool &isTailCall = CLI.IsTailCall;
679 CallingConv::ID CallConv = CLI.CallConv;
680 bool isVarArg = CLI.IsVarArg;
Colin LeMahieu2e3a26d2015-01-16 17:05:27 +0000681 bool doesNotReturn = CLI.DoesNotReturn;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000682
683 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000684 MachineFunction &MF = DAG.getMachineFunction();
Mehdi Amini44ede332015-07-09 02:09:04 +0000685 auto PtrVT = getPointerTy(MF.getDataLayout());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000686
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000687 // Check for varargs.
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000688 int NumNamedVarArgParams = -1;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000689 if (GlobalAddressSDNode *GAN = dyn_cast<GlobalAddressSDNode>(Callee)) {
690 const GlobalValue *GV = GAN->getGlobal();
691 Callee = DAG.getTargetGlobalAddress(GV, dl, MVT::i32);
692 if (const Function* F = dyn_cast<Function>(GV)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000693 // If a function has zero args and is a vararg function, that's
694 // disallowed so it must be an undeclared function. Do not assume
695 // varargs if the callee is undefined.
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000696 if (F->isVarArg() && F->getFunctionType()->getNumParams() != 0)
697 NumNamedVarArgParams = F->getFunctionType()->getNumParams();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000698 }
699 }
700
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000701 // Analyze operands of the call, assigning locations to each operand.
702 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000703 HexagonCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
704 *DAG.getContext(), NumNamedVarArgParams);
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000705
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000706 if (isVarArg)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000707 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon_VarArg);
708 else
709 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon);
710
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000711 auto Attr = MF.getFunction()->getFnAttribute("disable-tail-calls");
712 if (Attr.getValueAsString() == "true")
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000713 isTailCall = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000714
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000715 if (isTailCall) {
716 bool StructAttrFlag = MF.getFunction()->hasStructRetAttr();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000717 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
718 isVarArg, IsStructRet,
719 StructAttrFlag,
720 Outs, OutVals, Ins, DAG);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000721 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000722 CCValAssign &VA = ArgLocs[i];
723 if (VA.isMemLoc()) {
724 isTailCall = false;
725 break;
726 }
727 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000728 DEBUG(dbgs() << (isTailCall ? "Eligible for Tail Call\n"
729 : "Argument must be passed on stack. "
730 "Not eligible for Tail Call\n"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000731 }
732 // Get a count of how many bytes are to be pushed on the stack.
733 unsigned NumBytes = CCInfo.getNextStackOffset();
734 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
735 SmallVector<SDValue, 8> MemOpChains;
736
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000737 auto &HRI = *Subtarget.getRegisterInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +0000738 SDValue StackPtr =
739 DAG.getCopyFromReg(Chain, dl, HRI.getStackRegister(), PtrVT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000740
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000741 bool NeedsArgAlign = false;
742 unsigned LargestAlignSeen = 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000743 // Walk the register/memloc assignments, inserting copies/loads.
744 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
745 CCValAssign &VA = ArgLocs[i];
746 SDValue Arg = OutVals[i];
747 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000748 // Record if we need > 8 byte alignment on an argument.
749 bool ArgAlign = IsHvxVectorType(VA.getValVT());
750 NeedsArgAlign |= ArgAlign;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000751
752 // Promote the value if needed.
753 switch (VA.getLocInfo()) {
754 default:
755 // Loc info must be one of Full, SExt, ZExt, or AExt.
Craig Toppere55c5562012-02-07 02:50:20 +0000756 llvm_unreachable("Unknown loc info!");
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000757 case CCValAssign::BCvt:
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000758 case CCValAssign::Full:
759 break;
760 case CCValAssign::SExt:
761 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
762 break;
763 case CCValAssign::ZExt:
764 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
765 break;
766 case CCValAssign::AExt:
767 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
768 break;
769 }
770
771 if (VA.isMemLoc()) {
772 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000773 SDValue MemAddr = DAG.getConstant(LocMemOffset, dl,
774 StackPtr.getValueType());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000775 MemAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, MemAddr);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000776 if (ArgAlign)
777 LargestAlignSeen = std::max(LargestAlignSeen,
778 VA.getLocVT().getStoreSizeInBits() >> 3);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000779 if (Flags.isByVal()) {
780 // The argument is a struct passed by value. According to LLVM, "Arg"
781 // is is pointer.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000782 MemOpChains.push_back(CreateCopyOfByValArgument(Arg, MemAddr, Chain,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000783 Flags, DAG, dl));
784 } else {
Alex Lorenze40c8a22015-08-11 23:09:45 +0000785 MachinePointerInfo LocPI = MachinePointerInfo::getStack(
786 DAG.getMachineFunction(), LocMemOffset);
Justin Lebar9c375812016-07-15 18:27:10 +0000787 SDValue S = DAG.getStore(Chain, dl, Arg, MemAddr, LocPI);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000788 MemOpChains.push_back(S);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000789 }
790 continue;
791 }
792
793 // Arguments that can be passed on register must be kept at RegsToPass
794 // vector.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000795 if (VA.isRegLoc())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000796 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000797 }
798
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000799 if (NeedsArgAlign && Subtarget.hasV60TOps()) {
800 DEBUG(dbgs() << "Function needs byte stack align due to call args\n");
Matthias Braun941a7052016-07-28 18:40:00 +0000801 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000802 // V6 vectors passed by value have 64 or 128 byte alignment depending
803 // on whether we are 64 byte vector mode or 128 byte.
804 bool UseHVXDbl = Subtarget.useHVXDblOps();
805 assert(Subtarget.useHVXOps());
806 const unsigned ObjAlign = UseHVXDbl ? 128 : 64;
807 LargestAlignSeen = std::max(LargestAlignSeen, ObjAlign);
Matthias Braun941a7052016-07-28 18:40:00 +0000808 MFI.ensureMaxAlignment(LargestAlignSeen);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000809 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000810 // Transform all store nodes into one single node because all store
811 // nodes are independent of each other.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000812 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000813 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000814
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000815 if (!isTailCall) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000816 SDValue C = DAG.getConstant(NumBytes, dl, PtrVT, true);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000817 Chain = DAG.getCALLSEQ_START(Chain, C, dl);
818 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000819
820 // Build a sequence of copy-to-reg nodes chained together with token
821 // chain and flag operands which copy the outgoing args into registers.
Benjamin Kramerbde91762012-06-02 10:20:22 +0000822 // The InFlag in necessary since all emitted instructions must be
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000823 // stuck together.
824 SDValue InFlag;
825 if (!isTailCall) {
826 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
827 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
828 RegsToPass[i].second, InFlag);
829 InFlag = Chain.getValue(1);
830 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000831 } else {
832 // For tail calls lower the arguments to the 'real' stack slot.
833 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000834 // Force all the incoming stack arguments to be loaded from the stack
835 // before any new outgoing arguments are stored to the stack, because the
836 // outgoing stack slots may alias the incoming argument stack slots, and
837 // the alias isn't otherwise explicit. This is slightly more conservative
838 // than necessary, because it means that each store effectively depends
839 // on every argument instead of just those arguments it would clobber.
840 //
Benjamin Kramerbde91762012-06-02 10:20:22 +0000841 // Do not flag preceding copytoreg stuff together with the following stuff.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000842 InFlag = SDValue();
843 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
844 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
845 RegsToPass[i].second, InFlag);
846 InFlag = Chain.getValue(1);
847 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000848 InFlag = SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000849 }
850
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000851 bool LongCalls = MF.getSubtarget<HexagonSubtarget>().useLongCalls();
852 unsigned Flags = LongCalls ? HexagonII::HMOTF_ConstExtended : 0;
853
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000854 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
855 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
856 // node so that legalize doesn't hack it.
Tobias Edler von Kochb51460c2015-12-16 17:29:37 +0000857 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000858 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, PtrVT, 0, Flags);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000859 } else if (ExternalSymbolSDNode *S =
860 dyn_cast<ExternalSymbolSDNode>(Callee)) {
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000861 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), PtrVT, Flags);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000862 }
863
864 // Returns a chain & a flag for retval copy to use.
865 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
866 SmallVector<SDValue, 8> Ops;
867 Ops.push_back(Chain);
868 Ops.push_back(Callee);
869
870 // Add argument registers to the end of the list so that they are
871 // known live into the call.
872 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
873 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
874 RegsToPass[i].second.getValueType()));
875 }
876
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000877 if (InFlag.getNode())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000878 Ops.push_back(InFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000879
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +0000880 if (isTailCall) {
Matthias Braun941a7052016-07-28 18:40:00 +0000881 MF.getFrameInfo().setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +0000882 return DAG.getNode(HexagonISD::TC_RETURN, dl, NodeTys, Ops);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +0000883 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000884
Colin LeMahieu2e3a26d2015-01-16 17:05:27 +0000885 int OpCode = doesNotReturn ? HexagonISD::CALLv3nr : HexagonISD::CALLv3;
886 Chain = DAG.getNode(OpCode, dl, NodeTys, Ops);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000887 InFlag = Chain.getValue(1);
888
889 // Create the CALLSEQ_END node.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000890 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
891 DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000892 InFlag = Chain.getValue(1);
893
894 // Handle result values, copying them out of physregs into vregs that we
895 // return.
896 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
897 InVals, OutVals, Callee);
898}
899
900static bool getIndexedAddressParts(SDNode *Ptr, EVT VT,
901 bool isSEXTLoad, SDValue &Base,
902 SDValue &Offset, bool &isInc,
903 SelectionDAG &DAG) {
904 if (Ptr->getOpcode() != ISD::ADD)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000905 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000906
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000907 auto &HST = static_cast<const HexagonSubtarget&>(DAG.getSubtarget());
908 bool UseHVX = HST.useHVXOps();
909 bool UseHVXDbl = HST.useHVXDblOps();
910
911 bool ValidHVXDblType =
912 (UseHVX && UseHVXDbl) && (VT == MVT::v32i32 || VT == MVT::v16i64 ||
913 VT == MVT::v64i16 || VT == MVT::v128i8);
914 bool ValidHVXType =
915 UseHVX && !UseHVXDbl && (VT == MVT::v16i32 || VT == MVT::v8i64 ||
916 VT == MVT::v32i16 || VT == MVT::v64i8);
917
918 if (ValidHVXDblType || ValidHVXType ||
919 VT == MVT::i64 || VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000920 isInc = (Ptr->getOpcode() == ISD::ADD);
921 Base = Ptr->getOperand(0);
922 Offset = Ptr->getOperand(1);
923 // Ensure that Offset is a constant.
924 return (isa<ConstantSDNode>(Offset));
925 }
926
927 return false;
928}
929
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000930/// getPostIndexedAddressParts - returns true by value, base pointer and
931/// offset pointer and addressing mode by reference if this node can be
932/// combined with a load / store to form a post-indexed load / store.
933bool HexagonTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
934 SDValue &Base,
935 SDValue &Offset,
936 ISD::MemIndexedMode &AM,
937 SelectionDAG &DAG) const
938{
939 EVT VT;
940 SDValue Ptr;
941 bool isSEXTLoad = false;
942
943 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
944 VT = LD->getMemoryVT();
945 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
946 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
947 VT = ST->getMemoryVT();
948 if (ST->getValue().getValueType() == MVT::i64 && ST->isTruncatingStore()) {
949 return false;
950 }
951 } else {
952 return false;
953 }
954
Chad Rosier64dc8aa2012-01-06 20:11:59 +0000955 bool isInc = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000956 bool isLegal = getIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
957 isInc, DAG);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000958 if (isLegal) {
959 auto &HII = *Subtarget.getInstrInfo();
960 int32_t OffsetVal = cast<ConstantSDNode>(Offset.getNode())->getSExtValue();
961 if (HII.isValidAutoIncImm(VT, OffsetVal)) {
962 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
963 return true;
964 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000965 }
966
967 return false;
968}
969
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000970SDValue
971HexagonTargetLowering::LowerINLINEASM(SDValue Op, SelectionDAG &DAG) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000972 SDNode *Node = Op.getNode();
973 MachineFunction &MF = DAG.getMachineFunction();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000974 auto &FuncInfo = *MF.getInfo<HexagonMachineFunctionInfo>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000975 switch (Node->getOpcode()) {
976 case ISD::INLINEASM: {
977 unsigned NumOps = Node->getNumOperands();
978 if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue)
979 --NumOps; // Ignore the flag operand.
980
981 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000982 if (FuncInfo.hasClobberLR())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000983 break;
984 unsigned Flags =
985 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
986 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
987 ++i; // Skip the ID value.
988
989 switch (InlineAsm::getKind(Flags)) {
990 default: llvm_unreachable("Bad flags!");
991 case InlineAsm::Kind_RegDef:
992 case InlineAsm::Kind_RegUse:
993 case InlineAsm::Kind_Imm:
994 case InlineAsm::Kind_Clobber:
995 case InlineAsm::Kind_Mem: {
996 for (; NumVals; --NumVals, ++i) {}
997 break;
998 }
999 case InlineAsm::Kind_RegDefEarlyClobber: {
1000 for (; NumVals; --NumVals, ++i) {
1001 unsigned Reg =
1002 cast<RegisterSDNode>(Node->getOperand(i))->getReg();
1003
1004 // Check it to be lr
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001005 const HexagonRegisterInfo *QRI = Subtarget.getRegisterInfo();
Eric Christopherdbe1cb02014-06-27 00:13:52 +00001006 if (Reg == QRI->getRARegister()) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001007 FuncInfo.setHasClobberLR(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001008 break;
1009 }
1010 }
1011 break;
1012 }
1013 }
1014 }
1015 }
1016 } // Node->getOpcode
1017 return Op;
1018}
1019
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00001020// Need to transform ISD::PREFETCH into something that doesn't inherit
1021// all of the properties of ISD::PREFETCH, specifically SDNPMayLoad and
1022// SDNPMayStore.
1023SDValue HexagonTargetLowering::LowerPREFETCH(SDValue Op,
1024 SelectionDAG &DAG) const {
1025 SDValue Chain = Op.getOperand(0);
1026 SDValue Addr = Op.getOperand(1);
1027 // Lower it to DCFETCH($reg, #0). A "pat" will try to merge the offset in,
1028 // if the "reg" is fed by an "add".
1029 SDLoc DL(Op);
1030 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
1031 return DAG.getNode(HexagonISD::DCFETCH, DL, MVT::Other, Chain, Addr, Zero);
1032}
1033
1034SDValue HexagonTargetLowering::LowerINTRINSIC_VOID(SDValue Op,
1035 SelectionDAG &DAG) const {
1036 SDValue Chain = Op.getOperand(0);
1037 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1038 // Lower the hexagon_prefetch builtin to DCFETCH, as above.
1039 if (IntNo == Intrinsic::hexagon_prefetch) {
1040 SDValue Addr = Op.getOperand(2);
1041 SDLoc DL(Op);
1042 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
1043 return DAG.getNode(HexagonISD::DCFETCH, DL, MVT::Other, Chain, Addr, Zero);
1044 }
1045 return SDValue();
1046}
1047
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001048SDValue
1049HexagonTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
1050 SelectionDAG &DAG) const {
1051 SDValue Chain = Op.getOperand(0);
1052 SDValue Size = Op.getOperand(1);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001053 SDValue Align = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001054 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001055
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001056 ConstantSDNode *AlignConst = dyn_cast<ConstantSDNode>(Align);
1057 assert(AlignConst && "Non-constant Align in LowerDYNAMIC_STACKALLOC");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001058
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001059 unsigned A = AlignConst->getSExtValue();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001060 auto &HFI = *Subtarget.getFrameLowering();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001061 // "Zero" means natural stack alignment.
1062 if (A == 0)
1063 A = HFI.getStackAlignment();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001064
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001065 DEBUG({
Krzysztof Parzyszek9ee04e42015-04-22 17:19:44 +00001066 dbgs () << LLVM_FUNCTION_NAME << " Align: " << A << " Size: ";
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001067 Size.getNode()->dump(&DAG);
1068 dbgs() << "\n";
1069 });
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001070
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001071 SDValue AC = DAG.getConstant(A, dl, MVT::i32);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001072 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00001073 SDValue AA = DAG.getNode(HexagonISD::ALLOCA, dl, VTs, Chain, Size, AC);
Nirav Davebfdb4832016-06-23 17:52:57 +00001074
1075 DAG.ReplaceAllUsesOfValueWith(Op, AA);
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00001076 return AA;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001077}
1078
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001079SDValue HexagonTargetLowering::LowerFormalArguments(
1080 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
1081 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
1082 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001083
1084 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00001085 MachineFrameInfo &MFI = MF.getFrameInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001086 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001087 auto &FuncInfo = *MF.getInfo<HexagonMachineFunctionInfo>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001088
1089 // Assign locations to all of the incoming arguments.
1090 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001091 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1092 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001093
1094 CCInfo.AnalyzeFormalArguments(Ins, CC_Hexagon);
1095
1096 // For LLVM, in the case when returning a struct by value (>8byte),
1097 // the first argument is a pointer that points to the location on caller's
1098 // stack where the return value will be stored. For Hexagon, the location on
1099 // caller's stack is passed only when the struct size is smaller than (and
1100 // equal to) 8 bytes. If not, no address will be passed into callee and
1101 // callee return the result direclty through R0/R1.
1102
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001103 SmallVector<SDValue, 8> MemOps;
1104 bool UseHVX = Subtarget.useHVXOps(), UseHVXDbl = Subtarget.useHVXDblOps();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001105
1106 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1107 CCValAssign &VA = ArgLocs[i];
1108 ISD::ArgFlagsTy Flags = Ins[i].Flags;
1109 unsigned ObjSize;
1110 unsigned StackLocation;
1111 int FI;
1112
1113 if ( (VA.isRegLoc() && !Flags.isByVal())
1114 || (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() > 8)) {
1115 // Arguments passed in registers
1116 // 1. int, long long, ptr args that get allocated in register.
1117 // 2. Large struct that gets an register to put its address in.
1118 EVT RegVT = VA.getLocVT();
Sirish Pande69295b82012-05-10 20:20:25 +00001119 if (RegVT == MVT::i8 || RegVT == MVT::i16 ||
1120 RegVT == MVT::i32 || RegVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001121 unsigned VReg =
Craig Topperc7242e02012-04-20 07:30:17 +00001122 RegInfo.createVirtualRegister(&Hexagon::IntRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001123 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1124 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Colin LeMahieu4379d102015-01-28 22:08:16 +00001125 } else if (RegVT == MVT::i64 || RegVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001126 unsigned VReg =
Craig Topperc7242e02012-04-20 07:30:17 +00001127 RegInfo.createVirtualRegister(&Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001128 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1129 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001130
1131 // Single Vector
1132 } else if ((RegVT == MVT::v8i64 || RegVT == MVT::v16i32 ||
1133 RegVT == MVT::v32i16 || RegVT == MVT::v64i8)) {
1134 unsigned VReg =
1135 RegInfo.createVirtualRegister(&Hexagon::VectorRegsRegClass);
1136 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1137 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1138 } else if (UseHVX && UseHVXDbl &&
1139 ((RegVT == MVT::v16i64 || RegVT == MVT::v32i32 ||
1140 RegVT == MVT::v64i16 || RegVT == MVT::v128i8))) {
1141 unsigned VReg =
1142 RegInfo.createVirtualRegister(&Hexagon::VectorRegs128BRegClass);
1143 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1144 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1145
1146 // Double Vector
1147 } else if ((RegVT == MVT::v16i64 || RegVT == MVT::v32i32 ||
1148 RegVT == MVT::v64i16 || RegVT == MVT::v128i8)) {
1149 unsigned VReg =
1150 RegInfo.createVirtualRegister(&Hexagon::VecDblRegsRegClass);
1151 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1152 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1153 } else if (UseHVX && UseHVXDbl &&
1154 ((RegVT == MVT::v32i64 || RegVT == MVT::v64i32 ||
1155 RegVT == MVT::v128i16 || RegVT == MVT::v256i8))) {
1156 unsigned VReg =
1157 RegInfo.createVirtualRegister(&Hexagon::VecDblRegs128BRegClass);
1158 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1159 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1160 } else if (RegVT == MVT::v512i1 || RegVT == MVT::v1024i1) {
1161 assert(0 && "need to support VecPred regs");
1162 unsigned VReg =
1163 RegInfo.createVirtualRegister(&Hexagon::VecPredRegsRegClass);
1164 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1165 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001166 } else {
1167 assert (0);
1168 }
1169 } else if (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() <= 8) {
1170 assert (0 && "ByValSize must be bigger than 8 bytes");
1171 } else {
1172 // Sanity check.
1173 assert(VA.isMemLoc());
1174
1175 if (Flags.isByVal()) {
1176 // If it's a byval parameter, then we need to compute the
1177 // "real" size, not the size of the pointer.
1178 ObjSize = Flags.getByValSize();
1179 } else {
1180 ObjSize = VA.getLocVT().getStoreSizeInBits() >> 3;
1181 }
1182
1183 StackLocation = HEXAGON_LRFP_SIZE + VA.getLocMemOffset();
1184 // Create the frame index object for this incoming parameter...
Matthias Braun941a7052016-07-28 18:40:00 +00001185 FI = MFI.CreateFixedObject(ObjSize, StackLocation, true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001186
1187 // Create the SelectionDAG nodes cordl, responding to a load
1188 // from this parameter.
1189 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
1190
1191 if (Flags.isByVal()) {
1192 // If it's a pass-by-value aggregate, then do not dereference the stack
1193 // location. Instead, we should generate a reference to the stack
1194 // location.
1195 InVals.push_back(FIN);
1196 } else {
Justin Lebar9c375812016-07-15 18:27:10 +00001197 InVals.push_back(
1198 DAG.getLoad(VA.getLocVT(), dl, Chain, FIN, MachinePointerInfo()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001199 }
1200 }
1201 }
1202
1203 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001204 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001205
1206 if (isVarArg) {
1207 // This will point to the next argument passed via stack.
Matthias Braun941a7052016-07-28 18:40:00 +00001208 int FrameIndex = MFI.CreateFixedObject(Hexagon_PointerSize,
1209 HEXAGON_LRFP_SIZE +
1210 CCInfo.getNextStackOffset(),
1211 true);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001212 FuncInfo.setVarArgsFrameIndex(FrameIndex);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001213 }
1214
1215 return Chain;
1216}
1217
1218SDValue
1219HexagonTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
1220 // VASTART stores the address of the VarArgsFrameIndex slot into the
1221 // memory location argument.
1222 MachineFunction &MF = DAG.getMachineFunction();
1223 HexagonMachineFunctionInfo *QFI = MF.getInfo<HexagonMachineFunctionInfo>();
1224 SDValue Addr = DAG.getFrameIndex(QFI->getVarArgsFrameIndex(), MVT::i32);
1225 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Justin Lebar9c375812016-07-15 18:27:10 +00001226 return DAG.getStore(Op.getOperand(0), SDLoc(Op), Addr, Op.getOperand(1),
1227 MachinePointerInfo(SV));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001228}
1229
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001230// Creates a SPLAT instruction for a constant value VAL.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001231static SDValue createSplat(SelectionDAG &DAG, const SDLoc &dl, EVT VT,
1232 SDValue Val) {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001233 if (VT.getSimpleVT() == MVT::v4i8)
1234 return DAG.getNode(HexagonISD::VSPLATB, dl, VT, Val);
1235
1236 if (VT.getSimpleVT() == MVT::v4i16)
1237 return DAG.getNode(HexagonISD::VSPLATH, dl, VT, Val);
1238
1239 return SDValue();
1240}
1241
1242static bool isSExtFree(SDValue N) {
1243 // A sign-extend of a truncate of a sign-extend is free.
1244 if (N.getOpcode() == ISD::TRUNCATE &&
1245 N.getOperand(0).getOpcode() == ISD::AssertSext)
1246 return true;
1247 // We have sign-extended loads.
1248 if (N.getOpcode() == ISD::LOAD)
1249 return true;
1250 return false;
1251}
1252
1253SDValue HexagonTargetLowering::LowerCTPOP(SDValue Op, SelectionDAG &DAG) const {
1254 SDLoc dl(Op);
1255 SDValue InpVal = Op.getOperand(0);
1256 if (isa<ConstantSDNode>(InpVal)) {
1257 uint64_t V = cast<ConstantSDNode>(InpVal)->getZExtValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001258 return DAG.getTargetConstant(countPopulation(V), dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001259 }
1260 SDValue PopOut = DAG.getNode(HexagonISD::POPCOUNT, dl, MVT::i32, InpVal);
1261 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, PopOut);
1262}
1263
1264SDValue HexagonTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1265 SDLoc dl(Op);
1266
1267 SDValue LHS = Op.getOperand(0);
1268 SDValue RHS = Op.getOperand(1);
1269 SDValue Cmp = Op.getOperand(2);
1270 ISD::CondCode CC = cast<CondCodeSDNode>(Cmp)->get();
1271
1272 EVT VT = Op.getValueType();
1273 EVT LHSVT = LHS.getValueType();
1274 EVT RHSVT = RHS.getValueType();
1275
1276 if (LHSVT == MVT::v2i16) {
1277 assert(ISD::isSignedIntSetCC(CC) || ISD::isUnsignedIntSetCC(CC));
1278 unsigned ExtOpc = ISD::isSignedIntSetCC(CC) ? ISD::SIGN_EXTEND
1279 : ISD::ZERO_EXTEND;
1280 SDValue LX = DAG.getNode(ExtOpc, dl, MVT::v2i32, LHS);
1281 SDValue RX = DAG.getNode(ExtOpc, dl, MVT::v2i32, RHS);
1282 SDValue SC = DAG.getNode(ISD::SETCC, dl, MVT::v2i1, LX, RX, Cmp);
1283 return SC;
1284 }
1285
1286 // Treat all other vector types as legal.
1287 if (VT.isVector())
1288 return Op;
1289
1290 // Equals and not equals should use sign-extend, not zero-extend, since
1291 // we can represent small negative values in the compare instructions.
1292 // The LLVM default is to use zero-extend arbitrarily in these cases.
1293 if ((CC == ISD::SETEQ || CC == ISD::SETNE) &&
1294 (RHSVT == MVT::i8 || RHSVT == MVT::i16) &&
1295 (LHSVT == MVT::i8 || LHSVT == MVT::i16)) {
1296 ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS);
1297 if (C && C->getAPIntValue().isNegative()) {
1298 LHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, LHS);
1299 RHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, RHS);
1300 return DAG.getNode(ISD::SETCC, dl, Op.getValueType(),
1301 LHS, RHS, Op.getOperand(2));
1302 }
1303 if (isSExtFree(LHS) || isSExtFree(RHS)) {
1304 LHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, LHS);
1305 RHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, RHS);
1306 return DAG.getNode(ISD::SETCC, dl, Op.getValueType(),
1307 LHS, RHS, Op.getOperand(2));
1308 }
1309 }
1310 return SDValue();
1311}
1312
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001313SDValue
1314HexagonTargetLowering::LowerVSELECT(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001315 SDValue PredOp = Op.getOperand(0);
1316 SDValue Op1 = Op.getOperand(1), Op2 = Op.getOperand(2);
1317 EVT OpVT = Op1.getValueType();
1318 SDLoc DL(Op);
1319
1320 if (OpVT == MVT::v2i16) {
1321 SDValue X1 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op1);
1322 SDValue X2 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op2);
1323 SDValue SL = DAG.getNode(ISD::VSELECT, DL, MVT::v2i32, PredOp, X1, X2);
1324 SDValue TR = DAG.getNode(ISD::TRUNCATE, DL, MVT::v2i16, SL);
1325 return TR;
1326 }
1327
1328 return SDValue();
1329}
1330
1331// Handle only specific vector loads.
1332SDValue HexagonTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1333 EVT VT = Op.getValueType();
1334 SDLoc DL(Op);
1335 LoadSDNode *LoadNode = cast<LoadSDNode>(Op);
1336 SDValue Chain = LoadNode->getChain();
1337 SDValue Ptr = Op.getOperand(1);
1338 SDValue LoweredLoad;
1339 SDValue Result;
1340 SDValue Base = LoadNode->getBasePtr();
1341 ISD::LoadExtType Ext = LoadNode->getExtensionType();
1342 unsigned Alignment = LoadNode->getAlignment();
1343 SDValue LoadChain;
1344
1345 if(Ext == ISD::NON_EXTLOAD)
1346 Ext = ISD::ZEXTLOAD;
1347
1348 if (VT == MVT::v4i16) {
1349 if (Alignment == 2) {
1350 SDValue Loads[4];
1351 // Base load.
1352 Loads[0] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Base,
Justin Lebar9c375812016-07-15 18:27:10 +00001353 LoadNode->getPointerInfo(), MVT::i16, Alignment,
1354 LoadNode->getMemOperand()->getFlags());
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001355 // Base+2 load.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001356 SDValue Increment = DAG.getConstant(2, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001357 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1358 Loads[1] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
Justin Lebar9c375812016-07-15 18:27:10 +00001359 LoadNode->getPointerInfo(), MVT::i16, Alignment,
1360 LoadNode->getMemOperand()->getFlags());
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001361 // SHL 16, then OR base and base+2.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001362 SDValue ShiftAmount = DAG.getConstant(16, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001363 SDValue Tmp1 = DAG.getNode(ISD::SHL, DL, MVT::i32, Loads[1], ShiftAmount);
1364 SDValue Tmp2 = DAG.getNode(ISD::OR, DL, MVT::i32, Tmp1, Loads[0]);
1365 // Base + 4.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001366 Increment = DAG.getConstant(4, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001367 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1368 Loads[2] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
Justin Lebar9c375812016-07-15 18:27:10 +00001369 LoadNode->getPointerInfo(), MVT::i16, Alignment,
1370 LoadNode->getMemOperand()->getFlags());
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001371 // Base + 6.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001372 Increment = DAG.getConstant(6, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001373 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1374 Loads[3] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
Justin Lebar9c375812016-07-15 18:27:10 +00001375 LoadNode->getPointerInfo(), MVT::i16, Alignment,
1376 LoadNode->getMemOperand()->getFlags());
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001377 // SHL 16, then OR base+4 and base+6.
1378 Tmp1 = DAG.getNode(ISD::SHL, DL, MVT::i32, Loads[3], ShiftAmount);
1379 SDValue Tmp4 = DAG.getNode(ISD::OR, DL, MVT::i32, Tmp1, Loads[2]);
1380 // Combine to i64. This could be optimised out later if we can
1381 // affect reg allocation of this code.
1382 Result = DAG.getNode(HexagonISD::COMBINE, DL, MVT::i64, Tmp4, Tmp2);
1383 LoadChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
1384 Loads[0].getValue(1), Loads[1].getValue(1),
1385 Loads[2].getValue(1), Loads[3].getValue(1));
1386 } else {
1387 // Perform default type expansion.
1388 Result = DAG.getLoad(MVT::i64, DL, Chain, Ptr, LoadNode->getPointerInfo(),
Justin Lebar9c375812016-07-15 18:27:10 +00001389 LoadNode->getAlignment(),
1390 LoadNode->getMemOperand()->getFlags());
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001391 LoadChain = Result.getValue(1);
1392 }
1393 } else
1394 llvm_unreachable("Custom lowering unsupported load");
1395
1396 Result = DAG.getNode(ISD::BITCAST, DL, VT, Result);
1397 // Since we pretend to lower a load, we need the original chain
1398 // info attached to the result.
1399 SDValue Ops[] = { Result, LoadChain };
1400
1401 return DAG.getMergeValues(Ops, DL);
1402}
1403
1404
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001405SDValue
Sirish Pande69295b82012-05-10 20:20:25 +00001406HexagonTargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
1407 EVT ValTy = Op.getValueType();
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001408 ConstantPoolSDNode *CPN = cast<ConstantPoolSDNode>(Op);
1409 unsigned Align = CPN->getAlignment();
Rafael Espindola405e25a2016-06-26 22:24:01 +00001410 bool IsPositionIndependent = isPositionIndependent();
1411 unsigned char TF = IsPositionIndependent ? HexagonII::MO_PCREL : 0;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001412
1413 SDValue T;
1414 if (CPN->isMachineConstantPoolEntry())
1415 T = DAG.getTargetConstantPool(CPN->getMachineCPVal(), ValTy, Align, TF);
Sirish Pande69295b82012-05-10 20:20:25 +00001416 else
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001417 T = DAG.getTargetConstantPool(CPN->getConstVal(), ValTy, Align, TF);
Rafael Espindola405e25a2016-06-26 22:24:01 +00001418 if (IsPositionIndependent)
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001419 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), ValTy, T);
1420 return DAG.getNode(HexagonISD::CP, SDLoc(Op), ValTy, T);
1421}
1422
1423SDValue
1424HexagonTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
1425 EVT VT = Op.getValueType();
1426 int Idx = cast<JumpTableSDNode>(Op)->getIndex();
Rafael Espindola405e25a2016-06-26 22:24:01 +00001427 if (isPositionIndependent()) {
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001428 SDValue T = DAG.getTargetJumpTable(Idx, VT, HexagonII::MO_PCREL);
1429 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), VT, T);
1430 }
1431
1432 SDValue T = DAG.getTargetJumpTable(Idx, VT);
1433 return DAG.getNode(HexagonISD::JT, SDLoc(Op), VT, T);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001434}
1435
1436SDValue
1437HexagonTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001438 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001439 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00001440 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001441 MFI.setReturnAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001442
Bill Wendling908bf812014-01-06 00:43:20 +00001443 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001444 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001445
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001446 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001447 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001448 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1449 if (Depth) {
1450 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001451 SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001452 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
1453 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Justin Lebar9c375812016-07-15 18:27:10 +00001454 MachinePointerInfo());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001455 }
1456
1457 // Return LR, which contains the return address. Mark it an implicit live-in.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001458 unsigned Reg = MF.addLiveIn(HRI.getRARegister(), getRegClassFor(MVT::i32));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001459 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
1460}
1461
1462SDValue
1463HexagonTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001464 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Matthias Braun941a7052016-07-28 18:40:00 +00001465 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001466 MFI.setFrameAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001467
1468 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001469 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001470 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1471 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001472 HRI.getFrameRegister(), VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001473 while (Depth--)
1474 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00001475 MachinePointerInfo());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001476 return FrameAddr;
1477}
1478
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001479SDValue
1480HexagonTargetLowering::LowerATOMIC_FENCE(SDValue Op, SelectionDAG& DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001481 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001482 return DAG.getNode(HexagonISD::BARRIER, dl, MVT::Other, Op.getOperand(0));
1483}
1484
1485
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001486SDValue
1487HexagonTargetLowering::LowerGLOBALADDRESS(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001488 SDLoc dl(Op);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001489 auto *GAN = cast<GlobalAddressSDNode>(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00001490 auto PtrVT = getPointerTy(DAG.getDataLayout());
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001491 auto *GV = GAN->getGlobal();
1492 int64_t Offset = GAN->getOffset();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001493
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001494 auto &HLOF = *HTM.getObjFileLowering();
1495 Reloc::Model RM = HTM.getRelocationModel();
1496
1497 if (RM == Reloc::Static) {
1498 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset);
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +00001499 if (HLOF.isGlobalInSmallSection(GV, HTM))
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001500 return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, GA);
1501 return DAG.getNode(HexagonISD::CONST32, dl, PtrVT, GA);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001502 }
1503
Rafael Espindola3beef8d2016-06-27 23:15:57 +00001504 bool UsePCRel = getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001505 if (UsePCRel) {
1506 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset,
1507 HexagonII::MO_PCREL);
1508 return DAG.getNode(HexagonISD::AT_PCREL, dl, PtrVT, GA);
1509 }
1510
1511 // Use GOT index.
1512 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
1513 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, HexagonII::MO_GOT);
1514 SDValue Off = DAG.getConstant(Offset, dl, MVT::i32);
1515 return DAG.getNode(HexagonISD::AT_GOT, dl, PtrVT, GOT, GA, Off);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001516}
1517
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001518// Specifies that for loads and stores VT can be promoted to PromotedLdStVT.
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001519SDValue
1520HexagonTargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
1521 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001522 SDLoc dl(Op);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001523 EVT PtrVT = getPointerTy(DAG.getDataLayout());
1524
1525 Reloc::Model RM = HTM.getRelocationModel();
1526 if (RM == Reloc::Static) {
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001527 SDValue A = DAG.getTargetBlockAddress(BA, PtrVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001528 return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, A);
1529 }
1530
1531 SDValue A = DAG.getTargetBlockAddress(BA, PtrVT, 0, HexagonII::MO_PCREL);
1532 return DAG.getNode(HexagonISD::AT_PCREL, dl, PtrVT, A);
1533}
1534
1535SDValue
1536HexagonTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op, SelectionDAG &DAG)
1537 const {
1538 EVT PtrVT = getPointerTy(DAG.getDataLayout());
1539 SDValue GOTSym = DAG.getTargetExternalSymbol(HEXAGON_GOT_SYM_NAME, PtrVT,
1540 HexagonII::MO_PCREL);
1541 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), PtrVT, GOTSym);
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001542}
1543
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001544SDValue
1545HexagonTargetLowering::GetDynamicTLSAddr(SelectionDAG &DAG, SDValue Chain,
1546 GlobalAddressSDNode *GA, SDValue *InFlag, EVT PtrVT, unsigned ReturnReg,
1547 unsigned char OperandFlags) const {
Matthias Braun941a7052016-07-28 18:40:00 +00001548 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001549 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1550 SDLoc dl(GA);
1551 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
1552 GA->getValueType(0),
1553 GA->getOffset(),
1554 OperandFlags);
1555 // Create Operands for the call.The Operands should have the following:
1556 // 1. Chain SDValue
1557 // 2. Callee which in this case is the Global address value.
1558 // 3. Registers live into the call.In this case its R0, as we
1559 // have just one argument to be passed.
1560 // 4. InFlag if there is any.
1561 // Note: The order is important.
1562
1563 if (InFlag) {
1564 SDValue Ops[] = { Chain, TGA,
1565 DAG.getRegister(Hexagon::R0, PtrVT), *InFlag };
1566 Chain = DAG.getNode(HexagonISD::CALLv3, dl, NodeTys, Ops);
1567 } else {
1568 SDValue Ops[] = { Chain, TGA, DAG.getRegister(Hexagon::R0, PtrVT)};
1569 Chain = DAG.getNode(HexagonISD::CALLv3, dl, NodeTys, Ops);
1570 }
1571
1572 // Inform MFI that function has calls.
Matthias Braun941a7052016-07-28 18:40:00 +00001573 MFI.setAdjustsStack(true);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001574
1575 SDValue Flag = Chain.getValue(1);
1576 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
1577}
1578
1579//
1580// Lower using the intial executable model for TLS addresses
1581//
1582SDValue
1583HexagonTargetLowering::LowerToTLSInitialExecModel(GlobalAddressSDNode *GA,
1584 SelectionDAG &DAG) const {
1585 SDLoc dl(GA);
1586 int64_t Offset = GA->getOffset();
1587 auto PtrVT = getPointerTy(DAG.getDataLayout());
1588
1589 // Get the thread pointer.
1590 SDValue TP = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Hexagon::UGP, PtrVT);
1591
Rafael Espindola405e25a2016-06-26 22:24:01 +00001592 bool IsPositionIndependent = isPositionIndependent();
1593 unsigned char TF =
1594 IsPositionIndependent ? HexagonII::MO_IEGOT : HexagonII::MO_IE;
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001595
1596 // First generate the TLS symbol address
1597 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT,
1598 Offset, TF);
1599
1600 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1601
Rafael Espindola405e25a2016-06-26 22:24:01 +00001602 if (IsPositionIndependent) {
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001603 // Generate the GOT pointer in case of position independent code
1604 SDValue GOT = LowerGLOBAL_OFFSET_TABLE(Sym, DAG);
1605
1606 // Add the TLS Symbol address to GOT pointer.This gives
1607 // GOT relative relocation for the symbol.
1608 Sym = DAG.getNode(ISD::ADD, dl, PtrVT, GOT, Sym);
1609 }
1610
1611 // Load the offset value for TLS symbol.This offset is relative to
1612 // thread pointer.
Justin Lebar9c375812016-07-15 18:27:10 +00001613 SDValue LoadOffset =
1614 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Sym, MachinePointerInfo());
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001615
1616 // Address of the thread local variable is the add of thread
1617 // pointer and the offset of the variable.
1618 return DAG.getNode(ISD::ADD, dl, PtrVT, TP, LoadOffset);
1619}
1620
1621//
1622// Lower using the local executable model for TLS addresses
1623//
1624SDValue
1625HexagonTargetLowering::LowerToTLSLocalExecModel(GlobalAddressSDNode *GA,
1626 SelectionDAG &DAG) const {
1627 SDLoc dl(GA);
1628 int64_t Offset = GA->getOffset();
1629 auto PtrVT = getPointerTy(DAG.getDataLayout());
1630
1631 // Get the thread pointer.
1632 SDValue TP = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Hexagon::UGP, PtrVT);
1633 // Generate the TLS symbol address
1634 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT, Offset,
1635 HexagonII::MO_TPREL);
1636 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1637
1638 // Address of the thread local variable is the add of thread
1639 // pointer and the offset of the variable.
1640 return DAG.getNode(ISD::ADD, dl, PtrVT, TP, Sym);
1641}
1642
1643//
1644// Lower using the general dynamic model for TLS addresses
1645//
1646SDValue
1647HexagonTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
1648 SelectionDAG &DAG) const {
1649 SDLoc dl(GA);
1650 int64_t Offset = GA->getOffset();
1651 auto PtrVT = getPointerTy(DAG.getDataLayout());
1652
1653 // First generate the TLS symbol address
1654 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT, Offset,
1655 HexagonII::MO_GDGOT);
1656
1657 // Then, generate the GOT pointer
1658 SDValue GOT = LowerGLOBAL_OFFSET_TABLE(TGA, DAG);
1659
1660 // Add the TLS symbol and the GOT pointer
1661 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1662 SDValue Chain = DAG.getNode(ISD::ADD, dl, PtrVT, GOT, Sym);
1663
1664 // Copy over the argument to R0
1665 SDValue InFlag;
1666 Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, Hexagon::R0, Chain, InFlag);
1667 InFlag = Chain.getValue(1);
1668
1669 return GetDynamicTLSAddr(DAG, Chain, GA, &InFlag, PtrVT,
1670 Hexagon::R0, HexagonII::MO_GDPLT);
1671}
1672
1673//
1674// Lower TLS addresses.
1675//
1676// For now for dynamic models, we only support the general dynamic model.
1677//
1678SDValue
1679HexagonTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1680 SelectionDAG &DAG) const {
1681 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1682
1683 switch (HTM.getTLSModel(GA->getGlobal())) {
1684 case TLSModel::GeneralDynamic:
1685 case TLSModel::LocalDynamic:
1686 return LowerToTLSGeneralDynamicModel(GA, DAG);
1687 case TLSModel::InitialExec:
1688 return LowerToTLSInitialExecModel(GA, DAG);
1689 case TLSModel::LocalExec:
1690 return LowerToTLSLocalExecModel(GA, DAG);
1691 }
1692 llvm_unreachable("Bogus TLS model");
1693}
1694
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001695//===----------------------------------------------------------------------===//
1696// TargetLowering Implementation
1697//===----------------------------------------------------------------------===//
1698
Eric Christopherd737b762015-02-02 22:11:36 +00001699HexagonTargetLowering::HexagonTargetLowering(const TargetMachine &TM,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001700 const HexagonSubtarget &ST)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001701 : TargetLowering(TM), HTM(static_cast<const HexagonTargetMachine&>(TM)),
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001702 Subtarget(ST) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001703 bool IsV4 = !Subtarget.hasV5TOps();
1704 auto &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00001705 bool UseHVX = Subtarget.useHVXOps();
1706 bool UseHVXSgl = Subtarget.useHVXSglOps();
1707 bool UseHVXDbl = Subtarget.useHVXDblOps();
Sirish Pande69295b82012-05-10 20:20:25 +00001708
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001709 setPrefLoopAlignment(4);
1710 setPrefFunctionAlignment(4);
1711 setMinFunctionAlignment(2);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001712 setStackPointerRegisterToSaveRestore(HRI.getStackRegister());
1713
Krzysztof Parzyszekf228c952016-06-22 16:07:10 +00001714 setMaxAtomicSizeInBitsSupported(64);
1715 setMinCmpXchgSizeInBits(32);
1716
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001717 if (EnableHexSDNodeSched)
1718 setSchedulingPreference(Sched::VLIW);
1719 else
1720 setSchedulingPreference(Sched::Source);
1721
1722 // Limits for inline expansion of memcpy/memmove
1723 MaxStoresPerMemcpy = MaxStoresPerMemcpyCL;
1724 MaxStoresPerMemcpyOptSize = MaxStoresPerMemcpyOptSizeCL;
1725 MaxStoresPerMemmove = MaxStoresPerMemmoveCL;
1726 MaxStoresPerMemmoveOptSize = MaxStoresPerMemmoveOptSizeCL;
1727 MaxStoresPerMemset = MaxStoresPerMemsetCL;
1728 MaxStoresPerMemsetOptSize = MaxStoresPerMemsetOptSizeCL;
1729
1730 //
1731 // Set up register classes.
1732 //
1733
1734 addRegisterClass(MVT::i1, &Hexagon::PredRegsRegClass);
1735 addRegisterClass(MVT::v2i1, &Hexagon::PredRegsRegClass); // bbbbaaaa
1736 addRegisterClass(MVT::v4i1, &Hexagon::PredRegsRegClass); // ddccbbaa
1737 addRegisterClass(MVT::v8i1, &Hexagon::PredRegsRegClass); // hgfedcba
1738 addRegisterClass(MVT::i32, &Hexagon::IntRegsRegClass);
1739 addRegisterClass(MVT::v4i8, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001740 addRegisterClass(MVT::v2i16, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001741 addRegisterClass(MVT::i64, &Hexagon::DoubleRegsRegClass);
1742 addRegisterClass(MVT::v8i8, &Hexagon::DoubleRegsRegClass);
1743 addRegisterClass(MVT::v4i16, &Hexagon::DoubleRegsRegClass);
1744 addRegisterClass(MVT::v2i32, &Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001745
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001746 if (Subtarget.hasV5TOps()) {
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001747 addRegisterClass(MVT::f32, &Hexagon::IntRegsRegClass);
1748 addRegisterClass(MVT::f64, &Hexagon::DoubleRegsRegClass);
1749 }
Sirish Pande69295b82012-05-10 20:20:25 +00001750
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001751 if (Subtarget.hasV60TOps()) {
1752 if (Subtarget.useHVXSglOps()) {
1753 addRegisterClass(MVT::v64i8, &Hexagon::VectorRegsRegClass);
1754 addRegisterClass(MVT::v32i16, &Hexagon::VectorRegsRegClass);
1755 addRegisterClass(MVT::v16i32, &Hexagon::VectorRegsRegClass);
1756 addRegisterClass(MVT::v8i64, &Hexagon::VectorRegsRegClass);
1757 addRegisterClass(MVT::v128i8, &Hexagon::VecDblRegsRegClass);
1758 addRegisterClass(MVT::v64i16, &Hexagon::VecDblRegsRegClass);
1759 addRegisterClass(MVT::v32i32, &Hexagon::VecDblRegsRegClass);
1760 addRegisterClass(MVT::v16i64, &Hexagon::VecDblRegsRegClass);
1761 addRegisterClass(MVT::v512i1, &Hexagon::VecPredRegsRegClass);
1762 } else if (Subtarget.useHVXDblOps()) {
1763 addRegisterClass(MVT::v128i8, &Hexagon::VectorRegs128BRegClass);
1764 addRegisterClass(MVT::v64i16, &Hexagon::VectorRegs128BRegClass);
1765 addRegisterClass(MVT::v32i32, &Hexagon::VectorRegs128BRegClass);
1766 addRegisterClass(MVT::v16i64, &Hexagon::VectorRegs128BRegClass);
1767 addRegisterClass(MVT::v256i8, &Hexagon::VecDblRegs128BRegClass);
1768 addRegisterClass(MVT::v128i16, &Hexagon::VecDblRegs128BRegClass);
1769 addRegisterClass(MVT::v64i32, &Hexagon::VecDblRegs128BRegClass);
1770 addRegisterClass(MVT::v32i64, &Hexagon::VecDblRegs128BRegClass);
1771 addRegisterClass(MVT::v1024i1, &Hexagon::VecPredRegs128BRegClass);
1772 }
1773
1774 }
1775
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001776 //
1777 // Handling of scalar operations.
1778 //
1779 // All operations default to "legal", except:
1780 // - indexed loads and stores (pre-/post-incremented),
1781 // - ANY_EXTEND_VECTOR_INREG, ATOMIC_CMP_SWAP_WITH_SUCCESS, CONCAT_VECTORS,
1782 // ConstantFP, DEBUGTRAP, FCEIL, FCOPYSIGN, FEXP, FEXP2, FFLOOR, FGETSIGN,
1783 // FLOG, FLOG2, FLOG10, FMAXNUM, FMINNUM, FNEARBYINT, FRINT, FROUND, TRAP,
1784 // FTRUNC, PREFETCH, SIGN_EXTEND_VECTOR_INREG, ZERO_EXTEND_VECTOR_INREG,
1785 // which default to "expand" for at least one type.
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001786
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001787 // Misc operations.
1788 setOperationAction(ISD::ConstantFP, MVT::f32, Legal); // Default: expand
1789 setOperationAction(ISD::ConstantFP, MVT::f64, Legal); // Default: expand
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001790
1791 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001792 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001793 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001794 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
1795 setOperationAction(ISD::INLINEASM, MVT::Other, Custom);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00001796 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
1797 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001798 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001799 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001800 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001801 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001802
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001803 // Custom legalize GlobalAddress nodes into CONST32.
1804 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001805 setOperationAction(ISD::GlobalAddress, MVT::i8, Custom);
1806 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001807
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001808 // Hexagon needs to optimize cases with negative constants.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001809 setOperationAction(ISD::SETCC, MVT::i8, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001810 setOperationAction(ISD::SETCC, MVT::i16, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001811
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001812 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
1813 setOperationAction(ISD::VASTART, MVT::Other, Custom);
1814 setOperationAction(ISD::VAEND, MVT::Other, Expand);
1815 setOperationAction(ISD::VAARG, MVT::Other, Expand);
1816
1817 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
1818 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
1819 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
1820
1821 if (EmitJumpTables)
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001822 setMinimumJumpTableEntries(MinimumJumpTables);
Krzysztof Parzyszeka61f7da2016-01-13 21:43:13 +00001823 else
1824 setMinimumJumpTableEntries(INT_MAX);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001825 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001826
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001827 // Hexagon has instructions for add/sub with carry. The problem with
1828 // modeling these instructions is that they produce 2 results: Rdd and Px.
1829 // To model the update of Px, we will have to use Defs[p0..p3] which will
1830 // cause any predicate live range to spill. So, we pretend we dont't have
1831 // these instructions.
1832 setOperationAction(ISD::ADDE, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001833 setOperationAction(ISD::ADDE, MVT::i16, Expand);
1834 setOperationAction(ISD::ADDE, MVT::i32, Expand);
1835 setOperationAction(ISD::ADDE, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001836 setOperationAction(ISD::SUBE, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001837 setOperationAction(ISD::SUBE, MVT::i16, Expand);
1838 setOperationAction(ISD::SUBE, MVT::i32, Expand);
1839 setOperationAction(ISD::SUBE, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001840 setOperationAction(ISD::ADDC, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001841 setOperationAction(ISD::ADDC, MVT::i16, Expand);
1842 setOperationAction(ISD::ADDC, MVT::i32, Expand);
1843 setOperationAction(ISD::ADDC, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001844 setOperationAction(ISD::SUBC, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001845 setOperationAction(ISD::SUBC, MVT::i16, Expand);
1846 setOperationAction(ISD::SUBC, MVT::i32, Expand);
1847 setOperationAction(ISD::SUBC, MVT::i64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001848
Krzysztof Parzyszek2c4487d2015-04-13 20:37:01 +00001849 // Only add and sub that detect overflow are the saturating ones.
1850 for (MVT VT : MVT::integer_valuetypes()) {
1851 setOperationAction(ISD::UADDO, VT, Expand);
1852 setOperationAction(ISD::SADDO, VT, Expand);
1853 setOperationAction(ISD::USUBO, VT, Expand);
1854 setOperationAction(ISD::SSUBO, VT, Expand);
1855 }
1856
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001857 setOperationAction(ISD::CTLZ, MVT::i8, Promote);
1858 setOperationAction(ISD::CTLZ, MVT::i16, Promote);
1859 setOperationAction(ISD::CTTZ, MVT::i8, Promote);
1860 setOperationAction(ISD::CTTZ, MVT::i16, Promote);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001861
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001862 // In V5, popcount can count # of 1s in i64 but returns i32.
1863 // On V4 it will be expanded (set later).
1864 setOperationAction(ISD::CTPOP, MVT::i8, Promote);
1865 setOperationAction(ISD::CTPOP, MVT::i16, Promote);
1866 setOperationAction(ISD::CTPOP, MVT::i32, Promote);
1867 setOperationAction(ISD::CTPOP, MVT::i64, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001868
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001869 // We custom lower i64 to i64 mul, so that it is not considered as a legal
1870 // operation. There is a pattern that will match i64 mul and transform it
1871 // to a series of instructions.
1872 setOperationAction(ISD::MUL, MVT::i64, Expand);
Colin LeMahieude68b662015-02-05 21:13:25 +00001873 setOperationAction(ISD::MULHS, MVT::i64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001874
Benjamin Kramer62460692015-04-25 14:46:53 +00001875 for (unsigned IntExpOp :
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001876 { ISD::SDIV, ISD::UDIV, ISD::SREM, ISD::UREM,
1877 ISD::SDIVREM, ISD::UDIVREM, ISD::ROTL, ISD::ROTR,
1878 ISD::BSWAP, ISD::SHL_PARTS, ISD::SRA_PARTS, ISD::SRL_PARTS,
1879 ISD::SMUL_LOHI, ISD::UMUL_LOHI }) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001880 setOperationAction(IntExpOp, MVT::i32, Expand);
1881 setOperationAction(IntExpOp, MVT::i64, Expand);
1882 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001883
Benjamin Kramer62460692015-04-25 14:46:53 +00001884 for (unsigned FPExpOp :
1885 {ISD::FDIV, ISD::FREM, ISD::FSQRT, ISD::FSIN, ISD::FCOS, ISD::FSINCOS,
1886 ISD::FPOW, ISD::FCOPYSIGN}) {
1887 setOperationAction(FPExpOp, MVT::f32, Expand);
1888 setOperationAction(FPExpOp, MVT::f64, Expand);
1889 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001890
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001891 // No extending loads from i32.
1892 for (MVT VT : MVT::integer_valuetypes()) {
1893 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
1894 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
1895 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
1896 }
1897 // Turn FP truncstore into trunc + store.
1898 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
1899 // Turn FP extload into load/fextend.
1900 for (MVT VT : MVT::fp_valuetypes())
1901 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001902
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001903 // Expand BR_CC and SELECT_CC for all integer and fp types.
1904 for (MVT VT : MVT::integer_valuetypes()) {
1905 setOperationAction(ISD::BR_CC, VT, Expand);
1906 setOperationAction(ISD::SELECT_CC, VT, Expand);
1907 }
1908 for (MVT VT : MVT::fp_valuetypes()) {
1909 setOperationAction(ISD::BR_CC, VT, Expand);
1910 setOperationAction(ISD::SELECT_CC, VT, Expand);
1911 }
1912 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001913
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001914 //
1915 // Handling of vector operations.
1916 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001917
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001918 // Custom lower v4i16 load only. Let v4i16 store to be
1919 // promoted for now.
1920 promoteLdStType(MVT::v4i8, MVT::i32);
1921 promoteLdStType(MVT::v2i16, MVT::i32);
1922 promoteLdStType(MVT::v8i8, MVT::i64);
1923 promoteLdStType(MVT::v2i32, MVT::i64);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001924
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001925 setOperationAction(ISD::LOAD, MVT::v4i16, Custom);
1926 setOperationAction(ISD::STORE, MVT::v4i16, Promote);
1927 AddPromotedToType(ISD::LOAD, MVT::v4i16, MVT::i64);
1928 AddPromotedToType(ISD::STORE, MVT::v4i16, MVT::i64);
1929
1930 // Set the action for vector operations to "expand", then override it with
1931 // either "custom" or "legal" for specific cases.
Craig Topper26260942015-10-18 05:15:34 +00001932 static const unsigned VectExpOps[] = {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001933 // Integer arithmetic:
1934 ISD::ADD, ISD::SUB, ISD::MUL, ISD::SDIV, ISD::UDIV,
1935 ISD::SREM, ISD::UREM, ISD::SDIVREM, ISD::UDIVREM, ISD::ADDC,
1936 ISD::SUBC, ISD::SADDO, ISD::UADDO, ISD::SSUBO, ISD::USUBO,
1937 ISD::SMUL_LOHI, ISD::UMUL_LOHI,
1938 // Logical/bit:
1939 ISD::AND, ISD::OR, ISD::XOR, ISD::ROTL, ISD::ROTR,
Craig Topper33772c52016-04-28 03:34:31 +00001940 ISD::CTPOP, ISD::CTLZ, ISD::CTTZ,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001941 // Floating point arithmetic/math functions:
1942 ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FMA, ISD::FDIV,
1943 ISD::FREM, ISD::FNEG, ISD::FABS, ISD::FSQRT, ISD::FSIN,
1944 ISD::FCOS, ISD::FPOWI, ISD::FPOW, ISD::FLOG, ISD::FLOG2,
1945 ISD::FLOG10, ISD::FEXP, ISD::FEXP2, ISD::FCEIL, ISD::FTRUNC,
1946 ISD::FRINT, ISD::FNEARBYINT, ISD::FROUND, ISD::FFLOOR,
1947 ISD::FMINNUM, ISD::FMAXNUM, ISD::FSINCOS,
1948 // Misc:
1949 ISD::SELECT, ISD::ConstantPool,
1950 // Vector:
1951 ISD::BUILD_VECTOR, ISD::SCALAR_TO_VECTOR,
1952 ISD::EXTRACT_VECTOR_ELT, ISD::INSERT_VECTOR_ELT,
1953 ISD::EXTRACT_SUBVECTOR, ISD::INSERT_SUBVECTOR,
1954 ISD::CONCAT_VECTORS, ISD::VECTOR_SHUFFLE
1955 };
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001956
1957 for (MVT VT : MVT::vector_valuetypes()) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001958 for (unsigned VectExpOp : VectExpOps)
1959 setOperationAction(VectExpOp, VT, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001960
1961 // Expand all extended loads and truncating stores:
1962 for (MVT TargetVT : MVT::vector_valuetypes()) {
1963 setLoadExtAction(ISD::EXTLOAD, TargetVT, VT, Expand);
1964 setTruncStoreAction(VT, TargetVT, Expand);
1965 }
1966
1967 setOperationAction(ISD::SRA, VT, Custom);
1968 setOperationAction(ISD::SHL, VT, Custom);
1969 setOperationAction(ISD::SRL, VT, Custom);
1970 }
1971
1972 // Types natively supported:
Benjamin Kramer62460692015-04-25 14:46:53 +00001973 for (MVT NativeVT : {MVT::v2i1, MVT::v4i1, MVT::v8i1, MVT::v32i1, MVT::v64i1,
1974 MVT::v4i8, MVT::v8i8, MVT::v2i16, MVT::v4i16, MVT::v1i32,
1975 MVT::v2i32, MVT::v1i64}) {
1976 setOperationAction(ISD::BUILD_VECTOR, NativeVT, Custom);
1977 setOperationAction(ISD::EXTRACT_VECTOR_ELT, NativeVT, Custom);
1978 setOperationAction(ISD::INSERT_VECTOR_ELT, NativeVT, Custom);
1979 setOperationAction(ISD::EXTRACT_SUBVECTOR, NativeVT, Custom);
1980 setOperationAction(ISD::INSERT_SUBVECTOR, NativeVT, Custom);
1981 setOperationAction(ISD::CONCAT_VECTORS, NativeVT, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001982
Benjamin Kramer62460692015-04-25 14:46:53 +00001983 setOperationAction(ISD::ADD, NativeVT, Legal);
1984 setOperationAction(ISD::SUB, NativeVT, Legal);
1985 setOperationAction(ISD::MUL, NativeVT, Legal);
1986 setOperationAction(ISD::AND, NativeVT, Legal);
1987 setOperationAction(ISD::OR, NativeVT, Legal);
1988 setOperationAction(ISD::XOR, NativeVT, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001989 }
1990
1991 setOperationAction(ISD::SETCC, MVT::v2i16, Custom);
1992 setOperationAction(ISD::VSELECT, MVT::v2i16, Custom);
1993 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
1994 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001995 if (UseHVX) {
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00001996 if (UseHVXSgl) {
1997 setOperationAction(ISD::CONCAT_VECTORS, MVT::v128i8, Custom);
1998 setOperationAction(ISD::CONCAT_VECTORS, MVT::v64i16, Custom);
1999 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i32, Custom);
2000 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i64, Custom);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002001 // We try to generate the vpack{e/o} instructions. If we fail
2002 // we fall back upon ExpandOp.
2003 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v64i8, Custom);
2004 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v32i16, Custom);
2005 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v64i8, Custom);
2006 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v32i16, Custom);
2007 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v16i32, Custom);
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002008 } else if (UseHVXDbl) {
2009 setOperationAction(ISD::CONCAT_VECTORS, MVT::v256i8, Custom);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002010 setOperationAction(ISD::CONCAT_VECTORS, MVT::v128i16, Custom);
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002011 setOperationAction(ISD::CONCAT_VECTORS, MVT::v64i32, Custom);
2012 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i64, Custom);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002013 // We try to generate the vpack{e/o} instructions. If we fail
2014 // we fall back upon ExpandOp.
2015 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v128i8, Custom);
2016 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v64i16, Custom);
2017 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v4i32, Custom);
2018 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v128i8, Custom);
2019 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v64i16, Custom);
2020 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v32i32, Custom);
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002021 } else {
2022 llvm_unreachable("Unrecognized HVX mode");
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002023 }
2024 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002025 // Subtarget-specific operation actions.
2026 //
2027 if (Subtarget.hasV5TOps()) {
2028 setOperationAction(ISD::FMA, MVT::f64, Expand);
2029 setOperationAction(ISD::FADD, MVT::f64, Expand);
2030 setOperationAction(ISD::FSUB, MVT::f64, Expand);
2031 setOperationAction(ISD::FMUL, MVT::f64, Expand);
2032
2033 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Promote);
2034 setOperationAction(ISD::FP_TO_UINT, MVT::i8, Promote);
2035 setOperationAction(ISD::FP_TO_UINT, MVT::i16, Promote);
2036 setOperationAction(ISD::FP_TO_SINT, MVT::i1, Promote);
2037 setOperationAction(ISD::FP_TO_SINT, MVT::i8, Promote);
2038 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
2039 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote);
2040 setOperationAction(ISD::UINT_TO_FP, MVT::i8, Promote);
2041 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote);
2042 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote);
2043 setOperationAction(ISD::SINT_TO_FP, MVT::i8, Promote);
2044 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
2045
2046 } else { // V4
2047 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
2048 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Expand);
2049 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
2050 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
2051 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Expand);
2052 setOperationAction(ISD::FP_TO_SINT, MVT::f32, Expand);
2053 setOperationAction(ISD::FP_EXTEND, MVT::f32, Expand);
2054 setOperationAction(ISD::FP_ROUND, MVT::f64, Expand);
2055 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
2056
2057 setOperationAction(ISD::CTPOP, MVT::i8, Expand);
2058 setOperationAction(ISD::CTPOP, MVT::i16, Expand);
2059 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
2060 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
2061
2062 // Expand these operations for both f32 and f64:
Benjamin Kramer62460692015-04-25 14:46:53 +00002063 for (unsigned FPExpOpV4 :
2064 {ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FABS, ISD::FNEG, ISD::FMA}) {
2065 setOperationAction(FPExpOpV4, MVT::f32, Expand);
2066 setOperationAction(FPExpOpV4, MVT::f64, Expand);
2067 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002068
Benjamin Kramer62460692015-04-25 14:46:53 +00002069 for (ISD::CondCode FPExpCCV4 :
2070 {ISD::SETOEQ, ISD::SETOGT, ISD::SETOLT, ISD::SETOGE, ISD::SETOLE,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002071 ISD::SETUO, ISD::SETO}) {
Benjamin Kramer62460692015-04-25 14:46:53 +00002072 setCondCodeAction(FPExpCCV4, MVT::f32, Expand);
2073 setCondCodeAction(FPExpCCV4, MVT::f64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002074 }
2075 }
2076
2077 // Handling of indexed loads/stores: default is "expand".
2078 //
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +00002079 for (MVT VT : {MVT::i8, MVT::i16, MVT::i32, MVT::i64}) {
2080 setIndexedLoadAction(ISD::POST_INC, VT, Legal);
2081 setIndexedStoreAction(ISD::POST_INC, VT, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002082 }
2083
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +00002084 if (UseHVXSgl) {
2085 for (MVT VT : {MVT::v64i8, MVT::v32i16, MVT::v16i32, MVT::v8i64,
2086 MVT::v128i8, MVT::v64i16, MVT::v32i32, MVT::v16i64}) {
2087 setIndexedLoadAction(ISD::POST_INC, VT, Legal);
2088 setIndexedStoreAction(ISD::POST_INC, VT, Legal);
2089 }
2090 } else if (UseHVXDbl) {
2091 for (MVT VT : {MVT::v128i8, MVT::v64i16, MVT::v32i32, MVT::v16i64,
2092 MVT::v256i8, MVT::v128i16, MVT::v64i32, MVT::v32i64}) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002093 setIndexedLoadAction(ISD::POST_INC, VT, Legal);
2094 setIndexedStoreAction(ISD::POST_INC, VT, Legal);
2095 }
2096 }
2097
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002098 computeRegisterProperties(&HRI);
2099
2100 //
2101 // Library calls for unsupported operations
2102 //
2103 bool FastMath = EnableFastMath;
2104
Benjamin Kramera37c8092015-04-25 14:46:46 +00002105 setLibcallName(RTLIB::SDIV_I32, "__hexagon_divsi3");
2106 setLibcallName(RTLIB::SDIV_I64, "__hexagon_divdi3");
2107 setLibcallName(RTLIB::UDIV_I32, "__hexagon_udivsi3");
2108 setLibcallName(RTLIB::UDIV_I64, "__hexagon_udivdi3");
2109 setLibcallName(RTLIB::SREM_I32, "__hexagon_modsi3");
2110 setLibcallName(RTLIB::SREM_I64, "__hexagon_moddi3");
2111 setLibcallName(RTLIB::UREM_I32, "__hexagon_umodsi3");
2112 setLibcallName(RTLIB::UREM_I64, "__hexagon_umoddi3");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002113
Benjamin Kramera37c8092015-04-25 14:46:46 +00002114 setLibcallName(RTLIB::SINTTOFP_I128_F64, "__hexagon_floattidf");
2115 setLibcallName(RTLIB::SINTTOFP_I128_F32, "__hexagon_floattisf");
2116 setLibcallName(RTLIB::FPTOUINT_F32_I128, "__hexagon_fixunssfti");
2117 setLibcallName(RTLIB::FPTOUINT_F64_I128, "__hexagon_fixunsdfti");
2118 setLibcallName(RTLIB::FPTOSINT_F32_I128, "__hexagon_fixsfti");
2119 setLibcallName(RTLIB::FPTOSINT_F64_I128, "__hexagon_fixdfti");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002120
2121 if (IsV4) {
2122 // Handle single-precision floating point operations on V4.
Benjamin Kramera37c8092015-04-25 14:46:46 +00002123 if (FastMath) {
2124 setLibcallName(RTLIB::ADD_F32, "__hexagon_fast_addsf3");
2125 setLibcallName(RTLIB::SUB_F32, "__hexagon_fast_subsf3");
2126 setLibcallName(RTLIB::MUL_F32, "__hexagon_fast_mulsf3");
2127 setLibcallName(RTLIB::OGT_F32, "__hexagon_fast_gtsf2");
2128 setLibcallName(RTLIB::OLT_F32, "__hexagon_fast_ltsf2");
2129 // Double-precision compares.
2130 setLibcallName(RTLIB::OGT_F64, "__hexagon_fast_gtdf2");
2131 setLibcallName(RTLIB::OLT_F64, "__hexagon_fast_ltdf2");
2132 } else {
2133 setLibcallName(RTLIB::ADD_F32, "__hexagon_addsf3");
2134 setLibcallName(RTLIB::SUB_F32, "__hexagon_subsf3");
2135 setLibcallName(RTLIB::MUL_F32, "__hexagon_mulsf3");
2136 setLibcallName(RTLIB::OGT_F32, "__hexagon_gtsf2");
2137 setLibcallName(RTLIB::OLT_F32, "__hexagon_ltsf2");
2138 // Double-precision compares.
2139 setLibcallName(RTLIB::OGT_F64, "__hexagon_gtdf2");
2140 setLibcallName(RTLIB::OLT_F64, "__hexagon_ltdf2");
2141 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002142 }
2143
2144 // This is the only fast library function for sqrtd.
2145 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00002146 setLibcallName(RTLIB::SQRT_F64, "__hexagon_fast2_sqrtdf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002147
Benjamin Kramera37c8092015-04-25 14:46:46 +00002148 // Prefix is: nothing for "slow-math",
2149 // "fast2_" for V4 fast-math and V5+ fast-math double-precision
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002150 // (actually, keep fast-math and fast-math2 separate for now)
Benjamin Kramera37c8092015-04-25 14:46:46 +00002151 if (FastMath) {
2152 setLibcallName(RTLIB::ADD_F64, "__hexagon_fast_adddf3");
2153 setLibcallName(RTLIB::SUB_F64, "__hexagon_fast_subdf3");
2154 setLibcallName(RTLIB::MUL_F64, "__hexagon_fast_muldf3");
2155 setLibcallName(RTLIB::DIV_F64, "__hexagon_fast_divdf3");
2156 // Calling __hexagon_fast2_divsf3 with fast-math on V5 (ok).
2157 setLibcallName(RTLIB::DIV_F32, "__hexagon_fast_divsf3");
2158 } else {
2159 setLibcallName(RTLIB::ADD_F64, "__hexagon_adddf3");
2160 setLibcallName(RTLIB::SUB_F64, "__hexagon_subdf3");
2161 setLibcallName(RTLIB::MUL_F64, "__hexagon_muldf3");
2162 setLibcallName(RTLIB::DIV_F64, "__hexagon_divdf3");
2163 setLibcallName(RTLIB::DIV_F32, "__hexagon_divsf3");
2164 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002165
2166 if (Subtarget.hasV5TOps()) {
2167 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00002168 setLibcallName(RTLIB::SQRT_F32, "__hexagon_fast2_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002169 else
Benjamin Kramera37c8092015-04-25 14:46:46 +00002170 setLibcallName(RTLIB::SQRT_F32, "__hexagon_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002171 } else {
2172 // V4
Benjamin Kramera37c8092015-04-25 14:46:46 +00002173 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__hexagon_floatsisf");
2174 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__hexagon_floatsidf");
2175 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__hexagon_floatdisf");
2176 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__hexagon_floatdidf");
2177 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__hexagon_floatunsisf");
2178 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__hexagon_floatunsidf");
2179 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__hexagon_floatundisf");
2180 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__hexagon_floatundidf");
2181 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__hexagon_fixunssfsi");
2182 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__hexagon_fixunssfdi");
2183 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__hexagon_fixunsdfsi");
2184 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__hexagon_fixunsdfdi");
2185 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__hexagon_fixsfsi");
2186 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__hexagon_fixsfdi");
2187 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__hexagon_fixdfsi");
2188 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__hexagon_fixdfdi");
2189 setLibcallName(RTLIB::FPEXT_F32_F64, "__hexagon_extendsfdf2");
2190 setLibcallName(RTLIB::FPROUND_F64_F32, "__hexagon_truncdfsf2");
2191 setLibcallName(RTLIB::OEQ_F32, "__hexagon_eqsf2");
2192 setLibcallName(RTLIB::OEQ_F64, "__hexagon_eqdf2");
2193 setLibcallName(RTLIB::OGE_F32, "__hexagon_gesf2");
2194 setLibcallName(RTLIB::OGE_F64, "__hexagon_gedf2");
2195 setLibcallName(RTLIB::OLE_F32, "__hexagon_lesf2");
2196 setLibcallName(RTLIB::OLE_F64, "__hexagon_ledf2");
2197 setLibcallName(RTLIB::UNE_F32, "__hexagon_nesf2");
2198 setLibcallName(RTLIB::UNE_F64, "__hexagon_nedf2");
2199 setLibcallName(RTLIB::UO_F32, "__hexagon_unordsf2");
2200 setLibcallName(RTLIB::UO_F64, "__hexagon_unorddf2");
2201 setLibcallName(RTLIB::O_F32, "__hexagon_unordsf2");
2202 setLibcallName(RTLIB::O_F64, "__hexagon_unorddf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002203 }
2204
2205 // These cause problems when the shift amount is non-constant.
2206 setLibcallName(RTLIB::SHL_I128, nullptr);
2207 setLibcallName(RTLIB::SRL_I128, nullptr);
2208 setLibcallName(RTLIB::SRA_I128, nullptr);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002209}
2210
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002211
2212const char* HexagonTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00002213 switch ((HexagonISD::NodeType)Opcode) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002214 case HexagonISD::ALLOCA: return "HexagonISD::ALLOCA";
2215 case HexagonISD::ARGEXTEND: return "HexagonISD::ARGEXTEND";
2216 case HexagonISD::AT_GOT: return "HexagonISD::AT_GOT";
2217 case HexagonISD::AT_PCREL: return "HexagonISD::AT_PCREL";
2218 case HexagonISD::BARRIER: return "HexagonISD::BARRIER";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002219 case HexagonISD::CALLR: return "HexagonISD::CALLR";
2220 case HexagonISD::CALLv3nr: return "HexagonISD::CALLv3nr";
2221 case HexagonISD::CALLv3: return "HexagonISD::CALLv3";
2222 case HexagonISD::COMBINE: return "HexagonISD::COMBINE";
2223 case HexagonISD::CONST32_GP: return "HexagonISD::CONST32_GP";
2224 case HexagonISD::CONST32: return "HexagonISD::CONST32";
2225 case HexagonISD::CP: return "HexagonISD::CP";
2226 case HexagonISD::DCFETCH: return "HexagonISD::DCFETCH";
2227 case HexagonISD::EH_RETURN: return "HexagonISD::EH_RETURN";
2228 case HexagonISD::EXTRACTU: return "HexagonISD::EXTRACTU";
2229 case HexagonISD::EXTRACTURP: return "HexagonISD::EXTRACTURP";
2230 case HexagonISD::FCONST32: return "HexagonISD::FCONST32";
2231 case HexagonISD::INSERT: return "HexagonISD::INSERT";
2232 case HexagonISD::INSERTRP: return "HexagonISD::INSERTRP";
2233 case HexagonISD::JT: return "HexagonISD::JT";
2234 case HexagonISD::PACKHL: return "HexagonISD::PACKHL";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002235 case HexagonISD::POPCOUNT: return "HexagonISD::POPCOUNT";
2236 case HexagonISD::RET_FLAG: return "HexagonISD::RET_FLAG";
2237 case HexagonISD::SHUFFEB: return "HexagonISD::SHUFFEB";
2238 case HexagonISD::SHUFFEH: return "HexagonISD::SHUFFEH";
2239 case HexagonISD::SHUFFOB: return "HexagonISD::SHUFFOB";
2240 case HexagonISD::SHUFFOH: return "HexagonISD::SHUFFOH";
2241 case HexagonISD::TC_RETURN: return "HexagonISD::TC_RETURN";
2242 case HexagonISD::VCMPBEQ: return "HexagonISD::VCMPBEQ";
2243 case HexagonISD::VCMPBGT: return "HexagonISD::VCMPBGT";
2244 case HexagonISD::VCMPBGTU: return "HexagonISD::VCMPBGTU";
2245 case HexagonISD::VCMPHEQ: return "HexagonISD::VCMPHEQ";
2246 case HexagonISD::VCMPHGT: return "HexagonISD::VCMPHGT";
2247 case HexagonISD::VCMPHGTU: return "HexagonISD::VCMPHGTU";
2248 case HexagonISD::VCMPWEQ: return "HexagonISD::VCMPWEQ";
2249 case HexagonISD::VCMPWGT: return "HexagonISD::VCMPWGT";
2250 case HexagonISD::VCMPWGTU: return "HexagonISD::VCMPWGTU";
Krzysztof Parzyszekc168c012015-12-03 16:47:20 +00002251 case HexagonISD::VCOMBINE: return "HexagonISD::VCOMBINE";
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002252 case HexagonISD::VPACK: return "HexagonISD::VPACK";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002253 case HexagonISD::VSHLH: return "HexagonISD::VSHLH";
2254 case HexagonISD::VSHLW: return "HexagonISD::VSHLW";
2255 case HexagonISD::VSPLATB: return "HexagonISD::VSPLTB";
2256 case HexagonISD::VSPLATH: return "HexagonISD::VSPLATH";
2257 case HexagonISD::VSRAH: return "HexagonISD::VSRAH";
2258 case HexagonISD::VSRAW: return "HexagonISD::VSRAW";
2259 case HexagonISD::VSRLH: return "HexagonISD::VSRLH";
2260 case HexagonISD::VSRLW: return "HexagonISD::VSRLW";
2261 case HexagonISD::VSXTBH: return "HexagonISD::VSXTBH";
2262 case HexagonISD::VSXTBW: return "HexagonISD::VSXTBW";
Matthias Braund04893f2015-05-07 21:33:59 +00002263 case HexagonISD::OP_END: break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002264 }
Matthias Braund04893f2015-05-07 21:33:59 +00002265 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002266}
2267
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002268bool HexagonTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002269 EVT MTy1 = EVT::getEVT(Ty1);
2270 EVT MTy2 = EVT::getEVT(Ty2);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002271 if (!MTy1.isSimple() || !MTy2.isSimple())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002272 return false;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002273 return (MTy1.getSimpleVT() == MVT::i64) && (MTy2.getSimpleVT() == MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002274}
2275
2276bool HexagonTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002277 if (!VT1.isSimple() || !VT2.isSimple())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002278 return false;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002279 return (VT1.getSimpleVT() == MVT::i64) && (VT2.getSimpleVT() == MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002280}
2281
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002282// Should we expand the build vector with shuffles?
2283bool
2284HexagonTargetLowering::shouldExpandBuildVectorWithShuffles(EVT VT,
2285 unsigned DefinedValues) const {
2286
2287 // Hexagon vector shuffle operates on element sizes of bytes or halfwords
2288 EVT EltVT = VT.getVectorElementType();
2289 int EltBits = EltVT.getSizeInBits();
2290 if ((EltBits != 8) && (EltBits != 16))
2291 return false;
2292
2293 return TargetLowering::shouldExpandBuildVectorWithShuffles(VT, DefinedValues);
2294}
2295
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002296static StridedLoadKind isStridedLoad(ArrayRef<int> &Mask) {
2297 int even_start = -2;
2298 int odd_start = -1;
2299 size_t mask_len = Mask.size();
2300 for (auto idx : Mask) {
2301 if ((idx - even_start) == 2)
2302 even_start = idx;
2303 else
2304 break;
2305 }
2306 if (even_start == (int)(mask_len * 2) - 2)
2307 return StridedLoadKind::Even;
2308 for (auto idx : Mask) {
2309 if ((idx - odd_start) == 2)
2310 odd_start = idx;
2311 else
2312 break;
2313 }
2314 if (odd_start == (int)(mask_len * 2) - 1)
2315 return StridedLoadKind::Odd;
2316
2317 return StridedLoadKind::NoPattern;
2318}
2319
2320// Lower a vector shuffle (V1, V2, V3). V1 and V2 are the two vectors
2321// to select data from, V3 is the permutation.
2322SDValue
2323HexagonTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG)
2324 const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002325 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
2326 SDValue V1 = Op.getOperand(0);
2327 SDValue V2 = Op.getOperand(1);
2328 SDLoc dl(Op);
2329 EVT VT = Op.getValueType();
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002330 bool UseHVX = Subtarget.useHVXOps();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002331
Sanjay Patel57195842016-03-14 17:28:46 +00002332 if (V2.isUndef())
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002333 V2 = V1;
2334
2335 if (SVN->isSplat()) {
2336 int Lane = SVN->getSplatIndex();
2337 if (Lane == -1) Lane = 0;
2338
2339 // Test if V1 is a SCALAR_TO_VECTOR.
2340 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR)
2341 return createSplat(DAG, dl, VT, V1.getOperand(0));
2342
2343 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
2344 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
2345 // reaches it).
2346 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
2347 !isa<ConstantSDNode>(V1.getOperand(0))) {
2348 bool IsScalarToVector = true;
2349 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
Sanjay Patel75068522016-03-14 18:09:43 +00002350 if (!V1.getOperand(i).isUndef()) {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002351 IsScalarToVector = false;
2352 break;
2353 }
2354 if (IsScalarToVector)
2355 return createSplat(DAG, dl, VT, V1.getOperand(0));
2356 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002357 return createSplat(DAG, dl, VT, DAG.getConstant(Lane, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002358 }
2359
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002360 if (UseHVX) {
2361 ArrayRef<int> Mask = SVN->getMask();
2362 size_t MaskLen = Mask.size();
2363 int ElemSizeInBits = VT.getVectorElementType().getSizeInBits();
2364 if ((Subtarget.useHVXSglOps() && (ElemSizeInBits * MaskLen) == 64 * 8) ||
2365 (Subtarget.useHVXDblOps() && (ElemSizeInBits * MaskLen) == 128 * 8)) {
2366 // Return 1 for odd and 2 of even
2367 StridedLoadKind Pattern = isStridedLoad(Mask);
2368
2369 if (Pattern == StridedLoadKind::NoPattern)
2370 return SDValue();
2371
2372 SDValue Vec0 = Op.getOperand(0);
2373 SDValue Vec1 = Op.getOperand(1);
2374 SDValue StridePattern = DAG.getConstant(Pattern, dl, MVT::i32);
2375 SDValue Ops[] = { Vec1, Vec0, StridePattern };
2376 return DAG.getNode(HexagonISD::VPACK, dl, VT, Ops);
2377 }
2378 // We used to assert in the "else" part here, but that is bad for Halide
2379 // Halide creates intermediate double registers by interleaving two
2380 // concatenated vector registers. The interleaving requires vector_shuffle
2381 // nodes and we shouldn't barf on a double register result of a
2382 // vector_shuffle because it is most likely an intermediate result.
2383 }
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002384 // FIXME: We need to support more general vector shuffles. See
2385 // below the comment from the ARM backend that deals in the general
2386 // case with the vector shuffles. For now, let expand handle these.
2387 return SDValue();
2388
2389 // If the shuffle is not directly supported and it has 4 elements, use
2390 // the PerfectShuffle-generated table to synthesize it from other shuffles.
2391}
2392
2393// If BUILD_VECTOR has same base element repeated several times,
2394// report true.
2395static bool isCommonSplatElement(BuildVectorSDNode *BVN) {
2396 unsigned NElts = BVN->getNumOperands();
2397 SDValue V0 = BVN->getOperand(0);
2398
2399 for (unsigned i = 1, e = NElts; i != e; ++i) {
2400 if (BVN->getOperand(i) != V0)
2401 return false;
2402 }
2403 return true;
2404}
2405
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002406// Lower a vector shift. Try to convert
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002407// <VT> = SHL/SRA/SRL <VT> by <VT> to Hexagon specific
2408// <VT> = SHL/SRA/SRL <VT> by <IT/i32>.
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002409SDValue
2410HexagonTargetLowering::LowerVECTOR_SHIFT(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002411 BuildVectorSDNode *BVN = 0;
2412 SDValue V1 = Op.getOperand(0);
2413 SDValue V2 = Op.getOperand(1);
2414 SDValue V3;
2415 SDLoc dl(Op);
2416 EVT VT = Op.getValueType();
2417
2418 if ((BVN = dyn_cast<BuildVectorSDNode>(V1.getNode())) &&
2419 isCommonSplatElement(BVN))
2420 V3 = V2;
2421 else if ((BVN = dyn_cast<BuildVectorSDNode>(V2.getNode())) &&
2422 isCommonSplatElement(BVN))
2423 V3 = V1;
2424 else
2425 return SDValue();
2426
2427 SDValue CommonSplat = BVN->getOperand(0);
2428 SDValue Result;
2429
2430 if (VT.getSimpleVT() == MVT::v4i16) {
2431 switch (Op.getOpcode()) {
2432 case ISD::SRA:
2433 Result = DAG.getNode(HexagonISD::VSRAH, dl, VT, V3, CommonSplat);
2434 break;
2435 case ISD::SHL:
2436 Result = DAG.getNode(HexagonISD::VSHLH, dl, VT, V3, CommonSplat);
2437 break;
2438 case ISD::SRL:
2439 Result = DAG.getNode(HexagonISD::VSRLH, dl, VT, V3, CommonSplat);
2440 break;
2441 default:
2442 return SDValue();
2443 }
2444 } else if (VT.getSimpleVT() == MVT::v2i32) {
2445 switch (Op.getOpcode()) {
2446 case ISD::SRA:
2447 Result = DAG.getNode(HexagonISD::VSRAW, dl, VT, V3, CommonSplat);
2448 break;
2449 case ISD::SHL:
2450 Result = DAG.getNode(HexagonISD::VSHLW, dl, VT, V3, CommonSplat);
2451 break;
2452 case ISD::SRL:
2453 Result = DAG.getNode(HexagonISD::VSRLW, dl, VT, V3, CommonSplat);
2454 break;
2455 default:
2456 return SDValue();
2457 }
2458 } else {
2459 return SDValue();
2460 }
2461
2462 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
2463}
2464
2465SDValue
2466HexagonTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
2467 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
2468 SDLoc dl(Op);
2469 EVT VT = Op.getValueType();
2470
2471 unsigned Size = VT.getSizeInBits();
2472
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002473 // Only handle vectors of 64 bits or shorter.
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002474 if (Size > 64)
2475 return SDValue();
2476
2477 APInt APSplatBits, APSplatUndef;
2478 unsigned SplatBitSize;
2479 bool HasAnyUndefs;
2480 unsigned NElts = BVN->getNumOperands();
2481
2482 // Try to generate a SPLAT instruction.
2483 if ((VT.getSimpleVT() == MVT::v4i8 || VT.getSimpleVT() == MVT::v4i16) &&
2484 (BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
2485 HasAnyUndefs, 0, true) && SplatBitSize <= 16)) {
2486 unsigned SplatBits = APSplatBits.getZExtValue();
2487 int32_t SextVal = ((int32_t) (SplatBits << (32 - SplatBitSize)) >>
2488 (32 - SplatBitSize));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002489 return createSplat(DAG, dl, VT, DAG.getConstant(SextVal, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002490 }
2491
2492 // Try to generate COMBINE to build v2i32 vectors.
2493 if (VT.getSimpleVT() == MVT::v2i32) {
2494 SDValue V0 = BVN->getOperand(0);
2495 SDValue V1 = BVN->getOperand(1);
2496
Sanjay Patel57195842016-03-14 17:28:46 +00002497 if (V0.isUndef())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002498 V0 = DAG.getConstant(0, dl, MVT::i32);
Sanjay Patel57195842016-03-14 17:28:46 +00002499 if (V1.isUndef())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002500 V1 = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002501
2502 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(V0);
2503 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(V1);
2504 // If the element isn't a constant, it is in a register:
2505 // generate a COMBINE Register Register instruction.
2506 if (!C0 || !C1)
2507 return DAG.getNode(HexagonISD::COMBINE, dl, VT, V1, V0);
2508
2509 // If one of the operands is an 8 bit integer constant, generate
2510 // a COMBINE Immediate Immediate instruction.
2511 if (isInt<8>(C0->getSExtValue()) ||
2512 isInt<8>(C1->getSExtValue()))
2513 return DAG.getNode(HexagonISD::COMBINE, dl, VT, V1, V0);
2514 }
2515
2516 // Try to generate a S2_packhl to build v2i16 vectors.
2517 if (VT.getSimpleVT() == MVT::v2i16) {
2518 for (unsigned i = 0, e = NElts; i != e; ++i) {
Sanjay Patel57195842016-03-14 17:28:46 +00002519 if (BVN->getOperand(i).isUndef())
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002520 continue;
2521 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(BVN->getOperand(i));
2522 // If the element isn't a constant, it is in a register:
2523 // generate a S2_packhl instruction.
2524 if (!Cst) {
2525 SDValue pack = DAG.getNode(HexagonISD::PACKHL, dl, MVT::v4i16,
2526 BVN->getOperand(1), BVN->getOperand(0));
2527
2528 return DAG.getTargetExtractSubreg(Hexagon::subreg_loreg, dl, MVT::v2i16,
2529 pack);
2530 }
2531 }
2532 }
2533
2534 // In the general case, generate a CONST32 or a CONST64 for constant vectors,
2535 // and insert_vector_elt for all the other cases.
2536 uint64_t Res = 0;
2537 unsigned EltSize = Size / NElts;
2538 SDValue ConstVal;
2539 uint64_t Mask = ~uint64_t(0ULL) >> (64 - EltSize);
2540 bool HasNonConstantElements = false;
2541
2542 for (unsigned i = 0, e = NElts; i != e; ++i) {
2543 // LLVM's BUILD_VECTOR operands are in Little Endian mode, whereas Hexagon's
2544 // combine, const64, etc. are Big Endian.
2545 unsigned OpIdx = NElts - i - 1;
2546 SDValue Operand = BVN->getOperand(OpIdx);
Sanjay Patel57195842016-03-14 17:28:46 +00002547 if (Operand.isUndef())
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002548 continue;
2549
2550 int64_t Val = 0;
2551 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Operand))
2552 Val = Cst->getSExtValue();
2553 else
2554 HasNonConstantElements = true;
2555
2556 Val &= Mask;
2557 Res = (Res << EltSize) | Val;
2558 }
2559
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002560 if (Size > 64)
2561 return SDValue();
2562
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002563 if (Size == 64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002564 ConstVal = DAG.getConstant(Res, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002565 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002566 ConstVal = DAG.getConstant(Res, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002567
2568 // When there are non constant operands, add them with INSERT_VECTOR_ELT to
2569 // ConstVal, the constant part of the vector.
2570 if (HasNonConstantElements) {
2571 EVT EltVT = VT.getVectorElementType();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002572 SDValue Width = DAG.getConstant(EltVT.getSizeInBits(), dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002573 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002574 DAG.getConstant(32, dl, MVT::i64));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002575
2576 for (unsigned i = 0, e = NElts; i != e; ++i) {
2577 // LLVM's BUILD_VECTOR operands are in Little Endian mode, whereas Hexagon
2578 // is Big Endian.
2579 unsigned OpIdx = NElts - i - 1;
2580 SDValue Operand = BVN->getOperand(OpIdx);
Benjamin Kramer619c4e52015-04-10 11:24:51 +00002581 if (isa<ConstantSDNode>(Operand))
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002582 // This operand is already in ConstVal.
2583 continue;
2584
2585 if (VT.getSizeInBits() == 64 &&
2586 Operand.getValueType().getSizeInBits() == 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002587 SDValue C = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002588 Operand = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, Operand);
2589 }
2590
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002591 SDValue Idx = DAG.getConstant(OpIdx, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002592 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i64, Idx, Width);
2593 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2594 const SDValue Ops[] = {ConstVal, Operand, Combined};
2595
2596 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002597 ConstVal = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002598 else
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002599 ConstVal = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002600 }
2601 }
2602
2603 return DAG.getNode(ISD::BITCAST, dl, VT, ConstVal);
2604}
2605
2606SDValue
2607HexagonTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
2608 SelectionDAG &DAG) const {
2609 SDLoc dl(Op);
Krzysztof Parzyszekc168c012015-12-03 16:47:20 +00002610 bool UseHVX = Subtarget.useHVXOps();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002611 EVT VT = Op.getValueType();
2612 unsigned NElts = Op.getNumOperands();
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002613 SDValue Vec0 = Op.getOperand(0);
2614 EVT VecVT = Vec0.getValueType();
2615 unsigned Width = VecVT.getSizeInBits();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002616
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002617 if (NElts == 2) {
2618 MVT ST = VecVT.getSimpleVT();
2619 // We are trying to concat two v2i16 to a single v4i16, or two v4i8
2620 // into a single v8i8.
2621 if (ST == MVT::v2i16 || ST == MVT::v4i8)
2622 return DAG.getNode(HexagonISD::COMBINE, dl, VT, Op.getOperand(1), Vec0);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002623
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002624 if (UseHVX) {
2625 assert((Width == 64*8 && Subtarget.useHVXSglOps()) ||
2626 (Width == 128*8 && Subtarget.useHVXDblOps()));
2627 SDValue Vec1 = Op.getOperand(1);
2628 MVT OpTy = Subtarget.useHVXSglOps() ? MVT::v16i32 : MVT::v32i32;
2629 MVT ReTy = Subtarget.useHVXSglOps() ? MVT::v32i32 : MVT::v64i32;
2630 SDValue B0 = DAG.getNode(ISD::BITCAST, dl, OpTy, Vec0);
2631 SDValue B1 = DAG.getNode(ISD::BITCAST, dl, OpTy, Vec1);
2632 SDValue VC = DAG.getNode(HexagonISD::VCOMBINE, dl, ReTy, B1, B0);
2633 return DAG.getNode(ISD::BITCAST, dl, VT, VC);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002634 }
2635 }
2636
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002637 if (VT.getSizeInBits() != 32 && VT.getSizeInBits() != 64)
2638 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002639
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002640 SDValue C0 = DAG.getConstant(0, dl, MVT::i64);
2641 SDValue C32 = DAG.getConstant(32, dl, MVT::i64);
2642 SDValue W = DAG.getConstant(Width, dl, MVT::i64);
2643 // Create the "width" part of the argument to insert_rp/insertp_rp.
2644 SDValue S = DAG.getNode(ISD::SHL, dl, MVT::i64, W, C32);
2645 SDValue V = C0;
2646
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002647 for (unsigned i = 0, e = NElts; i != e; ++i) {
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002648 unsigned N = NElts-i-1;
2649 SDValue OpN = Op.getOperand(N);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002650
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002651 if (VT.getSizeInBits() == 64 && OpN.getValueType().getSizeInBits() == 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002652 SDValue C = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002653 OpN = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, OpN);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002654 }
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002655 SDValue Idx = DAG.getConstant(N, dl, MVT::i64);
2656 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i64, Idx, W);
2657 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, S, Offset);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002658 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002659 V = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, {V, OpN, Or});
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002660 else if (VT.getSizeInBits() == 64)
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002661 V = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, {V, OpN, Or});
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002662 else
2663 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002664 }
2665
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002666 return DAG.getNode(ISD::BITCAST, dl, VT, V);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002667}
2668
2669SDValue
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002670HexagonTargetLowering::LowerEXTRACT_SUBVECTOR_HVX(SDValue Op,
2671 SelectionDAG &DAG) const {
2672 EVT VT = Op.getOperand(0).getValueType();
2673 SDLoc dl(Op);
2674 bool UseHVX = Subtarget.useHVXOps();
2675 bool UseHVXSgl = Subtarget.useHVXSglOps();
2676 // Just in case...
2677
2678 if (!VT.isVector() || !UseHVX)
2679 return SDValue();
2680
2681 EVT ResVT = Op.getValueType();
2682 unsigned ResSize = ResVT.getSizeInBits();
2683 unsigned VectorSizeInBits = UseHVXSgl ? (64 * 8) : (128 * 8);
2684 unsigned OpSize = VT.getSizeInBits();
2685
2686 // We deal only with cases where the result is the vector size
2687 // and the vector operand is a double register.
2688 if (!(ResVT.isByteSized() && ResSize == VectorSizeInBits) ||
2689 !(VT.isByteSized() && OpSize == 2 * VectorSizeInBits))
2690 return SDValue();
2691
2692 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Op.getOperand(1));
2693 if (!Cst)
2694 return SDValue();
2695 unsigned Val = Cst->getZExtValue();
2696
2697 // These two will get lowered to an appropriate EXTRACT_SUBREG in ISel.
2698 if (Val == 0) {
2699 SDValue Vec = Op.getOperand(0);
2700 unsigned Subreg = Hexagon::subreg_loreg;
2701 return DAG.getTargetExtractSubreg(Subreg, dl, ResVT, Vec);
2702 }
2703
2704 if (ResVT.getVectorNumElements() == Val) {
2705 SDValue Vec = Op.getOperand(0);
2706 unsigned Subreg = Hexagon::subreg_hireg;
2707 return DAG.getTargetExtractSubreg(Subreg, dl, ResVT, Vec);
2708 }
2709
2710 return SDValue();
2711}
2712
2713SDValue
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002714HexagonTargetLowering::LowerEXTRACT_VECTOR(SDValue Op,
2715 SelectionDAG &DAG) const {
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002716 // If we are dealing with EXTRACT_SUBVECTOR on a HVX type, we may
2717 // be able to simplify it to an EXTRACT_SUBREG.
2718 if (Op.getOpcode() == ISD::EXTRACT_SUBVECTOR && Subtarget.useHVXOps() &&
2719 IsHvxVectorType(Op.getValueType().getSimpleVT()))
2720 return LowerEXTRACT_SUBVECTOR_HVX(Op, DAG);
2721
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002722 EVT VT = Op.getValueType();
2723 int VTN = VT.isVector() ? VT.getVectorNumElements() : 1;
2724 SDLoc dl(Op);
2725 SDValue Idx = Op.getOperand(1);
2726 SDValue Vec = Op.getOperand(0);
2727 EVT VecVT = Vec.getValueType();
2728 EVT EltVT = VecVT.getVectorElementType();
2729 int EltSize = EltVT.getSizeInBits();
2730 SDValue Width = DAG.getConstant(Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT ?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002731 EltSize : VTN * EltSize, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002732
2733 // Constant element number.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002734 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Idx)) {
2735 uint64_t X = CI->getZExtValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002736 SDValue Offset = DAG.getConstant(X * EltSize, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002737 const SDValue Ops[] = {Vec, Width, Offset};
2738
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002739 ConstantSDNode *CW = dyn_cast<ConstantSDNode>(Width);
2740 assert(CW && "Non constant width in LowerEXTRACT_VECTOR");
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002741
2742 SDValue N;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002743 MVT SVT = VecVT.getSimpleVT();
2744 uint64_t W = CW->getZExtValue();
2745
2746 if (W == 32) {
2747 // Translate this node into EXTRACT_SUBREG.
2748 unsigned Subreg = (X == 0) ? Hexagon::subreg_loreg : 0;
2749
2750 if (X == 0)
2751 Subreg = Hexagon::subreg_loreg;
2752 else if (SVT == MVT::v2i32 && X == 1)
2753 Subreg = Hexagon::subreg_hireg;
2754 else if (SVT == MVT::v4i16 && X == 2)
2755 Subreg = Hexagon::subreg_hireg;
2756 else if (SVT == MVT::v8i8 && X == 4)
2757 Subreg = Hexagon::subreg_hireg;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002758 else
2759 llvm_unreachable("Bad offset");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002760 N = DAG.getTargetExtractSubreg(Subreg, dl, MVT::i32, Vec);
2761
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002762 } else if (SVT.getSizeInBits() == 32) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002763 N = DAG.getNode(HexagonISD::EXTRACTU, dl, MVT::i32, Ops);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002764 } else if (SVT.getSizeInBits() == 64) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002765 N = DAG.getNode(HexagonISD::EXTRACTU, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002766 if (VT.getSizeInBits() == 32)
2767 N = DAG.getTargetExtractSubreg(Hexagon::subreg_loreg, dl, MVT::i32, N);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002768 } else
2769 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002770
2771 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2772 }
2773
2774 // Variable element number.
2775 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i32, Idx,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002776 DAG.getConstant(EltSize, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002777 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002778 DAG.getConstant(32, dl, MVT::i64));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002779 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2780
2781 const SDValue Ops[] = {Vec, Combined};
2782
2783 SDValue N;
2784 if (VecVT.getSizeInBits() == 32) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002785 N = DAG.getNode(HexagonISD::EXTRACTURP, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002786 } else {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002787 N = DAG.getNode(HexagonISD::EXTRACTURP, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002788 if (VT.getSizeInBits() == 32)
2789 N = DAG.getTargetExtractSubreg(Hexagon::subreg_loreg, dl, MVT::i32, N);
2790 }
2791 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2792}
2793
2794SDValue
2795HexagonTargetLowering::LowerINSERT_VECTOR(SDValue Op,
2796 SelectionDAG &DAG) const {
2797 EVT VT = Op.getValueType();
2798 int VTN = VT.isVector() ? VT.getVectorNumElements() : 1;
2799 SDLoc dl(Op);
2800 SDValue Vec = Op.getOperand(0);
2801 SDValue Val = Op.getOperand(1);
2802 SDValue Idx = Op.getOperand(2);
2803 EVT VecVT = Vec.getValueType();
2804 EVT EltVT = VecVT.getVectorElementType();
2805 int EltSize = EltVT.getSizeInBits();
2806 SDValue Width = DAG.getConstant(Op.getOpcode() == ISD::INSERT_VECTOR_ELT ?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002807 EltSize : VTN * EltSize, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002808
2809 if (ConstantSDNode *C = cast<ConstantSDNode>(Idx)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002810 SDValue Offset = DAG.getConstant(C->getSExtValue() * EltSize, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002811 const SDValue Ops[] = {Vec, Val, Width, Offset};
2812
2813 SDValue N;
2814 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002815 N = DAG.getNode(HexagonISD::INSERT, dl, MVT::i32, Ops);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002816 else if (VT.getSizeInBits() == 64)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002817 N = DAG.getNode(HexagonISD::INSERT, dl, MVT::i64, Ops);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002818 else
2819 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002820
2821 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2822 }
2823
2824 // Variable element number.
2825 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i32, Idx,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002826 DAG.getConstant(EltSize, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002827 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002828 DAG.getConstant(32, dl, MVT::i64));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002829 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2830
2831 if (VT.getSizeInBits() == 64 &&
2832 Val.getValueType().getSizeInBits() == 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002833 SDValue C = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002834 Val = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, Val);
2835 }
2836
2837 const SDValue Ops[] = {Vec, Val, Combined};
2838
2839 SDValue N;
2840 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002841 N = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, Ops);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002842 else if (VT.getSizeInBits() == 64)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002843 N = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, Ops);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002844 else
2845 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002846
2847 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2848}
2849
Tim Northovera4415852013-08-06 09:12:35 +00002850bool
2851HexagonTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
2852 // Assuming the caller does not have either a signext or zeroext modifier, and
2853 // only one value is accepted, any reasonable truncation is allowed.
2854 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
2855 return false;
2856
2857 // FIXME: in principle up to 64-bit could be made safe, but it would be very
2858 // fragile at the moment: any support for multiple value returns would be
2859 // liable to disallow tail calls involving i64 -> iN truncation in many cases.
2860 return Ty1->getPrimitiveSizeInBits() <= 32;
2861}
2862
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002863SDValue
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002864HexagonTargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
2865 SDValue Chain = Op.getOperand(0);
2866 SDValue Offset = Op.getOperand(1);
2867 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002868 SDLoc dl(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002869 auto PtrVT = getPointerTy(DAG.getDataLayout());
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002870
2871 // Mark function as containing a call to EH_RETURN.
2872 HexagonMachineFunctionInfo *FuncInfo =
2873 DAG.getMachineFunction().getInfo<HexagonMachineFunctionInfo>();
2874 FuncInfo->setHasEHReturn();
2875
2876 unsigned OffsetReg = Hexagon::R28;
2877
Mehdi Amini44ede332015-07-09 02:09:04 +00002878 SDValue StoreAddr =
2879 DAG.getNode(ISD::ADD, dl, PtrVT, DAG.getRegister(Hexagon::R30, PtrVT),
2880 DAG.getIntPtrConstant(4, dl));
Justin Lebar9c375812016-07-15 18:27:10 +00002881 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo());
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002882 Chain = DAG.getCopyToReg(Chain, dl, OffsetReg, Offset);
2883
2884 // Not needed we already use it as explict input to EH_RETURN.
2885 // MF.getRegInfo().addLiveOut(OffsetReg);
2886
2887 return DAG.getNode(HexagonISD::EH_RETURN, dl, MVT::Other, Chain);
2888}
2889
2890SDValue
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002891HexagonTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002892 unsigned Opc = Op.getOpcode();
2893 switch (Opc) {
2894 default:
2895#ifndef NDEBUG
2896 Op.getNode()->dumpr(&DAG);
2897 if (Opc > HexagonISD::OP_BEGIN && Opc < HexagonISD::OP_END)
2898 errs() << "Check for a non-legal type in this operation\n";
2899#endif
2900 llvm_unreachable("Should not custom lower this!");
2901 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
2902 case ISD::INSERT_SUBVECTOR: return LowerINSERT_VECTOR(Op, DAG);
2903 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR(Op, DAG);
2904 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_VECTOR(Op, DAG);
2905 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR(Op, DAG);
2906 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2907 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002908 case ISD::SRA:
2909 case ISD::SHL:
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002910 case ISD::SRL: return LowerVECTOR_SHIFT(Op, DAG);
2911 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002912 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002913 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
2914 // Frame & Return address. Currently unimplemented.
2915 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
2916 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00002917 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002918 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
2919 case ISD::GlobalAddress: return LowerGLOBALADDRESS(Op, DAG);
2920 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002921 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002922 case ISD::VASTART: return LowerVASTART(Op, DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002923 // Custom lower some vector loads.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002924 case ISD::LOAD: return LowerLOAD(Op, DAG);
2925 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
2926 case ISD::SETCC: return LowerSETCC(Op, DAG);
2927 case ISD::VSELECT: return LowerVSELECT(Op, DAG);
2928 case ISD::CTPOP: return LowerCTPOP(Op, DAG);
2929 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00002930 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002931 case ISD::INLINEASM: return LowerINLINEASM(Op, DAG);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00002932 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002933 }
2934}
2935
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002936/// Returns relocation base for the given PIC jumptable.
2937SDValue
2938HexagonTargetLowering::getPICJumpTableRelocBase(SDValue Table,
2939 SelectionDAG &DAG) const {
2940 int Idx = cast<JumpTableSDNode>(Table)->getIndex();
2941 EVT VT = Table.getValueType();
2942 SDValue T = DAG.getTargetJumpTable(Idx, VT, HexagonII::MO_PCREL);
2943 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Table), VT, T);
2944}
2945
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00002946MachineBasicBlock *HexagonTargetLowering::EmitInstrWithCustomInserter(
2947 MachineInstr &MI, MachineBasicBlock *BB) const {
2948 switch (MI.getOpcode()) {
2949 case Hexagon::ALLOCA: {
2950 MachineFunction *MF = BB->getParent();
2951 auto *FuncInfo = MF->getInfo<HexagonMachineFunctionInfo>();
2952 FuncInfo->addAllocaAdjustInst(&MI);
2953 return BB;
2954 }
2955 default:
2956 llvm_unreachable("Unexpected instr type to insert");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002957 } // switch
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002958}
2959
2960//===----------------------------------------------------------------------===//
2961// Inline Assembly Support
2962//===----------------------------------------------------------------------===//
2963
Krzysztof Parzyszekca3b5322016-05-18 14:34:51 +00002964TargetLowering::ConstraintType
2965HexagonTargetLowering::getConstraintType(StringRef Constraint) const {
2966 if (Constraint.size() == 1) {
2967 switch (Constraint[0]) {
2968 case 'q':
2969 case 'v':
2970 if (Subtarget.useHVXOps())
2971 return C_Register;
2972 break;
2973 }
2974 }
2975 return TargetLowering::getConstraintType(Constraint);
2976}
2977
Eric Christopher11e4df72015-02-26 22:38:43 +00002978std::pair<unsigned, const TargetRegisterClass *>
2979HexagonTargetLowering::getRegForInlineAsmConstraint(
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00002980 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002981 bool UseHVX = Subtarget.useHVXOps(), UseHVXDbl = Subtarget.useHVXDblOps();
2982
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002983 if (Constraint.size() == 1) {
2984 switch (Constraint[0]) {
2985 case 'r': // R0-R31
Chad Rosier295bd432013-06-22 18:37:38 +00002986 switch (VT.SimpleTy) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002987 default:
Craig Toppere55c5562012-02-07 02:50:20 +00002988 llvm_unreachable("getRegForInlineAsmConstraint Unhandled data type");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002989 case MVT::i32:
2990 case MVT::i16:
2991 case MVT::i8:
Sirish Pande69295b82012-05-10 20:20:25 +00002992 case MVT::f32:
Craig Topperc7242e02012-04-20 07:30:17 +00002993 return std::make_pair(0U, &Hexagon::IntRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002994 case MVT::i64:
Sirish Pande69295b82012-05-10 20:20:25 +00002995 case MVT::f64:
Craig Topperc7242e02012-04-20 07:30:17 +00002996 return std::make_pair(0U, &Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002997 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002998 case 'q': // q0-q3
2999 switch (VT.SimpleTy) {
3000 default:
3001 llvm_unreachable("getRegForInlineAsmConstraint Unhandled data type");
3002 case MVT::v1024i1:
3003 case MVT::v512i1:
3004 case MVT::v32i16:
3005 case MVT::v16i32:
3006 case MVT::v64i8:
3007 case MVT::v8i64:
3008 return std::make_pair(0U, &Hexagon::VecPredRegsRegClass);
3009 }
3010 case 'v': // V0-V31
3011 switch (VT.SimpleTy) {
3012 default:
3013 llvm_unreachable("getRegForInlineAsmConstraint Unhandled data type");
3014 case MVT::v16i32:
3015 case MVT::v32i16:
3016 case MVT::v64i8:
3017 case MVT::v8i64:
3018 return std::make_pair(0U, &Hexagon::VectorRegsRegClass);
3019 case MVT::v32i32:
3020 case MVT::v64i16:
3021 case MVT::v16i64:
3022 case MVT::v128i8:
3023 if (Subtarget.hasV60TOps() && UseHVX && UseHVXDbl)
3024 return std::make_pair(0U, &Hexagon::VectorRegs128BRegClass);
3025 else
3026 return std::make_pair(0U, &Hexagon::VecDblRegsRegClass);
3027 case MVT::v256i8:
3028 case MVT::v128i16:
3029 case MVT::v64i32:
3030 case MVT::v32i64:
3031 return std::make_pair(0U, &Hexagon::VecDblRegs128BRegClass);
3032 }
3033
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003034 default:
Craig Toppere55c5562012-02-07 02:50:20 +00003035 llvm_unreachable("Unknown asm register class");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003036 }
3037 }
3038
Eric Christopher11e4df72015-02-26 22:38:43 +00003039 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003040}
3041
Sirish Pande69295b82012-05-10 20:20:25 +00003042/// isFPImmLegal - Returns true if the target can instruction select the
3043/// specified FP immediate natively. If false, the legalizer will
3044/// materialize the FP immediate as a load from a constant pool.
3045bool HexagonTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00003046 return Subtarget.hasV5TOps();
Sirish Pande69295b82012-05-10 20:20:25 +00003047}
3048
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003049/// isLegalAddressingMode - Return true if the addressing mode represented by
3050/// AM is legal for this target, for a load/store of the specified type.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +00003051bool HexagonTargetLowering::isLegalAddressingMode(const DataLayout &DL,
3052 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +00003053 unsigned AS) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003054 // Allows a signed-extended 11-bit immediate field.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00003055 if (AM.BaseOffs <= -(1LL << 13) || AM.BaseOffs >= (1LL << 13)-1)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003056 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003057
3058 // No global is ever allowed as a base.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00003059 if (AM.BaseGV)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003060 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003061
3062 int Scale = AM.Scale;
3063 if (Scale < 0) Scale = -Scale;
3064 switch (Scale) {
3065 case 0: // No scale reg, "r+i", "r", or just "i".
3066 break;
3067 default: // No scaled addressing mode.
3068 return false;
3069 }
3070 return true;
3071}
3072
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00003073/// Return true if folding a constant offset with the given GlobalAddress is
3074/// legal. It is frequently not legal in PIC relocation models.
3075bool HexagonTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA)
3076 const {
3077 return HTM.getRelocationModel() == Reloc::Static;
3078}
3079
3080
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003081/// isLegalICmpImmediate - Return true if the specified immediate is legal
3082/// icmp immediate, that is the target has icmp instructions which can compare
3083/// a register against the immediate without having to materialize the
3084/// immediate into a register.
3085bool HexagonTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
3086 return Imm >= -512 && Imm <= 511;
3087}
3088
3089/// IsEligibleForTailCallOptimization - Check whether the call is eligible
3090/// for tail call optimization. Targets which want to do tail call
3091/// optimization should implement this function.
3092bool HexagonTargetLowering::IsEligibleForTailCallOptimization(
3093 SDValue Callee,
3094 CallingConv::ID CalleeCC,
3095 bool isVarArg,
3096 bool isCalleeStructRet,
3097 bool isCallerStructRet,
3098 const SmallVectorImpl<ISD::OutputArg> &Outs,
3099 const SmallVectorImpl<SDValue> &OutVals,
3100 const SmallVectorImpl<ISD::InputArg> &Ins,
3101 SelectionDAG& DAG) const {
3102 const Function *CallerF = DAG.getMachineFunction().getFunction();
3103 CallingConv::ID CallerCC = CallerF->getCallingConv();
3104 bool CCMatch = CallerCC == CalleeCC;
3105
3106 // ***************************************************************************
3107 // Look for obvious safe cases to perform tail call optimization that do not
3108 // require ABI changes.
3109 // ***************************************************************************
3110
3111 // If this is a tail call via a function pointer, then don't do it!
Craig Topper66059c92015-11-18 07:07:59 +00003112 if (!(isa<GlobalAddressSDNode>(Callee)) &&
3113 !(isa<ExternalSymbolSDNode>(Callee))) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003114 return false;
3115 }
3116
3117 // Do not optimize if the calling conventions do not match.
3118 if (!CCMatch)
3119 return false;
3120
3121 // Do not tail call optimize vararg calls.
3122 if (isVarArg)
3123 return false;
3124
3125 // Also avoid tail call optimization if either caller or callee uses struct
3126 // return semantics.
3127 if (isCalleeStructRet || isCallerStructRet)
3128 return false;
3129
3130 // In addition to the cases above, we also disable Tail Call Optimization if
3131 // the calling convention code that at least one outgoing argument needs to
3132 // go on the stack. We cannot check that here because at this point that
3133 // information is not available.
3134 return true;
3135}
Colin LeMahieu025f8602014-12-08 21:19:18 +00003136
3137// Return true when the given node fits in a positive half word.
3138bool llvm::isPositiveHalfWord(SDNode *N) {
3139 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N);
3140 if (CN && CN->getSExtValue() > 0 && isInt<16>(CN->getSExtValue()))
3141 return true;
3142
3143 switch (N->getOpcode()) {
3144 default:
3145 return false;
3146 case ISD::SIGN_EXTEND_INREG:
3147 return true;
3148 }
3149}
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003150
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +00003151bool HexagonTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
3152 unsigned AS, unsigned Align, bool *Fast) const {
3153 if (Fast)
3154 *Fast = false;
3155
3156 switch (VT.getSimpleVT().SimpleTy) {
3157 default:
3158 return false;
3159 case MVT::v64i8:
3160 case MVT::v128i8:
3161 case MVT::v256i8:
3162 case MVT::v32i16:
3163 case MVT::v64i16:
3164 case MVT::v128i16:
3165 case MVT::v16i32:
3166 case MVT::v32i32:
3167 case MVT::v64i32:
3168 case MVT::v8i64:
3169 case MVT::v16i64:
3170 case MVT::v32i64:
3171 return true;
3172 }
3173 return false;
3174}
3175
Krzysztof Parzyszek3e137e32016-07-29 17:50:47 +00003176
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003177std::pair<const TargetRegisterClass*, uint8_t>
3178HexagonTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
3179 MVT VT) const {
3180 const TargetRegisterClass *RRC = nullptr;
3181
3182 uint8_t Cost = 1;
3183 switch (VT.SimpleTy) {
3184 default:
3185 return TargetLowering::findRepresentativeClass(TRI, VT);
3186 case MVT::v64i8:
3187 case MVT::v32i16:
3188 case MVT::v16i32:
3189 case MVT::v8i64:
3190 RRC = &Hexagon::VectorRegsRegClass;
3191 break;
3192 case MVT::v128i8:
3193 case MVT::v64i16:
3194 case MVT::v32i32:
3195 case MVT::v16i64:
3196 if (Subtarget.hasV60TOps() && Subtarget.useHVXOps() &&
3197 Subtarget.useHVXDblOps())
3198 RRC = &Hexagon::VectorRegs128BRegClass;
3199 else
3200 RRC = &Hexagon::VecDblRegsRegClass;
3201 break;
3202 case MVT::v256i8:
3203 case MVT::v128i16:
3204 case MVT::v64i32:
3205 case MVT::v32i64:
3206 RRC = &Hexagon::VecDblRegs128BRegClass;
3207 break;
3208 }
3209 return std::make_pair(RRC, Cost);
3210}
3211
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003212Value *HexagonTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
3213 AtomicOrdering Ord) const {
3214 BasicBlock *BB = Builder.GetInsertBlock();
3215 Module *M = BB->getParent()->getParent();
3216 Type *Ty = cast<PointerType>(Addr->getType())->getElementType();
3217 unsigned SZ = Ty->getPrimitiveSizeInBits();
3218 assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic loads supported");
3219 Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_L2_loadw_locked
3220 : Intrinsic::hexagon_L4_loadd_locked;
3221 Value *Fn = Intrinsic::getDeclaration(M, IntID);
3222 return Builder.CreateCall(Fn, Addr, "larx");
3223}
3224
3225/// Perform a store-conditional operation to Addr. Return the status of the
3226/// store. This should be 0 if the store succeeded, non-zero otherwise.
3227Value *HexagonTargetLowering::emitStoreConditional(IRBuilder<> &Builder,
3228 Value *Val, Value *Addr, AtomicOrdering Ord) const {
3229 BasicBlock *BB = Builder.GetInsertBlock();
3230 Module *M = BB->getParent()->getParent();
3231 Type *Ty = Val->getType();
3232 unsigned SZ = Ty->getPrimitiveSizeInBits();
3233 assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic stores supported");
3234 Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_S2_storew_locked
3235 : Intrinsic::hexagon_S4_stored_locked;
3236 Value *Fn = Intrinsic::getDeclaration(M, IntID);
3237 Value *Call = Builder.CreateCall(Fn, {Addr, Val}, "stcx");
3238 Value *Cmp = Builder.CreateICmpEQ(Call, Builder.getInt32(0), "");
3239 Value *Ext = Builder.CreateZExt(Cmp, Type::getInt32Ty(M->getContext()));
3240 return Ext;
3241}
3242
Ahmed Bougacha52468672015-09-11 17:08:28 +00003243TargetLowering::AtomicExpansionKind
3244HexagonTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003245 // Do not expand loads and stores that don't exceed 64 bits.
Ahmed Bougacha52468672015-09-11 17:08:28 +00003246 return LI->getType()->getPrimitiveSizeInBits() > 64
Tim Northoverf520eff2015-12-02 18:12:57 +00003247 ? AtomicExpansionKind::LLOnly
Ahmed Bougacha52468672015-09-11 17:08:28 +00003248 : AtomicExpansionKind::None;
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003249}
3250
3251bool HexagonTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
3252 // Do not expand loads and stores that don't exceed 64 bits.
3253 return SI->getValueOperand()->getType()->getPrimitiveSizeInBits() > 64;
3254}
Krzysztof Parzyszekf228c952016-06-22 16:07:10 +00003255
3256bool HexagonTargetLowering::shouldExpandAtomicCmpXchgInIR(
3257 AtomicCmpXchgInst *AI) const {
3258 const DataLayout &DL = AI->getModule()->getDataLayout();
3259 unsigned Size = DL.getTypeStoreSize(AI->getCompareOperand()->getType());
3260 return Size >= 4 && Size <= 8;
3261}