blob: 087a6d69439931cbaa3e24f643e1239460f4cd00 [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"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "Hexagon.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000017#include "HexagonMachineFunctionInfo.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000018#include "HexagonRegisterInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000019#include "HexagonSubtarget.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "HexagonTargetMachine.h"
21#include "HexagonTargetObjectFile.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000022#include "llvm/ADT/APInt.h"
23#include "llvm/ADT/ArrayRef.h"
24#include "llvm/ADT/SmallVector.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000025#include "llvm/CodeGen/CallingConvLower.h"
26#include "llvm/CodeGen/MachineFrameInfo.h"
27#include "llvm/CodeGen/MachineFunction.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000028#include "llvm/CodeGen/MachineMemOperand.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000030#include "llvm/CodeGen/RuntimeLibcalls.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000031#include "llvm/CodeGen/SelectionDAG.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000032#include "llvm/CodeGen/TargetCallingConv.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000033#include "llvm/CodeGen/ValueTypes.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000034#include "llvm/IR/BasicBlock.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000035#include "llvm/IR/CallingConv.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000036#include "llvm/IR/DataLayout.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000037#include "llvm/IR/DerivedTypes.h"
38#include "llvm/IR/Function.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000039#include "llvm/IR/GlobalValue.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000040#include "llvm/IR/InlineAsm.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000041#include "llvm/IR/Instructions.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000042#include "llvm/IR/Intrinsics.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000043#include "llvm/IR/Module.h"
44#include "llvm/IR/Type.h"
45#include "llvm/IR/Value.h"
46#include "llvm/MC/MCRegisterInfo.h"
47#include "llvm/Support/Casting.h"
48#include "llvm/Support/CodeGen.h"
NAKAMURA Takumi54eed762012-04-21 15:31:36 +000049#include "llvm/Support/CommandLine.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000050#include "llvm/Support/Debug.h"
51#include "llvm/Support/ErrorHandling.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000052#include "llvm/Support/MathExtras.h"
NAKAMURA Takumie30303f2012-04-21 15:31:45 +000053#include "llvm/Support/raw_ostream.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000054#include "llvm/Target/TargetMachine.h"
55#include <algorithm>
56#include <cassert>
57#include <cstddef>
58#include <cstdint>
59#include <limits>
60#include <utility>
NAKAMURA Takumi54eed762012-04-21 15:31:36 +000061
Craig Topperb25fda92012-03-17 18:46:09 +000062using namespace llvm;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000063
Chandler Carruthe96dd892014-04-21 22:55:11 +000064#define DEBUG_TYPE "hexagon-lowering"
65
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +000066static cl::opt<bool> EmitJumpTables("hexagon-emit-jump-tables",
67 cl::init(true), cl::Hidden,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +000068 cl::desc("Control jump table emission on Hexagon target"));
69
70static cl::opt<bool> EnableHexSDNodeSched("enable-hexagon-sdnode-sched",
71 cl::Hidden, cl::ZeroOrMore, cl::init(false),
72 cl::desc("Enable Hexagon SDNode scheduling"));
73
74static cl::opt<bool> EnableFastMath("ffast-math",
75 cl::Hidden, cl::ZeroOrMore, cl::init(false),
76 cl::desc("Enable Fast Math processing"));
77
78static cl::opt<int> MinimumJumpTables("minimum-jump-tables",
79 cl::Hidden, cl::ZeroOrMore, cl::init(5),
80 cl::desc("Set minimum jump tables"));
81
82static cl::opt<int> MaxStoresPerMemcpyCL("max-store-memcpy",
83 cl::Hidden, cl::ZeroOrMore, cl::init(6),
84 cl::desc("Max #stores to inline memcpy"));
85
86static cl::opt<int> MaxStoresPerMemcpyOptSizeCL("max-store-memcpy-Os",
87 cl::Hidden, cl::ZeroOrMore, cl::init(4),
88 cl::desc("Max #stores to inline memcpy"));
89
90static cl::opt<int> MaxStoresPerMemmoveCL("max-store-memmove",
91 cl::Hidden, cl::ZeroOrMore, cl::init(6),
92 cl::desc("Max #stores to inline memmove"));
93
94static cl::opt<int> MaxStoresPerMemmoveOptSizeCL("max-store-memmove-Os",
95 cl::Hidden, cl::ZeroOrMore, cl::init(4),
96 cl::desc("Max #stores to inline memmove"));
97
98static cl::opt<int> MaxStoresPerMemsetCL("max-store-memset",
99 cl::Hidden, cl::ZeroOrMore, cl::init(8),
100 cl::desc("Max #stores to inline memset"));
101
102static cl::opt<int> MaxStoresPerMemsetOptSizeCL("max-store-memset-Os",
103 cl::Hidden, cl::ZeroOrMore, cl::init(4),
104 cl::desc("Max #stores to inline memset"));
105
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000106
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000107namespace {
Eugene Zelenko58655bb2016-12-17 01:09:05 +0000108
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +0000109 class HexagonCCState : public CCState {
110 unsigned NumNamedVarArgParams;
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000111
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +0000112 public:
113 HexagonCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
114 SmallVectorImpl<CCValAssign> &locs, LLVMContext &C,
115 int NumNamedVarArgParams)
116 : CCState(CC, isVarArg, MF, locs, C),
117 NumNamedVarArgParams(NumNamedVarArgParams) {}
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000118
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +0000119 unsigned getNumNamedVarArgParams() const { return NumNamedVarArgParams; }
120 };
121
122 enum StridedLoadKind {
123 Even = 0,
124 Odd,
125 NoPattern
126 };
Eugene Zelenko58655bb2016-12-17 01:09:05 +0000127
128} // end anonymous namespace
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000129
130// Implement calling convention for Hexagon.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000131
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000132static bool
133CC_Hexagon(unsigned ValNo, MVT ValVT,
134 MVT LocVT, CCValAssign::LocInfo LocInfo,
135 ISD::ArgFlagsTy ArgFlags, CCState &State);
136
137static bool
138CC_Hexagon32(unsigned ValNo, MVT ValVT,
139 MVT LocVT, CCValAssign::LocInfo LocInfo,
140 ISD::ArgFlagsTy ArgFlags, CCState &State);
141
142static bool
143CC_Hexagon64(unsigned ValNo, MVT ValVT,
144 MVT LocVT, CCValAssign::LocInfo LocInfo,
145 ISD::ArgFlagsTy ArgFlags, CCState &State);
146
147static bool
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000148CC_HexagonVector(unsigned ValNo, MVT ValVT,
149 MVT LocVT, CCValAssign::LocInfo LocInfo,
150 ISD::ArgFlagsTy ArgFlags, CCState &State);
151
152static bool
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000153RetCC_Hexagon(unsigned ValNo, MVT ValVT,
154 MVT LocVT, CCValAssign::LocInfo LocInfo,
155 ISD::ArgFlagsTy ArgFlags, CCState &State);
156
157static bool
158RetCC_Hexagon32(unsigned ValNo, MVT ValVT,
159 MVT LocVT, CCValAssign::LocInfo LocInfo,
160 ISD::ArgFlagsTy ArgFlags, CCState &State);
161
162static bool
163RetCC_Hexagon64(unsigned ValNo, MVT ValVT,
164 MVT LocVT, CCValAssign::LocInfo LocInfo,
165 ISD::ArgFlagsTy ArgFlags, CCState &State);
166
167static bool
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000168RetCC_HexagonVector(unsigned ValNo, MVT ValVT,
169 MVT LocVT, CCValAssign::LocInfo LocInfo,
170 ISD::ArgFlagsTy ArgFlags, CCState &State);
171
172static bool
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000173CC_Hexagon_VarArg (unsigned ValNo, MVT ValVT,
174 MVT LocVT, CCValAssign::LocInfo LocInfo,
175 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000176 HexagonCCState &HState = static_cast<HexagonCCState &>(State);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000177
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000178 if (ValNo < HState.getNumNamedVarArgParams()) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000179 // Deal with named arguments.
180 return CC_Hexagon(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State);
181 }
182
183 // Deal with un-named arguments.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000184 unsigned Offset;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000185 if (ArgFlags.isByVal()) {
186 // If pass-by-value, the size allocated on stack is decided
187 // by ArgFlags.getByValSize(), not by the size of LocVT.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000188 Offset = State.AllocateStack(ArgFlags.getByValSize(),
189 ArgFlags.getByValAlign());
190 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000191 return false;
192 }
Jyotsna Vermac7dcc2f2013-03-07 20:28:34 +0000193 if (LocVT == MVT::i1 || LocVT == MVT::i8 || LocVT == MVT::i16) {
194 LocVT = MVT::i32;
195 ValVT = MVT::i32;
196 if (ArgFlags.isSExt())
197 LocInfo = CCValAssign::SExt;
198 else if (ArgFlags.isZExt())
199 LocInfo = CCValAssign::ZExt;
200 else
201 LocInfo = CCValAssign::AExt;
202 }
Sirish Pande69295b82012-05-10 20:20:25 +0000203 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000204 Offset = State.AllocateStack(4, 4);
205 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000206 return false;
207 }
Sirish Pande69295b82012-05-10 20:20:25 +0000208 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000209 Offset = State.AllocateStack(8, 8);
210 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000211 return false;
212 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000213 if (LocVT == MVT::v2i64 || LocVT == MVT::v4i32 || LocVT == MVT::v8i16 ||
214 LocVT == MVT::v16i8) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000215 Offset = State.AllocateStack(16, 16);
216 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000217 return false;
218 }
219 if (LocVT == MVT::v4i64 || LocVT == MVT::v8i32 || LocVT == MVT::v16i16 ||
220 LocVT == MVT::v32i8) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000221 Offset = State.AllocateStack(32, 32);
222 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000223 return false;
224 }
225 if (LocVT == MVT::v8i64 || LocVT == MVT::v16i32 || LocVT == MVT::v32i16 ||
226 LocVT == MVT::v64i8 || LocVT == MVT::v512i1) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000227 Offset = State.AllocateStack(64, 64);
228 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000229 return false;
230 }
231 if (LocVT == MVT::v16i64 || LocVT == MVT::v32i32 || LocVT == MVT::v64i16 ||
232 LocVT == MVT::v128i8 || LocVT == MVT::v1024i1) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000233 Offset = State.AllocateStack(128, 128);
234 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000235 return false;
236 }
237 if (LocVT == MVT::v32i64 || LocVT == MVT::v64i32 || LocVT == MVT::v128i16 ||
238 LocVT == MVT::v256i8) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000239 Offset = State.AllocateStack(256, 256);
240 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000241 return false;
242 }
243
Craig Toppere73658d2014-04-28 04:05:08 +0000244 llvm_unreachable(nullptr);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000245}
246
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000247static bool CC_Hexagon (unsigned ValNo, MVT ValVT, MVT LocVT,
248 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, CCState &State) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000249 if (ArgFlags.isByVal()) {
250 // Passed on stack.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000251 unsigned Offset = State.AllocateStack(ArgFlags.getByValSize(),
252 ArgFlags.getByValAlign());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000253 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
254 return false;
255 }
256
Krzysztof Parzyszek8f23dd62017-03-01 17:30:10 +0000257 if (LocVT == MVT::i1) {
258 LocVT = MVT::i32;
259 } else if (LocVT == MVT::i8 || LocVT == MVT::i16) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000260 LocVT = MVT::i32;
261 ValVT = MVT::i32;
262 if (ArgFlags.isSExt())
263 LocInfo = CCValAssign::SExt;
264 else if (ArgFlags.isZExt())
265 LocInfo = CCValAssign::ZExt;
266 else
267 LocInfo = CCValAssign::AExt;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000268 } else if (LocVT == MVT::v4i8 || LocVT == MVT::v2i16) {
269 LocVT = MVT::i32;
270 LocInfo = CCValAssign::BCvt;
271 } else if (LocVT == MVT::v8i8 || LocVT == MVT::v4i16 || LocVT == MVT::v2i32) {
272 LocVT = MVT::i64;
273 LocInfo = CCValAssign::BCvt;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000274 }
275
Sirish Pande69295b82012-05-10 20:20:25 +0000276 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000277 if (!CC_Hexagon32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
278 return false;
279 }
280
Sirish Pande69295b82012-05-10 20:20:25 +0000281 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000282 if (!CC_Hexagon64(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
283 return false;
284 }
285
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000286 if (LocVT == MVT::v8i32 || LocVT == MVT::v16i16 || LocVT == MVT::v32i8) {
287 unsigned Offset = State.AllocateStack(ArgFlags.getByValSize(), 32);
288 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
289 return false;
290 }
291
Krzysztof Parzyszekac1966e2017-11-27 18:12:16 +0000292 auto &HST = State.getMachineFunction().getSubtarget<HexagonSubtarget>();
293 if (HST.isHVXVectorType(LocVT)) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000294 if (!CC_HexagonVector(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
295 return false;
296 }
297
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000298 return true; // CC didn't match.
299}
300
301
302static bool CC_Hexagon32(unsigned ValNo, MVT ValVT,
303 MVT LocVT, CCValAssign::LocInfo LocInfo,
304 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +0000305 static const MCPhysReg RegList[] = {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000306 Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3, Hexagon::R4,
307 Hexagon::R5
308 };
Tim Northover3b6b7ca2015-02-21 02:11:17 +0000309 if (unsigned Reg = State.AllocateReg(RegList)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000310 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
311 return false;
312 }
313
314 unsigned Offset = State.AllocateStack(4, 4);
315 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
316 return false;
317}
318
319static bool CC_Hexagon64(unsigned ValNo, MVT ValVT,
320 MVT LocVT, CCValAssign::LocInfo LocInfo,
321 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000322 if (unsigned Reg = State.AllocateReg(Hexagon::D0)) {
323 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
324 return false;
325 }
326
Craig Topper840beec2014-04-04 05:16:06 +0000327 static const MCPhysReg RegList1[] = {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000328 Hexagon::D1, Hexagon::D2
329 };
Craig Topper840beec2014-04-04 05:16:06 +0000330 static const MCPhysReg RegList2[] = {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000331 Hexagon::R1, Hexagon::R3
332 };
Tim Northover3b6b7ca2015-02-21 02:11:17 +0000333 if (unsigned Reg = State.AllocateReg(RegList1, RegList2)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000334 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
335 return false;
336 }
337
338 unsigned Offset = State.AllocateStack(8, 8, Hexagon::D2);
339 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
340 return false;
341}
342
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000343static bool CC_HexagonVector(unsigned ValNo, MVT ValVT,
344 MVT LocVT, CCValAssign::LocInfo LocInfo,
345 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000346 static const MCPhysReg VecLstS[] = {
347 Hexagon::V0, Hexagon::V1, Hexagon::V2, Hexagon::V3, Hexagon::V4,
348 Hexagon::V5, Hexagon::V6, Hexagon::V7, Hexagon::V8, Hexagon::V9,
349 Hexagon::V10, Hexagon::V11, Hexagon::V12, Hexagon::V13, Hexagon::V14,
350 Hexagon::V15
351 };
352 static const MCPhysReg VecLstD[] = {
353 Hexagon::W0, Hexagon::W1, Hexagon::W2, Hexagon::W3, Hexagon::W4,
354 Hexagon::W5, Hexagon::W6, Hexagon::W7
355 };
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000356 auto &MF = State.getMachineFunction();
357 auto &HST = MF.getSubtarget<HexagonSubtarget>();
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000358
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +0000359 if (HST.useHVX64BOps() &&
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000360 (LocVT == MVT::v8i64 || LocVT == MVT::v16i32 || LocVT == MVT::v32i16 ||
361 LocVT == MVT::v64i8 || LocVT == MVT::v512i1)) {
362 if (unsigned Reg = State.AllocateReg(VecLstS)) {
363 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
364 return false;
365 }
366 unsigned Offset = State.AllocateStack(64, 64);
367 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
368 return false;
369 }
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +0000370 if (HST.useHVX64BOps() && (LocVT == MVT::v16i64 || LocVT == MVT::v32i32 ||
Sumanth Gundapaneni9d954c42017-10-18 17:45:22 +0000371 LocVT == MVT::v64i16 || LocVT == MVT::v128i8)) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000372 if (unsigned Reg = State.AllocateReg(VecLstD)) {
373 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
374 return false;
375 }
376 unsigned Offset = State.AllocateStack(128, 128);
377 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
378 return false;
379 }
Sumanth Gundapaneni9d954c42017-10-18 17:45:22 +0000380 // 128B Mode
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +0000381 if (HST.useHVX128BOps() && (LocVT == MVT::v32i64 || LocVT == MVT::v64i32 ||
382 LocVT == MVT::v128i16 || LocVT == MVT::v256i8)) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000383 if (unsigned Reg = State.AllocateReg(VecLstD)) {
384 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
385 return false;
386 }
387 unsigned Offset = State.AllocateStack(256, 256);
388 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
389 return false;
390 }
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +0000391 if (HST.useHVX128BOps() &&
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000392 (LocVT == MVT::v16i64 || LocVT == MVT::v32i32 || LocVT == MVT::v64i16 ||
393 LocVT == MVT::v128i8 || LocVT == MVT::v1024i1)) {
394 if (unsigned Reg = State.AllocateReg(VecLstS)) {
395 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
396 return false;
397 }
398 unsigned Offset = State.AllocateStack(128, 128);
399 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
400 return false;
401 }
402 return true;
403}
404
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000405static bool RetCC_Hexagon(unsigned ValNo, MVT ValVT,
406 MVT LocVT, CCValAssign::LocInfo LocInfo,
407 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000408 auto &MF = State.getMachineFunction();
409 auto &HST = MF.getSubtarget<HexagonSubtarget>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000410
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000411 if (LocVT == MVT::i1) {
412 // Return values of type MVT::i1 still need to be assigned to R0, but
413 // the value type needs to remain i1. LowerCallResult will deal with it,
414 // but it needs to recognize i1 as the value type.
415 LocVT = MVT::i32;
416 } else if (LocVT == MVT::i8 || LocVT == MVT::i16) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000417 LocVT = MVT::i32;
418 ValVT = MVT::i32;
419 if (ArgFlags.isSExt())
420 LocInfo = CCValAssign::SExt;
421 else if (ArgFlags.isZExt())
422 LocInfo = CCValAssign::ZExt;
423 else
424 LocInfo = CCValAssign::AExt;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000425 } else if (LocVT == MVT::v4i8 || LocVT == MVT::v2i16) {
426 LocVT = MVT::i32;
427 LocInfo = CCValAssign::BCvt;
428 } else if (LocVT == MVT::v8i8 || LocVT == MVT::v4i16 || LocVT == MVT::v2i32) {
429 LocVT = MVT::i64;
430 LocInfo = CCValAssign::BCvt;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000431 } else if (LocVT == MVT::v64i8 || LocVT == MVT::v32i16 ||
432 LocVT == MVT::v16i32 || LocVT == MVT::v8i64 ||
433 LocVT == MVT::v512i1) {
434 LocVT = MVT::v16i32;
435 ValVT = MVT::v16i32;
436 LocInfo = CCValAssign::Full;
437 } else if (LocVT == MVT::v128i8 || LocVT == MVT::v64i16 ||
438 LocVT == MVT::v32i32 || LocVT == MVT::v16i64 ||
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +0000439 (LocVT == MVT::v1024i1 && HST.useHVX128BOps())) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000440 LocVT = MVT::v32i32;
441 ValVT = MVT::v32i32;
442 LocInfo = CCValAssign::Full;
443 } else if (LocVT == MVT::v256i8 || LocVT == MVT::v128i16 ||
444 LocVT == MVT::v64i32 || LocVT == MVT::v32i64) {
445 LocVT = MVT::v64i32;
446 ValVT = MVT::v64i32;
447 LocInfo = CCValAssign::Full;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000448 }
Sirish Pande69295b82012-05-10 20:20:25 +0000449 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000450 if (!RetCC_Hexagon32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000451 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000452 }
453
Sirish Pande69295b82012-05-10 20:20:25 +0000454 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000455 if (!RetCC_Hexagon64(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000456 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000457 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000458 if (LocVT == MVT::v16i32 || LocVT == MVT::v32i32 || LocVT == MVT::v64i32) {
459 if (!RetCC_HexagonVector(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000460 return false;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000461 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000462 return true; // CC didn't match.
463}
464
465static bool RetCC_Hexagon32(unsigned ValNo, MVT ValVT,
466 MVT LocVT, CCValAssign::LocInfo LocInfo,
467 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Sirish Pande69295b82012-05-10 20:20:25 +0000468 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Krzysztof Parzyszek14412ef2016-07-18 17:36:46 +0000469 // Note that use of registers beyond R1 is not ABI compliant. However there
470 // are (experimental) IR passes which generate internal functions that
471 // return structs using these additional registers.
472 static const uint16_t RegList[] = { Hexagon::R0, Hexagon::R1,
473 Hexagon::R2, Hexagon::R3,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000474 Hexagon::R4, Hexagon::R5 };
Krzysztof Parzyszek14412ef2016-07-18 17:36:46 +0000475 if (unsigned Reg = State.AllocateReg(RegList)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000476 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
477 return false;
478 }
479 }
480
Krzysztof Parzyszek56199522017-04-13 15:05:51 +0000481 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000482}
483
484static bool RetCC_Hexagon64(unsigned ValNo, MVT ValVT,
485 MVT LocVT, CCValAssign::LocInfo LocInfo,
486 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Sirish Pande69295b82012-05-10 20:20:25 +0000487 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000488 if (unsigned Reg = State.AllocateReg(Hexagon::D0)) {
489 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
490 return false;
491 }
492 }
493
Krzysztof Parzyszek56199522017-04-13 15:05:51 +0000494 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000495}
496
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000497static bool RetCC_HexagonVector(unsigned ValNo, MVT ValVT,
498 MVT LocVT, CCValAssign::LocInfo LocInfo,
499 ISD::ArgFlagsTy ArgFlags, CCState &State) {
500 auto &MF = State.getMachineFunction();
501 auto &HST = MF.getSubtarget<HexagonSubtarget>();
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000502
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000503 if (LocVT == MVT::v16i32) {
504 if (unsigned Reg = State.AllocateReg(Hexagon::V0)) {
505 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
506 return false;
507 }
508 } else if (LocVT == MVT::v32i32) {
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +0000509 unsigned Req = HST.useHVX128BOps() ? Hexagon::V0 : Hexagon::W0;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000510 if (unsigned Reg = State.AllocateReg(Req)) {
511 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
512 return false;
513 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000514 } else if (LocVT == MVT::v64i32) {
515 if (unsigned Reg = State.AllocateReg(Hexagon::W0)) {
516 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
517 return false;
518 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000519 }
520
Krzysztof Parzyszek56199522017-04-13 15:05:51 +0000521 return true;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000522}
523
Craig Topper18e69f42016-04-15 06:20:21 +0000524void HexagonTargetLowering::promoteLdStType(MVT VT, MVT PromotedLdStVT) {
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000525 if (VT != PromotedLdStVT) {
Craig Topper18e69f42016-04-15 06:20:21 +0000526 setOperationAction(ISD::LOAD, VT, Promote);
527 AddPromotedToType(ISD::LOAD, VT, PromotedLdStVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000528
Craig Topper18e69f42016-04-15 06:20:21 +0000529 setOperationAction(ISD::STORE, VT, Promote);
530 AddPromotedToType(ISD::STORE, VT, PromotedLdStVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000531 }
532}
533
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000534SDValue
535HexagonTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG)
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000536 const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000537 return SDValue();
538}
539
540/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
541/// by "Src" to address "Dst" of size "Size". Alignment information is
542/// specified by the specific parameter attribute. The copy will be passed as
543/// a byval function parameter. Sometimes what we are copying is the end of a
544/// larger object, the part that does not fit in registers.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000545static SDValue CreateCopyOfByValArgument(SDValue Src, SDValue Dst,
546 SDValue Chain, ISD::ArgFlagsTy Flags,
547 SelectionDAG &DAG, const SDLoc &dl) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000548 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000549 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
550 /*isVolatile=*/false, /*AlwaysInline=*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +0000551 /*isTailCall=*/false,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000552 MachinePointerInfo(), MachinePointerInfo());
553}
554
Krzysztof Parzyszek56199522017-04-13 15:05:51 +0000555bool
556HexagonTargetLowering::CanLowerReturn(
557 CallingConv::ID CallConv, MachineFunction &MF, bool isVarArg,
558 const SmallVectorImpl<ISD::OutputArg> &Outs,
559 LLVMContext &Context) const {
560 SmallVector<CCValAssign, 16> RVLocs;
561 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
562 return CCInfo.CheckReturn(Outs, RetCC_Hexagon);
563}
564
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000565// LowerReturn - Lower ISD::RET. If a struct is larger than 8 bytes and is
566// passed by value, the function prototype is modified to return void and
567// the value is stored in memory pointed by a pointer passed by caller.
568SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000569HexagonTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
570 bool isVarArg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000571 const SmallVectorImpl<ISD::OutputArg> &Outs,
572 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000573 const SDLoc &dl, SelectionDAG &DAG) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000574 // 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
Matt Arsenault31380752017-04-18 21:16:46 +0000607bool HexagonTargetLowering::mayBeEmittedAsTailCall(const CallInst *CI) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000608 // 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
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000619/// Chain/Glue are the input chain/glue to use, and that TheCall is the call
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000620/// 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(
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000623 SDValue Chain, SDValue Glue, CallingConv::ID CallConv, bool isVarArg,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000624 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(),
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000646 MVT::i32, Glue);
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000647 // 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)
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000652 // Don't glue this CopyFromReg, because it copies from a virtual
653 // register. If it is glued to the call, InstrEmitter will add it
654 // as an implicit def to the call (EmitMachineNode).
655 RetVal = DAG.getCopyFromReg(TPR.getValue(0), dl, PredR, MVT::i1);
656 Glue = TPR.getValue(1);
Krzysztof Parzyszek6f06b6e2017-10-23 19:35:25 +0000657 Chain = TPR.getValue(0);
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000658 } else {
659 RetVal = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000660 RVLocs[i].getValVT(), Glue);
661 Glue = RetVal.getValue(2);
Krzysztof Parzyszek6f06b6e2017-10-23 19:35:25 +0000662 Chain = RetVal.getValue(1);
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000663 }
664 InVals.push_back(RetVal.getValue(0));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000665 }
666
667 return Chain;
668}
669
670/// LowerCall - Functions arguments are copied from virtual regs to
671/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
672SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +0000673HexagonTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000674 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +0000675 SelectionDAG &DAG = CLI.DAG;
Craig Topperb94011f2013-07-14 04:42:23 +0000676 SDLoc &dl = CLI.DL;
677 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
678 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
679 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000680 SDValue Chain = CLI.Chain;
681 SDValue Callee = CLI.Callee;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000682 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000683 CallingConv::ID CallConv = CLI.CallConv;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000684 bool IsVarArg = CLI.IsVarArg;
685 bool DoesNotReturn = CLI.DoesNotReturn;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000686
687 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000688 MachineFunction &MF = DAG.getMachineFunction();
Krzysztof Parzyszekf67cd822017-07-11 17:11:54 +0000689 MachineFrameInfo &MFI = MF.getFrameInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +0000690 auto PtrVT = getPointerTy(MF.getDataLayout());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000691
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000692 // Check for varargs.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000693 unsigned NumNamedVarArgParams = -1U;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000694 if (GlobalAddressSDNode *GAN = dyn_cast<GlobalAddressSDNode>(Callee)) {
695 const GlobalValue *GV = GAN->getGlobal();
696 Callee = DAG.getTargetGlobalAddress(GV, dl, MVT::i32);
697 if (const Function* F = dyn_cast<Function>(GV)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000698 // If a function has zero args and is a vararg function, that's
699 // disallowed so it must be an undeclared function. Do not assume
700 // varargs if the callee is undefined.
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000701 if (F->isVarArg() && F->getFunctionType()->getNumParams() != 0)
702 NumNamedVarArgParams = F->getFunctionType()->getNumParams();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000703 }
704 }
705
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000706 // Analyze operands of the call, assigning locations to each operand.
707 SmallVector<CCValAssign, 16> ArgLocs;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000708 HexagonCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
Eric Christopherb5217502014-08-06 18:45:26 +0000709 *DAG.getContext(), NumNamedVarArgParams);
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000710
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000711 if (IsVarArg)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000712 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon_VarArg);
713 else
714 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon);
715
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000716 auto Attr = MF.getFunction()->getFnAttribute("disable-tail-calls");
717 if (Attr.getValueAsString() == "true")
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000718 IsTailCall = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000719
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000720 if (IsTailCall) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000721 bool StructAttrFlag = MF.getFunction()->hasStructRetAttr();
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000722 IsTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
723 IsVarArg, IsStructRet,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000724 StructAttrFlag,
725 Outs, OutVals, Ins, DAG);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000726 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000727 CCValAssign &VA = ArgLocs[i];
728 if (VA.isMemLoc()) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000729 IsTailCall = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000730 break;
731 }
732 }
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000733 DEBUG(dbgs() << (IsTailCall ? "Eligible for Tail Call\n"
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000734 : "Argument must be passed on stack. "
735 "Not eligible for Tail Call\n"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000736 }
737 // Get a count of how many bytes are to be pushed on the stack.
738 unsigned NumBytes = CCInfo.getNextStackOffset();
739 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
740 SmallVector<SDValue, 8> MemOpChains;
741
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000742 auto &HRI = *Subtarget.getRegisterInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +0000743 SDValue StackPtr =
744 DAG.getCopyFromReg(Chain, dl, HRI.getStackRegister(), PtrVT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000745
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000746 bool NeedsArgAlign = false;
747 unsigned LargestAlignSeen = 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000748 // Walk the register/memloc assignments, inserting copies/loads.
749 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
750 CCValAssign &VA = ArgLocs[i];
751 SDValue Arg = OutVals[i];
752 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000753 // Record if we need > 8 byte alignment on an argument.
Krzysztof Parzyszekac1966e2017-11-27 18:12:16 +0000754 bool ArgAlign = Subtarget.isHVXVectorType(VA.getValVT());
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000755 NeedsArgAlign |= ArgAlign;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000756
757 // Promote the value if needed.
758 switch (VA.getLocInfo()) {
759 default:
760 // Loc info must be one of Full, SExt, ZExt, or AExt.
Craig Toppere55c5562012-02-07 02:50:20 +0000761 llvm_unreachable("Unknown loc info!");
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000762 case CCValAssign::BCvt:
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000763 case CCValAssign::Full:
764 break;
765 case CCValAssign::SExt:
766 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
767 break;
768 case CCValAssign::ZExt:
769 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
770 break;
771 case CCValAssign::AExt:
772 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
773 break;
774 }
775
776 if (VA.isMemLoc()) {
777 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000778 SDValue MemAddr = DAG.getConstant(LocMemOffset, dl,
779 StackPtr.getValueType());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000780 MemAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, MemAddr);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000781 if (ArgAlign)
782 LargestAlignSeen = std::max(LargestAlignSeen,
783 VA.getLocVT().getStoreSizeInBits() >> 3);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000784 if (Flags.isByVal()) {
785 // The argument is a struct passed by value. According to LLVM, "Arg"
786 // is is pointer.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000787 MemOpChains.push_back(CreateCopyOfByValArgument(Arg, MemAddr, Chain,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000788 Flags, DAG, dl));
789 } else {
Alex Lorenze40c8a22015-08-11 23:09:45 +0000790 MachinePointerInfo LocPI = MachinePointerInfo::getStack(
791 DAG.getMachineFunction(), LocMemOffset);
Justin Lebar9c375812016-07-15 18:27:10 +0000792 SDValue S = DAG.getStore(Chain, dl, Arg, MemAddr, LocPI);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000793 MemOpChains.push_back(S);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000794 }
795 continue;
796 }
797
798 // Arguments that can be passed on register must be kept at RegsToPass
799 // vector.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000800 if (VA.isRegLoc())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000801 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000802 }
803
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000804 if (NeedsArgAlign && Subtarget.hasV60TOps()) {
805 DEBUG(dbgs() << "Function needs byte stack align due to call args\n");
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000806 // V6 vectors passed by value have 64 or 128 byte alignment depending
807 // on whether we are 64 byte vector mode or 128 byte.
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +0000808 bool UseHVX128B = Subtarget.useHVX128BOps();
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000809 assert(Subtarget.useHVXOps());
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +0000810 const unsigned ObjAlign = UseHVX128B ? 128 : 64;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000811 LargestAlignSeen = std::max(LargestAlignSeen, ObjAlign);
Matthias Braun941a7052016-07-28 18:40:00 +0000812 MFI.ensureMaxAlignment(LargestAlignSeen);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000813 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000814 // Transform all store nodes into one single node because all store
815 // nodes are independent of each other.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000816 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000817 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000818
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000819 SDValue Glue;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000820 if (!IsTailCall) {
Serge Pavlovd526b132017-05-09 13:35:13 +0000821 Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, dl);
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000822 Glue = Chain.getValue(1);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000823 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000824
825 // Build a sequence of copy-to-reg nodes chained together with token
826 // chain and flag operands which copy the outgoing args into registers.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000827 // The Glue is necessary since all emitted instructions must be
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000828 // stuck together.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000829 if (!IsTailCall) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000830 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
831 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000832 RegsToPass[i].second, Glue);
833 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000834 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000835 } else {
836 // For tail calls lower the arguments to the 'real' stack slot.
837 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000838 // Force all the incoming stack arguments to be loaded from the stack
839 // before any new outgoing arguments are stored to the stack, because the
840 // outgoing stack slots may alias the incoming argument stack slots, and
841 // the alias isn't otherwise explicit. This is slightly more conservative
842 // than necessary, because it means that each store effectively depends
843 // on every argument instead of just those arguments it would clobber.
844 //
Benjamin Kramerbde91762012-06-02 10:20:22 +0000845 // Do not flag preceding copytoreg stuff together with the following stuff.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000846 Glue = SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000847 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
848 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000849 RegsToPass[i].second, Glue);
850 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000851 }
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000852 Glue = SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000853 }
854
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000855 bool LongCalls = MF.getSubtarget<HexagonSubtarget>().useLongCalls();
856 unsigned Flags = LongCalls ? HexagonII::HMOTF_ConstExtended : 0;
857
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000858 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
859 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
860 // node so that legalize doesn't hack it.
Tobias Edler von Kochb51460c2015-12-16 17:29:37 +0000861 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000862 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, PtrVT, 0, Flags);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000863 } else if (ExternalSymbolSDNode *S =
864 dyn_cast<ExternalSymbolSDNode>(Callee)) {
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000865 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), PtrVT, Flags);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000866 }
867
868 // Returns a chain & a flag for retval copy to use.
869 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
870 SmallVector<SDValue, 8> Ops;
871 Ops.push_back(Chain);
872 Ops.push_back(Callee);
873
874 // Add argument registers to the end of the list so that they are
875 // known live into the call.
876 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
877 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
878 RegsToPass[i].second.getValueType()));
879 }
880
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000881 const uint32_t *Mask = HRI.getCallPreservedMask(MF, CallConv);
882 assert(Mask && "Missing call preserved mask for calling convention");
883 Ops.push_back(DAG.getRegisterMask(Mask));
884
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000885 if (Glue.getNode())
886 Ops.push_back(Glue);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000887
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000888 if (IsTailCall) {
Krzysztof Parzyszekf67cd822017-07-11 17:11:54 +0000889 MFI.setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +0000890 return DAG.getNode(HexagonISD::TC_RETURN, dl, NodeTys, Ops);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +0000891 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000892
Krzysztof Parzyszekf67cd822017-07-11 17:11:54 +0000893 // Set this here because we need to know this for "hasFP" in frame lowering.
894 // The target-independent code calls getFrameRegister before setting it, and
895 // getFrameRegister uses hasFP to determine whether the function has FP.
896 MFI.setHasCalls(true);
897
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +0000898 unsigned OpCode = DoesNotReturn ? HexagonISD::CALLnr : HexagonISD::CALL;
Colin LeMahieu2e3a26d2015-01-16 17:05:27 +0000899 Chain = DAG.getNode(OpCode, dl, NodeTys, Ops);
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000900 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000901
902 // Create the CALLSEQ_END node.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000903 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000904 DAG.getIntPtrConstant(0, dl, true), Glue, dl);
905 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000906
907 // Handle result values, copying them out of physregs into vregs that we
908 // return.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000909 return LowerCallResult(Chain, Glue, CallConv, IsVarArg, Ins, dl, DAG,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000910 InVals, OutVals, Callee);
911}
912
913static bool getIndexedAddressParts(SDNode *Ptr, EVT VT,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000914 SDValue &Base, SDValue &Offset,
915 bool &IsInc, SelectionDAG &DAG) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000916 if (Ptr->getOpcode() != ISD::ADD)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000917 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000918
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000919 auto &HST = static_cast<const HexagonSubtarget&>(DAG.getSubtarget());
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000920
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +0000921 bool ValidHVX128BType =
922 HST.useHVX128BOps() && (VT == MVT::v32i32 || VT == MVT::v16i64 ||
923 VT == MVT::v64i16 || VT == MVT::v128i8);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000924 bool ValidHVXType =
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +0000925 HST.useHVX64BOps() && (VT == MVT::v16i32 || VT == MVT::v8i64 ||
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000926 VT == MVT::v32i16 || VT == MVT::v64i8);
927
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +0000928 if (ValidHVX128BType || ValidHVXType || VT == MVT::i64 || VT == MVT::i32 ||
929 VT == MVT::i16 || VT == MVT::i8) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000930 IsInc = (Ptr->getOpcode() == ISD::ADD);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000931 Base = Ptr->getOperand(0);
932 Offset = Ptr->getOperand(1);
933 // Ensure that Offset is a constant.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000934 return isa<ConstantSDNode>(Offset);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000935 }
936
937 return false;
938}
939
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000940/// getPostIndexedAddressParts - returns true by value, base pointer and
941/// offset pointer and addressing mode by reference if this node can be
942/// combined with a load / store to form a post-indexed load / store.
943bool HexagonTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
944 SDValue &Base,
945 SDValue &Offset,
946 ISD::MemIndexedMode &AM,
947 SelectionDAG &DAG) const
948{
949 EVT VT;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000950
951 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
952 VT = LD->getMemoryVT();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000953 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
954 VT = ST->getMemoryVT();
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000955 if (ST->getValue().getValueType() == MVT::i64 && ST->isTruncatingStore())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000956 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000957 } else {
958 return false;
959 }
960
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000961 bool IsInc = false;
962 bool isLegal = getIndexedAddressParts(Op, VT, Base, Offset, IsInc, DAG);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000963 if (isLegal) {
964 auto &HII = *Subtarget.getInstrInfo();
965 int32_t OffsetVal = cast<ConstantSDNode>(Offset.getNode())->getSExtValue();
966 if (HII.isValidAutoIncImm(VT, OffsetVal)) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000967 AM = IsInc ? ISD::POST_INC : ISD::POST_DEC;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000968 return true;
969 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000970 }
971
972 return false;
973}
974
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000975SDValue
976HexagonTargetLowering::LowerINLINEASM(SDValue Op, SelectionDAG &DAG) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000977 MachineFunction &MF = DAG.getMachineFunction();
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000978 auto &HMFI = *MF.getInfo<HexagonMachineFunctionInfo>();
979 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
980 unsigned LR = HRI.getRARegister();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000981
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000982 if (Op.getOpcode() != ISD::INLINEASM || HMFI.hasClobberLR())
983 return Op;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000984
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000985 unsigned NumOps = Op.getNumOperands();
986 if (Op.getOperand(NumOps-1).getValueType() == MVT::Glue)
987 --NumOps; // Ignore the flag operand.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000988
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000989 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
990 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(i))->getZExtValue();
991 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
992 ++i; // Skip the ID value.
993
994 switch (InlineAsm::getKind(Flags)) {
995 default:
996 llvm_unreachable("Bad flags!");
997 case InlineAsm::Kind_RegUse:
998 case InlineAsm::Kind_Imm:
999 case InlineAsm::Kind_Mem:
1000 i += NumVals;
1001 break;
1002 case InlineAsm::Kind_Clobber:
1003 case InlineAsm::Kind_RegDef:
1004 case InlineAsm::Kind_RegDefEarlyClobber: {
1005 for (; NumVals; --NumVals, ++i) {
1006 unsigned Reg = cast<RegisterSDNode>(Op.getOperand(i))->getReg();
1007 if (Reg != LR)
1008 continue;
1009 HMFI.setHasClobberLR(true);
1010 return Op;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001011 }
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +00001012 break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001013 }
1014 }
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +00001015 }
1016
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001017 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
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +00001034// Custom-handle ISD::READCYCLECOUNTER because the target-independent SDNode
1035// is marked as having side-effects, while the register read on Hexagon does
1036// not have any. TableGen refuses to accept the direct pattern from that node
1037// to the A4_tfrcpp.
1038SDValue HexagonTargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
1039 SelectionDAG &DAG) const {
1040 SDValue Chain = Op.getOperand(0);
1041 SDLoc dl(Op);
1042 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
1043 return DAG.getNode(HexagonISD::READCYCLE, dl, VTs, Chain);
1044}
1045
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00001046SDValue HexagonTargetLowering::LowerINTRINSIC_VOID(SDValue Op,
1047 SelectionDAG &DAG) const {
1048 SDValue Chain = Op.getOperand(0);
1049 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1050 // Lower the hexagon_prefetch builtin to DCFETCH, as above.
1051 if (IntNo == Intrinsic::hexagon_prefetch) {
1052 SDValue Addr = Op.getOperand(2);
1053 SDLoc DL(Op);
1054 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
1055 return DAG.getNode(HexagonISD::DCFETCH, DL, MVT::Other, Chain, Addr, Zero);
1056 }
1057 return SDValue();
1058}
1059
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001060SDValue
1061HexagonTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
1062 SelectionDAG &DAG) const {
1063 SDValue Chain = Op.getOperand(0);
1064 SDValue Size = Op.getOperand(1);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001065 SDValue Align = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001066 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001067
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001068 ConstantSDNode *AlignConst = dyn_cast<ConstantSDNode>(Align);
1069 assert(AlignConst && "Non-constant Align in LowerDYNAMIC_STACKALLOC");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001070
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001071 unsigned A = AlignConst->getSExtValue();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001072 auto &HFI = *Subtarget.getFrameLowering();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001073 // "Zero" means natural stack alignment.
1074 if (A == 0)
1075 A = HFI.getStackAlignment();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001076
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001077 DEBUG({
Reid Kleckner40d72302016-10-20 00:22:23 +00001078 dbgs () << __func__ << " Align: " << A << " Size: ";
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001079 Size.getNode()->dump(&DAG);
1080 dbgs() << "\n";
1081 });
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001082
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001083 SDValue AC = DAG.getConstant(A, dl, MVT::i32);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001084 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00001085 SDValue AA = DAG.getNode(HexagonISD::ALLOCA, dl, VTs, Chain, Size, AC);
Nirav Davebfdb4832016-06-23 17:52:57 +00001086
1087 DAG.ReplaceAllUsesOfValueWith(Op, AA);
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00001088 return AA;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001089}
1090
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001091SDValue HexagonTargetLowering::LowerFormalArguments(
1092 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
1093 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
1094 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001095 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00001096 MachineFrameInfo &MFI = MF.getFrameInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001097 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001098 auto &FuncInfo = *MF.getInfo<HexagonMachineFunctionInfo>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001099
1100 // Assign locations to all of the incoming arguments.
1101 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001102 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1103 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001104
1105 CCInfo.AnalyzeFormalArguments(Ins, CC_Hexagon);
1106
1107 // For LLVM, in the case when returning a struct by value (>8byte),
1108 // the first argument is a pointer that points to the location on caller's
1109 // stack where the return value will be stored. For Hexagon, the location on
1110 // caller's stack is passed only when the struct size is smaller than (and
1111 // equal to) 8 bytes. If not, no address will be passed into callee and
1112 // callee return the result direclty through R0/R1.
1113
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001114 SmallVector<SDValue, 8> MemOps;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001115
1116 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1117 CCValAssign &VA = ArgLocs[i];
1118 ISD::ArgFlagsTy Flags = Ins[i].Flags;
1119 unsigned ObjSize;
1120 unsigned StackLocation;
1121 int FI;
1122
1123 if ( (VA.isRegLoc() && !Flags.isByVal())
1124 || (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() > 8)) {
1125 // Arguments passed in registers
1126 // 1. int, long long, ptr args that get allocated in register.
1127 // 2. Large struct that gets an register to put its address in.
1128 EVT RegVT = VA.getLocVT();
Sirish Pande69295b82012-05-10 20:20:25 +00001129 if (RegVT == MVT::i8 || RegVT == MVT::i16 ||
1130 RegVT == MVT::i32 || RegVT == MVT::f32) {
Krzysztof Parzyszek6acecc92017-11-22 20:43:00 +00001131 unsigned VReg =
Craig Topperc7242e02012-04-20 07:30:17 +00001132 RegInfo.createVirtualRegister(&Hexagon::IntRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001133 RegInfo.addLiveIn(VA.getLocReg(), VReg);
Krzysztof Parzyszek8f23dd62017-03-01 17:30:10 +00001134 SDValue Copy = DAG.getCopyFromReg(Chain, dl, VReg, RegVT);
1135 // Treat values of type MVT::i1 specially: they are passed in
1136 // registers of type i32, but they need to remain as values of
1137 // type i1 for consistency of the argument lowering.
1138 if (VA.getValVT() == MVT::i1) {
1139 // Generate a copy into a predicate register and use the value
1140 // of the register as the "InVal".
1141 unsigned PReg =
1142 RegInfo.createVirtualRegister(&Hexagon::PredRegsRegClass);
1143 SDNode *T = DAG.getMachineNode(Hexagon::C2_tfrrp, dl, MVT::i1,
1144 Copy.getValue(0));
1145 Copy = DAG.getCopyToReg(Copy.getValue(1), dl, PReg, SDValue(T, 0));
1146 Copy = DAG.getCopyFromReg(Copy, dl, PReg, MVT::i1);
1147 }
1148 InVals.push_back(Copy);
1149 Chain = Copy.getValue(1);
Colin LeMahieu4379d102015-01-28 22:08:16 +00001150 } else if (RegVT == MVT::i64 || RegVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001151 unsigned VReg =
Craig Topperc7242e02012-04-20 07:30:17 +00001152 RegInfo.createVirtualRegister(&Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001153 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1154 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001155
1156 // Single Vector
1157 } else if ((RegVT == MVT::v8i64 || RegVT == MVT::v16i32 ||
1158 RegVT == MVT::v32i16 || RegVT == MVT::v64i8)) {
1159 unsigned VReg =
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001160 RegInfo.createVirtualRegister(&Hexagon::HvxVRRegClass);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001161 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1162 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +00001163 } else if (Subtarget.useHVX128BOps() &&
Sumanth Gundapaneni9d954c42017-10-18 17:45:22 +00001164 ((RegVT == MVT::v16i64 || RegVT == MVT::v32i32 ||
1165 RegVT == MVT::v64i16 || RegVT == MVT::v128i8))) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001166 unsigned VReg =
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001167 RegInfo.createVirtualRegister(&Hexagon::HvxVRRegClass);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001168 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1169 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1170
1171 // Double Vector
1172 } else if ((RegVT == MVT::v16i64 || RegVT == MVT::v32i32 ||
1173 RegVT == MVT::v64i16 || RegVT == MVT::v128i8)) {
1174 unsigned VReg =
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001175 RegInfo.createVirtualRegister(&Hexagon::HvxWRRegClass);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001176 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1177 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +00001178 } else if (Subtarget.useHVX128BOps() &&
Sumanth Gundapaneni9d954c42017-10-18 17:45:22 +00001179 ((RegVT == MVT::v32i64 || RegVT == MVT::v64i32 ||
1180 RegVT == MVT::v128i16 || RegVT == MVT::v256i8))) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001181 unsigned VReg =
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001182 RegInfo.createVirtualRegister(&Hexagon::HvxWRRegClass);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001183 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1184 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1185 } else if (RegVT == MVT::v512i1 || RegVT == MVT::v1024i1) {
1186 assert(0 && "need to support VecPred regs");
1187 unsigned VReg =
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001188 RegInfo.createVirtualRegister(&Hexagon::HvxQRRegClass);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001189 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1190 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001191 } else {
1192 assert (0);
1193 }
1194 } else if (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() <= 8) {
1195 assert (0 && "ByValSize must be bigger than 8 bytes");
1196 } else {
1197 // Sanity check.
1198 assert(VA.isMemLoc());
1199
1200 if (Flags.isByVal()) {
1201 // If it's a byval parameter, then we need to compute the
1202 // "real" size, not the size of the pointer.
1203 ObjSize = Flags.getByValSize();
1204 } else {
1205 ObjSize = VA.getLocVT().getStoreSizeInBits() >> 3;
1206 }
1207
1208 StackLocation = HEXAGON_LRFP_SIZE + VA.getLocMemOffset();
1209 // Create the frame index object for this incoming parameter...
Matthias Braun941a7052016-07-28 18:40:00 +00001210 FI = MFI.CreateFixedObject(ObjSize, StackLocation, true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001211
1212 // Create the SelectionDAG nodes cordl, responding to a load
1213 // from this parameter.
1214 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
1215
1216 if (Flags.isByVal()) {
1217 // If it's a pass-by-value aggregate, then do not dereference the stack
1218 // location. Instead, we should generate a reference to the stack
1219 // location.
1220 InVals.push_back(FIN);
1221 } else {
Justin Lebar9c375812016-07-15 18:27:10 +00001222 InVals.push_back(
Krzysztof Parzyszek3e2046c2017-04-13 15:00:18 +00001223 DAG.getLoad(VA.getValVT(), dl, Chain, FIN, MachinePointerInfo()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001224 }
1225 }
1226 }
1227
1228 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001229 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001230
1231 if (isVarArg) {
1232 // This will point to the next argument passed via stack.
Matthias Braun941a7052016-07-28 18:40:00 +00001233 int FrameIndex = MFI.CreateFixedObject(Hexagon_PointerSize,
1234 HEXAGON_LRFP_SIZE +
1235 CCInfo.getNextStackOffset(),
1236 true);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001237 FuncInfo.setVarArgsFrameIndex(FrameIndex);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001238 }
1239
1240 return Chain;
1241}
1242
1243SDValue
1244HexagonTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
1245 // VASTART stores the address of the VarArgsFrameIndex slot into the
1246 // memory location argument.
1247 MachineFunction &MF = DAG.getMachineFunction();
1248 HexagonMachineFunctionInfo *QFI = MF.getInfo<HexagonMachineFunctionInfo>();
1249 SDValue Addr = DAG.getFrameIndex(QFI->getVarArgsFrameIndex(), MVT::i32);
1250 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Justin Lebar9c375812016-07-15 18:27:10 +00001251 return DAG.getStore(Op.getOperand(0), SDLoc(Op), Addr, Op.getOperand(1),
1252 MachinePointerInfo(SV));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001253}
1254
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001255static bool isSExtFree(SDValue N) {
1256 // A sign-extend of a truncate of a sign-extend is free.
1257 if (N.getOpcode() == ISD::TRUNCATE &&
1258 N.getOperand(0).getOpcode() == ISD::AssertSext)
1259 return true;
1260 // We have sign-extended loads.
1261 if (N.getOpcode() == ISD::LOAD)
1262 return true;
1263 return false;
1264}
1265
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001266SDValue HexagonTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1267 SDLoc dl(Op);
1268
1269 SDValue LHS = Op.getOperand(0);
1270 SDValue RHS = Op.getOperand(1);
1271 SDValue Cmp = Op.getOperand(2);
1272 ISD::CondCode CC = cast<CondCodeSDNode>(Cmp)->get();
1273
1274 EVT VT = Op.getValueType();
1275 EVT LHSVT = LHS.getValueType();
1276 EVT RHSVT = RHS.getValueType();
1277
1278 if (LHSVT == MVT::v2i16) {
1279 assert(ISD::isSignedIntSetCC(CC) || ISD::isUnsignedIntSetCC(CC));
1280 unsigned ExtOpc = ISD::isSignedIntSetCC(CC) ? ISD::SIGN_EXTEND
1281 : ISD::ZERO_EXTEND;
1282 SDValue LX = DAG.getNode(ExtOpc, dl, MVT::v2i32, LHS);
1283 SDValue RX = DAG.getNode(ExtOpc, dl, MVT::v2i32, RHS);
1284 SDValue SC = DAG.getNode(ISD::SETCC, dl, MVT::v2i1, LX, RX, Cmp);
1285 return SC;
1286 }
1287
1288 // Treat all other vector types as legal.
1289 if (VT.isVector())
1290 return Op;
1291
1292 // Equals and not equals should use sign-extend, not zero-extend, since
1293 // we can represent small negative values in the compare instructions.
1294 // The LLVM default is to use zero-extend arbitrarily in these cases.
1295 if ((CC == ISD::SETEQ || CC == ISD::SETNE) &&
1296 (RHSVT == MVT::i8 || RHSVT == MVT::i16) &&
1297 (LHSVT == MVT::i8 || LHSVT == MVT::i16)) {
1298 ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS);
1299 if (C && C->getAPIntValue().isNegative()) {
1300 LHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, LHS);
1301 RHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, RHS);
1302 return DAG.getNode(ISD::SETCC, dl, Op.getValueType(),
1303 LHS, RHS, Op.getOperand(2));
1304 }
1305 if (isSExtFree(LHS) || isSExtFree(RHS)) {
1306 LHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, LHS);
1307 RHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, RHS);
1308 return DAG.getNode(ISD::SETCC, dl, Op.getValueType(),
1309 LHS, RHS, Op.getOperand(2));
1310 }
1311 }
1312 return SDValue();
1313}
1314
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001315SDValue
1316HexagonTargetLowering::LowerVSELECT(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001317 SDValue PredOp = Op.getOperand(0);
1318 SDValue Op1 = Op.getOperand(1), Op2 = Op.getOperand(2);
1319 EVT OpVT = Op1.getValueType();
1320 SDLoc DL(Op);
1321
1322 if (OpVT == MVT::v2i16) {
1323 SDValue X1 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op1);
1324 SDValue X2 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op2);
1325 SDValue SL = DAG.getNode(ISD::VSELECT, DL, MVT::v2i32, PredOp, X1, X2);
1326 SDValue TR = DAG.getNode(ISD::TRUNCATE, DL, MVT::v2i16, SL);
1327 return TR;
1328 }
1329
1330 return SDValue();
1331}
1332
Krzysztof Parzyszek91ff5c62017-08-01 13:12:53 +00001333static Constant *convert_i1_to_i8(const Constant *ConstVal) {
1334 SmallVector<Constant *, 128> NewConst;
1335 const ConstantVector *CV = dyn_cast<ConstantVector>(ConstVal);
1336 if (!CV)
1337 return nullptr;
1338
1339 LLVMContext &Ctx = ConstVal->getContext();
1340 IRBuilder<> IRB(Ctx);
1341 unsigned NumVectorElements = CV->getNumOperands();
1342 assert(isPowerOf2_32(NumVectorElements) &&
1343 "conversion only supported for pow2 VectorSize!");
1344
1345 for (unsigned i = 0; i < NumVectorElements / 8; ++i) {
1346 uint8_t x = 0;
1347 for (unsigned j = 0; j < 8; ++j) {
1348 uint8_t y = CV->getOperand(i * 8 + j)->getUniqueInteger().getZExtValue();
1349 x |= y << (7 - j);
1350 }
1351 assert((x == 0 || x == 255) && "Either all 0's or all 1's expected!");
1352 NewConst.push_back(IRB.getInt8(x));
1353 }
1354 return ConstantVector::get(NewConst);
1355}
1356
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001357SDValue
Sirish Pande69295b82012-05-10 20:20:25 +00001358HexagonTargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
1359 EVT ValTy = Op.getValueType();
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001360 ConstantPoolSDNode *CPN = cast<ConstantPoolSDNode>(Op);
Krzysztof Parzyszek91ff5c62017-08-01 13:12:53 +00001361 Constant *CVal = nullptr;
1362 bool isVTi1Type = false;
1363 if (const Constant *ConstVal = dyn_cast<Constant>(CPN->getConstVal())) {
1364 Type *CValTy = ConstVal->getType();
1365 if (CValTy->isVectorTy() &&
1366 CValTy->getVectorElementType()->isIntegerTy(1)) {
1367 CVal = convert_i1_to_i8(ConstVal);
1368 isVTi1Type = (CVal != nullptr);
1369 }
1370 }
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001371 unsigned Align = CPN->getAlignment();
Rafael Espindola405e25a2016-06-26 22:24:01 +00001372 bool IsPositionIndependent = isPositionIndependent();
1373 unsigned char TF = IsPositionIndependent ? HexagonII::MO_PCREL : 0;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001374
Ron Lieberman822ee882016-08-13 23:41:11 +00001375 unsigned Offset = 0;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001376 SDValue T;
1377 if (CPN->isMachineConstantPoolEntry())
Ron Lieberman822ee882016-08-13 23:41:11 +00001378 T = DAG.getTargetConstantPool(CPN->getMachineCPVal(), ValTy, Align, Offset,
1379 TF);
Krzysztof Parzyszek91ff5c62017-08-01 13:12:53 +00001380 else if (isVTi1Type)
1381 T = DAG.getTargetConstantPool(CVal, ValTy, Align, Offset, TF);
Sirish Pande69295b82012-05-10 20:20:25 +00001382 else
Ron Lieberman822ee882016-08-13 23:41:11 +00001383 T = DAG.getTargetConstantPool(CPN->getConstVal(), ValTy, Align, Offset,
1384 TF);
1385
1386 assert(cast<ConstantPoolSDNode>(T)->getTargetFlags() == TF &&
1387 "Inconsistent target flag encountered");
1388
Rafael Espindola405e25a2016-06-26 22:24:01 +00001389 if (IsPositionIndependent)
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001390 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), ValTy, T);
1391 return DAG.getNode(HexagonISD::CP, SDLoc(Op), ValTy, T);
1392}
1393
1394SDValue
1395HexagonTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
1396 EVT VT = Op.getValueType();
1397 int Idx = cast<JumpTableSDNode>(Op)->getIndex();
Rafael Espindola405e25a2016-06-26 22:24:01 +00001398 if (isPositionIndependent()) {
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001399 SDValue T = DAG.getTargetJumpTable(Idx, VT, HexagonII::MO_PCREL);
1400 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), VT, T);
1401 }
1402
1403 SDValue T = DAG.getTargetJumpTable(Idx, VT);
1404 return DAG.getNode(HexagonISD::JT, SDLoc(Op), VT, T);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001405}
1406
1407SDValue
1408HexagonTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001409 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001410 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00001411 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001412 MFI.setReturnAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001413
Bill Wendling908bf812014-01-06 00:43:20 +00001414 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001415 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001416
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001417 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001418 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001419 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1420 if (Depth) {
1421 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001422 SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001423 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
1424 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Justin Lebar9c375812016-07-15 18:27:10 +00001425 MachinePointerInfo());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001426 }
1427
1428 // Return LR, which contains the return address. Mark it an implicit live-in.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001429 unsigned Reg = MF.addLiveIn(HRI.getRARegister(), getRegClassFor(MVT::i32));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001430 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
1431}
1432
1433SDValue
1434HexagonTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001435 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Matthias Braun941a7052016-07-28 18:40:00 +00001436 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001437 MFI.setFrameAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001438
1439 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001440 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001441 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1442 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001443 HRI.getFrameRegister(), VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001444 while (Depth--)
1445 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00001446 MachinePointerInfo());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001447 return FrameAddr;
1448}
1449
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001450SDValue
1451HexagonTargetLowering::LowerATOMIC_FENCE(SDValue Op, SelectionDAG& DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001452 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001453 return DAG.getNode(HexagonISD::BARRIER, dl, MVT::Other, Op.getOperand(0));
1454}
1455
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001456SDValue
1457HexagonTargetLowering::LowerGLOBALADDRESS(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001458 SDLoc dl(Op);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001459 auto *GAN = cast<GlobalAddressSDNode>(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00001460 auto PtrVT = getPointerTy(DAG.getDataLayout());
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001461 auto *GV = GAN->getGlobal();
1462 int64_t Offset = GAN->getOffset();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001463
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001464 auto &HLOF = *HTM.getObjFileLowering();
1465 Reloc::Model RM = HTM.getRelocationModel();
1466
1467 if (RM == Reloc::Static) {
1468 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset);
Peter Collingbourne67335642016-10-24 19:23:39 +00001469 const GlobalObject *GO = GV->getBaseObject();
1470 if (GO && HLOF.isGlobalInSmallSection(GO, HTM))
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001471 return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, GA);
1472 return DAG.getNode(HexagonISD::CONST32, dl, PtrVT, GA);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001473 }
1474
Rafael Espindola3beef8d2016-06-27 23:15:57 +00001475 bool UsePCRel = getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001476 if (UsePCRel) {
1477 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset,
1478 HexagonII::MO_PCREL);
1479 return DAG.getNode(HexagonISD::AT_PCREL, dl, PtrVT, GA);
1480 }
1481
1482 // Use GOT index.
1483 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
1484 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, HexagonII::MO_GOT);
1485 SDValue Off = DAG.getConstant(Offset, dl, MVT::i32);
1486 return DAG.getNode(HexagonISD::AT_GOT, dl, PtrVT, GOT, GA, Off);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001487}
1488
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001489// Specifies that for loads and stores VT can be promoted to PromotedLdStVT.
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001490SDValue
1491HexagonTargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
1492 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001493 SDLoc dl(Op);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001494 EVT PtrVT = getPointerTy(DAG.getDataLayout());
1495
1496 Reloc::Model RM = HTM.getRelocationModel();
1497 if (RM == Reloc::Static) {
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001498 SDValue A = DAG.getTargetBlockAddress(BA, PtrVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001499 return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, A);
1500 }
1501
1502 SDValue A = DAG.getTargetBlockAddress(BA, PtrVT, 0, HexagonII::MO_PCREL);
1503 return DAG.getNode(HexagonISD::AT_PCREL, dl, PtrVT, A);
1504}
1505
1506SDValue
1507HexagonTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op, SelectionDAG &DAG)
1508 const {
1509 EVT PtrVT = getPointerTy(DAG.getDataLayout());
1510 SDValue GOTSym = DAG.getTargetExternalSymbol(HEXAGON_GOT_SYM_NAME, PtrVT,
1511 HexagonII::MO_PCREL);
1512 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), PtrVT, GOTSym);
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001513}
1514
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001515SDValue
1516HexagonTargetLowering::GetDynamicTLSAddr(SelectionDAG &DAG, SDValue Chain,
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001517 GlobalAddressSDNode *GA, SDValue Glue, EVT PtrVT, unsigned ReturnReg,
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001518 unsigned char OperandFlags) const {
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001519 MachineFunction &MF = DAG.getMachineFunction();
1520 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001521 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1522 SDLoc dl(GA);
1523 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
1524 GA->getValueType(0),
1525 GA->getOffset(),
1526 OperandFlags);
1527 // Create Operands for the call.The Operands should have the following:
1528 // 1. Chain SDValue
1529 // 2. Callee which in this case is the Global address value.
1530 // 3. Registers live into the call.In this case its R0, as we
1531 // have just one argument to be passed.
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001532 // 4. Glue.
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001533 // Note: The order is important.
1534
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001535 const auto &HRI = *Subtarget.getRegisterInfo();
1536 const uint32_t *Mask = HRI.getCallPreservedMask(MF, CallingConv::C);
1537 assert(Mask && "Missing call preserved mask for calling convention");
1538 SDValue Ops[] = { Chain, TGA, DAG.getRegister(Hexagon::R0, PtrVT),
1539 DAG.getRegisterMask(Mask), Glue };
1540 Chain = DAG.getNode(HexagonISD::CALL, dl, NodeTys, Ops);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001541
1542 // Inform MFI that function has calls.
Matthias Braun941a7052016-07-28 18:40:00 +00001543 MFI.setAdjustsStack(true);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001544
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001545 Glue = Chain.getValue(1);
1546 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Glue);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001547}
1548
1549//
1550// Lower using the intial executable model for TLS addresses
1551//
1552SDValue
1553HexagonTargetLowering::LowerToTLSInitialExecModel(GlobalAddressSDNode *GA,
1554 SelectionDAG &DAG) const {
1555 SDLoc dl(GA);
1556 int64_t Offset = GA->getOffset();
1557 auto PtrVT = getPointerTy(DAG.getDataLayout());
1558
1559 // Get the thread pointer.
1560 SDValue TP = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Hexagon::UGP, PtrVT);
1561
Rafael Espindola405e25a2016-06-26 22:24:01 +00001562 bool IsPositionIndependent = isPositionIndependent();
1563 unsigned char TF =
1564 IsPositionIndependent ? HexagonII::MO_IEGOT : HexagonII::MO_IE;
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001565
1566 // First generate the TLS symbol address
1567 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT,
1568 Offset, TF);
1569
1570 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1571
Rafael Espindola405e25a2016-06-26 22:24:01 +00001572 if (IsPositionIndependent) {
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001573 // Generate the GOT pointer in case of position independent code
1574 SDValue GOT = LowerGLOBAL_OFFSET_TABLE(Sym, DAG);
1575
1576 // Add the TLS Symbol address to GOT pointer.This gives
1577 // GOT relative relocation for the symbol.
1578 Sym = DAG.getNode(ISD::ADD, dl, PtrVT, GOT, Sym);
1579 }
1580
1581 // Load the offset value for TLS symbol.This offset is relative to
1582 // thread pointer.
Justin Lebar9c375812016-07-15 18:27:10 +00001583 SDValue LoadOffset =
1584 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Sym, MachinePointerInfo());
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001585
1586 // Address of the thread local variable is the add of thread
1587 // pointer and the offset of the variable.
1588 return DAG.getNode(ISD::ADD, dl, PtrVT, TP, LoadOffset);
1589}
1590
1591//
1592// Lower using the local executable model for TLS addresses
1593//
1594SDValue
1595HexagonTargetLowering::LowerToTLSLocalExecModel(GlobalAddressSDNode *GA,
1596 SelectionDAG &DAG) const {
1597 SDLoc dl(GA);
1598 int64_t Offset = GA->getOffset();
1599 auto PtrVT = getPointerTy(DAG.getDataLayout());
1600
1601 // Get the thread pointer.
1602 SDValue TP = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Hexagon::UGP, PtrVT);
1603 // Generate the TLS symbol address
1604 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT, Offset,
1605 HexagonII::MO_TPREL);
1606 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1607
1608 // Address of the thread local variable is the add of thread
1609 // pointer and the offset of the variable.
1610 return DAG.getNode(ISD::ADD, dl, PtrVT, TP, Sym);
1611}
1612
1613//
1614// Lower using the general dynamic model for TLS addresses
1615//
1616SDValue
1617HexagonTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
1618 SelectionDAG &DAG) const {
1619 SDLoc dl(GA);
1620 int64_t Offset = GA->getOffset();
1621 auto PtrVT = getPointerTy(DAG.getDataLayout());
1622
1623 // First generate the TLS symbol address
1624 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT, Offset,
1625 HexagonII::MO_GDGOT);
1626
1627 // Then, generate the GOT pointer
1628 SDValue GOT = LowerGLOBAL_OFFSET_TABLE(TGA, DAG);
1629
1630 // Add the TLS symbol and the GOT pointer
1631 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1632 SDValue Chain = DAG.getNode(ISD::ADD, dl, PtrVT, GOT, Sym);
1633
1634 // Copy over the argument to R0
1635 SDValue InFlag;
1636 Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, Hexagon::R0, Chain, InFlag);
1637 InFlag = Chain.getValue(1);
1638
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +00001639 unsigned Flags =
1640 static_cast<const HexagonSubtarget &>(DAG.getSubtarget()).useLongCalls()
1641 ? HexagonII::MO_GDPLT | HexagonII::HMOTF_ConstExtended
1642 : HexagonII::MO_GDPLT;
1643
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001644 return GetDynamicTLSAddr(DAG, Chain, GA, InFlag, PtrVT,
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +00001645 Hexagon::R0, Flags);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001646}
1647
1648//
1649// Lower TLS addresses.
1650//
1651// For now for dynamic models, we only support the general dynamic model.
1652//
1653SDValue
1654HexagonTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1655 SelectionDAG &DAG) const {
1656 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1657
1658 switch (HTM.getTLSModel(GA->getGlobal())) {
1659 case TLSModel::GeneralDynamic:
1660 case TLSModel::LocalDynamic:
1661 return LowerToTLSGeneralDynamicModel(GA, DAG);
1662 case TLSModel::InitialExec:
1663 return LowerToTLSInitialExecModel(GA, DAG);
1664 case TLSModel::LocalExec:
1665 return LowerToTLSLocalExecModel(GA, DAG);
1666 }
1667 llvm_unreachable("Bogus TLS model");
1668}
1669
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001670//===----------------------------------------------------------------------===//
1671// TargetLowering Implementation
1672//===----------------------------------------------------------------------===//
1673
Eric Christopherd737b762015-02-02 22:11:36 +00001674HexagonTargetLowering::HexagonTargetLowering(const TargetMachine &TM,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001675 const HexagonSubtarget &ST)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001676 : TargetLowering(TM), HTM(static_cast<const HexagonTargetMachine&>(TM)),
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001677 Subtarget(ST) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001678 bool IsV4 = !Subtarget.hasV5TOps();
1679 auto &HRI = *Subtarget.getRegisterInfo();
Sirish Pande69295b82012-05-10 20:20:25 +00001680
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001681 setPrefLoopAlignment(4);
1682 setPrefFunctionAlignment(4);
1683 setMinFunctionAlignment(2);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001684 setStackPointerRegisterToSaveRestore(HRI.getStackRegister());
1685
Krzysztof Parzyszekf228c952016-06-22 16:07:10 +00001686 setMaxAtomicSizeInBitsSupported(64);
1687 setMinCmpXchgSizeInBits(32);
1688
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001689 if (EnableHexSDNodeSched)
1690 setSchedulingPreference(Sched::VLIW);
1691 else
1692 setSchedulingPreference(Sched::Source);
1693
1694 // Limits for inline expansion of memcpy/memmove
1695 MaxStoresPerMemcpy = MaxStoresPerMemcpyCL;
1696 MaxStoresPerMemcpyOptSize = MaxStoresPerMemcpyOptSizeCL;
1697 MaxStoresPerMemmove = MaxStoresPerMemmoveCL;
1698 MaxStoresPerMemmoveOptSize = MaxStoresPerMemmoveOptSizeCL;
1699 MaxStoresPerMemset = MaxStoresPerMemsetCL;
1700 MaxStoresPerMemsetOptSize = MaxStoresPerMemsetOptSizeCL;
1701
1702 //
1703 // Set up register classes.
1704 //
1705
1706 addRegisterClass(MVT::i1, &Hexagon::PredRegsRegClass);
1707 addRegisterClass(MVT::v2i1, &Hexagon::PredRegsRegClass); // bbbbaaaa
1708 addRegisterClass(MVT::v4i1, &Hexagon::PredRegsRegClass); // ddccbbaa
1709 addRegisterClass(MVT::v8i1, &Hexagon::PredRegsRegClass); // hgfedcba
1710 addRegisterClass(MVT::i32, &Hexagon::IntRegsRegClass);
1711 addRegisterClass(MVT::v4i8, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001712 addRegisterClass(MVT::v2i16, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001713 addRegisterClass(MVT::i64, &Hexagon::DoubleRegsRegClass);
1714 addRegisterClass(MVT::v8i8, &Hexagon::DoubleRegsRegClass);
1715 addRegisterClass(MVT::v4i16, &Hexagon::DoubleRegsRegClass);
1716 addRegisterClass(MVT::v2i32, &Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001717
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001718 if (Subtarget.hasV5TOps()) {
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001719 addRegisterClass(MVT::f32, &Hexagon::IntRegsRegClass);
1720 addRegisterClass(MVT::f64, &Hexagon::DoubleRegsRegClass);
1721 }
Sirish Pande69295b82012-05-10 20:20:25 +00001722
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001723 if (Subtarget.hasV60TOps()) {
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +00001724 if (Subtarget.useHVX64BOps()) {
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001725 addRegisterClass(MVT::v64i8, &Hexagon::HvxVRRegClass);
1726 addRegisterClass(MVT::v32i16, &Hexagon::HvxVRRegClass);
1727 addRegisterClass(MVT::v16i32, &Hexagon::HvxVRRegClass);
1728 addRegisterClass(MVT::v8i64, &Hexagon::HvxVRRegClass);
1729 addRegisterClass(MVT::v128i8, &Hexagon::HvxWRRegClass);
1730 addRegisterClass(MVT::v64i16, &Hexagon::HvxWRRegClass);
1731 addRegisterClass(MVT::v32i32, &Hexagon::HvxWRRegClass);
1732 addRegisterClass(MVT::v16i64, &Hexagon::HvxWRRegClass);
1733 addRegisterClass(MVT::v512i1, &Hexagon::HvxQRRegClass);
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +00001734 } else if (Subtarget.useHVX128BOps()) {
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001735 addRegisterClass(MVT::v128i8, &Hexagon::HvxVRRegClass);
1736 addRegisterClass(MVT::v64i16, &Hexagon::HvxVRRegClass);
1737 addRegisterClass(MVT::v32i32, &Hexagon::HvxVRRegClass);
1738 addRegisterClass(MVT::v16i64, &Hexagon::HvxVRRegClass);
1739 addRegisterClass(MVT::v256i8, &Hexagon::HvxWRRegClass);
1740 addRegisterClass(MVT::v128i16, &Hexagon::HvxWRRegClass);
1741 addRegisterClass(MVT::v64i32, &Hexagon::HvxWRRegClass);
1742 addRegisterClass(MVT::v32i64, &Hexagon::HvxWRRegClass);
1743 addRegisterClass(MVT::v1024i1, &Hexagon::HvxQRRegClass);
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001744 }
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001745 }
1746
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001747 //
1748 // Handling of scalar operations.
1749 //
1750 // All operations default to "legal", except:
1751 // - indexed loads and stores (pre-/post-incremented),
1752 // - ANY_EXTEND_VECTOR_INREG, ATOMIC_CMP_SWAP_WITH_SUCCESS, CONCAT_VECTORS,
1753 // ConstantFP, DEBUGTRAP, FCEIL, FCOPYSIGN, FEXP, FEXP2, FFLOOR, FGETSIGN,
1754 // FLOG, FLOG2, FLOG10, FMAXNUM, FMINNUM, FNEARBYINT, FRINT, FROUND, TRAP,
1755 // FTRUNC, PREFETCH, SIGN_EXTEND_VECTOR_INREG, ZERO_EXTEND_VECTOR_INREG,
1756 // which default to "expand" for at least one type.
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001757
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001758 // Misc operations.
1759 setOperationAction(ISD::ConstantFP, MVT::f32, Legal); // Default: expand
1760 setOperationAction(ISD::ConstantFP, MVT::f64, Legal); // Default: expand
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001761
1762 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001763 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001764 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001765 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
1766 setOperationAction(ISD::INLINEASM, MVT::Other, Custom);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00001767 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +00001768 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00001769 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001770 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001771 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001772 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001773 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001774
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001775 // Custom legalize GlobalAddress nodes into CONST32.
1776 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001777 setOperationAction(ISD::GlobalAddress, MVT::i8, Custom);
1778 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001779
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001780 // Hexagon needs to optimize cases with negative constants.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001781 setOperationAction(ISD::SETCC, MVT::i8, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001782 setOperationAction(ISD::SETCC, MVT::i16, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001783
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001784 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
1785 setOperationAction(ISD::VASTART, MVT::Other, Custom);
1786 setOperationAction(ISD::VAEND, MVT::Other, Expand);
1787 setOperationAction(ISD::VAARG, MVT::Other, Expand);
1788
1789 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
1790 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
1791 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
1792
1793 if (EmitJumpTables)
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001794 setMinimumJumpTableEntries(MinimumJumpTables);
Krzysztof Parzyszeka61f7da2016-01-13 21:43:13 +00001795 else
Eugene Zelenko58655bb2016-12-17 01:09:05 +00001796 setMinimumJumpTableEntries(std::numeric_limits<int>::max());
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001797 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001798
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001799 // Hexagon has instructions for add/sub with carry. The problem with
1800 // modeling these instructions is that they produce 2 results: Rdd and Px.
1801 // To model the update of Px, we will have to use Defs[p0..p3] which will
1802 // cause any predicate live range to spill. So, we pretend we dont't have
1803 // these instructions.
1804 setOperationAction(ISD::ADDE, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001805 setOperationAction(ISD::ADDE, MVT::i16, Expand);
1806 setOperationAction(ISD::ADDE, MVT::i32, Expand);
1807 setOperationAction(ISD::ADDE, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001808 setOperationAction(ISD::SUBE, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001809 setOperationAction(ISD::SUBE, MVT::i16, Expand);
1810 setOperationAction(ISD::SUBE, MVT::i32, Expand);
1811 setOperationAction(ISD::SUBE, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001812 setOperationAction(ISD::ADDC, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001813 setOperationAction(ISD::ADDC, MVT::i16, Expand);
1814 setOperationAction(ISD::ADDC, MVT::i32, Expand);
1815 setOperationAction(ISD::ADDC, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001816 setOperationAction(ISD::SUBC, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001817 setOperationAction(ISD::SUBC, MVT::i16, Expand);
1818 setOperationAction(ISD::SUBC, MVT::i32, Expand);
1819 setOperationAction(ISD::SUBC, MVT::i64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001820
Krzysztof Parzyszek2c4487d2015-04-13 20:37:01 +00001821 // Only add and sub that detect overflow are the saturating ones.
1822 for (MVT VT : MVT::integer_valuetypes()) {
1823 setOperationAction(ISD::UADDO, VT, Expand);
1824 setOperationAction(ISD::SADDO, VT, Expand);
1825 setOperationAction(ISD::USUBO, VT, Expand);
1826 setOperationAction(ISD::SSUBO, VT, Expand);
1827 }
1828
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001829 setOperationAction(ISD::CTLZ, MVT::i8, Promote);
1830 setOperationAction(ISD::CTLZ, MVT::i16, Promote);
1831 setOperationAction(ISD::CTTZ, MVT::i8, Promote);
1832 setOperationAction(ISD::CTTZ, MVT::i16, Promote);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001833
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001834 // In V5, popcount can count # of 1s in i64 but returns i32.
1835 // On V4 it will be expanded (set later).
1836 setOperationAction(ISD::CTPOP, MVT::i8, Promote);
1837 setOperationAction(ISD::CTPOP, MVT::i16, Promote);
1838 setOperationAction(ISD::CTPOP, MVT::i32, Promote);
Krzysztof Parzyszekaf5ff652017-02-23 15:02:09 +00001839 setOperationAction(ISD::CTPOP, MVT::i64, Legal);
1840
1841 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
1842 setOperationAction(ISD::BITREVERSE, MVT::i64, Legal);
1843 setOperationAction(ISD::BSWAP, MVT::i32, Legal);
1844 setOperationAction(ISD::BSWAP, MVT::i64, Legal);
Krzysztof Parzyszekef580172017-05-30 17:47:51 +00001845 setOperationAction(ISD::MUL, MVT::i64, Legal);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001846
Benjamin Kramer62460692015-04-25 14:46:53 +00001847 for (unsigned IntExpOp :
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001848 { ISD::SDIV, ISD::UDIV, ISD::SREM, ISD::UREM,
1849 ISD::SDIVREM, ISD::UDIVREM, ISD::ROTL, ISD::ROTR,
Krzysztof Parzyszekaf5ff652017-02-23 15:02:09 +00001850 ISD::SHL_PARTS, ISD::SRA_PARTS, ISD::SRL_PARTS,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001851 ISD::SMUL_LOHI, ISD::UMUL_LOHI }) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001852 setOperationAction(IntExpOp, MVT::i32, Expand);
1853 setOperationAction(IntExpOp, MVT::i64, Expand);
1854 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001855
Benjamin Kramer62460692015-04-25 14:46:53 +00001856 for (unsigned FPExpOp :
1857 {ISD::FDIV, ISD::FREM, ISD::FSQRT, ISD::FSIN, ISD::FCOS, ISD::FSINCOS,
1858 ISD::FPOW, ISD::FCOPYSIGN}) {
1859 setOperationAction(FPExpOp, MVT::f32, Expand);
1860 setOperationAction(FPExpOp, MVT::f64, Expand);
1861 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001862
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001863 // No extending loads from i32.
1864 for (MVT VT : MVT::integer_valuetypes()) {
1865 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
1866 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
1867 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
1868 }
1869 // Turn FP truncstore into trunc + store.
1870 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Michael Kuperstein2bc3d4d2016-08-18 20:08:15 +00001871 // Turn FP extload into load/fpextend.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001872 for (MVT VT : MVT::fp_valuetypes())
1873 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001874
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001875 // Expand BR_CC and SELECT_CC for all integer and fp types.
1876 for (MVT VT : MVT::integer_valuetypes()) {
1877 setOperationAction(ISD::BR_CC, VT, Expand);
1878 setOperationAction(ISD::SELECT_CC, VT, Expand);
1879 }
1880 for (MVT VT : MVT::fp_valuetypes()) {
1881 setOperationAction(ISD::BR_CC, VT, Expand);
1882 setOperationAction(ISD::SELECT_CC, VT, Expand);
1883 }
1884 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001885
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001886 //
1887 // Handling of vector operations.
1888 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001889
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001890 promoteLdStType(MVT::v4i8, MVT::i32);
1891 promoteLdStType(MVT::v2i16, MVT::i32);
1892 promoteLdStType(MVT::v8i8, MVT::i64);
Krzysztof Parzyszek5eef92e2017-07-17 15:45:45 +00001893 promoteLdStType(MVT::v4i16, MVT::i64);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001894 promoteLdStType(MVT::v2i32, MVT::i64);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001895
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001896 // Set the action for vector operations to "expand", then override it with
1897 // either "custom" or "legal" for specific cases.
Craig Topper26260942015-10-18 05:15:34 +00001898 static const unsigned VectExpOps[] = {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001899 // Integer arithmetic:
1900 ISD::ADD, ISD::SUB, ISD::MUL, ISD::SDIV, ISD::UDIV,
1901 ISD::SREM, ISD::UREM, ISD::SDIVREM, ISD::UDIVREM, ISD::ADDC,
1902 ISD::SUBC, ISD::SADDO, ISD::UADDO, ISD::SSUBO, ISD::USUBO,
1903 ISD::SMUL_LOHI, ISD::UMUL_LOHI,
1904 // Logical/bit:
1905 ISD::AND, ISD::OR, ISD::XOR, ISD::ROTL, ISD::ROTR,
Craig Topper33772c52016-04-28 03:34:31 +00001906 ISD::CTPOP, ISD::CTLZ, ISD::CTTZ,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001907 // Floating point arithmetic/math functions:
1908 ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FMA, ISD::FDIV,
1909 ISD::FREM, ISD::FNEG, ISD::FABS, ISD::FSQRT, ISD::FSIN,
Craig Topperf6d4dc52017-05-30 15:27:55 +00001910 ISD::FCOS, ISD::FPOW, ISD::FLOG, ISD::FLOG2,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001911 ISD::FLOG10, ISD::FEXP, ISD::FEXP2, ISD::FCEIL, ISD::FTRUNC,
1912 ISD::FRINT, ISD::FNEARBYINT, ISD::FROUND, ISD::FFLOOR,
1913 ISD::FMINNUM, ISD::FMAXNUM, ISD::FSINCOS,
1914 // Misc:
Krzysztof Parzyszek046da742016-10-27 14:30:16 +00001915 ISD::BR_CC, ISD::SELECT_CC, ISD::ConstantPool,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001916 // Vector:
1917 ISD::BUILD_VECTOR, ISD::SCALAR_TO_VECTOR,
1918 ISD::EXTRACT_VECTOR_ELT, ISD::INSERT_VECTOR_ELT,
1919 ISD::EXTRACT_SUBVECTOR, ISD::INSERT_SUBVECTOR,
1920 ISD::CONCAT_VECTORS, ISD::VECTOR_SHUFFLE
1921 };
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001922
1923 for (MVT VT : MVT::vector_valuetypes()) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001924 for (unsigned VectExpOp : VectExpOps)
1925 setOperationAction(VectExpOp, VT, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001926
Krzysztof Parzyszeka696b1b2016-09-08 17:42:14 +00001927 // Expand all extending loads and truncating stores:
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001928 for (MVT TargetVT : MVT::vector_valuetypes()) {
Krzysztof Parzyszeka696b1b2016-09-08 17:42:14 +00001929 if (TargetVT == VT)
1930 continue;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001931 setLoadExtAction(ISD::EXTLOAD, TargetVT, VT, Expand);
Krzysztof Parzyszeka696b1b2016-09-08 17:42:14 +00001932 setLoadExtAction(ISD::ZEXTLOAD, TargetVT, VT, Expand);
1933 setLoadExtAction(ISD::SEXTLOAD, TargetVT, VT, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001934 setTruncStoreAction(VT, TargetVT, Expand);
1935 }
1936
Krzysztof Parzyszek046da742016-10-27 14:30:16 +00001937 // Normalize all inputs to SELECT to be vectors of i32.
1938 if (VT.getVectorElementType() != MVT::i32) {
1939 MVT VT32 = MVT::getVectorVT(MVT::i32, VT.getSizeInBits()/32);
1940 setOperationAction(ISD::SELECT, VT, Promote);
1941 AddPromotedToType(ISD::SELECT, VT, VT32);
1942 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001943 setOperationAction(ISD::SRA, VT, Custom);
1944 setOperationAction(ISD::SHL, VT, Custom);
1945 setOperationAction(ISD::SRL, VT, Custom);
1946 }
1947
Krzysztof Parzyszek64e5d7d2017-10-20 19:33:12 +00001948 // Extending loads from (native) vectors of i8 into (native) vectors of i16
1949 // are legal.
1950 setLoadExtAction(ISD::EXTLOAD, MVT::v2i16, MVT::v2i8, Legal);
1951 setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i16, MVT::v2i8, Legal);
1952 setLoadExtAction(ISD::SEXTLOAD, MVT::v2i16, MVT::v2i8, Legal);
1953 setLoadExtAction(ISD::EXTLOAD, MVT::v4i16, MVT::v4i8, Legal);
1954 setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i16, MVT::v4i8, Legal);
1955 setLoadExtAction(ISD::SEXTLOAD, MVT::v4i16, MVT::v4i8, Legal);
1956
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001957 // Types natively supported:
Benjamin Kramer62460692015-04-25 14:46:53 +00001958 for (MVT NativeVT : {MVT::v2i1, MVT::v4i1, MVT::v8i1, MVT::v32i1, MVT::v64i1,
1959 MVT::v4i8, MVT::v8i8, MVT::v2i16, MVT::v4i16, MVT::v1i32,
1960 MVT::v2i32, MVT::v1i64}) {
1961 setOperationAction(ISD::BUILD_VECTOR, NativeVT, Custom);
1962 setOperationAction(ISD::EXTRACT_VECTOR_ELT, NativeVT, Custom);
1963 setOperationAction(ISD::INSERT_VECTOR_ELT, NativeVT, Custom);
1964 setOperationAction(ISD::EXTRACT_SUBVECTOR, NativeVT, Custom);
1965 setOperationAction(ISD::INSERT_SUBVECTOR, NativeVT, Custom);
1966 setOperationAction(ISD::CONCAT_VECTORS, NativeVT, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001967
Benjamin Kramer62460692015-04-25 14:46:53 +00001968 setOperationAction(ISD::ADD, NativeVT, Legal);
1969 setOperationAction(ISD::SUB, NativeVT, Legal);
1970 setOperationAction(ISD::MUL, NativeVT, Legal);
1971 setOperationAction(ISD::AND, NativeVT, Legal);
1972 setOperationAction(ISD::OR, NativeVT, Legal);
1973 setOperationAction(ISD::XOR, NativeVT, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001974 }
1975
1976 setOperationAction(ISD::SETCC, MVT::v2i16, Custom);
1977 setOperationAction(ISD::VSELECT, MVT::v2i16, Custom);
1978 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
1979 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00001980
Sumanth Gundapaneni9d954c42017-10-18 17:45:22 +00001981 if (Subtarget.useHVXOps()) {
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +00001982 if (Subtarget.useHVX64BOps()) {
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00001983 setOperationAction(ISD::CONCAT_VECTORS, MVT::v128i8, Custom);
1984 setOperationAction(ISD::CONCAT_VECTORS, MVT::v64i16, Custom);
1985 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i32, Custom);
1986 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i64, Custom);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00001987 // We try to generate the vpack{e/o} instructions. If we fail
1988 // we fall back upon ExpandOp.
1989 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v64i8, Custom);
1990 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v32i16, Custom);
1991 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v64i8, Custom);
1992 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v32i16, Custom);
1993 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v16i32, Custom);
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +00001994 } else if (Subtarget.useHVX128BOps()) {
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00001995 setOperationAction(ISD::CONCAT_VECTORS, MVT::v256i8, Custom);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001996 setOperationAction(ISD::CONCAT_VECTORS, MVT::v128i16, Custom);
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00001997 setOperationAction(ISD::CONCAT_VECTORS, MVT::v64i32, Custom);
1998 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i64, Custom);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00001999 // We try to generate the vpack{e/o} instructions. If we fail
2000 // we fall back upon ExpandOp.
2001 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v128i8, Custom);
2002 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v64i16, Custom);
2003 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v4i32, Custom);
2004 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v128i8, Custom);
2005 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v64i16, Custom);
2006 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v32i32, Custom);
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002007 } else {
2008 llvm_unreachable("Unrecognized HVX mode");
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002009 }
2010 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002011 // Subtarget-specific operation actions.
2012 //
2013 if (Subtarget.hasV5TOps()) {
2014 setOperationAction(ISD::FMA, MVT::f64, Expand);
2015 setOperationAction(ISD::FADD, MVT::f64, Expand);
2016 setOperationAction(ISD::FSUB, MVT::f64, Expand);
2017 setOperationAction(ISD::FMUL, MVT::f64, Expand);
2018
Krzysztof Parzyszekbd8ef4b2016-08-19 13:34:31 +00002019 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
2020 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
2021
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002022 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Promote);
2023 setOperationAction(ISD::FP_TO_UINT, MVT::i8, Promote);
2024 setOperationAction(ISD::FP_TO_UINT, MVT::i16, Promote);
2025 setOperationAction(ISD::FP_TO_SINT, MVT::i1, Promote);
2026 setOperationAction(ISD::FP_TO_SINT, MVT::i8, Promote);
2027 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
2028 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote);
2029 setOperationAction(ISD::UINT_TO_FP, MVT::i8, Promote);
2030 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote);
2031 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote);
2032 setOperationAction(ISD::SINT_TO_FP, MVT::i8, Promote);
2033 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002034 } else { // V4
2035 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
2036 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Expand);
2037 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
2038 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
2039 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Expand);
2040 setOperationAction(ISD::FP_TO_SINT, MVT::f32, Expand);
2041 setOperationAction(ISD::FP_EXTEND, MVT::f32, Expand);
2042 setOperationAction(ISD::FP_ROUND, MVT::f64, Expand);
2043 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
2044
2045 setOperationAction(ISD::CTPOP, MVT::i8, Expand);
2046 setOperationAction(ISD::CTPOP, MVT::i16, Expand);
2047 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
2048 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
2049
2050 // Expand these operations for both f32 and f64:
Benjamin Kramer62460692015-04-25 14:46:53 +00002051 for (unsigned FPExpOpV4 :
2052 {ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FABS, ISD::FNEG, ISD::FMA}) {
2053 setOperationAction(FPExpOpV4, MVT::f32, Expand);
2054 setOperationAction(FPExpOpV4, MVT::f64, Expand);
2055 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002056
Benjamin Kramer62460692015-04-25 14:46:53 +00002057 for (ISD::CondCode FPExpCCV4 :
2058 {ISD::SETOEQ, ISD::SETOGT, ISD::SETOLT, ISD::SETOGE, ISD::SETOLE,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002059 ISD::SETUO, ISD::SETO}) {
Benjamin Kramer62460692015-04-25 14:46:53 +00002060 setCondCodeAction(FPExpCCV4, MVT::f32, Expand);
2061 setCondCodeAction(FPExpCCV4, MVT::f64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002062 }
2063 }
2064
2065 // Handling of indexed loads/stores: default is "expand".
2066 //
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +00002067 for (MVT VT : {MVT::i8, MVT::i16, MVT::i32, MVT::i64}) {
2068 setIndexedLoadAction(ISD::POST_INC, VT, Legal);
2069 setIndexedStoreAction(ISD::POST_INC, VT, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002070 }
2071
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +00002072 if (Subtarget.useHVX64BOps()) {
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +00002073 for (MVT VT : {MVT::v64i8, MVT::v32i16, MVT::v16i32, MVT::v8i64,
2074 MVT::v128i8, MVT::v64i16, MVT::v32i32, MVT::v16i64}) {
2075 setIndexedLoadAction(ISD::POST_INC, VT, Legal);
2076 setIndexedStoreAction(ISD::POST_INC, VT, Legal);
2077 }
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +00002078 } else if (Subtarget.useHVX128BOps()) {
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +00002079 for (MVT VT : {MVT::v128i8, MVT::v64i16, MVT::v32i32, MVT::v16i64,
2080 MVT::v256i8, MVT::v128i16, MVT::v64i32, MVT::v32i64}) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002081 setIndexedLoadAction(ISD::POST_INC, VT, Legal);
2082 setIndexedStoreAction(ISD::POST_INC, VT, Legal);
2083 }
2084 }
2085
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002086 computeRegisterProperties(&HRI);
2087
2088 //
2089 // Library calls for unsupported operations
2090 //
2091 bool FastMath = EnableFastMath;
2092
Benjamin Kramera37c8092015-04-25 14:46:46 +00002093 setLibcallName(RTLIB::SDIV_I32, "__hexagon_divsi3");
2094 setLibcallName(RTLIB::SDIV_I64, "__hexagon_divdi3");
2095 setLibcallName(RTLIB::UDIV_I32, "__hexagon_udivsi3");
2096 setLibcallName(RTLIB::UDIV_I64, "__hexagon_udivdi3");
2097 setLibcallName(RTLIB::SREM_I32, "__hexagon_modsi3");
2098 setLibcallName(RTLIB::SREM_I64, "__hexagon_moddi3");
2099 setLibcallName(RTLIB::UREM_I32, "__hexagon_umodsi3");
2100 setLibcallName(RTLIB::UREM_I64, "__hexagon_umoddi3");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002101
Benjamin Kramera37c8092015-04-25 14:46:46 +00002102 setLibcallName(RTLIB::SINTTOFP_I128_F64, "__hexagon_floattidf");
2103 setLibcallName(RTLIB::SINTTOFP_I128_F32, "__hexagon_floattisf");
2104 setLibcallName(RTLIB::FPTOUINT_F32_I128, "__hexagon_fixunssfti");
2105 setLibcallName(RTLIB::FPTOUINT_F64_I128, "__hexagon_fixunsdfti");
2106 setLibcallName(RTLIB::FPTOSINT_F32_I128, "__hexagon_fixsfti");
2107 setLibcallName(RTLIB::FPTOSINT_F64_I128, "__hexagon_fixdfti");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002108
2109 if (IsV4) {
2110 // Handle single-precision floating point operations on V4.
Benjamin Kramera37c8092015-04-25 14:46:46 +00002111 if (FastMath) {
2112 setLibcallName(RTLIB::ADD_F32, "__hexagon_fast_addsf3");
2113 setLibcallName(RTLIB::SUB_F32, "__hexagon_fast_subsf3");
2114 setLibcallName(RTLIB::MUL_F32, "__hexagon_fast_mulsf3");
2115 setLibcallName(RTLIB::OGT_F32, "__hexagon_fast_gtsf2");
2116 setLibcallName(RTLIB::OLT_F32, "__hexagon_fast_ltsf2");
2117 // Double-precision compares.
2118 setLibcallName(RTLIB::OGT_F64, "__hexagon_fast_gtdf2");
2119 setLibcallName(RTLIB::OLT_F64, "__hexagon_fast_ltdf2");
2120 } else {
2121 setLibcallName(RTLIB::ADD_F32, "__hexagon_addsf3");
2122 setLibcallName(RTLIB::SUB_F32, "__hexagon_subsf3");
2123 setLibcallName(RTLIB::MUL_F32, "__hexagon_mulsf3");
2124 setLibcallName(RTLIB::OGT_F32, "__hexagon_gtsf2");
2125 setLibcallName(RTLIB::OLT_F32, "__hexagon_ltsf2");
2126 // Double-precision compares.
2127 setLibcallName(RTLIB::OGT_F64, "__hexagon_gtdf2");
2128 setLibcallName(RTLIB::OLT_F64, "__hexagon_ltdf2");
2129 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002130 }
2131
2132 // This is the only fast library function for sqrtd.
2133 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00002134 setLibcallName(RTLIB::SQRT_F64, "__hexagon_fast2_sqrtdf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002135
Benjamin Kramera37c8092015-04-25 14:46:46 +00002136 // Prefix is: nothing for "slow-math",
2137 // "fast2_" for V4 fast-math and V5+ fast-math double-precision
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002138 // (actually, keep fast-math and fast-math2 separate for now)
Benjamin Kramera37c8092015-04-25 14:46:46 +00002139 if (FastMath) {
2140 setLibcallName(RTLIB::ADD_F64, "__hexagon_fast_adddf3");
2141 setLibcallName(RTLIB::SUB_F64, "__hexagon_fast_subdf3");
2142 setLibcallName(RTLIB::MUL_F64, "__hexagon_fast_muldf3");
2143 setLibcallName(RTLIB::DIV_F64, "__hexagon_fast_divdf3");
2144 // Calling __hexagon_fast2_divsf3 with fast-math on V5 (ok).
2145 setLibcallName(RTLIB::DIV_F32, "__hexagon_fast_divsf3");
2146 } else {
2147 setLibcallName(RTLIB::ADD_F64, "__hexagon_adddf3");
2148 setLibcallName(RTLIB::SUB_F64, "__hexagon_subdf3");
2149 setLibcallName(RTLIB::MUL_F64, "__hexagon_muldf3");
2150 setLibcallName(RTLIB::DIV_F64, "__hexagon_divdf3");
2151 setLibcallName(RTLIB::DIV_F32, "__hexagon_divsf3");
2152 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002153
2154 if (Subtarget.hasV5TOps()) {
2155 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00002156 setLibcallName(RTLIB::SQRT_F32, "__hexagon_fast2_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002157 else
Benjamin Kramera37c8092015-04-25 14:46:46 +00002158 setLibcallName(RTLIB::SQRT_F32, "__hexagon_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002159 } else {
2160 // V4
Benjamin Kramera37c8092015-04-25 14:46:46 +00002161 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__hexagon_floatsisf");
2162 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__hexagon_floatsidf");
2163 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__hexagon_floatdisf");
2164 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__hexagon_floatdidf");
2165 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__hexagon_floatunsisf");
2166 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__hexagon_floatunsidf");
2167 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__hexagon_floatundisf");
2168 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__hexagon_floatundidf");
2169 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__hexagon_fixunssfsi");
2170 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__hexagon_fixunssfdi");
2171 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__hexagon_fixunsdfsi");
2172 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__hexagon_fixunsdfdi");
2173 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__hexagon_fixsfsi");
2174 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__hexagon_fixsfdi");
2175 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__hexagon_fixdfsi");
2176 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__hexagon_fixdfdi");
2177 setLibcallName(RTLIB::FPEXT_F32_F64, "__hexagon_extendsfdf2");
2178 setLibcallName(RTLIB::FPROUND_F64_F32, "__hexagon_truncdfsf2");
2179 setLibcallName(RTLIB::OEQ_F32, "__hexagon_eqsf2");
2180 setLibcallName(RTLIB::OEQ_F64, "__hexagon_eqdf2");
2181 setLibcallName(RTLIB::OGE_F32, "__hexagon_gesf2");
2182 setLibcallName(RTLIB::OGE_F64, "__hexagon_gedf2");
2183 setLibcallName(RTLIB::OLE_F32, "__hexagon_lesf2");
2184 setLibcallName(RTLIB::OLE_F64, "__hexagon_ledf2");
2185 setLibcallName(RTLIB::UNE_F32, "__hexagon_nesf2");
2186 setLibcallName(RTLIB::UNE_F64, "__hexagon_nedf2");
2187 setLibcallName(RTLIB::UO_F32, "__hexagon_unordsf2");
2188 setLibcallName(RTLIB::UO_F64, "__hexagon_unorddf2");
2189 setLibcallName(RTLIB::O_F32, "__hexagon_unordsf2");
2190 setLibcallName(RTLIB::O_F64, "__hexagon_unorddf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002191 }
2192
2193 // These cause problems when the shift amount is non-constant.
2194 setLibcallName(RTLIB::SHL_I128, nullptr);
2195 setLibcallName(RTLIB::SRL_I128, nullptr);
2196 setLibcallName(RTLIB::SRA_I128, nullptr);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002197}
2198
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002199const char* HexagonTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00002200 switch ((HexagonISD::NodeType)Opcode) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002201 case HexagonISD::ALLOCA: return "HexagonISD::ALLOCA";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002202 case HexagonISD::AT_GOT: return "HexagonISD::AT_GOT";
2203 case HexagonISD::AT_PCREL: return "HexagonISD::AT_PCREL";
2204 case HexagonISD::BARRIER: return "HexagonISD::BARRIER";
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002205 case HexagonISD::CALL: return "HexagonISD::CALL";
2206 case HexagonISD::CALLnr: return "HexagonISD::CALLnr";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002207 case HexagonISD::CALLR: return "HexagonISD::CALLR";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002208 case HexagonISD::COMBINE: return "HexagonISD::COMBINE";
2209 case HexagonISD::CONST32_GP: return "HexagonISD::CONST32_GP";
2210 case HexagonISD::CONST32: return "HexagonISD::CONST32";
2211 case HexagonISD::CP: return "HexagonISD::CP";
2212 case HexagonISD::DCFETCH: return "HexagonISD::DCFETCH";
2213 case HexagonISD::EH_RETURN: return "HexagonISD::EH_RETURN";
2214 case HexagonISD::EXTRACTU: return "HexagonISD::EXTRACTU";
2215 case HexagonISD::EXTRACTURP: return "HexagonISD::EXTRACTURP";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002216 case HexagonISD::INSERT: return "HexagonISD::INSERT";
2217 case HexagonISD::INSERTRP: return "HexagonISD::INSERTRP";
2218 case HexagonISD::JT: return "HexagonISD::JT";
2219 case HexagonISD::PACKHL: return "HexagonISD::PACKHL";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002220 case HexagonISD::RET_FLAG: return "HexagonISD::RET_FLAG";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002221 case HexagonISD::TC_RETURN: return "HexagonISD::TC_RETURN";
Krzysztof Parzyszekc168c012015-12-03 16:47:20 +00002222 case HexagonISD::VCOMBINE: return "HexagonISD::VCOMBINE";
Krzysztof Parzyszek302a9d42017-07-14 19:02:32 +00002223 case HexagonISD::VPACKE: return "HexagonISD::VPACKE";
2224 case HexagonISD::VPACKO: return "HexagonISD::VPACKO";
Krzysztof Parzyszekf85dd9f2017-07-10 20:16:44 +00002225 case HexagonISD::VASL: return "HexagonISD::VASL";
2226 case HexagonISD::VASR: return "HexagonISD::VASR";
2227 case HexagonISD::VLSR: return "HexagonISD::VLSR";
2228 case HexagonISD::VSPLAT: return "HexagonISD::VSPLAT";
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +00002229 case HexagonISD::READCYCLE: return "HexagonISD::READCYCLE";
Matthias Braund04893f2015-05-07 21:33:59 +00002230 case HexagonISD::OP_END: break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002231 }
Matthias Braund04893f2015-05-07 21:33:59 +00002232 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002233}
2234
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002235bool HexagonTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002236 EVT MTy1 = EVT::getEVT(Ty1);
2237 EVT MTy2 = EVT::getEVT(Ty2);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002238 if (!MTy1.isSimple() || !MTy2.isSimple())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002239 return false;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002240 return (MTy1.getSimpleVT() == MVT::i64) && (MTy2.getSimpleVT() == MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002241}
2242
2243bool HexagonTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002244 if (!VT1.isSimple() || !VT2.isSimple())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002245 return false;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002246 return (VT1.getSimpleVT() == MVT::i64) && (VT2.getSimpleVT() == MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002247}
2248
Krzysztof Parzyszekbd8ef4b2016-08-19 13:34:31 +00002249bool HexagonTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
2250 return isOperationLegalOrCustom(ISD::FMA, VT);
2251}
2252
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002253// Should we expand the build vector with shuffles?
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00002254bool HexagonTargetLowering::shouldExpandBuildVectorWithShuffles(EVT VT,
2255 unsigned DefinedValues) const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002256 // Hexagon vector shuffle operates on element sizes of bytes or halfwords
2257 EVT EltVT = VT.getVectorElementType();
2258 int EltBits = EltVT.getSizeInBits();
2259 if ((EltBits != 8) && (EltBits != 16))
2260 return false;
2261
2262 return TargetLowering::shouldExpandBuildVectorWithShuffles(VT, DefinedValues);
2263}
2264
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00002265static StridedLoadKind isStridedLoad(const ArrayRef<int> &Mask) {
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002266 int even_start = -2;
2267 int odd_start = -1;
2268 size_t mask_len = Mask.size();
2269 for (auto idx : Mask) {
2270 if ((idx - even_start) == 2)
2271 even_start = idx;
2272 else
2273 break;
2274 }
2275 if (even_start == (int)(mask_len * 2) - 2)
2276 return StridedLoadKind::Even;
2277 for (auto idx : Mask) {
2278 if ((idx - odd_start) == 2)
2279 odd_start = idx;
2280 else
2281 break;
2282 }
2283 if (odd_start == (int)(mask_len * 2) - 1)
2284 return StridedLoadKind::Odd;
2285
2286 return StridedLoadKind::NoPattern;
2287}
2288
Zvi Rackover1b736822017-07-26 08:06:58 +00002289bool HexagonTargetLowering::isShuffleMaskLegal(ArrayRef<int> Mask,
2290 EVT VT) const {
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00002291 if (Subtarget.useHVXOps())
2292 return isStridedLoad(Mask) != StridedLoadKind::NoPattern;
2293 return true;
2294}
2295
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002296// Lower a vector shuffle (V1, V2, V3). V1 and V2 are the two vectors
2297// to select data from, V3 is the permutation.
2298SDValue
2299HexagonTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG)
2300 const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002301 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
2302 SDValue V1 = Op.getOperand(0);
2303 SDValue V2 = Op.getOperand(1);
2304 SDLoc dl(Op);
2305 EVT VT = Op.getValueType();
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002306 bool UseHVX = Subtarget.useHVXOps();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002307
Sanjay Patel57195842016-03-14 17:28:46 +00002308 if (V2.isUndef())
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002309 V2 = V1;
2310
2311 if (SVN->isSplat()) {
2312 int Lane = SVN->getSplatIndex();
2313 if (Lane == -1) Lane = 0;
2314
2315 // Test if V1 is a SCALAR_TO_VECTOR.
2316 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR)
Krzysztof Parzyszek89b2d7c2017-07-13 18:17:58 +00002317 return DAG.getNode(HexagonISD::VSPLAT, dl, VT, V1.getOperand(0));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002318
2319 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
2320 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
2321 // reaches it).
2322 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
2323 !isa<ConstantSDNode>(V1.getOperand(0))) {
2324 bool IsScalarToVector = true;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00002325 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i) {
Sanjay Patel75068522016-03-14 18:09:43 +00002326 if (!V1.getOperand(i).isUndef()) {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002327 IsScalarToVector = false;
2328 break;
2329 }
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00002330 }
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002331 if (IsScalarToVector)
Krzysztof Parzyszek89b2d7c2017-07-13 18:17:58 +00002332 return DAG.getNode(HexagonISD::VSPLAT, dl, VT, V1.getOperand(0));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002333 }
Krzysztof Parzyszek89b2d7c2017-07-13 18:17:58 +00002334 return DAG.getNode(HexagonISD::VSPLAT, dl, VT,
2335 DAG.getConstant(Lane, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002336 }
2337
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002338 if (UseHVX) {
2339 ArrayRef<int> Mask = SVN->getMask();
2340 size_t MaskLen = Mask.size();
Krzysztof Parzyszek302a9d42017-07-14 19:02:32 +00002341 unsigned SizeInBits = VT.getScalarSizeInBits() * MaskLen;
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002342
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +00002343 if ((Subtarget.useHVX64BOps() && SizeInBits == 64 * 8) ||
2344 (Subtarget.useHVX128BOps() && SizeInBits == 128 * 8)) {
Krzysztof Parzyszek302a9d42017-07-14 19:02:32 +00002345 StridedLoadKind Pattern = isStridedLoad(Mask);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002346 if (Pattern == StridedLoadKind::NoPattern)
2347 return SDValue();
2348
Krzysztof Parzyszek302a9d42017-07-14 19:02:32 +00002349 unsigned Opc = Pattern == StridedLoadKind::Even ? HexagonISD::VPACKE
2350 : HexagonISD::VPACKO;
2351 return DAG.getNode(Opc, dl, VT, {Op.getOperand(1), Op.getOperand(0)});
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002352 }
2353 // We used to assert in the "else" part here, but that is bad for Halide
2354 // Halide creates intermediate double registers by interleaving two
2355 // concatenated vector registers. The interleaving requires vector_shuffle
2356 // nodes and we shouldn't barf on a double register result of a
2357 // vector_shuffle because it is most likely an intermediate result.
2358 }
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002359 // FIXME: We need to support more general vector shuffles. See
2360 // below the comment from the ARM backend that deals in the general
2361 // case with the vector shuffles. For now, let expand handle these.
2362 return SDValue();
2363
2364 // If the shuffle is not directly supported and it has 4 elements, use
2365 // the PerfectShuffle-generated table to synthesize it from other shuffles.
2366}
2367
2368// If BUILD_VECTOR has same base element repeated several times,
2369// report true.
2370static bool isCommonSplatElement(BuildVectorSDNode *BVN) {
2371 unsigned NElts = BVN->getNumOperands();
2372 SDValue V0 = BVN->getOperand(0);
2373
2374 for (unsigned i = 1, e = NElts; i != e; ++i) {
2375 if (BVN->getOperand(i) != V0)
2376 return false;
2377 }
2378 return true;
2379}
2380
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002381// Lower a vector shift. Try to convert
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002382// <VT> = SHL/SRA/SRL <VT> by <VT> to Hexagon specific
2383// <VT> = SHL/SRA/SRL <VT> by <IT/i32>.
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002384SDValue
2385HexagonTargetLowering::LowerVECTOR_SHIFT(SDValue Op, SelectionDAG &DAG) const {
Eugene Zelenko58655bb2016-12-17 01:09:05 +00002386 BuildVectorSDNode *BVN = nullptr;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002387 SDValue V1 = Op.getOperand(0);
2388 SDValue V2 = Op.getOperand(1);
2389 SDValue V3;
2390 SDLoc dl(Op);
2391 EVT VT = Op.getValueType();
2392
2393 if ((BVN = dyn_cast<BuildVectorSDNode>(V1.getNode())) &&
2394 isCommonSplatElement(BVN))
2395 V3 = V2;
2396 else if ((BVN = dyn_cast<BuildVectorSDNode>(V2.getNode())) &&
2397 isCommonSplatElement(BVN))
2398 V3 = V1;
2399 else
2400 return SDValue();
2401
2402 SDValue CommonSplat = BVN->getOperand(0);
2403 SDValue Result;
2404
2405 if (VT.getSimpleVT() == MVT::v4i16) {
2406 switch (Op.getOpcode()) {
2407 case ISD::SRA:
Krzysztof Parzyszekf85dd9f2017-07-10 20:16:44 +00002408 Result = DAG.getNode(HexagonISD::VASR, dl, VT, V3, CommonSplat);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002409 break;
2410 case ISD::SHL:
Krzysztof Parzyszekf85dd9f2017-07-10 20:16:44 +00002411 Result = DAG.getNode(HexagonISD::VASL, dl, VT, V3, CommonSplat);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002412 break;
2413 case ISD::SRL:
Krzysztof Parzyszekf85dd9f2017-07-10 20:16:44 +00002414 Result = DAG.getNode(HexagonISD::VLSR, dl, VT, V3, CommonSplat);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002415 break;
2416 default:
2417 return SDValue();
2418 }
2419 } else if (VT.getSimpleVT() == MVT::v2i32) {
2420 switch (Op.getOpcode()) {
2421 case ISD::SRA:
Krzysztof Parzyszekf85dd9f2017-07-10 20:16:44 +00002422 Result = DAG.getNode(HexagonISD::VASR, dl, VT, V3, CommonSplat);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002423 break;
2424 case ISD::SHL:
Krzysztof Parzyszekf85dd9f2017-07-10 20:16:44 +00002425 Result = DAG.getNode(HexagonISD::VASL, dl, VT, V3, CommonSplat);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002426 break;
2427 case ISD::SRL:
Krzysztof Parzyszekf85dd9f2017-07-10 20:16:44 +00002428 Result = DAG.getNode(HexagonISD::VLSR, dl, VT, V3, CommonSplat);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002429 break;
2430 default:
2431 return SDValue();
2432 }
2433 } else {
2434 return SDValue();
2435 }
2436
2437 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
2438}
2439
2440SDValue
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002441HexagonTargetLowering::buildVector32(ArrayRef<SDValue> Elem, const SDLoc &dl,
2442 MVT VecTy, SelectionDAG &DAG) const {
2443 MVT ElemTy = VecTy.getVectorElementType();
2444 assert(VecTy.getVectorNumElements() == Elem.size());
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002445
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002446 SmallVector<ConstantSDNode*,4> Consts;
2447 bool AllConst = true;
2448 for (SDValue V : Elem) {
2449 if (V.getOpcode() == ISD::UNDEF)
2450 V = DAG.getConstant(0, dl, ElemTy);
2451 auto *C = dyn_cast<ConstantSDNode>(V.getNode());
2452 Consts.push_back(C);
2453 AllConst = AllConst && C != nullptr;
2454 }
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002455
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002456 unsigned First, Num = Elem.size();
2457 for (First = 0; First != Num; ++First)
2458 if (Elem[First].getOpcode() != ISD::UNDEF)
2459 break;
2460 if (First == Num)
2461 return DAG.getUNDEF(VecTy);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002462
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002463 if (ElemTy == MVT::i16) {
2464 assert(Elem.size() == 2);
2465 if (AllConst) {
2466 uint32_t V = (Consts[0]->getZExtValue() & 0xFFFF) |
2467 Consts[1]->getZExtValue() << 16;
2468 return DAG.getBitcast(MVT::v2i16, DAG.getConstant(V, dl, MVT::i32));
Krzysztof Parzyszek89b2d7c2017-07-13 18:17:58 +00002469 }
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002470 SDNode *N = DAG.getMachineNode(Hexagon::A2_combine_ll, dl, MVT::i32,
2471 { Elem[1], Elem[0] });
2472 return DAG.getBitcast(MVT::v2i16, SDValue(N,0));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002473 }
2474
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002475 // First try generating a constant.
2476 assert(ElemTy == MVT::i8 && Num == 4);
2477 if (AllConst) {
2478 int32_t V = (Consts[0]->getZExtValue() & 0xFF) |
2479 (Consts[1]->getZExtValue() & 0xFF) << 8 |
2480 (Consts[1]->getZExtValue() & 0xFF) << 16 |
2481 Consts[2]->getZExtValue() << 24;
2482 return DAG.getBitcast(MVT::v4i8, DAG.getConstant(V, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002483 }
2484
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002485 // Then try splat.
2486 bool IsSplat = true;
2487 for (unsigned i = 0; i != Num; ++i) {
2488 if (i == First)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002489 continue;
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002490 if (Elem[i] == Elem[First] || Elem[i].getOpcode() == ISD::UNDEF)
2491 continue;
2492 IsSplat = false;
2493 break;
2494 }
2495 if (IsSplat)
2496 return DAG.getNode(HexagonISD::VSPLAT, dl, VecTy, Elem[First]);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002497
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002498 // Generate
2499 // (zxtb(Elem[0]) | (zxtb(Elem[1]) << 8)) |
2500 // (zxtb(Elem[2]) | (zxtb(Elem[3]) << 8)) << 16
2501 SDValue S8 = DAG.getConstant(8, dl, MVT::i32);
Krzysztof Parzyszek081e4582017-11-28 19:13:17 +00002502 SDValue V0 = DAG.getZeroExtendInReg(Elem[0], dl, MVT::i8);
2503 SDValue V1 = DAG.getZeroExtendInReg(Elem[1], dl, MVT::i8);
2504 SDValue V2 = DAG.getZeroExtendInReg(Elem[2], dl, MVT::i8);
2505 SDValue V3 = DAG.getZeroExtendInReg(Elem[3], dl, MVT::i8);
2506
2507 SDValue V4 = DAG.getNode(ISD::SHL, dl, MVT::i32, {V1, S8});
2508 SDValue V5 = DAG.getNode(ISD::SHL, dl, MVT::i32, {V3, S8});
2509 SDValue V6 = DAG.getNode(ISD::OR, dl, MVT::i32, {V0, V4});
2510 SDValue V7 = DAG.getNode(ISD::OR, dl, MVT::i32, {V2, V5});
2511 SDNode *T0 = DAG.getMachineNode(Hexagon::A2_combine_ll, dl, MVT::i32,
2512 {V7, V6});
2513 return DAG.getBitcast(MVT::v4i8, SDValue(T0,0));
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002514}
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002515
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002516SDValue
2517HexagonTargetLowering::buildVector64(ArrayRef<SDValue> Elem, const SDLoc &dl,
2518 MVT VecTy, SelectionDAG &DAG) const {
2519 MVT ElemTy = VecTy.getVectorElementType();
2520 assert(VecTy.getVectorNumElements() == Elem.size());
2521
2522 SmallVector<ConstantSDNode*,8> Consts;
2523 bool AllConst = true;
2524 for (SDValue V : Elem) {
2525 if (V.getOpcode() == ISD::UNDEF)
2526 V = DAG.getConstant(0, dl, ElemTy);
2527 auto *C = dyn_cast<ConstantSDNode>(V.getNode());
2528 Consts.push_back(C);
2529 AllConst = AllConst && C != nullptr;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002530 }
2531
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002532 unsigned First, Num = Elem.size();
2533 for (First = 0; First != Num; ++First)
2534 if (Elem[First].getOpcode() != ISD::UNDEF)
2535 break;
2536 if (First == Num)
2537 return DAG.getUNDEF(VecTy);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002538
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002539 // First try splat if possible.
2540 if (ElemTy == MVT::i16) {
2541 bool IsSplat = true;
2542 for (unsigned i = 0; i != Num; ++i) {
2543 if (i == First)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002544 continue;
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002545 if (Elem[i] == Elem[First] || Elem[i].getOpcode() == ISD::UNDEF)
2546 continue;
2547 IsSplat = false;
2548 break;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002549 }
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002550 if (IsSplat)
2551 return DAG.getNode(HexagonISD::VSPLAT, dl, VecTy, Elem[First]);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002552 }
2553
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002554 // Then try constant.
2555 if (AllConst) {
2556 uint64_t Val = 0;
2557 unsigned W = ElemTy.getSizeInBits();
2558 uint64_t Mask = (ElemTy == MVT::i8) ? 0xFFull
2559 : (ElemTy == MVT::i16) ? 0xFFFFull : 0xFFFFFFFFull;
2560 for (unsigned i = 0; i != Num; ++i)
2561 Val = (Val << W) | (Consts[i]->getZExtValue() & Mask);
2562 SDValue V0 = DAG.getConstant(Val, dl, MVT::i64);
2563 return DAG.getBitcast(VecTy, V0);
2564 }
2565
2566 // Build two 32-bit vectors and concatenate.
2567 MVT HalfTy = MVT::getVectorVT(ElemTy, Num/2);
2568 SDValue L = (ElemTy == MVT::i32)
2569 ? Elem[0]
2570 : buildVector32({Elem.data(), Num/2}, dl, HalfTy, DAG);
2571 SDValue H = (ElemTy == MVT::i32)
2572 ? Elem[1]
2573 : buildVector32({Elem.data()+Num/2, Num/2}, dl, HalfTy, DAG);
2574 unsigned Id = Hexagon::DoubleRegsRegClassID;
2575 SDNode *N = DAG.getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VecTy,
2576 { DAG.getTargetConstant(Id, dl, MVT::i32),
2577 L, DAG.getTargetConstant(Hexagon::isub_lo, dl, MVT::i32),
2578 H, DAG.getTargetConstant(Hexagon::isub_hi, dl, MVT::i32) });
2579 return SDValue(N, 0);
2580}
2581
2582SDValue
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002583HexagonTargetLowering::extractVector(SDValue VecV, SDValue IdxV,
2584 const SDLoc &dl, MVT ValTy, MVT ResTy,
2585 SelectionDAG &DAG) const {
2586 MVT VecTy = ty(VecV);
2587 assert(!ValTy.isVector() ||
2588 VecTy.getVectorElementType() == ValTy.getVectorElementType());
2589 unsigned VecWidth = VecTy.getSizeInBits();
2590 unsigned ValWidth = ValTy.getSizeInBits();
2591 unsigned ElemWidth = VecTy.getVectorElementType().getSizeInBits();
2592 assert(VecWidth == 32 || VecWidth == 64);
2593 assert((VecWidth % ElemWidth) == 0);
2594
2595 // Cast everything to scalar integer types.
2596 MVT ScalarTy = tyScalar(VecTy);
2597 VecV = DAG.getBitcast(ScalarTy, VecV);
2598
2599 SDValue WidthV = DAG.getConstant(ValWidth, dl, MVT::i32);
2600 SDValue ExtV;
2601
2602 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(IdxV)) {
2603 unsigned Off = C->getZExtValue() * ElemWidth;
2604 if (VecWidth == 64 && ValWidth == 32) {
2605 assert(Off == 0 || Off == 32);
2606 unsigned SubIdx = Off == 0 ? Hexagon::isub_lo : Hexagon::isub_hi;
2607 ExtV = DAG.getTargetExtractSubreg(SubIdx, dl, MVT::i32, VecV);
2608 } else if (Off == 0 && (ValWidth % 8) == 0) {
2609 ExtV = DAG.getZeroExtendInReg(VecV, dl, tyScalar(ValTy));
2610 } else {
2611 SDValue OffV = DAG.getConstant(Off, dl, MVT::i32);
2612 // The return type of EXTRACTU must be the same as the type of the
2613 // input vector.
2614 ExtV = DAG.getNode(HexagonISD::EXTRACTU, dl, ScalarTy,
2615 {VecV, WidthV, OffV});
2616 }
2617 } else {
2618 if (ty(IdxV) != MVT::i32)
2619 IdxV = DAG.getZExtOrTrunc(IdxV, dl, MVT::i32);
2620 SDValue OffV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
2621 DAG.getConstant(ElemWidth, dl, MVT::i32));
2622 // EXTRACTURP takes width/offset in a 64-bit pair.
2623 SDValue CombV = DAG.getNode(HexagonISD::COMBINE, dl, MVT::i64,
2624 {WidthV, OffV});
2625 ExtV = DAG.getNode(HexagonISD::EXTRACTURP, dl, ScalarTy,
2626 {VecV, CombV});
2627 }
2628
2629 // Cast ExtV to the requested result type.
2630 ExtV = DAG.getZExtOrTrunc(ExtV, dl, tyScalar(ResTy));
2631 ExtV = DAG.getBitcast(ResTy, ExtV);
2632 return ExtV;
2633}
2634
2635SDValue
2636HexagonTargetLowering::insertVector(SDValue VecV, SDValue ValV, SDValue IdxV,
2637 const SDLoc &dl, MVT ValTy,
2638 SelectionDAG &DAG) const {
2639 MVT VecTy = ty(VecV);
2640 unsigned VecWidth = VecTy.getSizeInBits();
2641 unsigned ValWidth = ValTy.getSizeInBits();
2642 assert(VecWidth == 32 || VecWidth == 64);
2643 assert((VecWidth % ValWidth) == 0);
2644
2645 // Cast everything to scalar integer types.
2646 MVT ScalarTy = MVT::getIntegerVT(VecWidth);
2647 // The actual type of ValV may be different than ValTy (which is related
2648 // to the vector type).
2649 unsigned VW = ty(ValV).getSizeInBits();
2650 ValV = DAG.getBitcast(MVT::getIntegerVT(VW), ValV);
2651 VecV = DAG.getBitcast(ScalarTy, VecV);
2652 if (VW != VecWidth)
2653 ValV = DAG.getAnyExtOrTrunc(ValV, dl, ScalarTy);
2654
2655 SDValue WidthV = DAG.getConstant(ValWidth, dl, MVT::i32);
2656 SDValue InsV;
2657
2658 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(IdxV)) {
2659 unsigned W = C->getZExtValue() * ValWidth;
2660 SDValue OffV = DAG.getConstant(W, dl, MVT::i32);
2661 InsV = DAG.getNode(HexagonISD::INSERT, dl, ScalarTy,
2662 {VecV, ValV, WidthV, OffV});
2663 } else {
2664 if (ty(IdxV) != MVT::i32)
2665 IdxV = DAG.getZExtOrTrunc(IdxV, dl, MVT::i32);
2666 SDValue OffV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV, WidthV);
2667 // INSERTRP takes width/offset in a 64-bit pair.
2668 SDValue CombV = DAG.getNode(HexagonISD::COMBINE, dl, MVT::i64,
2669 {WidthV, OffV});
2670 InsV = DAG.getNode(HexagonISD::INSERTRP, dl, ScalarTy,
2671 {VecV, ValV, CombV});
2672 }
2673
2674 return DAG.getNode(ISD::BITCAST, dl, VecTy, InsV);
2675}
2676
2677SDValue
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002678HexagonTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
2679 MVT VT = Op.getValueType().getSimpleVT();
2680 unsigned BW = VT.getSizeInBits();
2681 if (BW == 32 || BW == 64) {
2682 SmallVector<SDValue,8> Ops;
2683 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i)
2684 Ops.push_back(Op.getOperand(i));
2685 if (BW == 32)
2686 return buildVector32(Ops, SDLoc(Op), VT, DAG);
2687 return buildVector64(Ops, SDLoc(Op), VT, DAG);
2688 }
2689
2690 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002691}
2692
2693SDValue
2694HexagonTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
2695 SelectionDAG &DAG) const {
2696 SDLoc dl(Op);
Krzysztof Parzyszekc168c012015-12-03 16:47:20 +00002697 bool UseHVX = Subtarget.useHVXOps();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002698 EVT VT = Op.getValueType();
2699 unsigned NElts = Op.getNumOperands();
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002700 SDValue Vec0 = Op.getOperand(0);
2701 EVT VecVT = Vec0.getValueType();
2702 unsigned Width = VecVT.getSizeInBits();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002703
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002704 if (NElts == 2) {
2705 MVT ST = VecVT.getSimpleVT();
2706 // We are trying to concat two v2i16 to a single v4i16, or two v4i8
2707 // into a single v8i8.
2708 if (ST == MVT::v2i16 || ST == MVT::v4i8)
2709 return DAG.getNode(HexagonISD::COMBINE, dl, VT, Op.getOperand(1), Vec0);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002710
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002711 if (UseHVX) {
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +00002712 assert((Width == 64 * 8 && Subtarget.useHVX64BOps()) ||
2713 (Width == 128 * 8 && Subtarget.useHVX128BOps()));
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002714 SDValue Vec1 = Op.getOperand(1);
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +00002715 MVT OpTy = Subtarget.useHVX64BOps() ? MVT::v16i32 : MVT::v32i32;
2716 MVT ReTy = Subtarget.useHVX64BOps() ? MVT::v32i32 : MVT::v64i32;
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002717 SDValue B0 = DAG.getNode(ISD::BITCAST, dl, OpTy, Vec0);
2718 SDValue B1 = DAG.getNode(ISD::BITCAST, dl, OpTy, Vec1);
2719 SDValue VC = DAG.getNode(HexagonISD::VCOMBINE, dl, ReTy, B1, B0);
2720 return DAG.getNode(ISD::BITCAST, dl, VT, VC);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002721 }
2722 }
2723
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002724 if (VT.getSizeInBits() != 32 && VT.getSizeInBits() != 64)
2725 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002726
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002727 SDValue C0 = DAG.getConstant(0, dl, MVT::i64);
2728 SDValue C32 = DAG.getConstant(32, dl, MVT::i64);
2729 SDValue W = DAG.getConstant(Width, dl, MVT::i64);
2730 // Create the "width" part of the argument to insert_rp/insertp_rp.
2731 SDValue S = DAG.getNode(ISD::SHL, dl, MVT::i64, W, C32);
2732 SDValue V = C0;
2733
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002734 for (unsigned i = 0, e = NElts; i != e; ++i) {
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002735 unsigned N = NElts-i-1;
2736 SDValue OpN = Op.getOperand(N);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002737
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00002738 if (VT.getSizeInBits() == 64 && OpN.getValueSizeInBits() == 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002739 SDValue C = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002740 OpN = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, OpN);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002741 }
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002742 SDValue Idx = DAG.getConstant(N, dl, MVT::i64);
2743 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i64, Idx, W);
2744 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, S, Offset);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002745 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002746 V = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, {V, OpN, Or});
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002747 else if (VT.getSizeInBits() == 64)
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002748 V = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, {V, OpN, Or});
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002749 else
2750 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002751 }
2752
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002753 return DAG.getNode(ISD::BITCAST, dl, VT, V);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002754}
2755
2756SDValue
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002757HexagonTargetLowering::LowerEXTRACT_SUBVECTOR_HVX(SDValue Op,
2758 SelectionDAG &DAG) const {
2759 EVT VT = Op.getOperand(0).getValueType();
2760 SDLoc dl(Op);
2761 bool UseHVX = Subtarget.useHVXOps();
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +00002762 bool UseHVX64B = Subtarget.useHVX64BOps();
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002763 // Just in case...
2764
2765 if (!VT.isVector() || !UseHVX)
2766 return SDValue();
2767
2768 EVT ResVT = Op.getValueType();
2769 unsigned ResSize = ResVT.getSizeInBits();
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +00002770 unsigned VectorSizeInBits = UseHVX64B ? (64 * 8) : (128 * 8);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002771 unsigned OpSize = VT.getSizeInBits();
2772
2773 // We deal only with cases where the result is the vector size
2774 // and the vector operand is a double register.
2775 if (!(ResVT.isByteSized() && ResSize == VectorSizeInBits) ||
2776 !(VT.isByteSized() && OpSize == 2 * VectorSizeInBits))
2777 return SDValue();
2778
2779 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Op.getOperand(1));
2780 if (!Cst)
2781 return SDValue();
2782 unsigned Val = Cst->getZExtValue();
2783
2784 // These two will get lowered to an appropriate EXTRACT_SUBREG in ISel.
2785 if (Val == 0) {
2786 SDValue Vec = Op.getOperand(0);
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00002787 return DAG.getTargetExtractSubreg(Hexagon::vsub_lo, dl, ResVT, Vec);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002788 }
2789
2790 if (ResVT.getVectorNumElements() == Val) {
2791 SDValue Vec = Op.getOperand(0);
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00002792 return DAG.getTargetExtractSubreg(Hexagon::vsub_hi, dl, ResVT, Vec);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002793 }
2794
2795 return SDValue();
2796}
2797
2798SDValue
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002799HexagonTargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
2800 SelectionDAG &DAG) const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002801 SDValue Vec = Op.getOperand(0);
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002802 MVT ElemTy = ty(Vec).getVectorElementType();
2803 return extractVector(Vec, Op.getOperand(1), SDLoc(Op), ElemTy, ty(Op), DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002804}
2805
2806SDValue
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002807HexagonTargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op,
2808 SelectionDAG &DAG) const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002809 SDValue Vec = Op.getOperand(0);
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002810 MVT VecTy = ty(Vec);
2811 if (Subtarget.useHVXOps() && Subtarget.isHVXVectorType(VecTy))
2812 return LowerEXTRACT_SUBVECTOR_HVX(Op, DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002813
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002814 return extractVector(Vec, Op.getOperand(1), SDLoc(Op), ty(Op), ty(Op), DAG);
2815}
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002816
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002817SDValue
2818HexagonTargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op,
2819 SelectionDAG &DAG) const {
2820 MVT VecTy = ty(Op);
2821 return insertVector(Op.getOperand(0), Op.getOperand(1), Op.getOperand(2),
2822 SDLoc(Op), VecTy.getVectorElementType(), DAG);
2823}
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002824
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002825SDValue
2826HexagonTargetLowering::LowerINSERT_SUBVECTOR(SDValue Op,
2827 SelectionDAG &DAG) const {
2828 SDValue ValV = Op.getOperand(1);
2829 return insertVector(Op.getOperand(0), ValV, Op.getOperand(2),
2830 SDLoc(Op), ty(ValV), DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002831}
2832
Tim Northovera4415852013-08-06 09:12:35 +00002833bool
2834HexagonTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
2835 // Assuming the caller does not have either a signext or zeroext modifier, and
2836 // only one value is accepted, any reasonable truncation is allowed.
2837 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
2838 return false;
2839
2840 // FIXME: in principle up to 64-bit could be made safe, but it would be very
2841 // fragile at the moment: any support for multiple value returns would be
2842 // liable to disallow tail calls involving i64 -> iN truncation in many cases.
2843 return Ty1->getPrimitiveSizeInBits() <= 32;
2844}
2845
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002846SDValue
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002847HexagonTargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
2848 SDValue Chain = Op.getOperand(0);
2849 SDValue Offset = Op.getOperand(1);
2850 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002851 SDLoc dl(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002852 auto PtrVT = getPointerTy(DAG.getDataLayout());
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002853
2854 // Mark function as containing a call to EH_RETURN.
2855 HexagonMachineFunctionInfo *FuncInfo =
2856 DAG.getMachineFunction().getInfo<HexagonMachineFunctionInfo>();
2857 FuncInfo->setHasEHReturn();
2858
2859 unsigned OffsetReg = Hexagon::R28;
2860
Mehdi Amini44ede332015-07-09 02:09:04 +00002861 SDValue StoreAddr =
2862 DAG.getNode(ISD::ADD, dl, PtrVT, DAG.getRegister(Hexagon::R30, PtrVT),
2863 DAG.getIntPtrConstant(4, dl));
Justin Lebar9c375812016-07-15 18:27:10 +00002864 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo());
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002865 Chain = DAG.getCopyToReg(Chain, dl, OffsetReg, Offset);
2866
2867 // Not needed we already use it as explict input to EH_RETURN.
2868 // MF.getRegInfo().addLiveOut(OffsetReg);
2869
2870 return DAG.getNode(HexagonISD::EH_RETURN, dl, MVT::Other, Chain);
2871}
2872
2873SDValue
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002874HexagonTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002875 unsigned Opc = Op.getOpcode();
2876 switch (Opc) {
2877 default:
2878#ifndef NDEBUG
2879 Op.getNode()->dumpr(&DAG);
2880 if (Opc > HexagonISD::OP_BEGIN && Opc < HexagonISD::OP_END)
2881 errs() << "Check for a non-legal type in this operation\n";
2882#endif
2883 llvm_unreachable("Should not custom lower this!");
2884 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002885 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
2886 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
2887 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
2888 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002889 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2890 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002891 case ISD::SRA:
2892 case ISD::SHL:
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002893 case ISD::SRL: return LowerVECTOR_SHIFT(Op, DAG);
2894 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002895 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002896 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
2897 // Frame & Return address. Currently unimplemented.
2898 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
2899 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00002900 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002901 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
2902 case ISD::GlobalAddress: return LowerGLOBALADDRESS(Op, DAG);
2903 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002904 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002905 case ISD::VASTART: return LowerVASTART(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002906 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
2907 case ISD::SETCC: return LowerSETCC(Op, DAG);
2908 case ISD::VSELECT: return LowerVSELECT(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002909 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00002910 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002911 case ISD::INLINEASM: return LowerINLINEASM(Op, DAG);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00002912 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG);
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +00002913 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002914 }
2915}
2916
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002917/// Returns relocation base for the given PIC jumptable.
2918SDValue
2919HexagonTargetLowering::getPICJumpTableRelocBase(SDValue Table,
2920 SelectionDAG &DAG) const {
2921 int Idx = cast<JumpTableSDNode>(Table)->getIndex();
2922 EVT VT = Table.getValueType();
2923 SDValue T = DAG.getTargetJumpTable(Idx, VT, HexagonII::MO_PCREL);
2924 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Table), VT, T);
2925}
2926
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002927//===----------------------------------------------------------------------===//
2928// Inline Assembly Support
2929//===----------------------------------------------------------------------===//
2930
Krzysztof Parzyszekca3b5322016-05-18 14:34:51 +00002931TargetLowering::ConstraintType
2932HexagonTargetLowering::getConstraintType(StringRef Constraint) const {
2933 if (Constraint.size() == 1) {
2934 switch (Constraint[0]) {
2935 case 'q':
2936 case 'v':
2937 if (Subtarget.useHVXOps())
Krzysztof Parzyszek3ad0d012017-07-21 17:51:27 +00002938 return C_RegisterClass;
2939 break;
2940 case 'a':
2941 return C_RegisterClass;
2942 default:
Krzysztof Parzyszekca3b5322016-05-18 14:34:51 +00002943 break;
2944 }
2945 }
2946 return TargetLowering::getConstraintType(Constraint);
2947}
2948
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002949std::pair<unsigned, const TargetRegisterClass*>
Eric Christopher11e4df72015-02-26 22:38:43 +00002950HexagonTargetLowering::getRegForInlineAsmConstraint(
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00002951 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002952
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002953 if (Constraint.size() == 1) {
2954 switch (Constraint[0]) {
2955 case 'r': // R0-R31
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002956 switch (VT.SimpleTy) {
2957 default:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002958 return {0u, nullptr};
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002959 case MVT::i1:
2960 case MVT::i8:
2961 case MVT::i16:
2962 case MVT::i32:
2963 case MVT::f32:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002964 return {0u, &Hexagon::IntRegsRegClass};
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002965 case MVT::i64:
2966 case MVT::f64:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002967 return {0u, &Hexagon::DoubleRegsRegClass};
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002968 }
Krzysztof Parzyszek3ad0d012017-07-21 17:51:27 +00002969 break;
2970 case 'a': // M0-M1
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002971 if (VT != MVT::i32)
2972 return {0u, nullptr};
2973 return {0u, &Hexagon::ModRegsRegClass};
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002974 case 'q': // q0-q3
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002975 switch (VT.getSizeInBits()) {
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002976 default:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002977 return {0u, nullptr};
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002978 case 512:
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002979 case 1024:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002980 return {0u, &Hexagon::HvxQRRegClass};
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002981 }
Krzysztof Parzyszek3ad0d012017-07-21 17:51:27 +00002982 break;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002983 case 'v': // V0-V31
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002984 switch (VT.getSizeInBits()) {
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002985 default:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002986 return {0u, nullptr};
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002987 case 512:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002988 return {0u, &Hexagon::HvxVRRegClass};
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002989 case 1024:
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +00002990 if (Subtarget.hasV60TOps() && Subtarget.useHVX128BOps())
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002991 return {0u, &Hexagon::HvxVRRegClass};
2992 return {0u, &Hexagon::HvxWRRegClass};
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002993 case 2048:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002994 return {0u, &Hexagon::HvxWRRegClass};
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002995 }
Krzysztof Parzyszek3ad0d012017-07-21 17:51:27 +00002996 break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002997 default:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002998 return {0u, nullptr};
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002999 }
3000 }
3001
Eric Christopher11e4df72015-02-26 22:38:43 +00003002 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003003}
3004
Sirish Pande69295b82012-05-10 20:20:25 +00003005/// isFPImmLegal - Returns true if the target can instruction select the
3006/// specified FP immediate natively. If false, the legalizer will
3007/// materialize the FP immediate as a load from a constant pool.
3008bool HexagonTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00003009 return Subtarget.hasV5TOps();
Sirish Pande69295b82012-05-10 20:20:25 +00003010}
3011
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003012/// isLegalAddressingMode - Return true if the addressing mode represented by
3013/// AM is legal for this target, for a load/store of the specified type.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +00003014bool HexagonTargetLowering::isLegalAddressingMode(const DataLayout &DL,
3015 const AddrMode &AM, Type *Ty,
Jonas Paulsson024e3192017-07-21 11:59:37 +00003016 unsigned AS, Instruction *I) const {
Krzysztof Parzyszeked4e7822016-08-03 15:06:18 +00003017 if (Ty->isSized()) {
3018 // When LSR detects uses of the same base address to access different
3019 // types (e.g. unions), it will assume a conservative type for these
3020 // uses:
3021 // LSR Use: Kind=Address of void in addrspace(4294967295), ...
3022 // The type Ty passed here would then be "void". Skip the alignment
3023 // checks, but do not return false right away, since that confuses
3024 // LSR into crashing.
3025 unsigned A = DL.getABITypeAlignment(Ty);
3026 // The base offset must be a multiple of the alignment.
3027 if ((AM.BaseOffs % A) != 0)
3028 return false;
3029 // The shifted offset must fit in 11 bits.
3030 if (!isInt<11>(AM.BaseOffs >> Log2_32(A)))
3031 return false;
3032 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003033
3034 // No global is ever allowed as a base.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00003035 if (AM.BaseGV)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003036 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003037
3038 int Scale = AM.Scale;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00003039 if (Scale < 0)
3040 Scale = -Scale;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003041 switch (Scale) {
3042 case 0: // No scale reg, "r+i", "r", or just "i".
3043 break;
3044 default: // No scaled addressing mode.
3045 return false;
3046 }
3047 return true;
3048}
3049
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00003050/// Return true if folding a constant offset with the given GlobalAddress is
3051/// legal. It is frequently not legal in PIC relocation models.
3052bool HexagonTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA)
3053 const {
3054 return HTM.getRelocationModel() == Reloc::Static;
3055}
3056
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003057/// isLegalICmpImmediate - Return true if the specified immediate is legal
3058/// icmp immediate, that is the target has icmp instructions which can compare
3059/// a register against the immediate without having to materialize the
3060/// immediate into a register.
3061bool HexagonTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
3062 return Imm >= -512 && Imm <= 511;
3063}
3064
3065/// IsEligibleForTailCallOptimization - Check whether the call is eligible
3066/// for tail call optimization. Targets which want to do tail call
3067/// optimization should implement this function.
3068bool HexagonTargetLowering::IsEligibleForTailCallOptimization(
3069 SDValue Callee,
3070 CallingConv::ID CalleeCC,
3071 bool isVarArg,
3072 bool isCalleeStructRet,
3073 bool isCallerStructRet,
3074 const SmallVectorImpl<ISD::OutputArg> &Outs,
3075 const SmallVectorImpl<SDValue> &OutVals,
3076 const SmallVectorImpl<ISD::InputArg> &Ins,
3077 SelectionDAG& DAG) const {
3078 const Function *CallerF = DAG.getMachineFunction().getFunction();
3079 CallingConv::ID CallerCC = CallerF->getCallingConv();
3080 bool CCMatch = CallerCC == CalleeCC;
3081
3082 // ***************************************************************************
3083 // Look for obvious safe cases to perform tail call optimization that do not
3084 // require ABI changes.
3085 // ***************************************************************************
3086
3087 // If this is a tail call via a function pointer, then don't do it!
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00003088 if (!isa<GlobalAddressSDNode>(Callee) &&
3089 !isa<ExternalSymbolSDNode>(Callee)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003090 return false;
3091 }
3092
Krzysztof Parzyszek0ba97542016-08-19 15:02:18 +00003093 // Do not optimize if the calling conventions do not match and the conventions
3094 // used are not C or Fast.
3095 if (!CCMatch) {
3096 bool R = (CallerCC == CallingConv::C || CallerCC == CallingConv::Fast);
3097 bool E = (CalleeCC == CallingConv::C || CalleeCC == CallingConv::Fast);
3098 // If R & E, then ok.
3099 if (!R || !E)
3100 return false;
3101 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003102
3103 // Do not tail call optimize vararg calls.
3104 if (isVarArg)
3105 return false;
3106
3107 // Also avoid tail call optimization if either caller or callee uses struct
3108 // return semantics.
3109 if (isCalleeStructRet || isCallerStructRet)
3110 return false;
3111
3112 // In addition to the cases above, we also disable Tail Call Optimization if
3113 // the calling convention code that at least one outgoing argument needs to
3114 // go on the stack. We cannot check that here because at this point that
3115 // information is not available.
3116 return true;
3117}
Colin LeMahieu025f8602014-12-08 21:19:18 +00003118
Krzysztof Parzyszek3e409e12016-08-02 18:34:31 +00003119/// Returns the target specific optimal type for load and store operations as
3120/// a result of memset, memcpy, and memmove lowering.
3121///
3122/// If DstAlign is zero that means it's safe to destination alignment can
3123/// satisfy any constraint. Similarly if SrcAlign is zero it means there isn't
3124/// a need to check it against alignment requirement, probably because the
3125/// source does not need to be loaded. If 'IsMemset' is true, that means it's
3126/// expanding a memset. If 'ZeroMemset' is true, that means it's a memset of
3127/// zero. 'MemcpyStrSrc' indicates whether the memcpy source is constant so it
3128/// does not need to be loaded. It returns EVT::Other if the type should be
3129/// determined using generic target-independent logic.
3130EVT HexagonTargetLowering::getOptimalMemOpType(uint64_t Size,
3131 unsigned DstAlign, unsigned SrcAlign, bool IsMemset, bool ZeroMemset,
3132 bool MemcpyStrSrc, MachineFunction &MF) const {
3133
3134 auto Aligned = [](unsigned GivenA, unsigned MinA) -> bool {
3135 return (GivenA % MinA) == 0;
3136 };
3137
3138 if (Size >= 8 && Aligned(DstAlign, 8) && (IsMemset || Aligned(SrcAlign, 8)))
3139 return MVT::i64;
3140 if (Size >= 4 && Aligned(DstAlign, 4) && (IsMemset || Aligned(SrcAlign, 4)))
3141 return MVT::i32;
3142 if (Size >= 2 && Aligned(DstAlign, 2) && (IsMemset || Aligned(SrcAlign, 2)))
3143 return MVT::i16;
3144
3145 return MVT::Other;
3146}
3147
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +00003148bool HexagonTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
3149 unsigned AS, unsigned Align, bool *Fast) const {
3150 if (Fast)
3151 *Fast = false;
3152
3153 switch (VT.getSimpleVT().SimpleTy) {
3154 default:
3155 return false;
3156 case MVT::v64i8:
3157 case MVT::v128i8:
3158 case MVT::v256i8:
3159 case MVT::v32i16:
3160 case MVT::v64i16:
3161 case MVT::v128i16:
3162 case MVT::v16i32:
3163 case MVT::v32i32:
3164 case MVT::v64i32:
3165 case MVT::v8i64:
3166 case MVT::v16i64:
3167 case MVT::v32i64:
3168 return true;
3169 }
3170 return false;
3171}
3172
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003173std::pair<const TargetRegisterClass*, uint8_t>
3174HexagonTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
3175 MVT VT) const {
3176 const TargetRegisterClass *RRC = nullptr;
3177
3178 uint8_t Cost = 1;
3179 switch (VT.SimpleTy) {
3180 default:
3181 return TargetLowering::findRepresentativeClass(TRI, VT);
3182 case MVT::v64i8:
3183 case MVT::v32i16:
3184 case MVT::v16i32:
3185 case MVT::v8i64:
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00003186 RRC = &Hexagon::HvxVRRegClass;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003187 break;
3188 case MVT::v128i8:
3189 case MVT::v64i16:
3190 case MVT::v32i32:
3191 case MVT::v16i64:
3192 if (Subtarget.hasV60TOps() && Subtarget.useHVXOps() &&
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +00003193 Subtarget.useHVX128BOps())
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00003194 RRC = &Hexagon::HvxVRRegClass;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003195 else
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00003196 RRC = &Hexagon::HvxWRRegClass;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003197 break;
3198 case MVT::v256i8:
3199 case MVT::v128i16:
3200 case MVT::v64i32:
3201 case MVT::v32i64:
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00003202 RRC = &Hexagon::HvxWRRegClass;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003203 break;
3204 }
3205 return std::make_pair(RRC, Cost);
3206}
3207
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003208Value *HexagonTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
3209 AtomicOrdering Ord) const {
3210 BasicBlock *BB = Builder.GetInsertBlock();
3211 Module *M = BB->getParent()->getParent();
3212 Type *Ty = cast<PointerType>(Addr->getType())->getElementType();
3213 unsigned SZ = Ty->getPrimitiveSizeInBits();
3214 assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic loads supported");
3215 Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_L2_loadw_locked
3216 : Intrinsic::hexagon_L4_loadd_locked;
3217 Value *Fn = Intrinsic::getDeclaration(M, IntID);
3218 return Builder.CreateCall(Fn, Addr, "larx");
3219}
3220
3221/// Perform a store-conditional operation to Addr. Return the status of the
3222/// store. This should be 0 if the store succeeded, non-zero otherwise.
3223Value *HexagonTargetLowering::emitStoreConditional(IRBuilder<> &Builder,
3224 Value *Val, Value *Addr, AtomicOrdering Ord) const {
3225 BasicBlock *BB = Builder.GetInsertBlock();
3226 Module *M = BB->getParent()->getParent();
3227 Type *Ty = Val->getType();
3228 unsigned SZ = Ty->getPrimitiveSizeInBits();
3229 assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic stores supported");
3230 Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_S2_storew_locked
3231 : Intrinsic::hexagon_S4_stored_locked;
3232 Value *Fn = Intrinsic::getDeclaration(M, IntID);
3233 Value *Call = Builder.CreateCall(Fn, {Addr, Val}, "stcx");
3234 Value *Cmp = Builder.CreateICmpEQ(Call, Builder.getInt32(0), "");
3235 Value *Ext = Builder.CreateZExt(Cmp, Type::getInt32Ty(M->getContext()));
3236 return Ext;
3237}
3238
Ahmed Bougacha52468672015-09-11 17:08:28 +00003239TargetLowering::AtomicExpansionKind
3240HexagonTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003241 // Do not expand loads and stores that don't exceed 64 bits.
Ahmed Bougacha52468672015-09-11 17:08:28 +00003242 return LI->getType()->getPrimitiveSizeInBits() > 64
Tim Northoverf520eff2015-12-02 18:12:57 +00003243 ? AtomicExpansionKind::LLOnly
Ahmed Bougacha52468672015-09-11 17:08:28 +00003244 : AtomicExpansionKind::None;
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003245}
3246
3247bool HexagonTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
3248 // Do not expand loads and stores that don't exceed 64 bits.
3249 return SI->getValueOperand()->getType()->getPrimitiveSizeInBits() > 64;
3250}
Krzysztof Parzyszekf228c952016-06-22 16:07:10 +00003251
3252bool HexagonTargetLowering::shouldExpandAtomicCmpXchgInIR(
3253 AtomicCmpXchgInst *AI) const {
3254 const DataLayout &DL = AI->getModule()->getDataLayout();
3255 unsigned Size = DL.getTypeStoreSize(AI->getCompareOperand()->getType());
3256 return Size >= 4 && Size <= 8;
3257}