blob: 718e09ac57cd19d1963787d437a5704d3f5de4e3 [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
Krzysztof Parzyszek708c9f52017-12-14 18:35:24 +0000132static const MVT LegalV64[] = { MVT::v64i8, MVT::v32i16, MVT::v16i32 };
133static const MVT LegalW64[] = { MVT::v128i8, MVT::v64i16, MVT::v32i32 };
134static const MVT LegalV128[] = { MVT::v128i8, MVT::v64i16, MVT::v32i32 };
135static const MVT LegalW128[] = { MVT::v256i8, MVT::v128i16, MVT::v64i32 };
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000136
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000137static bool
138CC_Hexagon(unsigned ValNo, MVT ValVT,
139 MVT LocVT, CCValAssign::LocInfo LocInfo,
140 ISD::ArgFlagsTy ArgFlags, CCState &State);
141
142static bool
143CC_Hexagon32(unsigned ValNo, MVT ValVT,
144 MVT LocVT, CCValAssign::LocInfo LocInfo,
145 ISD::ArgFlagsTy ArgFlags, CCState &State);
146
147static bool
148CC_Hexagon64(unsigned ValNo, MVT ValVT,
149 MVT LocVT, CCValAssign::LocInfo LocInfo,
150 ISD::ArgFlagsTy ArgFlags, CCState &State);
151
152static bool
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000153CC_HexagonVector(unsigned ValNo, MVT ValVT,
154 MVT LocVT, CCValAssign::LocInfo LocInfo,
155 ISD::ArgFlagsTy ArgFlags, CCState &State);
156
157static bool
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000158RetCC_Hexagon(unsigned ValNo, MVT ValVT,
159 MVT LocVT, CCValAssign::LocInfo LocInfo,
160 ISD::ArgFlagsTy ArgFlags, CCState &State);
161
162static bool
163RetCC_Hexagon32(unsigned ValNo, MVT ValVT,
164 MVT LocVT, CCValAssign::LocInfo LocInfo,
165 ISD::ArgFlagsTy ArgFlags, CCState &State);
166
167static bool
168RetCC_Hexagon64(unsigned ValNo, MVT ValVT,
169 MVT LocVT, CCValAssign::LocInfo LocInfo,
170 ISD::ArgFlagsTy ArgFlags, CCState &State);
171
172static bool
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000173RetCC_HexagonVector(unsigned ValNo, MVT ValVT,
174 MVT LocVT, CCValAssign::LocInfo LocInfo,
175 ISD::ArgFlagsTy ArgFlags, CCState &State);
176
177static bool
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000178CC_Hexagon_VarArg (unsigned ValNo, MVT ValVT,
179 MVT LocVT, CCValAssign::LocInfo LocInfo,
180 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000181 HexagonCCState &HState = static_cast<HexagonCCState &>(State);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000182
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000183 if (ValNo < HState.getNumNamedVarArgParams()) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000184 // Deal with named arguments.
185 return CC_Hexagon(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State);
186 }
187
188 // Deal with un-named arguments.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000189 unsigned Offset;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000190 if (ArgFlags.isByVal()) {
191 // If pass-by-value, the size allocated on stack is decided
192 // by ArgFlags.getByValSize(), not by the size of LocVT.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000193 Offset = State.AllocateStack(ArgFlags.getByValSize(),
194 ArgFlags.getByValAlign());
195 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000196 return false;
197 }
Jyotsna Vermac7dcc2f2013-03-07 20:28:34 +0000198 if (LocVT == MVT::i1 || LocVT == MVT::i8 || LocVT == MVT::i16) {
199 LocVT = MVT::i32;
200 ValVT = MVT::i32;
201 if (ArgFlags.isSExt())
202 LocInfo = CCValAssign::SExt;
203 else if (ArgFlags.isZExt())
204 LocInfo = CCValAssign::ZExt;
205 else
206 LocInfo = CCValAssign::AExt;
207 }
Sirish Pande69295b82012-05-10 20:20:25 +0000208 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000209 Offset = State.AllocateStack(4, 4);
210 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000211 return false;
212 }
Sirish Pande69295b82012-05-10 20:20:25 +0000213 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000214 Offset = State.AllocateStack(8, 8);
215 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000216 return false;
217 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000218 if (LocVT == MVT::v2i64 || LocVT == MVT::v4i32 || LocVT == MVT::v8i16 ||
219 LocVT == MVT::v16i8) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000220 Offset = State.AllocateStack(16, 16);
221 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000222 return false;
223 }
224 if (LocVT == MVT::v4i64 || LocVT == MVT::v8i32 || LocVT == MVT::v16i16 ||
225 LocVT == MVT::v32i8) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000226 Offset = State.AllocateStack(32, 32);
227 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000228 return false;
229 }
Krzysztof Parzyszek708c9f52017-12-14 18:35:24 +0000230 if (LocVT == MVT::v16i32 || LocVT == MVT::v32i16 ||
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000231 LocVT == MVT::v64i8 || LocVT == MVT::v512i1) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000232 Offset = State.AllocateStack(64, 64);
233 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000234 return false;
235 }
Krzysztof Parzyszek708c9f52017-12-14 18:35:24 +0000236 if (LocVT == MVT::v32i32 || LocVT == MVT::v64i16 ||
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000237 LocVT == MVT::v128i8 || LocVT == MVT::v1024i1) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000238 Offset = State.AllocateStack(128, 128);
239 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000240 return false;
241 }
Krzysztof Parzyszek708c9f52017-12-14 18:35:24 +0000242 if (LocVT == MVT::v64i32 || LocVT == MVT::v128i16 ||
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000243 LocVT == MVT::v256i8) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000244 Offset = State.AllocateStack(256, 256);
245 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000246 return false;
247 }
248
Craig Toppere73658d2014-04-28 04:05:08 +0000249 llvm_unreachable(nullptr);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000250}
251
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000252static bool CC_Hexagon (unsigned ValNo, MVT ValVT, MVT LocVT,
253 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, CCState &State) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000254 if (ArgFlags.isByVal()) {
255 // Passed on stack.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000256 unsigned Offset = State.AllocateStack(ArgFlags.getByValSize(),
257 ArgFlags.getByValAlign());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000258 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
259 return false;
260 }
261
Krzysztof Parzyszek8f23dd62017-03-01 17:30:10 +0000262 if (LocVT == MVT::i1) {
263 LocVT = MVT::i32;
264 } else if (LocVT == MVT::i8 || LocVT == MVT::i16) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000265 LocVT = MVT::i32;
266 ValVT = MVT::i32;
267 if (ArgFlags.isSExt())
268 LocInfo = CCValAssign::SExt;
269 else if (ArgFlags.isZExt())
270 LocInfo = CCValAssign::ZExt;
271 else
272 LocInfo = CCValAssign::AExt;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000273 } else if (LocVT == MVT::v4i8 || LocVT == MVT::v2i16) {
274 LocVT = MVT::i32;
275 LocInfo = CCValAssign::BCvt;
276 } else if (LocVT == MVT::v8i8 || LocVT == MVT::v4i16 || LocVT == MVT::v2i32) {
277 LocVT = MVT::i64;
278 LocInfo = CCValAssign::BCvt;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000279 }
280
Sirish Pande69295b82012-05-10 20:20:25 +0000281 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000282 if (!CC_Hexagon32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
283 return false;
284 }
285
Sirish Pande69295b82012-05-10 20:20:25 +0000286 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000287 if (!CC_Hexagon64(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
288 return false;
289 }
290
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000291 if (LocVT == MVT::v8i32 || LocVT == MVT::v16i16 || LocVT == MVT::v32i8) {
292 unsigned Offset = State.AllocateStack(ArgFlags.getByValSize(), 32);
293 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
294 return false;
295 }
296
Krzysztof Parzyszekac1966e2017-11-27 18:12:16 +0000297 auto &HST = State.getMachineFunction().getSubtarget<HexagonSubtarget>();
298 if (HST.isHVXVectorType(LocVT)) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000299 if (!CC_HexagonVector(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
300 return false;
301 }
302
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000303 return true; // CC didn't match.
304}
305
306
307static bool CC_Hexagon32(unsigned ValNo, MVT ValVT,
308 MVT LocVT, CCValAssign::LocInfo LocInfo,
309 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +0000310 static const MCPhysReg RegList[] = {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000311 Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3, Hexagon::R4,
312 Hexagon::R5
313 };
Tim Northover3b6b7ca2015-02-21 02:11:17 +0000314 if (unsigned Reg = State.AllocateReg(RegList)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000315 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
316 return false;
317 }
318
319 unsigned Offset = State.AllocateStack(4, 4);
320 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
321 return false;
322}
323
324static bool CC_Hexagon64(unsigned ValNo, MVT ValVT,
325 MVT LocVT, CCValAssign::LocInfo LocInfo,
326 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000327 if (unsigned Reg = State.AllocateReg(Hexagon::D0)) {
328 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
329 return false;
330 }
331
Craig Topper840beec2014-04-04 05:16:06 +0000332 static const MCPhysReg RegList1[] = {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000333 Hexagon::D1, Hexagon::D2
334 };
Craig Topper840beec2014-04-04 05:16:06 +0000335 static const MCPhysReg RegList2[] = {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000336 Hexagon::R1, Hexagon::R3
337 };
Tim Northover3b6b7ca2015-02-21 02:11:17 +0000338 if (unsigned Reg = State.AllocateReg(RegList1, RegList2)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000339 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
340 return false;
341 }
342
343 unsigned Offset = State.AllocateStack(8, 8, Hexagon::D2);
344 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
345 return false;
346}
347
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000348static bool CC_HexagonVector(unsigned ValNo, MVT ValVT,
349 MVT LocVT, CCValAssign::LocInfo LocInfo,
350 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000351 static const MCPhysReg VecLstS[] = {
352 Hexagon::V0, Hexagon::V1, Hexagon::V2, Hexagon::V3, Hexagon::V4,
353 Hexagon::V5, Hexagon::V6, Hexagon::V7, Hexagon::V8, Hexagon::V9,
354 Hexagon::V10, Hexagon::V11, Hexagon::V12, Hexagon::V13, Hexagon::V14,
355 Hexagon::V15
356 };
357 static const MCPhysReg VecLstD[] = {
358 Hexagon::W0, Hexagon::W1, Hexagon::W2, Hexagon::W3, Hexagon::W4,
359 Hexagon::W5, Hexagon::W6, Hexagon::W7
360 };
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000361 auto &MF = State.getMachineFunction();
362 auto &HST = MF.getSubtarget<HexagonSubtarget>();
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000363
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +0000364 if (HST.useHVX64BOps() &&
Krzysztof Parzyszek708c9f52017-12-14 18:35:24 +0000365 (LocVT == MVT::v16i32 || LocVT == MVT::v32i16 ||
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000366 LocVT == MVT::v64i8 || LocVT == MVT::v512i1)) {
367 if (unsigned Reg = State.AllocateReg(VecLstS)) {
368 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
369 return false;
370 }
371 unsigned Offset = State.AllocateStack(64, 64);
372 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
373 return false;
374 }
Krzysztof Parzyszek708c9f52017-12-14 18:35:24 +0000375 if (HST.useHVX64BOps() && (LocVT == MVT::v32i32 ||
Sumanth Gundapaneni9d954c42017-10-18 17:45:22 +0000376 LocVT == MVT::v64i16 || LocVT == MVT::v128i8)) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000377 if (unsigned Reg = State.AllocateReg(VecLstD)) {
378 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
379 return false;
380 }
381 unsigned Offset = State.AllocateStack(128, 128);
382 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
383 return false;
384 }
Sumanth Gundapaneni9d954c42017-10-18 17:45:22 +0000385 // 128B Mode
Krzysztof Parzyszek708c9f52017-12-14 18:35:24 +0000386 if (HST.useHVX128BOps() && (LocVT == MVT::v64i32 ||
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +0000387 LocVT == MVT::v128i16 || LocVT == MVT::v256i8)) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000388 if (unsigned Reg = State.AllocateReg(VecLstD)) {
389 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
390 return false;
391 }
392 unsigned Offset = State.AllocateStack(256, 256);
393 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
394 return false;
395 }
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +0000396 if (HST.useHVX128BOps() &&
Krzysztof Parzyszek708c9f52017-12-14 18:35:24 +0000397 (LocVT == MVT::v32i32 || LocVT == MVT::v64i16 ||
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000398 LocVT == MVT::v128i8 || LocVT == MVT::v1024i1)) {
399 if (unsigned Reg = State.AllocateReg(VecLstS)) {
400 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
401 return false;
402 }
403 unsigned Offset = State.AllocateStack(128, 128);
404 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
405 return false;
406 }
407 return true;
408}
409
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000410static bool RetCC_Hexagon(unsigned ValNo, MVT ValVT,
411 MVT LocVT, CCValAssign::LocInfo LocInfo,
412 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000413 auto &MF = State.getMachineFunction();
414 auto &HST = MF.getSubtarget<HexagonSubtarget>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000415
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000416 if (LocVT == MVT::i1) {
417 // Return values of type MVT::i1 still need to be assigned to R0, but
418 // the value type needs to remain i1. LowerCallResult will deal with it,
419 // but it needs to recognize i1 as the value type.
420 LocVT = MVT::i32;
421 } else if (LocVT == MVT::i8 || LocVT == MVT::i16) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000422 LocVT = MVT::i32;
423 ValVT = MVT::i32;
424 if (ArgFlags.isSExt())
425 LocInfo = CCValAssign::SExt;
426 else if (ArgFlags.isZExt())
427 LocInfo = CCValAssign::ZExt;
428 else
429 LocInfo = CCValAssign::AExt;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000430 } else if (LocVT == MVT::v4i8 || LocVT == MVT::v2i16) {
431 LocVT = MVT::i32;
432 LocInfo = CCValAssign::BCvt;
433 } else if (LocVT == MVT::v8i8 || LocVT == MVT::v4i16 || LocVT == MVT::v2i32) {
434 LocVT = MVT::i64;
435 LocInfo = CCValAssign::BCvt;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000436 } else if (LocVT == MVT::v64i8 || LocVT == MVT::v32i16 ||
Krzysztof Parzyszek708c9f52017-12-14 18:35:24 +0000437 LocVT == MVT::v16i32 || LocVT == MVT::v512i1) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000438 LocVT = MVT::v16i32;
439 ValVT = MVT::v16i32;
440 LocInfo = CCValAssign::Full;
441 } else if (LocVT == MVT::v128i8 || LocVT == MVT::v64i16 ||
Krzysztof Parzyszek708c9f52017-12-14 18:35:24 +0000442 LocVT == MVT::v32i32 ||
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +0000443 (LocVT == MVT::v1024i1 && HST.useHVX128BOps())) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000444 LocVT = MVT::v32i32;
445 ValVT = MVT::v32i32;
446 LocInfo = CCValAssign::Full;
447 } else if (LocVT == MVT::v256i8 || LocVT == MVT::v128i16 ||
Krzysztof Parzyszek708c9f52017-12-14 18:35:24 +0000448 LocVT == MVT::v64i32) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000449 LocVT = MVT::v64i32;
450 ValVT = MVT::v64i32;
451 LocInfo = CCValAssign::Full;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000452 }
Sirish Pande69295b82012-05-10 20:20:25 +0000453 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000454 if (!RetCC_Hexagon32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000455 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000456 }
457
Sirish Pande69295b82012-05-10 20:20:25 +0000458 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000459 if (!RetCC_Hexagon64(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000460 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000461 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000462 if (LocVT == MVT::v16i32 || LocVT == MVT::v32i32 || LocVT == MVT::v64i32) {
463 if (!RetCC_HexagonVector(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000464 return false;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000465 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000466 return true; // CC didn't match.
467}
468
469static bool RetCC_Hexagon32(unsigned ValNo, MVT ValVT,
470 MVT LocVT, CCValAssign::LocInfo LocInfo,
471 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Sirish Pande69295b82012-05-10 20:20:25 +0000472 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Krzysztof Parzyszek14412ef2016-07-18 17:36:46 +0000473 // Note that use of registers beyond R1 is not ABI compliant. However there
474 // are (experimental) IR passes which generate internal functions that
475 // return structs using these additional registers.
476 static const uint16_t RegList[] = { Hexagon::R0, Hexagon::R1,
477 Hexagon::R2, Hexagon::R3,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000478 Hexagon::R4, Hexagon::R5 };
Krzysztof Parzyszek14412ef2016-07-18 17:36:46 +0000479 if (unsigned Reg = State.AllocateReg(RegList)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000480 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
481 return false;
482 }
483 }
484
Krzysztof Parzyszek56199522017-04-13 15:05:51 +0000485 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000486}
487
488static bool RetCC_Hexagon64(unsigned ValNo, MVT ValVT,
489 MVT LocVT, CCValAssign::LocInfo LocInfo,
490 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Sirish Pande69295b82012-05-10 20:20:25 +0000491 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000492 if (unsigned Reg = State.AllocateReg(Hexagon::D0)) {
493 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
494 return false;
495 }
496 }
497
Krzysztof Parzyszek56199522017-04-13 15:05:51 +0000498 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000499}
500
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000501static bool RetCC_HexagonVector(unsigned ValNo, MVT ValVT,
502 MVT LocVT, CCValAssign::LocInfo LocInfo,
503 ISD::ArgFlagsTy ArgFlags, CCState &State) {
504 auto &MF = State.getMachineFunction();
505 auto &HST = MF.getSubtarget<HexagonSubtarget>();
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000506
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000507 if (LocVT == MVT::v16i32) {
508 if (unsigned Reg = State.AllocateReg(Hexagon::V0)) {
509 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
510 return false;
511 }
512 } else if (LocVT == MVT::v32i32) {
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +0000513 unsigned Req = HST.useHVX128BOps() ? Hexagon::V0 : Hexagon::W0;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000514 if (unsigned Reg = State.AllocateReg(Req)) {
515 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
516 return false;
517 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000518 } else if (LocVT == MVT::v64i32) {
519 if (unsigned Reg = State.AllocateReg(Hexagon::W0)) {
520 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
521 return false;
522 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000523 }
524
Krzysztof Parzyszek56199522017-04-13 15:05:51 +0000525 return true;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000526}
527
Craig Topper18e69f42016-04-15 06:20:21 +0000528void HexagonTargetLowering::promoteLdStType(MVT VT, MVT PromotedLdStVT) {
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000529 if (VT != PromotedLdStVT) {
Craig Topper18e69f42016-04-15 06:20:21 +0000530 setOperationAction(ISD::LOAD, VT, Promote);
531 AddPromotedToType(ISD::LOAD, VT, PromotedLdStVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000532
Craig Topper18e69f42016-04-15 06:20:21 +0000533 setOperationAction(ISD::STORE, VT, Promote);
534 AddPromotedToType(ISD::STORE, VT, PromotedLdStVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000535 }
536}
537
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000538SDValue
539HexagonTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG)
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000540 const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000541 return SDValue();
542}
543
544/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
545/// by "Src" to address "Dst" of size "Size". Alignment information is
546/// specified by the specific parameter attribute. The copy will be passed as
547/// a byval function parameter. Sometimes what we are copying is the end of a
548/// larger object, the part that does not fit in registers.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000549static SDValue CreateCopyOfByValArgument(SDValue Src, SDValue Dst,
550 SDValue Chain, ISD::ArgFlagsTy Flags,
551 SelectionDAG &DAG, const SDLoc &dl) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000552 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000553 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
554 /*isVolatile=*/false, /*AlwaysInline=*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +0000555 /*isTailCall=*/false,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000556 MachinePointerInfo(), MachinePointerInfo());
557}
558
Krzysztof Parzyszek56199522017-04-13 15:05:51 +0000559bool
560HexagonTargetLowering::CanLowerReturn(
561 CallingConv::ID CallConv, MachineFunction &MF, bool isVarArg,
562 const SmallVectorImpl<ISD::OutputArg> &Outs,
563 LLVMContext &Context) const {
564 SmallVector<CCValAssign, 16> RVLocs;
565 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
566 return CCInfo.CheckReturn(Outs, RetCC_Hexagon);
567}
568
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000569// LowerReturn - Lower ISD::RET. If a struct is larger than 8 bytes and is
570// passed by value, the function prototype is modified to return void and
571// the value is stored in memory pointed by a pointer passed by caller.
572SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000573HexagonTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
574 bool isVarArg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000575 const SmallVectorImpl<ISD::OutputArg> &Outs,
576 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000577 const SDLoc &dl, SelectionDAG &DAG) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000578 // CCValAssign - represent the assignment of the return value to locations.
579 SmallVector<CCValAssign, 16> RVLocs;
580
581 // CCState - Info about the registers and stack slot.
Eric Christopherb5217502014-08-06 18:45:26 +0000582 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
583 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000584
585 // Analyze return values of ISD::RET
586 CCInfo.AnalyzeReturn(Outs, RetCC_Hexagon);
587
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000588 SDValue Flag;
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000589 SmallVector<SDValue, 4> RetOps(1, Chain);
590
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000591 // Copy the result values into the output registers.
592 for (unsigned i = 0; i != RVLocs.size(); ++i) {
593 CCValAssign &VA = RVLocs[i];
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000594
595 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), OutVals[i], Flag);
596
597 // Guarantee that all emitted copies are stuck together with flags.
598 Flag = Chain.getValue(1);
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000599 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000600 }
601
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000602 RetOps[0] = Chain; // Update chain.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000603
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000604 // Add the flag if we have it.
605 if (Flag.getNode())
606 RetOps.push_back(Flag);
607
Craig Topper48d114b2014-04-26 18:35:24 +0000608 return DAG.getNode(HexagonISD::RET_FLAG, dl, MVT::Other, RetOps);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000609}
610
Matt Arsenault31380752017-04-18 21:16:46 +0000611bool HexagonTargetLowering::mayBeEmittedAsTailCall(const CallInst *CI) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000612 // If either no tail call or told not to tail call at all, don't.
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000613 auto Attr =
614 CI->getParent()->getParent()->getFnAttribute("disable-tail-calls");
615 if (!CI->isTailCall() || Attr.getValueAsString() == "true")
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000616 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000617
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000618 return true;
619}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000620
621/// LowerCallResult - Lower the result values of an ISD::CALL into the
622/// appropriate copies out of appropriate physical registers. This assumes that
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000623/// Chain/Glue are the input chain/glue to use, and that TheCall is the call
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000624/// being lowered. Returns a SDNode with the same number of values as the
625/// ISD::CALL.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000626SDValue HexagonTargetLowering::LowerCallResult(
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000627 SDValue Chain, SDValue Glue, CallingConv::ID CallConv, bool isVarArg,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000628 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
629 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals,
630 const SmallVectorImpl<SDValue> &OutVals, SDValue Callee) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000631 // Assign locations to each value returned by this call.
632 SmallVector<CCValAssign, 16> RVLocs;
633
Eric Christopherb5217502014-08-06 18:45:26 +0000634 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
635 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000636
637 CCInfo.AnalyzeCallResult(Ins, RetCC_Hexagon);
638
639 // Copy all of the result registers out of their specified physreg.
640 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000641 SDValue RetVal;
642 if (RVLocs[i].getValVT() == MVT::i1) {
643 // Return values of type MVT::i1 require special handling. The reason
644 // is that MVT::i1 is associated with the PredRegs register class, but
645 // values of that type are still returned in R0. Generate an explicit
646 // copy into a predicate register from R0, and treat the value of the
647 // predicate register as the call result.
648 auto &MRI = DAG.getMachineFunction().getRegInfo();
649 SDValue FR0 = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000650 MVT::i32, Glue);
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000651 // FR0 = (Value, Chain, Glue)
652 unsigned PredR = MRI.createVirtualRegister(&Hexagon::PredRegsRegClass);
653 SDValue TPR = DAG.getCopyToReg(FR0.getValue(1), dl, PredR,
654 FR0.getValue(0), FR0.getValue(2));
655 // TPR = (Chain, Glue)
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000656 // Don't glue this CopyFromReg, because it copies from a virtual
657 // register. If it is glued to the call, InstrEmitter will add it
658 // as an implicit def to the call (EmitMachineNode).
659 RetVal = DAG.getCopyFromReg(TPR.getValue(0), dl, PredR, MVT::i1);
660 Glue = TPR.getValue(1);
Krzysztof Parzyszek6f06b6e2017-10-23 19:35:25 +0000661 Chain = TPR.getValue(0);
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000662 } else {
663 RetVal = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000664 RVLocs[i].getValVT(), Glue);
665 Glue = RetVal.getValue(2);
Krzysztof Parzyszek6f06b6e2017-10-23 19:35:25 +0000666 Chain = RetVal.getValue(1);
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000667 }
668 InVals.push_back(RetVal.getValue(0));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000669 }
670
671 return Chain;
672}
673
674/// LowerCall - Functions arguments are copied from virtual regs to
675/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
676SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +0000677HexagonTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000678 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +0000679 SelectionDAG &DAG = CLI.DAG;
Craig Topperb94011f2013-07-14 04:42:23 +0000680 SDLoc &dl = CLI.DL;
681 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
682 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
683 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000684 SDValue Chain = CLI.Chain;
685 SDValue Callee = CLI.Callee;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000686 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000687 CallingConv::ID CallConv = CLI.CallConv;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000688 bool IsVarArg = CLI.IsVarArg;
689 bool DoesNotReturn = CLI.DoesNotReturn;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000690
691 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000692 MachineFunction &MF = DAG.getMachineFunction();
Krzysztof Parzyszekf67cd822017-07-11 17:11:54 +0000693 MachineFrameInfo &MFI = MF.getFrameInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +0000694 auto PtrVT = getPointerTy(MF.getDataLayout());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000695
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000696 // Check for varargs.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000697 unsigned NumNamedVarArgParams = -1U;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000698 if (GlobalAddressSDNode *GAN = dyn_cast<GlobalAddressSDNode>(Callee)) {
699 const GlobalValue *GV = GAN->getGlobal();
700 Callee = DAG.getTargetGlobalAddress(GV, dl, MVT::i32);
701 if (const Function* F = dyn_cast<Function>(GV)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000702 // If a function has zero args and is a vararg function, that's
703 // disallowed so it must be an undeclared function. Do not assume
704 // varargs if the callee is undefined.
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000705 if (F->isVarArg() && F->getFunctionType()->getNumParams() != 0)
706 NumNamedVarArgParams = F->getFunctionType()->getNumParams();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000707 }
708 }
709
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000710 // Analyze operands of the call, assigning locations to each operand.
711 SmallVector<CCValAssign, 16> ArgLocs;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000712 HexagonCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
Eric Christopherb5217502014-08-06 18:45:26 +0000713 *DAG.getContext(), NumNamedVarArgParams);
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000714
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000715 if (IsVarArg)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000716 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon_VarArg);
717 else
718 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon);
719
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000720 auto Attr = MF.getFunction()->getFnAttribute("disable-tail-calls");
721 if (Attr.getValueAsString() == "true")
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000722 IsTailCall = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000723
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000724 if (IsTailCall) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000725 bool StructAttrFlag = MF.getFunction()->hasStructRetAttr();
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000726 IsTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
727 IsVarArg, IsStructRet,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000728 StructAttrFlag,
729 Outs, OutVals, Ins, DAG);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000730 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000731 CCValAssign &VA = ArgLocs[i];
732 if (VA.isMemLoc()) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000733 IsTailCall = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000734 break;
735 }
736 }
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000737 DEBUG(dbgs() << (IsTailCall ? "Eligible for Tail Call\n"
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000738 : "Argument must be passed on stack. "
739 "Not eligible for Tail Call\n"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000740 }
741 // Get a count of how many bytes are to be pushed on the stack.
742 unsigned NumBytes = CCInfo.getNextStackOffset();
743 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
744 SmallVector<SDValue, 8> MemOpChains;
745
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000746 auto &HRI = *Subtarget.getRegisterInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +0000747 SDValue StackPtr =
748 DAG.getCopyFromReg(Chain, dl, HRI.getStackRegister(), PtrVT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000749
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000750 bool NeedsArgAlign = false;
751 unsigned LargestAlignSeen = 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000752 // Walk the register/memloc assignments, inserting copies/loads.
753 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
754 CCValAssign &VA = ArgLocs[i];
755 SDValue Arg = OutVals[i];
756 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000757 // Record if we need > 8 byte alignment on an argument.
Krzysztof Parzyszekac1966e2017-11-27 18:12:16 +0000758 bool ArgAlign = Subtarget.isHVXVectorType(VA.getValVT());
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000759 NeedsArgAlign |= ArgAlign;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000760
761 // Promote the value if needed.
762 switch (VA.getLocInfo()) {
763 default:
764 // Loc info must be one of Full, SExt, ZExt, or AExt.
Craig Toppere55c5562012-02-07 02:50:20 +0000765 llvm_unreachable("Unknown loc info!");
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000766 case CCValAssign::BCvt:
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000767 case CCValAssign::Full:
768 break;
769 case CCValAssign::SExt:
770 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
771 break;
772 case CCValAssign::ZExt:
773 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
774 break;
775 case CCValAssign::AExt:
776 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
777 break;
778 }
779
780 if (VA.isMemLoc()) {
781 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000782 SDValue MemAddr = DAG.getConstant(LocMemOffset, dl,
783 StackPtr.getValueType());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000784 MemAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, MemAddr);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000785 if (ArgAlign)
786 LargestAlignSeen = std::max(LargestAlignSeen,
787 VA.getLocVT().getStoreSizeInBits() >> 3);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000788 if (Flags.isByVal()) {
789 // The argument is a struct passed by value. According to LLVM, "Arg"
790 // is is pointer.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000791 MemOpChains.push_back(CreateCopyOfByValArgument(Arg, MemAddr, Chain,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000792 Flags, DAG, dl));
793 } else {
Alex Lorenze40c8a22015-08-11 23:09:45 +0000794 MachinePointerInfo LocPI = MachinePointerInfo::getStack(
795 DAG.getMachineFunction(), LocMemOffset);
Justin Lebar9c375812016-07-15 18:27:10 +0000796 SDValue S = DAG.getStore(Chain, dl, Arg, MemAddr, LocPI);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000797 MemOpChains.push_back(S);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000798 }
799 continue;
800 }
801
802 // Arguments that can be passed on register must be kept at RegsToPass
803 // vector.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000804 if (VA.isRegLoc())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000805 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000806 }
807
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000808 if (NeedsArgAlign && Subtarget.hasV60TOps()) {
809 DEBUG(dbgs() << "Function needs byte stack align due to call args\n");
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000810 // V6 vectors passed by value have 64 or 128 byte alignment depending
811 // on whether we are 64 byte vector mode or 128 byte.
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +0000812 bool UseHVX128B = Subtarget.useHVX128BOps();
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000813 assert(Subtarget.useHVXOps());
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +0000814 const unsigned ObjAlign = UseHVX128B ? 128 : 64;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000815 LargestAlignSeen = std::max(LargestAlignSeen, ObjAlign);
Matthias Braun941a7052016-07-28 18:40:00 +0000816 MFI.ensureMaxAlignment(LargestAlignSeen);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000817 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000818 // Transform all store nodes into one single node because all store
819 // nodes are independent of each other.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000820 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000821 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000822
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000823 SDValue Glue;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000824 if (!IsTailCall) {
Serge Pavlovd526b132017-05-09 13:35:13 +0000825 Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, dl);
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000826 Glue = Chain.getValue(1);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000827 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000828
829 // Build a sequence of copy-to-reg nodes chained together with token
830 // chain and flag operands which copy the outgoing args into registers.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000831 // The Glue is necessary since all emitted instructions must be
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000832 // stuck together.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000833 if (!IsTailCall) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000834 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
835 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000836 RegsToPass[i].second, Glue);
837 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000838 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000839 } else {
840 // For tail calls lower the arguments to the 'real' stack slot.
841 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000842 // Force all the incoming stack arguments to be loaded from the stack
843 // before any new outgoing arguments are stored to the stack, because the
844 // outgoing stack slots may alias the incoming argument stack slots, and
845 // the alias isn't otherwise explicit. This is slightly more conservative
846 // than necessary, because it means that each store effectively depends
847 // on every argument instead of just those arguments it would clobber.
848 //
Benjamin Kramerbde91762012-06-02 10:20:22 +0000849 // Do not flag preceding copytoreg stuff together with the following stuff.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000850 Glue = SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000851 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
852 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000853 RegsToPass[i].second, Glue);
854 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000855 }
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000856 Glue = SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000857 }
858
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000859 bool LongCalls = MF.getSubtarget<HexagonSubtarget>().useLongCalls();
860 unsigned Flags = LongCalls ? HexagonII::HMOTF_ConstExtended : 0;
861
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000862 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
863 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
864 // node so that legalize doesn't hack it.
Tobias Edler von Kochb51460c2015-12-16 17:29:37 +0000865 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000866 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, PtrVT, 0, Flags);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000867 } else if (ExternalSymbolSDNode *S =
868 dyn_cast<ExternalSymbolSDNode>(Callee)) {
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000869 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), PtrVT, Flags);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000870 }
871
872 // Returns a chain & a flag for retval copy to use.
873 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
874 SmallVector<SDValue, 8> Ops;
875 Ops.push_back(Chain);
876 Ops.push_back(Callee);
877
878 // Add argument registers to the end of the list so that they are
879 // known live into the call.
880 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
881 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
882 RegsToPass[i].second.getValueType()));
883 }
884
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000885 const uint32_t *Mask = HRI.getCallPreservedMask(MF, CallConv);
886 assert(Mask && "Missing call preserved mask for calling convention");
887 Ops.push_back(DAG.getRegisterMask(Mask));
888
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000889 if (Glue.getNode())
890 Ops.push_back(Glue);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000891
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000892 if (IsTailCall) {
Krzysztof Parzyszekf67cd822017-07-11 17:11:54 +0000893 MFI.setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +0000894 return DAG.getNode(HexagonISD::TC_RETURN, dl, NodeTys, Ops);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +0000895 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000896
Krzysztof Parzyszekf67cd822017-07-11 17:11:54 +0000897 // Set this here because we need to know this for "hasFP" in frame lowering.
898 // The target-independent code calls getFrameRegister before setting it, and
899 // getFrameRegister uses hasFP to determine whether the function has FP.
900 MFI.setHasCalls(true);
901
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +0000902 unsigned OpCode = DoesNotReturn ? HexagonISD::CALLnr : HexagonISD::CALL;
Colin LeMahieu2e3a26d2015-01-16 17:05:27 +0000903 Chain = DAG.getNode(OpCode, dl, NodeTys, Ops);
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000904 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000905
906 // Create the CALLSEQ_END node.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000907 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000908 DAG.getIntPtrConstant(0, dl, true), Glue, dl);
909 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000910
911 // Handle result values, copying them out of physregs into vregs that we
912 // return.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000913 return LowerCallResult(Chain, Glue, CallConv, IsVarArg, Ins, dl, DAG,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000914 InVals, OutVals, Callee);
915}
916
917static bool getIndexedAddressParts(SDNode *Ptr, EVT VT,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000918 SDValue &Base, SDValue &Offset,
919 bool &IsInc, SelectionDAG &DAG) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000920 if (Ptr->getOpcode() != ISD::ADD)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000921 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000922
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000923 auto &HST = static_cast<const HexagonSubtarget&>(DAG.getSubtarget());
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000924
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +0000925 bool ValidHVX128BType =
Krzysztof Parzyszek708c9f52017-12-14 18:35:24 +0000926 HST.useHVX128BOps() && (VT == MVT::v32i32 ||
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +0000927 VT == MVT::v64i16 || VT == MVT::v128i8);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000928 bool ValidHVXType =
Krzysztof Parzyszek708c9f52017-12-14 18:35:24 +0000929 HST.useHVX64BOps() && (VT == MVT::v16i32 ||
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000930 VT == MVT::v32i16 || VT == MVT::v64i8);
931
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +0000932 if (ValidHVX128BType || ValidHVXType || VT == MVT::i64 || VT == MVT::i32 ||
933 VT == MVT::i16 || VT == MVT::i8) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000934 IsInc = (Ptr->getOpcode() == ISD::ADD);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000935 Base = Ptr->getOperand(0);
936 Offset = Ptr->getOperand(1);
937 // Ensure that Offset is a constant.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000938 return isa<ConstantSDNode>(Offset);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000939 }
940
941 return false;
942}
943
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000944/// getPostIndexedAddressParts - returns true by value, base pointer and
945/// offset pointer and addressing mode by reference if this node can be
946/// combined with a load / store to form a post-indexed load / store.
947bool HexagonTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
948 SDValue &Base,
949 SDValue &Offset,
950 ISD::MemIndexedMode &AM,
951 SelectionDAG &DAG) const
952{
953 EVT VT;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000954
955 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
956 VT = LD->getMemoryVT();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000957 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
958 VT = ST->getMemoryVT();
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000959 if (ST->getValue().getValueType() == MVT::i64 && ST->isTruncatingStore())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000960 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000961 } else {
962 return false;
963 }
964
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000965 bool IsInc = false;
966 bool isLegal = getIndexedAddressParts(Op, VT, Base, Offset, IsInc, DAG);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000967 if (isLegal) {
968 auto &HII = *Subtarget.getInstrInfo();
969 int32_t OffsetVal = cast<ConstantSDNode>(Offset.getNode())->getSExtValue();
970 if (HII.isValidAutoIncImm(VT, OffsetVal)) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000971 AM = IsInc ? ISD::POST_INC : ISD::POST_DEC;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000972 return true;
973 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000974 }
975
976 return false;
977}
978
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000979SDValue
980HexagonTargetLowering::LowerINLINEASM(SDValue Op, SelectionDAG &DAG) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000981 MachineFunction &MF = DAG.getMachineFunction();
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000982 auto &HMFI = *MF.getInfo<HexagonMachineFunctionInfo>();
983 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
984 unsigned LR = HRI.getRARegister();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000985
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000986 if (Op.getOpcode() != ISD::INLINEASM || HMFI.hasClobberLR())
987 return Op;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000988
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000989 unsigned NumOps = Op.getNumOperands();
990 if (Op.getOperand(NumOps-1).getValueType() == MVT::Glue)
991 --NumOps; // Ignore the flag operand.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000992
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000993 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
994 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(i))->getZExtValue();
995 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
996 ++i; // Skip the ID value.
997
998 switch (InlineAsm::getKind(Flags)) {
999 default:
1000 llvm_unreachable("Bad flags!");
1001 case InlineAsm::Kind_RegUse:
1002 case InlineAsm::Kind_Imm:
1003 case InlineAsm::Kind_Mem:
1004 i += NumVals;
1005 break;
1006 case InlineAsm::Kind_Clobber:
1007 case InlineAsm::Kind_RegDef:
1008 case InlineAsm::Kind_RegDefEarlyClobber: {
1009 for (; NumVals; --NumVals, ++i) {
1010 unsigned Reg = cast<RegisterSDNode>(Op.getOperand(i))->getReg();
1011 if (Reg != LR)
1012 continue;
1013 HMFI.setHasClobberLR(true);
1014 return Op;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001015 }
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +00001016 break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001017 }
1018 }
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +00001019 }
1020
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001021 return Op;
1022}
1023
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00001024// Need to transform ISD::PREFETCH into something that doesn't inherit
1025// all of the properties of ISD::PREFETCH, specifically SDNPMayLoad and
1026// SDNPMayStore.
1027SDValue HexagonTargetLowering::LowerPREFETCH(SDValue Op,
1028 SelectionDAG &DAG) const {
1029 SDValue Chain = Op.getOperand(0);
1030 SDValue Addr = Op.getOperand(1);
1031 // Lower it to DCFETCH($reg, #0). A "pat" will try to merge the offset in,
1032 // if the "reg" is fed by an "add".
1033 SDLoc DL(Op);
1034 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
1035 return DAG.getNode(HexagonISD::DCFETCH, DL, MVT::Other, Chain, Addr, Zero);
1036}
1037
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +00001038// Custom-handle ISD::READCYCLECOUNTER because the target-independent SDNode
1039// is marked as having side-effects, while the register read on Hexagon does
1040// not have any. TableGen refuses to accept the direct pattern from that node
1041// to the A4_tfrcpp.
1042SDValue HexagonTargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
1043 SelectionDAG &DAG) const {
1044 SDValue Chain = Op.getOperand(0);
1045 SDLoc dl(Op);
1046 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
1047 return DAG.getNode(HexagonISD::READCYCLE, dl, VTs, Chain);
1048}
1049
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00001050SDValue HexagonTargetLowering::LowerINTRINSIC_VOID(SDValue Op,
1051 SelectionDAG &DAG) const {
1052 SDValue Chain = Op.getOperand(0);
1053 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1054 // Lower the hexagon_prefetch builtin to DCFETCH, as above.
1055 if (IntNo == Intrinsic::hexagon_prefetch) {
1056 SDValue Addr = Op.getOperand(2);
1057 SDLoc DL(Op);
1058 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
1059 return DAG.getNode(HexagonISD::DCFETCH, DL, MVT::Other, Chain, Addr, Zero);
1060 }
1061 return SDValue();
1062}
1063
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001064SDValue
1065HexagonTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
1066 SelectionDAG &DAG) const {
1067 SDValue Chain = Op.getOperand(0);
1068 SDValue Size = Op.getOperand(1);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001069 SDValue Align = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001070 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001071
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001072 ConstantSDNode *AlignConst = dyn_cast<ConstantSDNode>(Align);
1073 assert(AlignConst && "Non-constant Align in LowerDYNAMIC_STACKALLOC");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001074
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001075 unsigned A = AlignConst->getSExtValue();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001076 auto &HFI = *Subtarget.getFrameLowering();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001077 // "Zero" means natural stack alignment.
1078 if (A == 0)
1079 A = HFI.getStackAlignment();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001080
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001081 DEBUG({
Reid Kleckner40d72302016-10-20 00:22:23 +00001082 dbgs () << __func__ << " Align: " << A << " Size: ";
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001083 Size.getNode()->dump(&DAG);
1084 dbgs() << "\n";
1085 });
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001086
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001087 SDValue AC = DAG.getConstant(A, dl, MVT::i32);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001088 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00001089 SDValue AA = DAG.getNode(HexagonISD::ALLOCA, dl, VTs, Chain, Size, AC);
Nirav Davebfdb4832016-06-23 17:52:57 +00001090
1091 DAG.ReplaceAllUsesOfValueWith(Op, AA);
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00001092 return AA;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001093}
1094
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001095SDValue HexagonTargetLowering::LowerFormalArguments(
1096 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
1097 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
1098 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001099 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00001100 MachineFrameInfo &MFI = MF.getFrameInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001101 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001102 auto &FuncInfo = *MF.getInfo<HexagonMachineFunctionInfo>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001103
1104 // Assign locations to all of the incoming arguments.
1105 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001106 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1107 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001108
1109 CCInfo.AnalyzeFormalArguments(Ins, CC_Hexagon);
1110
1111 // For LLVM, in the case when returning a struct by value (>8byte),
1112 // the first argument is a pointer that points to the location on caller's
1113 // stack where the return value will be stored. For Hexagon, the location on
1114 // caller's stack is passed only when the struct size is smaller than (and
1115 // equal to) 8 bytes. If not, no address will be passed into callee and
1116 // callee return the result direclty through R0/R1.
1117
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001118 SmallVector<SDValue, 8> MemOps;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001119
1120 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1121 CCValAssign &VA = ArgLocs[i];
1122 ISD::ArgFlagsTy Flags = Ins[i].Flags;
1123 unsigned ObjSize;
1124 unsigned StackLocation;
1125 int FI;
1126
1127 if ( (VA.isRegLoc() && !Flags.isByVal())
1128 || (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() > 8)) {
1129 // Arguments passed in registers
1130 // 1. int, long long, ptr args that get allocated in register.
1131 // 2. Large struct that gets an register to put its address in.
1132 EVT RegVT = VA.getLocVT();
Sirish Pande69295b82012-05-10 20:20:25 +00001133 if (RegVT == MVT::i8 || RegVT == MVT::i16 ||
1134 RegVT == MVT::i32 || RegVT == MVT::f32) {
Krzysztof Parzyszek6acecc92017-11-22 20:43:00 +00001135 unsigned VReg =
Craig Topperc7242e02012-04-20 07:30:17 +00001136 RegInfo.createVirtualRegister(&Hexagon::IntRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001137 RegInfo.addLiveIn(VA.getLocReg(), VReg);
Krzysztof Parzyszek8f23dd62017-03-01 17:30:10 +00001138 SDValue Copy = DAG.getCopyFromReg(Chain, dl, VReg, RegVT);
1139 // Treat values of type MVT::i1 specially: they are passed in
1140 // registers of type i32, but they need to remain as values of
1141 // type i1 for consistency of the argument lowering.
1142 if (VA.getValVT() == MVT::i1) {
1143 // Generate a copy into a predicate register and use the value
1144 // of the register as the "InVal".
1145 unsigned PReg =
1146 RegInfo.createVirtualRegister(&Hexagon::PredRegsRegClass);
1147 SDNode *T = DAG.getMachineNode(Hexagon::C2_tfrrp, dl, MVT::i1,
1148 Copy.getValue(0));
1149 Copy = DAG.getCopyToReg(Copy.getValue(1), dl, PReg, SDValue(T, 0));
1150 Copy = DAG.getCopyFromReg(Copy, dl, PReg, MVT::i1);
1151 }
1152 InVals.push_back(Copy);
1153 Chain = Copy.getValue(1);
Colin LeMahieu4379d102015-01-28 22:08:16 +00001154 } else if (RegVT == MVT::i64 || RegVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001155 unsigned VReg =
Craig Topperc7242e02012-04-20 07:30:17 +00001156 RegInfo.createVirtualRegister(&Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001157 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1158 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001159
1160 // Single Vector
Krzysztof Parzyszek708c9f52017-12-14 18:35:24 +00001161 } else if ((RegVT == MVT::v16i32 ||
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001162 RegVT == MVT::v32i16 || RegVT == MVT::v64i8)) {
1163 unsigned VReg =
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001164 RegInfo.createVirtualRegister(&Hexagon::HvxVRRegClass);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001165 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1166 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +00001167 } else if (Subtarget.useHVX128BOps() &&
Krzysztof Parzyszek708c9f52017-12-14 18:35:24 +00001168 ((RegVT == MVT::v32i32 ||
Sumanth Gundapaneni9d954c42017-10-18 17:45:22 +00001169 RegVT == MVT::v64i16 || RegVT == MVT::v128i8))) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001170 unsigned VReg =
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001171 RegInfo.createVirtualRegister(&Hexagon::HvxVRRegClass);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001172 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1173 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1174
1175 // Double Vector
Krzysztof Parzyszek708c9f52017-12-14 18:35:24 +00001176 } else if ((RegVT == MVT::v32i32 ||
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001177 RegVT == MVT::v64i16 || RegVT == MVT::v128i8)) {
1178 unsigned VReg =
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001179 RegInfo.createVirtualRegister(&Hexagon::HvxWRRegClass);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001180 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1181 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +00001182 } else if (Subtarget.useHVX128BOps() &&
Krzysztof Parzyszek708c9f52017-12-14 18:35:24 +00001183 ((RegVT == MVT::v64i32 ||
Sumanth Gundapaneni9d954c42017-10-18 17:45:22 +00001184 RegVT == MVT::v128i16 || RegVT == MVT::v256i8))) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001185 unsigned VReg =
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001186 RegInfo.createVirtualRegister(&Hexagon::HvxWRRegClass);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001187 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1188 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1189 } else if (RegVT == MVT::v512i1 || RegVT == MVT::v1024i1) {
1190 assert(0 && "need to support VecPred regs");
1191 unsigned VReg =
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001192 RegInfo.createVirtualRegister(&Hexagon::HvxQRRegClass);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001193 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1194 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001195 } else {
1196 assert (0);
1197 }
1198 } else if (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() <= 8) {
1199 assert (0 && "ByValSize must be bigger than 8 bytes");
1200 } else {
1201 // Sanity check.
1202 assert(VA.isMemLoc());
1203
1204 if (Flags.isByVal()) {
1205 // If it's a byval parameter, then we need to compute the
1206 // "real" size, not the size of the pointer.
1207 ObjSize = Flags.getByValSize();
1208 } else {
1209 ObjSize = VA.getLocVT().getStoreSizeInBits() >> 3;
1210 }
1211
1212 StackLocation = HEXAGON_LRFP_SIZE + VA.getLocMemOffset();
1213 // Create the frame index object for this incoming parameter...
Matthias Braun941a7052016-07-28 18:40:00 +00001214 FI = MFI.CreateFixedObject(ObjSize, StackLocation, true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001215
1216 // Create the SelectionDAG nodes cordl, responding to a load
1217 // from this parameter.
1218 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
1219
1220 if (Flags.isByVal()) {
1221 // If it's a pass-by-value aggregate, then do not dereference the stack
1222 // location. Instead, we should generate a reference to the stack
1223 // location.
1224 InVals.push_back(FIN);
1225 } else {
Justin Lebar9c375812016-07-15 18:27:10 +00001226 InVals.push_back(
Krzysztof Parzyszek3e2046c2017-04-13 15:00:18 +00001227 DAG.getLoad(VA.getValVT(), dl, Chain, FIN, MachinePointerInfo()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001228 }
1229 }
1230 }
1231
1232 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001233 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001234
1235 if (isVarArg) {
1236 // This will point to the next argument passed via stack.
Matthias Braun941a7052016-07-28 18:40:00 +00001237 int FrameIndex = MFI.CreateFixedObject(Hexagon_PointerSize,
1238 HEXAGON_LRFP_SIZE +
1239 CCInfo.getNextStackOffset(),
1240 true);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001241 FuncInfo.setVarArgsFrameIndex(FrameIndex);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001242 }
1243
1244 return Chain;
1245}
1246
1247SDValue
1248HexagonTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
1249 // VASTART stores the address of the VarArgsFrameIndex slot into the
1250 // memory location argument.
1251 MachineFunction &MF = DAG.getMachineFunction();
1252 HexagonMachineFunctionInfo *QFI = MF.getInfo<HexagonMachineFunctionInfo>();
1253 SDValue Addr = DAG.getFrameIndex(QFI->getVarArgsFrameIndex(), MVT::i32);
1254 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Justin Lebar9c375812016-07-15 18:27:10 +00001255 return DAG.getStore(Op.getOperand(0), SDLoc(Op), Addr, Op.getOperand(1),
1256 MachinePointerInfo(SV));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001257}
1258
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001259static bool isSExtFree(SDValue N) {
1260 // A sign-extend of a truncate of a sign-extend is free.
1261 if (N.getOpcode() == ISD::TRUNCATE &&
1262 N.getOperand(0).getOpcode() == ISD::AssertSext)
1263 return true;
1264 // We have sign-extended loads.
1265 if (N.getOpcode() == ISD::LOAD)
1266 return true;
1267 return false;
1268}
1269
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001270SDValue HexagonTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1271 SDLoc dl(Op);
1272
1273 SDValue LHS = Op.getOperand(0);
1274 SDValue RHS = Op.getOperand(1);
Krzysztof Parzyszek47076052017-12-14 21:28:48 +00001275 if (Subtarget.useHVXOps() && Subtarget.isHVXVectorType(ty(LHS)))
1276 return LowerHvxSetCC(Op, DAG);
1277
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001278 SDValue Cmp = Op.getOperand(2);
1279 ISD::CondCode CC = cast<CondCodeSDNode>(Cmp)->get();
1280
1281 EVT VT = Op.getValueType();
1282 EVT LHSVT = LHS.getValueType();
1283 EVT RHSVT = RHS.getValueType();
1284
1285 if (LHSVT == MVT::v2i16) {
1286 assert(ISD::isSignedIntSetCC(CC) || ISD::isUnsignedIntSetCC(CC));
1287 unsigned ExtOpc = ISD::isSignedIntSetCC(CC) ? ISD::SIGN_EXTEND
1288 : ISD::ZERO_EXTEND;
1289 SDValue LX = DAG.getNode(ExtOpc, dl, MVT::v2i32, LHS);
1290 SDValue RX = DAG.getNode(ExtOpc, dl, MVT::v2i32, RHS);
1291 SDValue SC = DAG.getNode(ISD::SETCC, dl, MVT::v2i1, LX, RX, Cmp);
1292 return SC;
1293 }
1294
1295 // Treat all other vector types as legal.
1296 if (VT.isVector())
1297 return Op;
1298
1299 // Equals and not equals should use sign-extend, not zero-extend, since
1300 // we can represent small negative values in the compare instructions.
1301 // The LLVM default is to use zero-extend arbitrarily in these cases.
1302 if ((CC == ISD::SETEQ || CC == ISD::SETNE) &&
1303 (RHSVT == MVT::i8 || RHSVT == MVT::i16) &&
1304 (LHSVT == MVT::i8 || LHSVT == MVT::i16)) {
1305 ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS);
1306 if (C && C->getAPIntValue().isNegative()) {
1307 LHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, LHS);
1308 RHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, RHS);
1309 return DAG.getNode(ISD::SETCC, dl, Op.getValueType(),
1310 LHS, RHS, Op.getOperand(2));
1311 }
1312 if (isSExtFree(LHS) || isSExtFree(RHS)) {
1313 LHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, LHS);
1314 RHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, RHS);
1315 return DAG.getNode(ISD::SETCC, dl, Op.getValueType(),
1316 LHS, RHS, Op.getOperand(2));
1317 }
1318 }
1319 return SDValue();
1320}
1321
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001322SDValue
1323HexagonTargetLowering::LowerVSELECT(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001324 SDValue PredOp = Op.getOperand(0);
1325 SDValue Op1 = Op.getOperand(1), Op2 = Op.getOperand(2);
1326 EVT OpVT = Op1.getValueType();
1327 SDLoc DL(Op);
1328
1329 if (OpVT == MVT::v2i16) {
1330 SDValue X1 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op1);
1331 SDValue X2 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op2);
1332 SDValue SL = DAG.getNode(ISD::VSELECT, DL, MVT::v2i32, PredOp, X1, X2);
1333 SDValue TR = DAG.getNode(ISD::TRUNCATE, DL, MVT::v2i16, SL);
1334 return TR;
1335 }
1336
1337 return SDValue();
1338}
1339
Krzysztof Parzyszek91ff5c62017-08-01 13:12:53 +00001340static Constant *convert_i1_to_i8(const Constant *ConstVal) {
1341 SmallVector<Constant *, 128> NewConst;
1342 const ConstantVector *CV = dyn_cast<ConstantVector>(ConstVal);
1343 if (!CV)
1344 return nullptr;
1345
1346 LLVMContext &Ctx = ConstVal->getContext();
1347 IRBuilder<> IRB(Ctx);
1348 unsigned NumVectorElements = CV->getNumOperands();
1349 assert(isPowerOf2_32(NumVectorElements) &&
1350 "conversion only supported for pow2 VectorSize!");
1351
1352 for (unsigned i = 0; i < NumVectorElements / 8; ++i) {
1353 uint8_t x = 0;
1354 for (unsigned j = 0; j < 8; ++j) {
1355 uint8_t y = CV->getOperand(i * 8 + j)->getUniqueInteger().getZExtValue();
1356 x |= y << (7 - j);
1357 }
1358 assert((x == 0 || x == 255) && "Either all 0's or all 1's expected!");
1359 NewConst.push_back(IRB.getInt8(x));
1360 }
1361 return ConstantVector::get(NewConst);
1362}
1363
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001364SDValue
Sirish Pande69295b82012-05-10 20:20:25 +00001365HexagonTargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
1366 EVT ValTy = Op.getValueType();
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001367 ConstantPoolSDNode *CPN = cast<ConstantPoolSDNode>(Op);
Krzysztof Parzyszek91ff5c62017-08-01 13:12:53 +00001368 Constant *CVal = nullptr;
1369 bool isVTi1Type = false;
1370 if (const Constant *ConstVal = dyn_cast<Constant>(CPN->getConstVal())) {
1371 Type *CValTy = ConstVal->getType();
1372 if (CValTy->isVectorTy() &&
1373 CValTy->getVectorElementType()->isIntegerTy(1)) {
1374 CVal = convert_i1_to_i8(ConstVal);
1375 isVTi1Type = (CVal != nullptr);
1376 }
1377 }
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001378 unsigned Align = CPN->getAlignment();
Rafael Espindola405e25a2016-06-26 22:24:01 +00001379 bool IsPositionIndependent = isPositionIndependent();
1380 unsigned char TF = IsPositionIndependent ? HexagonII::MO_PCREL : 0;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001381
Ron Lieberman822ee882016-08-13 23:41:11 +00001382 unsigned Offset = 0;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001383 SDValue T;
1384 if (CPN->isMachineConstantPoolEntry())
Ron Lieberman822ee882016-08-13 23:41:11 +00001385 T = DAG.getTargetConstantPool(CPN->getMachineCPVal(), ValTy, Align, Offset,
1386 TF);
Krzysztof Parzyszek91ff5c62017-08-01 13:12:53 +00001387 else if (isVTi1Type)
1388 T = DAG.getTargetConstantPool(CVal, ValTy, Align, Offset, TF);
Sirish Pande69295b82012-05-10 20:20:25 +00001389 else
Ron Lieberman822ee882016-08-13 23:41:11 +00001390 T = DAG.getTargetConstantPool(CPN->getConstVal(), ValTy, Align, Offset,
1391 TF);
1392
1393 assert(cast<ConstantPoolSDNode>(T)->getTargetFlags() == TF &&
1394 "Inconsistent target flag encountered");
1395
Rafael Espindola405e25a2016-06-26 22:24:01 +00001396 if (IsPositionIndependent)
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001397 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), ValTy, T);
1398 return DAG.getNode(HexagonISD::CP, SDLoc(Op), ValTy, T);
1399}
1400
1401SDValue
1402HexagonTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
1403 EVT VT = Op.getValueType();
1404 int Idx = cast<JumpTableSDNode>(Op)->getIndex();
Rafael Espindola405e25a2016-06-26 22:24:01 +00001405 if (isPositionIndependent()) {
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001406 SDValue T = DAG.getTargetJumpTable(Idx, VT, HexagonII::MO_PCREL);
1407 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), VT, T);
1408 }
1409
1410 SDValue T = DAG.getTargetJumpTable(Idx, VT);
1411 return DAG.getNode(HexagonISD::JT, SDLoc(Op), VT, T);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001412}
1413
1414SDValue
1415HexagonTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001416 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001417 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00001418 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001419 MFI.setReturnAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001420
Bill Wendling908bf812014-01-06 00:43:20 +00001421 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001422 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001423
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001424 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001425 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001426 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1427 if (Depth) {
1428 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001429 SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001430 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
1431 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Justin Lebar9c375812016-07-15 18:27:10 +00001432 MachinePointerInfo());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001433 }
1434
1435 // Return LR, which contains the return address. Mark it an implicit live-in.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001436 unsigned Reg = MF.addLiveIn(HRI.getRARegister(), getRegClassFor(MVT::i32));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001437 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
1438}
1439
1440SDValue
1441HexagonTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001442 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Matthias Braun941a7052016-07-28 18:40:00 +00001443 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001444 MFI.setFrameAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001445
1446 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001447 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001448 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1449 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001450 HRI.getFrameRegister(), VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001451 while (Depth--)
1452 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00001453 MachinePointerInfo());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001454 return FrameAddr;
1455}
1456
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001457SDValue
1458HexagonTargetLowering::LowerATOMIC_FENCE(SDValue Op, SelectionDAG& DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001459 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001460 return DAG.getNode(HexagonISD::BARRIER, dl, MVT::Other, Op.getOperand(0));
1461}
1462
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001463SDValue
1464HexagonTargetLowering::LowerGLOBALADDRESS(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001465 SDLoc dl(Op);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001466 auto *GAN = cast<GlobalAddressSDNode>(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00001467 auto PtrVT = getPointerTy(DAG.getDataLayout());
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001468 auto *GV = GAN->getGlobal();
1469 int64_t Offset = GAN->getOffset();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001470
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001471 auto &HLOF = *HTM.getObjFileLowering();
1472 Reloc::Model RM = HTM.getRelocationModel();
1473
1474 if (RM == Reloc::Static) {
1475 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset);
Peter Collingbourne67335642016-10-24 19:23:39 +00001476 const GlobalObject *GO = GV->getBaseObject();
1477 if (GO && HLOF.isGlobalInSmallSection(GO, HTM))
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001478 return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, GA);
1479 return DAG.getNode(HexagonISD::CONST32, dl, PtrVT, GA);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001480 }
1481
Rafael Espindola3beef8d2016-06-27 23:15:57 +00001482 bool UsePCRel = getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001483 if (UsePCRel) {
1484 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset,
1485 HexagonII::MO_PCREL);
1486 return DAG.getNode(HexagonISD::AT_PCREL, dl, PtrVT, GA);
1487 }
1488
1489 // Use GOT index.
1490 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
1491 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, HexagonII::MO_GOT);
1492 SDValue Off = DAG.getConstant(Offset, dl, MVT::i32);
1493 return DAG.getNode(HexagonISD::AT_GOT, dl, PtrVT, GOT, GA, Off);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001494}
1495
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001496// Specifies that for loads and stores VT can be promoted to PromotedLdStVT.
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001497SDValue
1498HexagonTargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
1499 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001500 SDLoc dl(Op);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001501 EVT PtrVT = getPointerTy(DAG.getDataLayout());
1502
1503 Reloc::Model RM = HTM.getRelocationModel();
1504 if (RM == Reloc::Static) {
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001505 SDValue A = DAG.getTargetBlockAddress(BA, PtrVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001506 return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, A);
1507 }
1508
1509 SDValue A = DAG.getTargetBlockAddress(BA, PtrVT, 0, HexagonII::MO_PCREL);
1510 return DAG.getNode(HexagonISD::AT_PCREL, dl, PtrVT, A);
1511}
1512
1513SDValue
1514HexagonTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op, SelectionDAG &DAG)
1515 const {
1516 EVT PtrVT = getPointerTy(DAG.getDataLayout());
1517 SDValue GOTSym = DAG.getTargetExternalSymbol(HEXAGON_GOT_SYM_NAME, PtrVT,
1518 HexagonII::MO_PCREL);
1519 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), PtrVT, GOTSym);
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001520}
1521
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001522SDValue
1523HexagonTargetLowering::GetDynamicTLSAddr(SelectionDAG &DAG, SDValue Chain,
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001524 GlobalAddressSDNode *GA, SDValue Glue, EVT PtrVT, unsigned ReturnReg,
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001525 unsigned char OperandFlags) const {
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001526 MachineFunction &MF = DAG.getMachineFunction();
1527 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001528 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1529 SDLoc dl(GA);
1530 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
1531 GA->getValueType(0),
1532 GA->getOffset(),
1533 OperandFlags);
1534 // Create Operands for the call.The Operands should have the following:
1535 // 1. Chain SDValue
1536 // 2. Callee which in this case is the Global address value.
1537 // 3. Registers live into the call.In this case its R0, as we
1538 // have just one argument to be passed.
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001539 // 4. Glue.
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001540 // Note: The order is important.
1541
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001542 const auto &HRI = *Subtarget.getRegisterInfo();
1543 const uint32_t *Mask = HRI.getCallPreservedMask(MF, CallingConv::C);
1544 assert(Mask && "Missing call preserved mask for calling convention");
1545 SDValue Ops[] = { Chain, TGA, DAG.getRegister(Hexagon::R0, PtrVT),
1546 DAG.getRegisterMask(Mask), Glue };
1547 Chain = DAG.getNode(HexagonISD::CALL, dl, NodeTys, Ops);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001548
1549 // Inform MFI that function has calls.
Matthias Braun941a7052016-07-28 18:40:00 +00001550 MFI.setAdjustsStack(true);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001551
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001552 Glue = Chain.getValue(1);
1553 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Glue);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001554}
1555
1556//
1557// Lower using the intial executable model for TLS addresses
1558//
1559SDValue
1560HexagonTargetLowering::LowerToTLSInitialExecModel(GlobalAddressSDNode *GA,
1561 SelectionDAG &DAG) const {
1562 SDLoc dl(GA);
1563 int64_t Offset = GA->getOffset();
1564 auto PtrVT = getPointerTy(DAG.getDataLayout());
1565
1566 // Get the thread pointer.
1567 SDValue TP = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Hexagon::UGP, PtrVT);
1568
Rafael Espindola405e25a2016-06-26 22:24:01 +00001569 bool IsPositionIndependent = isPositionIndependent();
1570 unsigned char TF =
1571 IsPositionIndependent ? HexagonII::MO_IEGOT : HexagonII::MO_IE;
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001572
1573 // First generate the TLS symbol address
1574 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT,
1575 Offset, TF);
1576
1577 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1578
Rafael Espindola405e25a2016-06-26 22:24:01 +00001579 if (IsPositionIndependent) {
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001580 // Generate the GOT pointer in case of position independent code
1581 SDValue GOT = LowerGLOBAL_OFFSET_TABLE(Sym, DAG);
1582
1583 // Add the TLS Symbol address to GOT pointer.This gives
1584 // GOT relative relocation for the symbol.
1585 Sym = DAG.getNode(ISD::ADD, dl, PtrVT, GOT, Sym);
1586 }
1587
1588 // Load the offset value for TLS symbol.This offset is relative to
1589 // thread pointer.
Justin Lebar9c375812016-07-15 18:27:10 +00001590 SDValue LoadOffset =
1591 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Sym, MachinePointerInfo());
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001592
1593 // Address of the thread local variable is the add of thread
1594 // pointer and the offset of the variable.
1595 return DAG.getNode(ISD::ADD, dl, PtrVT, TP, LoadOffset);
1596}
1597
1598//
1599// Lower using the local executable model for TLS addresses
1600//
1601SDValue
1602HexagonTargetLowering::LowerToTLSLocalExecModel(GlobalAddressSDNode *GA,
1603 SelectionDAG &DAG) const {
1604 SDLoc dl(GA);
1605 int64_t Offset = GA->getOffset();
1606 auto PtrVT = getPointerTy(DAG.getDataLayout());
1607
1608 // Get the thread pointer.
1609 SDValue TP = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Hexagon::UGP, PtrVT);
1610 // Generate the TLS symbol address
1611 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT, Offset,
1612 HexagonII::MO_TPREL);
1613 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1614
1615 // Address of the thread local variable is the add of thread
1616 // pointer and the offset of the variable.
1617 return DAG.getNode(ISD::ADD, dl, PtrVT, TP, Sym);
1618}
1619
1620//
1621// Lower using the general dynamic model for TLS addresses
1622//
1623SDValue
1624HexagonTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
1625 SelectionDAG &DAG) const {
1626 SDLoc dl(GA);
1627 int64_t Offset = GA->getOffset();
1628 auto PtrVT = getPointerTy(DAG.getDataLayout());
1629
1630 // First generate the TLS symbol address
1631 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT, Offset,
1632 HexagonII::MO_GDGOT);
1633
1634 // Then, generate the GOT pointer
1635 SDValue GOT = LowerGLOBAL_OFFSET_TABLE(TGA, DAG);
1636
1637 // Add the TLS symbol and the GOT pointer
1638 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1639 SDValue Chain = DAG.getNode(ISD::ADD, dl, PtrVT, GOT, Sym);
1640
1641 // Copy over the argument to R0
1642 SDValue InFlag;
1643 Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, Hexagon::R0, Chain, InFlag);
1644 InFlag = Chain.getValue(1);
1645
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +00001646 unsigned Flags =
1647 static_cast<const HexagonSubtarget &>(DAG.getSubtarget()).useLongCalls()
1648 ? HexagonII::MO_GDPLT | HexagonII::HMOTF_ConstExtended
1649 : HexagonII::MO_GDPLT;
1650
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001651 return GetDynamicTLSAddr(DAG, Chain, GA, InFlag, PtrVT,
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +00001652 Hexagon::R0, Flags);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001653}
1654
1655//
1656// Lower TLS addresses.
1657//
1658// For now for dynamic models, we only support the general dynamic model.
1659//
1660SDValue
1661HexagonTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1662 SelectionDAG &DAG) const {
1663 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1664
1665 switch (HTM.getTLSModel(GA->getGlobal())) {
1666 case TLSModel::GeneralDynamic:
1667 case TLSModel::LocalDynamic:
1668 return LowerToTLSGeneralDynamicModel(GA, DAG);
1669 case TLSModel::InitialExec:
1670 return LowerToTLSInitialExecModel(GA, DAG);
1671 case TLSModel::LocalExec:
1672 return LowerToTLSLocalExecModel(GA, DAG);
1673 }
1674 llvm_unreachable("Bogus TLS model");
1675}
1676
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001677//===----------------------------------------------------------------------===//
1678// TargetLowering Implementation
1679//===----------------------------------------------------------------------===//
1680
Eric Christopherd737b762015-02-02 22:11:36 +00001681HexagonTargetLowering::HexagonTargetLowering(const TargetMachine &TM,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001682 const HexagonSubtarget &ST)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001683 : TargetLowering(TM), HTM(static_cast<const HexagonTargetMachine&>(TM)),
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001684 Subtarget(ST) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001685 bool IsV4 = !Subtarget.hasV5TOps();
1686 auto &HRI = *Subtarget.getRegisterInfo();
Sirish Pande69295b82012-05-10 20:20:25 +00001687
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001688 setPrefLoopAlignment(4);
1689 setPrefFunctionAlignment(4);
1690 setMinFunctionAlignment(2);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001691 setStackPointerRegisterToSaveRestore(HRI.getStackRegister());
1692
Krzysztof Parzyszekf228c952016-06-22 16:07:10 +00001693 setMaxAtomicSizeInBitsSupported(64);
1694 setMinCmpXchgSizeInBits(32);
1695
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001696 if (EnableHexSDNodeSched)
1697 setSchedulingPreference(Sched::VLIW);
1698 else
1699 setSchedulingPreference(Sched::Source);
1700
1701 // Limits for inline expansion of memcpy/memmove
1702 MaxStoresPerMemcpy = MaxStoresPerMemcpyCL;
1703 MaxStoresPerMemcpyOptSize = MaxStoresPerMemcpyOptSizeCL;
1704 MaxStoresPerMemmove = MaxStoresPerMemmoveCL;
1705 MaxStoresPerMemmoveOptSize = MaxStoresPerMemmoveOptSizeCL;
1706 MaxStoresPerMemset = MaxStoresPerMemsetCL;
1707 MaxStoresPerMemsetOptSize = MaxStoresPerMemsetOptSizeCL;
1708
1709 //
1710 // Set up register classes.
1711 //
1712
1713 addRegisterClass(MVT::i1, &Hexagon::PredRegsRegClass);
1714 addRegisterClass(MVT::v2i1, &Hexagon::PredRegsRegClass); // bbbbaaaa
1715 addRegisterClass(MVT::v4i1, &Hexagon::PredRegsRegClass); // ddccbbaa
1716 addRegisterClass(MVT::v8i1, &Hexagon::PredRegsRegClass); // hgfedcba
1717 addRegisterClass(MVT::i32, &Hexagon::IntRegsRegClass);
1718 addRegisterClass(MVT::v4i8, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001719 addRegisterClass(MVT::v2i16, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001720 addRegisterClass(MVT::i64, &Hexagon::DoubleRegsRegClass);
1721 addRegisterClass(MVT::v8i8, &Hexagon::DoubleRegsRegClass);
1722 addRegisterClass(MVT::v4i16, &Hexagon::DoubleRegsRegClass);
1723 addRegisterClass(MVT::v2i32, &Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001724
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001725 if (Subtarget.hasV5TOps()) {
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001726 addRegisterClass(MVT::f32, &Hexagon::IntRegsRegClass);
1727 addRegisterClass(MVT::f64, &Hexagon::DoubleRegsRegClass);
1728 }
Sirish Pande69295b82012-05-10 20:20:25 +00001729
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001730 if (Subtarget.hasV60TOps()) {
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +00001731 if (Subtarget.useHVX64BOps()) {
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001732 addRegisterClass(MVT::v64i8, &Hexagon::HvxVRRegClass);
1733 addRegisterClass(MVT::v32i16, &Hexagon::HvxVRRegClass);
1734 addRegisterClass(MVT::v16i32, &Hexagon::HvxVRRegClass);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001735 addRegisterClass(MVT::v128i8, &Hexagon::HvxWRRegClass);
1736 addRegisterClass(MVT::v64i16, &Hexagon::HvxWRRegClass);
1737 addRegisterClass(MVT::v32i32, &Hexagon::HvxWRRegClass);
Krzysztof Parzyszek47076052017-12-14 21:28:48 +00001738 addRegisterClass(MVT::v16i1, &Hexagon::HvxQRRegClass);
1739 addRegisterClass(MVT::v32i1, &Hexagon::HvxQRRegClass);
1740 addRegisterClass(MVT::v64i1, &Hexagon::HvxQRRegClass);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001741 addRegisterClass(MVT::v512i1, &Hexagon::HvxQRRegClass);
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +00001742 } else if (Subtarget.useHVX128BOps()) {
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001743 addRegisterClass(MVT::v128i8, &Hexagon::HvxVRRegClass);
1744 addRegisterClass(MVT::v64i16, &Hexagon::HvxVRRegClass);
1745 addRegisterClass(MVT::v32i32, &Hexagon::HvxVRRegClass);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001746 addRegisterClass(MVT::v256i8, &Hexagon::HvxWRRegClass);
1747 addRegisterClass(MVT::v128i16, &Hexagon::HvxWRRegClass);
1748 addRegisterClass(MVT::v64i32, &Hexagon::HvxWRRegClass);
Krzysztof Parzyszek47076052017-12-14 21:28:48 +00001749 addRegisterClass(MVT::v32i1, &Hexagon::HvxQRRegClass);
1750 addRegisterClass(MVT::v64i1, &Hexagon::HvxQRRegClass);
1751 addRegisterClass(MVT::v128i1, &Hexagon::HvxQRRegClass);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001752 addRegisterClass(MVT::v1024i1, &Hexagon::HvxQRRegClass);
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001753 }
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001754 }
1755
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001756 //
1757 // Handling of scalar operations.
1758 //
1759 // All operations default to "legal", except:
1760 // - indexed loads and stores (pre-/post-incremented),
1761 // - ANY_EXTEND_VECTOR_INREG, ATOMIC_CMP_SWAP_WITH_SUCCESS, CONCAT_VECTORS,
1762 // ConstantFP, DEBUGTRAP, FCEIL, FCOPYSIGN, FEXP, FEXP2, FFLOOR, FGETSIGN,
1763 // FLOG, FLOG2, FLOG10, FMAXNUM, FMINNUM, FNEARBYINT, FRINT, FROUND, TRAP,
1764 // FTRUNC, PREFETCH, SIGN_EXTEND_VECTOR_INREG, ZERO_EXTEND_VECTOR_INREG,
1765 // which default to "expand" for at least one type.
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001766
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001767 // Misc operations.
1768 setOperationAction(ISD::ConstantFP, MVT::f32, Legal); // Default: expand
1769 setOperationAction(ISD::ConstantFP, MVT::f64, Legal); // Default: expand
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001770
1771 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001772 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001773 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001774 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
1775 setOperationAction(ISD::INLINEASM, MVT::Other, Custom);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00001776 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +00001777 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00001778 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001779 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001780 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001781 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001782 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001783
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001784 // Custom legalize GlobalAddress nodes into CONST32.
1785 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001786 setOperationAction(ISD::GlobalAddress, MVT::i8, Custom);
1787 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001788
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001789 // Hexagon needs to optimize cases with negative constants.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001790 setOperationAction(ISD::SETCC, MVT::i8, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001791 setOperationAction(ISD::SETCC, MVT::i16, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001792
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001793 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
1794 setOperationAction(ISD::VASTART, MVT::Other, Custom);
1795 setOperationAction(ISD::VAEND, MVT::Other, Expand);
1796 setOperationAction(ISD::VAARG, MVT::Other, Expand);
1797
1798 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
1799 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
1800 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
1801
1802 if (EmitJumpTables)
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001803 setMinimumJumpTableEntries(MinimumJumpTables);
Krzysztof Parzyszeka61f7da2016-01-13 21:43:13 +00001804 else
Eugene Zelenko58655bb2016-12-17 01:09:05 +00001805 setMinimumJumpTableEntries(std::numeric_limits<int>::max());
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001806 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001807
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001808 // Hexagon has instructions for add/sub with carry. The problem with
1809 // modeling these instructions is that they produce 2 results: Rdd and Px.
1810 // To model the update of Px, we will have to use Defs[p0..p3] which will
1811 // cause any predicate live range to spill. So, we pretend we dont't have
1812 // these instructions.
1813 setOperationAction(ISD::ADDE, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001814 setOperationAction(ISD::ADDE, MVT::i16, Expand);
1815 setOperationAction(ISD::ADDE, MVT::i32, Expand);
1816 setOperationAction(ISD::ADDE, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001817 setOperationAction(ISD::SUBE, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001818 setOperationAction(ISD::SUBE, MVT::i16, Expand);
1819 setOperationAction(ISD::SUBE, MVT::i32, Expand);
1820 setOperationAction(ISD::SUBE, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001821 setOperationAction(ISD::ADDC, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001822 setOperationAction(ISD::ADDC, MVT::i16, Expand);
1823 setOperationAction(ISD::ADDC, MVT::i32, Expand);
1824 setOperationAction(ISD::ADDC, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001825 setOperationAction(ISD::SUBC, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001826 setOperationAction(ISD::SUBC, MVT::i16, Expand);
1827 setOperationAction(ISD::SUBC, MVT::i32, Expand);
1828 setOperationAction(ISD::SUBC, MVT::i64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001829
Krzysztof Parzyszek2c4487d2015-04-13 20:37:01 +00001830 // Only add and sub that detect overflow are the saturating ones.
1831 for (MVT VT : MVT::integer_valuetypes()) {
1832 setOperationAction(ISD::UADDO, VT, Expand);
1833 setOperationAction(ISD::SADDO, VT, Expand);
1834 setOperationAction(ISD::USUBO, VT, Expand);
1835 setOperationAction(ISD::SSUBO, VT, Expand);
1836 }
1837
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001838 setOperationAction(ISD::CTLZ, MVT::i8, Promote);
1839 setOperationAction(ISD::CTLZ, MVT::i16, Promote);
1840 setOperationAction(ISD::CTTZ, MVT::i8, Promote);
1841 setOperationAction(ISD::CTTZ, MVT::i16, Promote);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001842
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001843 // In V5, popcount can count # of 1s in i64 but returns i32.
1844 // On V4 it will be expanded (set later).
1845 setOperationAction(ISD::CTPOP, MVT::i8, Promote);
1846 setOperationAction(ISD::CTPOP, MVT::i16, Promote);
1847 setOperationAction(ISD::CTPOP, MVT::i32, Promote);
Krzysztof Parzyszekaf5ff652017-02-23 15:02:09 +00001848 setOperationAction(ISD::CTPOP, MVT::i64, Legal);
1849
1850 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
1851 setOperationAction(ISD::BITREVERSE, MVT::i64, Legal);
1852 setOperationAction(ISD::BSWAP, MVT::i32, Legal);
1853 setOperationAction(ISD::BSWAP, MVT::i64, Legal);
Krzysztof Parzyszekef580172017-05-30 17:47:51 +00001854 setOperationAction(ISD::MUL, MVT::i64, Legal);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001855
Benjamin Kramer62460692015-04-25 14:46:53 +00001856 for (unsigned IntExpOp :
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001857 { ISD::SDIV, ISD::UDIV, ISD::SREM, ISD::UREM,
1858 ISD::SDIVREM, ISD::UDIVREM, ISD::ROTL, ISD::ROTR,
Krzysztof Parzyszekaf5ff652017-02-23 15:02:09 +00001859 ISD::SHL_PARTS, ISD::SRA_PARTS, ISD::SRL_PARTS,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001860 ISD::SMUL_LOHI, ISD::UMUL_LOHI }) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001861 setOperationAction(IntExpOp, MVT::i32, Expand);
1862 setOperationAction(IntExpOp, MVT::i64, Expand);
1863 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001864
Benjamin Kramer62460692015-04-25 14:46:53 +00001865 for (unsigned FPExpOp :
1866 {ISD::FDIV, ISD::FREM, ISD::FSQRT, ISD::FSIN, ISD::FCOS, ISD::FSINCOS,
1867 ISD::FPOW, ISD::FCOPYSIGN}) {
1868 setOperationAction(FPExpOp, MVT::f32, Expand);
1869 setOperationAction(FPExpOp, MVT::f64, Expand);
1870 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001871
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001872 // No extending loads from i32.
1873 for (MVT VT : MVT::integer_valuetypes()) {
1874 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
1875 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
1876 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
1877 }
1878 // Turn FP truncstore into trunc + store.
1879 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Michael Kuperstein2bc3d4d2016-08-18 20:08:15 +00001880 // Turn FP extload into load/fpextend.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001881 for (MVT VT : MVT::fp_valuetypes())
1882 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001883
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001884 // Expand BR_CC and SELECT_CC for all integer and fp types.
1885 for (MVT VT : MVT::integer_valuetypes()) {
1886 setOperationAction(ISD::BR_CC, VT, Expand);
1887 setOperationAction(ISD::SELECT_CC, VT, Expand);
1888 }
1889 for (MVT VT : MVT::fp_valuetypes()) {
1890 setOperationAction(ISD::BR_CC, VT, Expand);
1891 setOperationAction(ISD::SELECT_CC, VT, Expand);
1892 }
1893 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001894
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001895 //
1896 // Handling of vector operations.
1897 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001898
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001899 promoteLdStType(MVT::v4i8, MVT::i32);
1900 promoteLdStType(MVT::v2i16, MVT::i32);
1901 promoteLdStType(MVT::v8i8, MVT::i64);
Krzysztof Parzyszek5eef92e2017-07-17 15:45:45 +00001902 promoteLdStType(MVT::v4i16, MVT::i64);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001903 promoteLdStType(MVT::v2i32, MVT::i64);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001904
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001905 // Set the action for vector operations to "expand", then override it with
1906 // either "custom" or "legal" for specific cases.
Craig Topper26260942015-10-18 05:15:34 +00001907 static const unsigned VectExpOps[] = {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001908 // Integer arithmetic:
1909 ISD::ADD, ISD::SUB, ISD::MUL, ISD::SDIV, ISD::UDIV,
1910 ISD::SREM, ISD::UREM, ISD::SDIVREM, ISD::UDIVREM, ISD::ADDC,
1911 ISD::SUBC, ISD::SADDO, ISD::UADDO, ISD::SSUBO, ISD::USUBO,
1912 ISD::SMUL_LOHI, ISD::UMUL_LOHI,
1913 // Logical/bit:
1914 ISD::AND, ISD::OR, ISD::XOR, ISD::ROTL, ISD::ROTR,
Craig Topper33772c52016-04-28 03:34:31 +00001915 ISD::CTPOP, ISD::CTLZ, ISD::CTTZ,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001916 // Floating point arithmetic/math functions:
1917 ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FMA, ISD::FDIV,
1918 ISD::FREM, ISD::FNEG, ISD::FABS, ISD::FSQRT, ISD::FSIN,
Craig Topperf6d4dc52017-05-30 15:27:55 +00001919 ISD::FCOS, ISD::FPOW, ISD::FLOG, ISD::FLOG2,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001920 ISD::FLOG10, ISD::FEXP, ISD::FEXP2, ISD::FCEIL, ISD::FTRUNC,
1921 ISD::FRINT, ISD::FNEARBYINT, ISD::FROUND, ISD::FFLOOR,
1922 ISD::FMINNUM, ISD::FMAXNUM, ISD::FSINCOS,
1923 // Misc:
Krzysztof Parzyszek046da742016-10-27 14:30:16 +00001924 ISD::BR_CC, ISD::SELECT_CC, ISD::ConstantPool,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001925 // Vector:
1926 ISD::BUILD_VECTOR, ISD::SCALAR_TO_VECTOR,
1927 ISD::EXTRACT_VECTOR_ELT, ISD::INSERT_VECTOR_ELT,
1928 ISD::EXTRACT_SUBVECTOR, ISD::INSERT_SUBVECTOR,
1929 ISD::CONCAT_VECTORS, ISD::VECTOR_SHUFFLE
1930 };
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001931
1932 for (MVT VT : MVT::vector_valuetypes()) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001933 for (unsigned VectExpOp : VectExpOps)
1934 setOperationAction(VectExpOp, VT, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001935
Krzysztof Parzyszeka696b1b2016-09-08 17:42:14 +00001936 // Expand all extending loads and truncating stores:
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001937 for (MVT TargetVT : MVT::vector_valuetypes()) {
Krzysztof Parzyszeka696b1b2016-09-08 17:42:14 +00001938 if (TargetVT == VT)
1939 continue;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001940 setLoadExtAction(ISD::EXTLOAD, TargetVT, VT, Expand);
Krzysztof Parzyszeka696b1b2016-09-08 17:42:14 +00001941 setLoadExtAction(ISD::ZEXTLOAD, TargetVT, VT, Expand);
1942 setLoadExtAction(ISD::SEXTLOAD, TargetVT, VT, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001943 setTruncStoreAction(VT, TargetVT, Expand);
1944 }
1945
Krzysztof Parzyszek046da742016-10-27 14:30:16 +00001946 // Normalize all inputs to SELECT to be vectors of i32.
1947 if (VT.getVectorElementType() != MVT::i32) {
1948 MVT VT32 = MVT::getVectorVT(MVT::i32, VT.getSizeInBits()/32);
1949 setOperationAction(ISD::SELECT, VT, Promote);
1950 AddPromotedToType(ISD::SELECT, VT, VT32);
1951 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001952 setOperationAction(ISD::SRA, VT, Custom);
1953 setOperationAction(ISD::SHL, VT, Custom);
1954 setOperationAction(ISD::SRL, VT, Custom);
1955 }
1956
Krzysztof Parzyszek64e5d7d2017-10-20 19:33:12 +00001957 // Extending loads from (native) vectors of i8 into (native) vectors of i16
1958 // are legal.
1959 setLoadExtAction(ISD::EXTLOAD, MVT::v2i16, MVT::v2i8, Legal);
1960 setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i16, MVT::v2i8, Legal);
1961 setLoadExtAction(ISD::SEXTLOAD, MVT::v2i16, MVT::v2i8, Legal);
1962 setLoadExtAction(ISD::EXTLOAD, MVT::v4i16, MVT::v4i8, Legal);
1963 setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i16, MVT::v4i8, Legal);
1964 setLoadExtAction(ISD::SEXTLOAD, MVT::v4i16, MVT::v4i8, Legal);
1965
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001966 // Types natively supported:
Benjamin Kramer62460692015-04-25 14:46:53 +00001967 for (MVT NativeVT : {MVT::v2i1, MVT::v4i1, MVT::v8i1, MVT::v32i1, MVT::v64i1,
1968 MVT::v4i8, MVT::v8i8, MVT::v2i16, MVT::v4i16, MVT::v1i32,
1969 MVT::v2i32, MVT::v1i64}) {
1970 setOperationAction(ISD::BUILD_VECTOR, NativeVT, Custom);
1971 setOperationAction(ISD::EXTRACT_VECTOR_ELT, NativeVT, Custom);
1972 setOperationAction(ISD::INSERT_VECTOR_ELT, NativeVT, Custom);
1973 setOperationAction(ISD::EXTRACT_SUBVECTOR, NativeVT, Custom);
1974 setOperationAction(ISD::INSERT_SUBVECTOR, NativeVT, Custom);
1975 setOperationAction(ISD::CONCAT_VECTORS, NativeVT, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001976
Benjamin Kramer62460692015-04-25 14:46:53 +00001977 setOperationAction(ISD::ADD, NativeVT, Legal);
1978 setOperationAction(ISD::SUB, NativeVT, Legal);
1979 setOperationAction(ISD::MUL, NativeVT, Legal);
1980 setOperationAction(ISD::AND, NativeVT, Legal);
1981 setOperationAction(ISD::OR, NativeVT, Legal);
1982 setOperationAction(ISD::XOR, NativeVT, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001983 }
1984
1985 setOperationAction(ISD::SETCC, MVT::v2i16, Custom);
1986 setOperationAction(ISD::VSELECT, MVT::v2i16, Custom);
1987 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
1988 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00001989
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001990 auto setPromoteTo = [this] (unsigned Opc, MVT FromTy, MVT ToTy) {
1991 setOperationAction(Opc, FromTy, Promote);
1992 AddPromotedToType(Opc, FromTy, ToTy);
1993 };
1994
Sumanth Gundapaneni9d954c42017-10-18 17:45:22 +00001995 if (Subtarget.useHVXOps()) {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001996 bool Use64b = Subtarget.useHVX64BOps();
1997 ArrayRef<MVT> LegalV = Use64b ? LegalV64 : LegalV128;
1998 ArrayRef<MVT> LegalW = Use64b ? LegalW64 : LegalW128;
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00001999 MVT ByteV = Use64b ? MVT::v64i8 : MVT::v128i8;
2000 MVT ByteW = Use64b ? MVT::v128i8 : MVT::v256i8;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002001
2002 setOperationAction(ISD::VECTOR_SHUFFLE, ByteV, Legal);
2003 setOperationAction(ISD::VECTOR_SHUFFLE, ByteW, Legal);
2004 setOperationAction(ISD::CONCAT_VECTORS, ByteW, Legal);
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00002005 setOperationAction(ISD::AND, ByteV, Legal);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002006 setOperationAction(ISD::OR, ByteV, Legal);
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00002007 setOperationAction(ISD::XOR, ByteV, Legal);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002008
2009 for (MVT T : LegalV) {
2010 setIndexedLoadAction(ISD::POST_INC, T, Legal);
2011 setIndexedStoreAction(ISD::POST_INC, T, Legal);
2012
Krzysztof Parzyszek47076052017-12-14 21:28:48 +00002013 setOperationAction(ISD::ADD, T, Legal);
2014 setOperationAction(ISD::SUB, T, Legal);
2015 setOperationAction(ISD::VSELECT, T, Legal);
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00002016
Krzysztof Parzyszek47076052017-12-14 21:28:48 +00002017 setOperationAction(ISD::MUL, T, Custom);
2018 setOperationAction(ISD::SETCC, T, Custom);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002019 setOperationAction(ISD::BUILD_VECTOR, T, Custom);
2020 setOperationAction(ISD::INSERT_SUBVECTOR, T, Custom);
2021 setOperationAction(ISD::INSERT_VECTOR_ELT, T, Custom);
2022 setOperationAction(ISD::EXTRACT_SUBVECTOR, T, Custom);
2023 setOperationAction(ISD::EXTRACT_VECTOR_ELT, T, Custom);
2024 }
2025
2026 for (MVT T : LegalV) {
2027 if (T == ByteV)
2028 continue;
2029 // Promote all shuffles and concats to operate on vectors of bytes.
2030 setPromoteTo(ISD::VECTOR_SHUFFLE, T, ByteV);
2031 setPromoteTo(ISD::CONCAT_VECTORS, T, ByteV);
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00002032 setPromoteTo(ISD::AND, T, ByteV);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002033 setPromoteTo(ISD::OR, T, ByteV);
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00002034 setPromoteTo(ISD::XOR, T, ByteV);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002035 }
2036
2037 for (MVT T : LegalW) {
2038 if (T == ByteW)
2039 continue;
2040 // Promote all shuffles and concats to operate on vectors of bytes.
2041 setPromoteTo(ISD::VECTOR_SHUFFLE, T, ByteW);
2042 setPromoteTo(ISD::CONCAT_VECTORS, T, ByteW);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002043 }
2044 }
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002045
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002046 // Subtarget-specific operation actions.
2047 //
2048 if (Subtarget.hasV5TOps()) {
2049 setOperationAction(ISD::FMA, MVT::f64, Expand);
2050 setOperationAction(ISD::FADD, MVT::f64, Expand);
2051 setOperationAction(ISD::FSUB, MVT::f64, Expand);
2052 setOperationAction(ISD::FMUL, MVT::f64, Expand);
2053
Krzysztof Parzyszekbd8ef4b2016-08-19 13:34:31 +00002054 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
2055 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
2056
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002057 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Promote);
2058 setOperationAction(ISD::FP_TO_UINT, MVT::i8, Promote);
2059 setOperationAction(ISD::FP_TO_UINT, MVT::i16, Promote);
2060 setOperationAction(ISD::FP_TO_SINT, MVT::i1, Promote);
2061 setOperationAction(ISD::FP_TO_SINT, MVT::i8, Promote);
2062 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
2063 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote);
2064 setOperationAction(ISD::UINT_TO_FP, MVT::i8, Promote);
2065 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote);
2066 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote);
2067 setOperationAction(ISD::SINT_TO_FP, MVT::i8, Promote);
2068 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002069 } else { // V4
2070 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
2071 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Expand);
2072 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
2073 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
2074 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Expand);
2075 setOperationAction(ISD::FP_TO_SINT, MVT::f32, Expand);
2076 setOperationAction(ISD::FP_EXTEND, MVT::f32, Expand);
2077 setOperationAction(ISD::FP_ROUND, MVT::f64, Expand);
2078 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
2079
2080 setOperationAction(ISD::CTPOP, MVT::i8, Expand);
2081 setOperationAction(ISD::CTPOP, MVT::i16, Expand);
2082 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
2083 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
2084
2085 // Expand these operations for both f32 and f64:
Benjamin Kramer62460692015-04-25 14:46:53 +00002086 for (unsigned FPExpOpV4 :
2087 {ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FABS, ISD::FNEG, ISD::FMA}) {
2088 setOperationAction(FPExpOpV4, MVT::f32, Expand);
2089 setOperationAction(FPExpOpV4, MVT::f64, Expand);
2090 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002091
Benjamin Kramer62460692015-04-25 14:46:53 +00002092 for (ISD::CondCode FPExpCCV4 :
2093 {ISD::SETOEQ, ISD::SETOGT, ISD::SETOLT, ISD::SETOGE, ISD::SETOLE,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002094 ISD::SETUO, ISD::SETO}) {
Benjamin Kramer62460692015-04-25 14:46:53 +00002095 setCondCodeAction(FPExpCCV4, MVT::f32, Expand);
2096 setCondCodeAction(FPExpCCV4, MVT::f64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002097 }
2098 }
2099
2100 // Handling of indexed loads/stores: default is "expand".
2101 //
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +00002102 for (MVT VT : {MVT::i8, MVT::i16, MVT::i32, MVT::i64}) {
2103 setIndexedLoadAction(ISD::POST_INC, VT, Legal);
2104 setIndexedStoreAction(ISD::POST_INC, VT, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002105 }
2106
2107 computeRegisterProperties(&HRI);
2108
2109 //
2110 // Library calls for unsupported operations
2111 //
2112 bool FastMath = EnableFastMath;
2113
Benjamin Kramera37c8092015-04-25 14:46:46 +00002114 setLibcallName(RTLIB::SDIV_I32, "__hexagon_divsi3");
2115 setLibcallName(RTLIB::SDIV_I64, "__hexagon_divdi3");
2116 setLibcallName(RTLIB::UDIV_I32, "__hexagon_udivsi3");
2117 setLibcallName(RTLIB::UDIV_I64, "__hexagon_udivdi3");
2118 setLibcallName(RTLIB::SREM_I32, "__hexagon_modsi3");
2119 setLibcallName(RTLIB::SREM_I64, "__hexagon_moddi3");
2120 setLibcallName(RTLIB::UREM_I32, "__hexagon_umodsi3");
2121 setLibcallName(RTLIB::UREM_I64, "__hexagon_umoddi3");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002122
Benjamin Kramera37c8092015-04-25 14:46:46 +00002123 setLibcallName(RTLIB::SINTTOFP_I128_F64, "__hexagon_floattidf");
2124 setLibcallName(RTLIB::SINTTOFP_I128_F32, "__hexagon_floattisf");
2125 setLibcallName(RTLIB::FPTOUINT_F32_I128, "__hexagon_fixunssfti");
2126 setLibcallName(RTLIB::FPTOUINT_F64_I128, "__hexagon_fixunsdfti");
2127 setLibcallName(RTLIB::FPTOSINT_F32_I128, "__hexagon_fixsfti");
2128 setLibcallName(RTLIB::FPTOSINT_F64_I128, "__hexagon_fixdfti");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002129
2130 if (IsV4) {
2131 // Handle single-precision floating point operations on V4.
Benjamin Kramera37c8092015-04-25 14:46:46 +00002132 if (FastMath) {
2133 setLibcallName(RTLIB::ADD_F32, "__hexagon_fast_addsf3");
2134 setLibcallName(RTLIB::SUB_F32, "__hexagon_fast_subsf3");
2135 setLibcallName(RTLIB::MUL_F32, "__hexagon_fast_mulsf3");
2136 setLibcallName(RTLIB::OGT_F32, "__hexagon_fast_gtsf2");
2137 setLibcallName(RTLIB::OLT_F32, "__hexagon_fast_ltsf2");
2138 // Double-precision compares.
2139 setLibcallName(RTLIB::OGT_F64, "__hexagon_fast_gtdf2");
2140 setLibcallName(RTLIB::OLT_F64, "__hexagon_fast_ltdf2");
2141 } else {
2142 setLibcallName(RTLIB::ADD_F32, "__hexagon_addsf3");
2143 setLibcallName(RTLIB::SUB_F32, "__hexagon_subsf3");
2144 setLibcallName(RTLIB::MUL_F32, "__hexagon_mulsf3");
2145 setLibcallName(RTLIB::OGT_F32, "__hexagon_gtsf2");
2146 setLibcallName(RTLIB::OLT_F32, "__hexagon_ltsf2");
2147 // Double-precision compares.
2148 setLibcallName(RTLIB::OGT_F64, "__hexagon_gtdf2");
2149 setLibcallName(RTLIB::OLT_F64, "__hexagon_ltdf2");
2150 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002151 }
2152
2153 // This is the only fast library function for sqrtd.
2154 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00002155 setLibcallName(RTLIB::SQRT_F64, "__hexagon_fast2_sqrtdf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002156
Benjamin Kramera37c8092015-04-25 14:46:46 +00002157 // Prefix is: nothing for "slow-math",
2158 // "fast2_" for V4 fast-math and V5+ fast-math double-precision
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002159 // (actually, keep fast-math and fast-math2 separate for now)
Benjamin Kramera37c8092015-04-25 14:46:46 +00002160 if (FastMath) {
2161 setLibcallName(RTLIB::ADD_F64, "__hexagon_fast_adddf3");
2162 setLibcallName(RTLIB::SUB_F64, "__hexagon_fast_subdf3");
2163 setLibcallName(RTLIB::MUL_F64, "__hexagon_fast_muldf3");
2164 setLibcallName(RTLIB::DIV_F64, "__hexagon_fast_divdf3");
2165 // Calling __hexagon_fast2_divsf3 with fast-math on V5 (ok).
2166 setLibcallName(RTLIB::DIV_F32, "__hexagon_fast_divsf3");
2167 } else {
2168 setLibcallName(RTLIB::ADD_F64, "__hexagon_adddf3");
2169 setLibcallName(RTLIB::SUB_F64, "__hexagon_subdf3");
2170 setLibcallName(RTLIB::MUL_F64, "__hexagon_muldf3");
2171 setLibcallName(RTLIB::DIV_F64, "__hexagon_divdf3");
2172 setLibcallName(RTLIB::DIV_F32, "__hexagon_divsf3");
2173 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002174
2175 if (Subtarget.hasV5TOps()) {
2176 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00002177 setLibcallName(RTLIB::SQRT_F32, "__hexagon_fast2_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002178 else
Benjamin Kramera37c8092015-04-25 14:46:46 +00002179 setLibcallName(RTLIB::SQRT_F32, "__hexagon_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002180 } else {
2181 // V4
Benjamin Kramera37c8092015-04-25 14:46:46 +00002182 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__hexagon_floatsisf");
2183 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__hexagon_floatsidf");
2184 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__hexagon_floatdisf");
2185 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__hexagon_floatdidf");
2186 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__hexagon_floatunsisf");
2187 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__hexagon_floatunsidf");
2188 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__hexagon_floatundisf");
2189 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__hexagon_floatundidf");
2190 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__hexagon_fixunssfsi");
2191 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__hexagon_fixunssfdi");
2192 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__hexagon_fixunsdfsi");
2193 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__hexagon_fixunsdfdi");
2194 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__hexagon_fixsfsi");
2195 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__hexagon_fixsfdi");
2196 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__hexagon_fixdfsi");
2197 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__hexagon_fixdfdi");
2198 setLibcallName(RTLIB::FPEXT_F32_F64, "__hexagon_extendsfdf2");
2199 setLibcallName(RTLIB::FPROUND_F64_F32, "__hexagon_truncdfsf2");
2200 setLibcallName(RTLIB::OEQ_F32, "__hexagon_eqsf2");
2201 setLibcallName(RTLIB::OEQ_F64, "__hexagon_eqdf2");
2202 setLibcallName(RTLIB::OGE_F32, "__hexagon_gesf2");
2203 setLibcallName(RTLIB::OGE_F64, "__hexagon_gedf2");
2204 setLibcallName(RTLIB::OLE_F32, "__hexagon_lesf2");
2205 setLibcallName(RTLIB::OLE_F64, "__hexagon_ledf2");
2206 setLibcallName(RTLIB::UNE_F32, "__hexagon_nesf2");
2207 setLibcallName(RTLIB::UNE_F64, "__hexagon_nedf2");
2208 setLibcallName(RTLIB::UO_F32, "__hexagon_unordsf2");
2209 setLibcallName(RTLIB::UO_F64, "__hexagon_unorddf2");
2210 setLibcallName(RTLIB::O_F32, "__hexagon_unordsf2");
2211 setLibcallName(RTLIB::O_F64, "__hexagon_unorddf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002212 }
2213
2214 // These cause problems when the shift amount is non-constant.
2215 setLibcallName(RTLIB::SHL_I128, nullptr);
2216 setLibcallName(RTLIB::SRL_I128, nullptr);
2217 setLibcallName(RTLIB::SRA_I128, nullptr);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002218}
2219
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002220const char* HexagonTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00002221 switch ((HexagonISD::NodeType)Opcode) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002222 case HexagonISD::ALLOCA: return "HexagonISD::ALLOCA";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002223 case HexagonISD::AT_GOT: return "HexagonISD::AT_GOT";
2224 case HexagonISD::AT_PCREL: return "HexagonISD::AT_PCREL";
2225 case HexagonISD::BARRIER: return "HexagonISD::BARRIER";
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002226 case HexagonISD::CALL: return "HexagonISD::CALL";
2227 case HexagonISD::CALLnr: return "HexagonISD::CALLnr";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002228 case HexagonISD::CALLR: return "HexagonISD::CALLR";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002229 case HexagonISD::COMBINE: return "HexagonISD::COMBINE";
2230 case HexagonISD::CONST32_GP: return "HexagonISD::CONST32_GP";
2231 case HexagonISD::CONST32: return "HexagonISD::CONST32";
2232 case HexagonISD::CP: return "HexagonISD::CP";
2233 case HexagonISD::DCFETCH: return "HexagonISD::DCFETCH";
2234 case HexagonISD::EH_RETURN: return "HexagonISD::EH_RETURN";
2235 case HexagonISD::EXTRACTU: return "HexagonISD::EXTRACTU";
2236 case HexagonISD::EXTRACTURP: return "HexagonISD::EXTRACTURP";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002237 case HexagonISD::INSERT: return "HexagonISD::INSERT";
2238 case HexagonISD::INSERTRP: return "HexagonISD::INSERTRP";
2239 case HexagonISD::JT: return "HexagonISD::JT";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002240 case HexagonISD::RET_FLAG: return "HexagonISD::RET_FLAG";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002241 case HexagonISD::TC_RETURN: return "HexagonISD::TC_RETURN";
Krzysztof Parzyszekc168c012015-12-03 16:47:20 +00002242 case HexagonISD::VCOMBINE: return "HexagonISD::VCOMBINE";
Krzysztof Parzyszek302a9d42017-07-14 19:02:32 +00002243 case HexagonISD::VPACKE: return "HexagonISD::VPACKE";
2244 case HexagonISD::VPACKO: return "HexagonISD::VPACKO";
Krzysztof Parzyszekf85dd9f2017-07-10 20:16:44 +00002245 case HexagonISD::VASL: return "HexagonISD::VASL";
2246 case HexagonISD::VASR: return "HexagonISD::VASR";
2247 case HexagonISD::VLSR: return "HexagonISD::VLSR";
2248 case HexagonISD::VSPLAT: return "HexagonISD::VSPLAT";
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002249 case HexagonISD::VEXTRACTW: return "HexagonISD::VEXTRACTW";
2250 case HexagonISD::VINSERTW0: return "HexagonISD::VINSERTW0";
2251 case HexagonISD::VROR: return "HexagonISD::VROR";
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +00002252 case HexagonISD::READCYCLE: return "HexagonISD::READCYCLE";
Matthias Braund04893f2015-05-07 21:33:59 +00002253 case HexagonISD::OP_END: break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002254 }
Matthias Braund04893f2015-05-07 21:33:59 +00002255 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002256}
2257
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00002258/// Given an intrinsic, checks if on the target the intrinsic will need to map
2259/// to a MemIntrinsicNode (touches memory). If this is the case, it returns
2260/// true and store the intrinsic information into the IntrinsicInfo that was
2261/// passed to the function.
2262bool HexagonTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
2263 const CallInst &I,
Matt Arsenault7d7adf42017-12-14 22:34:10 +00002264 MachineFunction &MF,
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00002265 unsigned Intrinsic) const {
2266 switch (Intrinsic) {
2267 case Intrinsic::hexagon_V6_vgathermw:
2268 case Intrinsic::hexagon_V6_vgathermw_128B:
2269 case Intrinsic::hexagon_V6_vgathermh:
2270 case Intrinsic::hexagon_V6_vgathermh_128B:
2271 case Intrinsic::hexagon_V6_vgathermhw:
2272 case Intrinsic::hexagon_V6_vgathermhw_128B:
2273 case Intrinsic::hexagon_V6_vgathermwq:
2274 case Intrinsic::hexagon_V6_vgathermwq_128B:
2275 case Intrinsic::hexagon_V6_vgathermhq:
2276 case Intrinsic::hexagon_V6_vgathermhq_128B:
2277 case Intrinsic::hexagon_V6_vgathermhwq:
2278 case Intrinsic::hexagon_V6_vgathermhwq_128B: {
2279 const Module &M = *I.getParent()->getParent()->getParent();
2280 Info.opc = ISD::INTRINSIC_W_CHAIN;
2281 Type *VecTy = I.getArgOperand(1)->getType();
2282 Info.memVT = MVT::getVT(VecTy);
2283 Info.ptrVal = I.getArgOperand(0);
2284 Info.offset = 0;
2285 Info.align = M.getDataLayout().getTypeAllocSizeInBits(VecTy) / 8;
Matt Arsenault11171332017-12-14 21:39:51 +00002286 Info.flags = MachineMemOperand::MOLoad |
2287 MachineMemOperand::MOStore |
2288 MachineMemOperand::MOVolatile;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00002289 return true;
2290 }
2291 default:
2292 break;
2293 }
2294 return false;
2295}
2296
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002297bool HexagonTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002298 EVT MTy1 = EVT::getEVT(Ty1);
2299 EVT MTy2 = EVT::getEVT(Ty2);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002300 if (!MTy1.isSimple() || !MTy2.isSimple())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002301 return false;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002302 return (MTy1.getSimpleVT() == MVT::i64) && (MTy2.getSimpleVT() == MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002303}
2304
2305bool HexagonTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002306 if (!VT1.isSimple() || !VT2.isSimple())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002307 return false;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002308 return (VT1.getSimpleVT() == MVT::i64) && (VT2.getSimpleVT() == MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002309}
2310
Krzysztof Parzyszekbd8ef4b2016-08-19 13:34:31 +00002311bool HexagonTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
2312 return isOperationLegalOrCustom(ISD::FMA, VT);
2313}
2314
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002315// Should we expand the build vector with shuffles?
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00002316bool HexagonTargetLowering::shouldExpandBuildVectorWithShuffles(EVT VT,
2317 unsigned DefinedValues) const {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002318 return false;
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002319}
2320
Zvi Rackover1b736822017-07-26 08:06:58 +00002321bool HexagonTargetLowering::isShuffleMaskLegal(ArrayRef<int> Mask,
2322 EVT VT) const {
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00002323 return true;
2324}
2325
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002326// Lower a vector shuffle (V1, V2, V3). V1 and V2 are the two vectors
2327// to select data from, V3 is the permutation.
2328SDValue
2329HexagonTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG)
2330 const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002331 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
2332 SDValue V1 = Op.getOperand(0);
2333 SDValue V2 = Op.getOperand(1);
2334 SDLoc dl(Op);
2335 EVT VT = Op.getValueType();
2336
Sanjay Patel57195842016-03-14 17:28:46 +00002337 if (V2.isUndef())
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002338 V2 = V1;
2339
2340 if (SVN->isSplat()) {
2341 int Lane = SVN->getSplatIndex();
2342 if (Lane == -1) Lane = 0;
2343
2344 // Test if V1 is a SCALAR_TO_VECTOR.
2345 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR)
Krzysztof Parzyszek89b2d7c2017-07-13 18:17:58 +00002346 return DAG.getNode(HexagonISD::VSPLAT, dl, VT, V1.getOperand(0));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002347
2348 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
2349 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
2350 // reaches it).
2351 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
2352 !isa<ConstantSDNode>(V1.getOperand(0))) {
2353 bool IsScalarToVector = true;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00002354 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i) {
Sanjay Patel75068522016-03-14 18:09:43 +00002355 if (!V1.getOperand(i).isUndef()) {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002356 IsScalarToVector = false;
2357 break;
2358 }
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00002359 }
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002360 if (IsScalarToVector)
Krzysztof Parzyszek89b2d7c2017-07-13 18:17:58 +00002361 return DAG.getNode(HexagonISD::VSPLAT, dl, VT, V1.getOperand(0));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002362 }
Krzysztof Parzyszek89b2d7c2017-07-13 18:17:58 +00002363 return DAG.getNode(HexagonISD::VSPLAT, dl, VT,
2364 DAG.getConstant(Lane, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002365 }
2366
2367 // FIXME: We need to support more general vector shuffles. See
2368 // below the comment from the ARM backend that deals in the general
2369 // case with the vector shuffles. For now, let expand handle these.
2370 return SDValue();
2371
2372 // If the shuffle is not directly supported and it has 4 elements, use
2373 // the PerfectShuffle-generated table to synthesize it from other shuffles.
2374}
2375
2376// If BUILD_VECTOR has same base element repeated several times,
2377// report true.
2378static bool isCommonSplatElement(BuildVectorSDNode *BVN) {
2379 unsigned NElts = BVN->getNumOperands();
2380 SDValue V0 = BVN->getOperand(0);
2381
2382 for (unsigned i = 1, e = NElts; i != e; ++i) {
2383 if (BVN->getOperand(i) != V0)
2384 return false;
2385 }
2386 return true;
2387}
2388
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002389// Lower a vector shift. Try to convert
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002390// <VT> = SHL/SRA/SRL <VT> by <VT> to Hexagon specific
2391// <VT> = SHL/SRA/SRL <VT> by <IT/i32>.
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002392SDValue
2393HexagonTargetLowering::LowerVECTOR_SHIFT(SDValue Op, SelectionDAG &DAG) const {
Eugene Zelenko58655bb2016-12-17 01:09:05 +00002394 BuildVectorSDNode *BVN = nullptr;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002395 SDValue V1 = Op.getOperand(0);
2396 SDValue V2 = Op.getOperand(1);
2397 SDValue V3;
2398 SDLoc dl(Op);
2399 EVT VT = Op.getValueType();
2400
2401 if ((BVN = dyn_cast<BuildVectorSDNode>(V1.getNode())) &&
2402 isCommonSplatElement(BVN))
2403 V3 = V2;
2404 else if ((BVN = dyn_cast<BuildVectorSDNode>(V2.getNode())) &&
2405 isCommonSplatElement(BVN))
2406 V3 = V1;
2407 else
2408 return SDValue();
2409
2410 SDValue CommonSplat = BVN->getOperand(0);
2411 SDValue Result;
2412
2413 if (VT.getSimpleVT() == MVT::v4i16) {
2414 switch (Op.getOpcode()) {
2415 case ISD::SRA:
Krzysztof Parzyszekf85dd9f2017-07-10 20:16:44 +00002416 Result = DAG.getNode(HexagonISD::VASR, dl, VT, V3, CommonSplat);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002417 break;
2418 case ISD::SHL:
Krzysztof Parzyszekf85dd9f2017-07-10 20:16:44 +00002419 Result = DAG.getNode(HexagonISD::VASL, dl, VT, V3, CommonSplat);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002420 break;
2421 case ISD::SRL:
Krzysztof Parzyszekf85dd9f2017-07-10 20:16:44 +00002422 Result = DAG.getNode(HexagonISD::VLSR, dl, VT, V3, CommonSplat);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002423 break;
2424 default:
2425 return SDValue();
2426 }
2427 } else if (VT.getSimpleVT() == MVT::v2i32) {
2428 switch (Op.getOpcode()) {
2429 case ISD::SRA:
Krzysztof Parzyszekf85dd9f2017-07-10 20:16:44 +00002430 Result = DAG.getNode(HexagonISD::VASR, dl, VT, V3, CommonSplat);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002431 break;
2432 case ISD::SHL:
Krzysztof Parzyszekf85dd9f2017-07-10 20:16:44 +00002433 Result = DAG.getNode(HexagonISD::VASL, dl, VT, V3, CommonSplat);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002434 break;
2435 case ISD::SRL:
Krzysztof Parzyszekf85dd9f2017-07-10 20:16:44 +00002436 Result = DAG.getNode(HexagonISD::VLSR, dl, VT, V3, CommonSplat);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002437 break;
2438 default:
2439 return SDValue();
2440 }
2441 } else {
2442 return SDValue();
2443 }
2444
2445 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
2446}
2447
2448SDValue
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002449HexagonTargetLowering::buildVector32(ArrayRef<SDValue> Elem, const SDLoc &dl,
2450 MVT VecTy, SelectionDAG &DAG) const {
2451 MVT ElemTy = VecTy.getVectorElementType();
2452 assert(VecTy.getVectorNumElements() == Elem.size());
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002453
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002454 SmallVector<ConstantSDNode*,4> Consts;
2455 bool AllConst = true;
2456 for (SDValue V : Elem) {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002457 if (isUndef(V))
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002458 V = DAG.getConstant(0, dl, ElemTy);
2459 auto *C = dyn_cast<ConstantSDNode>(V.getNode());
2460 Consts.push_back(C);
2461 AllConst = AllConst && C != nullptr;
2462 }
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002463
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002464 unsigned First, Num = Elem.size();
2465 for (First = 0; First != Num; ++First)
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002466 if (!isUndef(Elem[First]))
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002467 break;
2468 if (First == Num)
2469 return DAG.getUNDEF(VecTy);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002470
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002471 if (ElemTy == MVT::i16) {
2472 assert(Elem.size() == 2);
2473 if (AllConst) {
2474 uint32_t V = (Consts[0]->getZExtValue() & 0xFFFF) |
2475 Consts[1]->getZExtValue() << 16;
2476 return DAG.getBitcast(MVT::v2i16, DAG.getConstant(V, dl, MVT::i32));
Krzysztof Parzyszek89b2d7c2017-07-13 18:17:58 +00002477 }
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002478 SDValue N = getNode(Hexagon::A2_combine_ll, dl, MVT::i32,
2479 {Elem[1], Elem[0]}, DAG);
2480 return DAG.getBitcast(MVT::v2i16, N);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002481 }
2482
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002483 // First try generating a constant.
2484 assert(ElemTy == MVT::i8 && Num == 4);
2485 if (AllConst) {
2486 int32_t V = (Consts[0]->getZExtValue() & 0xFF) |
2487 (Consts[1]->getZExtValue() & 0xFF) << 8 |
2488 (Consts[1]->getZExtValue() & 0xFF) << 16 |
2489 Consts[2]->getZExtValue() << 24;
2490 return DAG.getBitcast(MVT::v4i8, DAG.getConstant(V, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002491 }
2492
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002493 // Then try splat.
2494 bool IsSplat = true;
2495 for (unsigned i = 0; i != Num; ++i) {
2496 if (i == First)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002497 continue;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002498 if (Elem[i] == Elem[First] || isUndef(Elem[i]))
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002499 continue;
2500 IsSplat = false;
2501 break;
2502 }
2503 if (IsSplat)
2504 return DAG.getNode(HexagonISD::VSPLAT, dl, VecTy, Elem[First]);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002505
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002506 // Generate
2507 // (zxtb(Elem[0]) | (zxtb(Elem[1]) << 8)) |
2508 // (zxtb(Elem[2]) | (zxtb(Elem[3]) << 8)) << 16
2509 SDValue S8 = DAG.getConstant(8, dl, MVT::i32);
Krzysztof Parzyszek081e4582017-11-28 19:13:17 +00002510 SDValue V0 = DAG.getZeroExtendInReg(Elem[0], dl, MVT::i8);
2511 SDValue V1 = DAG.getZeroExtendInReg(Elem[1], dl, MVT::i8);
2512 SDValue V2 = DAG.getZeroExtendInReg(Elem[2], dl, MVT::i8);
2513 SDValue V3 = DAG.getZeroExtendInReg(Elem[3], dl, MVT::i8);
2514
2515 SDValue V4 = DAG.getNode(ISD::SHL, dl, MVT::i32, {V1, S8});
2516 SDValue V5 = DAG.getNode(ISD::SHL, dl, MVT::i32, {V3, S8});
2517 SDValue V6 = DAG.getNode(ISD::OR, dl, MVT::i32, {V0, V4});
2518 SDValue V7 = DAG.getNode(ISD::OR, dl, MVT::i32, {V2, V5});
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002519
2520 SDValue T0 = getNode(Hexagon::A2_combine_ll, dl, MVT::i32, {V7, V6}, DAG);
2521 return DAG.getBitcast(MVT::v4i8, T0);
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002522}
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002523
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002524SDValue
2525HexagonTargetLowering::buildVector64(ArrayRef<SDValue> Elem, const SDLoc &dl,
2526 MVT VecTy, SelectionDAG &DAG) const {
2527 MVT ElemTy = VecTy.getVectorElementType();
2528 assert(VecTy.getVectorNumElements() == Elem.size());
2529
2530 SmallVector<ConstantSDNode*,8> Consts;
2531 bool AllConst = true;
2532 for (SDValue V : Elem) {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002533 if (isUndef(V))
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002534 V = DAG.getConstant(0, dl, ElemTy);
2535 auto *C = dyn_cast<ConstantSDNode>(V.getNode());
2536 Consts.push_back(C);
2537 AllConst = AllConst && C != nullptr;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002538 }
2539
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002540 unsigned First, Num = Elem.size();
2541 for (First = 0; First != Num; ++First)
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002542 if (!isUndef(Elem[First]))
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002543 break;
2544 if (First == Num)
2545 return DAG.getUNDEF(VecTy);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002546
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002547 // First try splat if possible.
2548 if (ElemTy == MVT::i16) {
2549 bool IsSplat = true;
2550 for (unsigned i = 0; i != Num; ++i) {
2551 if (i == First)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002552 continue;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002553 if (Elem[i] == Elem[First] || isUndef(Elem[i]))
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002554 continue;
2555 IsSplat = false;
2556 break;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002557 }
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002558 if (IsSplat)
2559 return DAG.getNode(HexagonISD::VSPLAT, dl, VecTy, Elem[First]);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002560 }
2561
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002562 // Then try constant.
2563 if (AllConst) {
2564 uint64_t Val = 0;
2565 unsigned W = ElemTy.getSizeInBits();
2566 uint64_t Mask = (ElemTy == MVT::i8) ? 0xFFull
2567 : (ElemTy == MVT::i16) ? 0xFFFFull : 0xFFFFFFFFull;
2568 for (unsigned i = 0; i != Num; ++i)
2569 Val = (Val << W) | (Consts[i]->getZExtValue() & Mask);
2570 SDValue V0 = DAG.getConstant(Val, dl, MVT::i64);
2571 return DAG.getBitcast(VecTy, V0);
2572 }
2573
2574 // Build two 32-bit vectors and concatenate.
2575 MVT HalfTy = MVT::getVectorVT(ElemTy, Num/2);
2576 SDValue L = (ElemTy == MVT::i32)
2577 ? Elem[0]
2578 : buildVector32({Elem.data(), Num/2}, dl, HalfTy, DAG);
2579 SDValue H = (ElemTy == MVT::i32)
2580 ? Elem[1]
2581 : buildVector32({Elem.data()+Num/2, Num/2}, dl, HalfTy, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002582 return DAG.getNode(HexagonISD::COMBINE, dl, VecTy, {H, L});
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002583}
2584
2585SDValue
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002586HexagonTargetLowering::extractVector(SDValue VecV, SDValue IdxV,
2587 const SDLoc &dl, MVT ValTy, MVT ResTy,
2588 SelectionDAG &DAG) const {
2589 MVT VecTy = ty(VecV);
2590 assert(!ValTy.isVector() ||
2591 VecTy.getVectorElementType() == ValTy.getVectorElementType());
2592 unsigned VecWidth = VecTy.getSizeInBits();
2593 unsigned ValWidth = ValTy.getSizeInBits();
2594 unsigned ElemWidth = VecTy.getVectorElementType().getSizeInBits();
2595 assert(VecWidth == 32 || VecWidth == 64);
2596 assert((VecWidth % ElemWidth) == 0);
2597
2598 // Cast everything to scalar integer types.
2599 MVT ScalarTy = tyScalar(VecTy);
2600 VecV = DAG.getBitcast(ScalarTy, VecV);
2601
2602 SDValue WidthV = DAG.getConstant(ValWidth, dl, MVT::i32);
2603 SDValue ExtV;
2604
2605 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(IdxV)) {
2606 unsigned Off = C->getZExtValue() * ElemWidth;
2607 if (VecWidth == 64 && ValWidth == 32) {
2608 assert(Off == 0 || Off == 32);
2609 unsigned SubIdx = Off == 0 ? Hexagon::isub_lo : Hexagon::isub_hi;
2610 ExtV = DAG.getTargetExtractSubreg(SubIdx, dl, MVT::i32, VecV);
2611 } else if (Off == 0 && (ValWidth % 8) == 0) {
2612 ExtV = DAG.getZeroExtendInReg(VecV, dl, tyScalar(ValTy));
2613 } else {
2614 SDValue OffV = DAG.getConstant(Off, dl, MVT::i32);
2615 // The return type of EXTRACTU must be the same as the type of the
2616 // input vector.
2617 ExtV = DAG.getNode(HexagonISD::EXTRACTU, dl, ScalarTy,
2618 {VecV, WidthV, OffV});
2619 }
2620 } else {
2621 if (ty(IdxV) != MVT::i32)
2622 IdxV = DAG.getZExtOrTrunc(IdxV, dl, MVT::i32);
2623 SDValue OffV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
2624 DAG.getConstant(ElemWidth, dl, MVT::i32));
2625 // EXTRACTURP takes width/offset in a 64-bit pair.
2626 SDValue CombV = DAG.getNode(HexagonISD::COMBINE, dl, MVT::i64,
2627 {WidthV, OffV});
2628 ExtV = DAG.getNode(HexagonISD::EXTRACTURP, dl, ScalarTy,
2629 {VecV, CombV});
2630 }
2631
2632 // Cast ExtV to the requested result type.
2633 ExtV = DAG.getZExtOrTrunc(ExtV, dl, tyScalar(ResTy));
2634 ExtV = DAG.getBitcast(ResTy, ExtV);
2635 return ExtV;
2636}
2637
2638SDValue
2639HexagonTargetLowering::insertVector(SDValue VecV, SDValue ValV, SDValue IdxV,
2640 const SDLoc &dl, MVT ValTy,
2641 SelectionDAG &DAG) const {
2642 MVT VecTy = ty(VecV);
2643 unsigned VecWidth = VecTy.getSizeInBits();
2644 unsigned ValWidth = ValTy.getSizeInBits();
2645 assert(VecWidth == 32 || VecWidth == 64);
2646 assert((VecWidth % ValWidth) == 0);
2647
2648 // Cast everything to scalar integer types.
2649 MVT ScalarTy = MVT::getIntegerVT(VecWidth);
2650 // The actual type of ValV may be different than ValTy (which is related
2651 // to the vector type).
2652 unsigned VW = ty(ValV).getSizeInBits();
2653 ValV = DAG.getBitcast(MVT::getIntegerVT(VW), ValV);
2654 VecV = DAG.getBitcast(ScalarTy, VecV);
2655 if (VW != VecWidth)
2656 ValV = DAG.getAnyExtOrTrunc(ValV, dl, ScalarTy);
2657
2658 SDValue WidthV = DAG.getConstant(ValWidth, dl, MVT::i32);
2659 SDValue InsV;
2660
2661 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(IdxV)) {
2662 unsigned W = C->getZExtValue() * ValWidth;
2663 SDValue OffV = DAG.getConstant(W, dl, MVT::i32);
2664 InsV = DAG.getNode(HexagonISD::INSERT, dl, ScalarTy,
2665 {VecV, ValV, WidthV, OffV});
2666 } else {
2667 if (ty(IdxV) != MVT::i32)
2668 IdxV = DAG.getZExtOrTrunc(IdxV, dl, MVT::i32);
2669 SDValue OffV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV, WidthV);
2670 // INSERTRP takes width/offset in a 64-bit pair.
2671 SDValue CombV = DAG.getNode(HexagonISD::COMBINE, dl, MVT::i64,
2672 {WidthV, OffV});
2673 InsV = DAG.getNode(HexagonISD::INSERTRP, dl, ScalarTy,
2674 {VecV, ValV, CombV});
2675 }
2676
2677 return DAG.getNode(ISD::BITCAST, dl, VecTy, InsV);
2678}
2679
2680SDValue
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002681HexagonTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002682 MVT VecTy = ty(Op);
2683 unsigned BW = VecTy.getSizeInBits();
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002684 if (BW == 32 || BW == 64) {
2685 SmallVector<SDValue,8> Ops;
2686 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i)
2687 Ops.push_back(Op.getOperand(i));
2688 if (BW == 32)
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002689 return buildVector32(Ops, SDLoc(Op), VecTy, DAG);
2690 return buildVector64(Ops, SDLoc(Op), VecTy, DAG);
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002691 }
2692
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002693 if (Subtarget.useHVXOps() && Subtarget.isHVXVectorType(VecTy))
2694 return LowerHvxBuildVector(Op, DAG);
2695
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002696 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002697}
2698
2699SDValue
2700HexagonTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
2701 SelectionDAG &DAG) const {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002702 MVT VecTy = ty(Op);
2703 assert(!Subtarget.useHVXOps() || !Subtarget.isHVXVectorType(VecTy));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002704
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002705 if (VecTy.getSizeInBits() == 64) {
2706 assert(Op.getNumOperands() == 2);
2707 return DAG.getNode(HexagonISD::COMBINE, SDLoc(Op), VecTy, Op.getOperand(1),
2708 Op.getOperand(0));
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002709 }
2710
2711 return SDValue();
2712}
2713
2714SDValue
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002715HexagonTargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
2716 SelectionDAG &DAG) const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002717 SDValue Vec = Op.getOperand(0);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002718 MVT VecTy = ty(Vec);
2719 if (Subtarget.useHVXOps() && Subtarget.isHVXVectorType(VecTy))
2720 return LowerHvxExtractElement(Op, DAG);
2721
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002722 MVT ElemTy = ty(Vec).getVectorElementType();
2723 return extractVector(Vec, Op.getOperand(1), SDLoc(Op), ElemTy, ty(Op), DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002724}
2725
2726SDValue
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002727HexagonTargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op,
2728 SelectionDAG &DAG) const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002729 SDValue Vec = Op.getOperand(0);
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002730 MVT VecTy = ty(Vec);
2731 if (Subtarget.useHVXOps() && Subtarget.isHVXVectorType(VecTy))
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002732 return LowerHvxExtractSubvector(Op, DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002733
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002734 return extractVector(Vec, Op.getOperand(1), SDLoc(Op), ty(Op), ty(Op), DAG);
2735}
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002736
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002737SDValue
2738HexagonTargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op,
2739 SelectionDAG &DAG) const {
2740 MVT VecTy = ty(Op);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002741 if (Subtarget.useHVXOps() && Subtarget.isHVXVectorType(VecTy))
2742 return LowerHvxInsertElement(Op, DAG);
2743
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002744 return insertVector(Op.getOperand(0), Op.getOperand(1), Op.getOperand(2),
2745 SDLoc(Op), VecTy.getVectorElementType(), DAG);
2746}
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002747
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002748SDValue
2749HexagonTargetLowering::LowerINSERT_SUBVECTOR(SDValue Op,
2750 SelectionDAG &DAG) const {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002751 if (Subtarget.useHVXOps() && Subtarget.isHVXVectorType(ty(Op)))
2752 return LowerHvxInsertSubvector(Op, DAG);
2753
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002754 SDValue ValV = Op.getOperand(1);
2755 return insertVector(Op.getOperand(0), ValV, Op.getOperand(2),
2756 SDLoc(Op), ty(ValV), DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002757}
2758
Tim Northovera4415852013-08-06 09:12:35 +00002759bool
2760HexagonTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
2761 // Assuming the caller does not have either a signext or zeroext modifier, and
2762 // only one value is accepted, any reasonable truncation is allowed.
2763 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
2764 return false;
2765
2766 // FIXME: in principle up to 64-bit could be made safe, but it would be very
2767 // fragile at the moment: any support for multiple value returns would be
2768 // liable to disallow tail calls involving i64 -> iN truncation in many cases.
2769 return Ty1->getPrimitiveSizeInBits() <= 32;
2770}
2771
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002772SDValue
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002773HexagonTargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
2774 SDValue Chain = Op.getOperand(0);
2775 SDValue Offset = Op.getOperand(1);
2776 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002777 SDLoc dl(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002778 auto PtrVT = getPointerTy(DAG.getDataLayout());
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002779
2780 // Mark function as containing a call to EH_RETURN.
2781 HexagonMachineFunctionInfo *FuncInfo =
2782 DAG.getMachineFunction().getInfo<HexagonMachineFunctionInfo>();
2783 FuncInfo->setHasEHReturn();
2784
2785 unsigned OffsetReg = Hexagon::R28;
2786
Mehdi Amini44ede332015-07-09 02:09:04 +00002787 SDValue StoreAddr =
2788 DAG.getNode(ISD::ADD, dl, PtrVT, DAG.getRegister(Hexagon::R30, PtrVT),
2789 DAG.getIntPtrConstant(4, dl));
Justin Lebar9c375812016-07-15 18:27:10 +00002790 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo());
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002791 Chain = DAG.getCopyToReg(Chain, dl, OffsetReg, Offset);
2792
2793 // Not needed we already use it as explict input to EH_RETURN.
2794 // MF.getRegInfo().addLiveOut(OffsetReg);
2795
2796 return DAG.getNode(HexagonISD::EH_RETURN, dl, MVT::Other, Chain);
2797}
2798
2799SDValue
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002800HexagonTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002801 unsigned Opc = Op.getOpcode();
2802 switch (Opc) {
2803 default:
2804#ifndef NDEBUG
2805 Op.getNode()->dumpr(&DAG);
2806 if (Opc > HexagonISD::OP_BEGIN && Opc < HexagonISD::OP_END)
2807 errs() << "Check for a non-legal type in this operation\n";
2808#endif
2809 llvm_unreachable("Should not custom lower this!");
2810 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002811 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
2812 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
2813 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
2814 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002815 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2816 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002817 case ISD::SRA:
2818 case ISD::SHL:
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002819 case ISD::SRL: return LowerVECTOR_SHIFT(Op, DAG);
2820 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002821 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002822 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
2823 // Frame & Return address. Currently unimplemented.
2824 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
2825 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00002826 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002827 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
2828 case ISD::GlobalAddress: return LowerGLOBALADDRESS(Op, DAG);
2829 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002830 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002831 case ISD::VASTART: return LowerVASTART(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002832 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
2833 case ISD::SETCC: return LowerSETCC(Op, DAG);
2834 case ISD::VSELECT: return LowerVSELECT(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002835 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00002836 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002837 case ISD::INLINEASM: return LowerINLINEASM(Op, DAG);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00002838 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG);
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +00002839 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00002840 case ISD::MUL:
2841 if (Subtarget.useHVXOps())
2842 return LowerHvxMul(Op, DAG);
2843 break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002844 }
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002845 return SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002846}
2847
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002848/// Returns relocation base for the given PIC jumptable.
2849SDValue
2850HexagonTargetLowering::getPICJumpTableRelocBase(SDValue Table,
2851 SelectionDAG &DAG) const {
2852 int Idx = cast<JumpTableSDNode>(Table)->getIndex();
2853 EVT VT = Table.getValueType();
2854 SDValue T = DAG.getTargetJumpTable(Idx, VT, HexagonII::MO_PCREL);
2855 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Table), VT, T);
2856}
2857
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002858//===----------------------------------------------------------------------===//
2859// Inline Assembly Support
2860//===----------------------------------------------------------------------===//
2861
Krzysztof Parzyszekca3b5322016-05-18 14:34:51 +00002862TargetLowering::ConstraintType
2863HexagonTargetLowering::getConstraintType(StringRef Constraint) const {
2864 if (Constraint.size() == 1) {
2865 switch (Constraint[0]) {
2866 case 'q':
2867 case 'v':
2868 if (Subtarget.useHVXOps())
Krzysztof Parzyszek3ad0d012017-07-21 17:51:27 +00002869 return C_RegisterClass;
2870 break;
2871 case 'a':
2872 return C_RegisterClass;
2873 default:
Krzysztof Parzyszekca3b5322016-05-18 14:34:51 +00002874 break;
2875 }
2876 }
2877 return TargetLowering::getConstraintType(Constraint);
2878}
2879
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002880std::pair<unsigned, const TargetRegisterClass*>
Eric Christopher11e4df72015-02-26 22:38:43 +00002881HexagonTargetLowering::getRegForInlineAsmConstraint(
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00002882 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002883
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002884 if (Constraint.size() == 1) {
2885 switch (Constraint[0]) {
2886 case 'r': // R0-R31
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002887 switch (VT.SimpleTy) {
2888 default:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002889 return {0u, nullptr};
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002890 case MVT::i1:
2891 case MVT::i8:
2892 case MVT::i16:
2893 case MVT::i32:
2894 case MVT::f32:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002895 return {0u, &Hexagon::IntRegsRegClass};
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002896 case MVT::i64:
2897 case MVT::f64:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002898 return {0u, &Hexagon::DoubleRegsRegClass};
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002899 }
Krzysztof Parzyszek3ad0d012017-07-21 17:51:27 +00002900 break;
2901 case 'a': // M0-M1
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002902 if (VT != MVT::i32)
2903 return {0u, nullptr};
2904 return {0u, &Hexagon::ModRegsRegClass};
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002905 case 'q': // q0-q3
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002906 switch (VT.getSizeInBits()) {
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002907 default:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002908 return {0u, nullptr};
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002909 case 512:
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002910 case 1024:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002911 return {0u, &Hexagon::HvxQRRegClass};
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002912 }
Krzysztof Parzyszek3ad0d012017-07-21 17:51:27 +00002913 break;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002914 case 'v': // V0-V31
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002915 switch (VT.getSizeInBits()) {
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002916 default:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002917 return {0u, nullptr};
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002918 case 512:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002919 return {0u, &Hexagon::HvxVRRegClass};
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002920 case 1024:
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +00002921 if (Subtarget.hasV60TOps() && Subtarget.useHVX128BOps())
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002922 return {0u, &Hexagon::HvxVRRegClass};
2923 return {0u, &Hexagon::HvxWRRegClass};
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002924 case 2048:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002925 return {0u, &Hexagon::HvxWRRegClass};
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002926 }
Krzysztof Parzyszek3ad0d012017-07-21 17:51:27 +00002927 break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002928 default:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002929 return {0u, nullptr};
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002930 }
2931 }
2932
Eric Christopher11e4df72015-02-26 22:38:43 +00002933 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002934}
2935
Sirish Pande69295b82012-05-10 20:20:25 +00002936/// isFPImmLegal - Returns true if the target can instruction select the
2937/// specified FP immediate natively. If false, the legalizer will
2938/// materialize the FP immediate as a load from a constant pool.
2939bool HexagonTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002940 return Subtarget.hasV5TOps();
Sirish Pande69295b82012-05-10 20:20:25 +00002941}
2942
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002943/// isLegalAddressingMode - Return true if the addressing mode represented by
2944/// AM is legal for this target, for a load/store of the specified type.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +00002945bool HexagonTargetLowering::isLegalAddressingMode(const DataLayout &DL,
2946 const AddrMode &AM, Type *Ty,
Jonas Paulsson024e3192017-07-21 11:59:37 +00002947 unsigned AS, Instruction *I) const {
Krzysztof Parzyszeked4e7822016-08-03 15:06:18 +00002948 if (Ty->isSized()) {
2949 // When LSR detects uses of the same base address to access different
2950 // types (e.g. unions), it will assume a conservative type for these
2951 // uses:
2952 // LSR Use: Kind=Address of void in addrspace(4294967295), ...
2953 // The type Ty passed here would then be "void". Skip the alignment
2954 // checks, but do not return false right away, since that confuses
2955 // LSR into crashing.
2956 unsigned A = DL.getABITypeAlignment(Ty);
2957 // The base offset must be a multiple of the alignment.
2958 if ((AM.BaseOffs % A) != 0)
2959 return false;
2960 // The shifted offset must fit in 11 bits.
2961 if (!isInt<11>(AM.BaseOffs >> Log2_32(A)))
2962 return false;
2963 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002964
2965 // No global is ever allowed as a base.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002966 if (AM.BaseGV)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002967 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002968
2969 int Scale = AM.Scale;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00002970 if (Scale < 0)
2971 Scale = -Scale;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002972 switch (Scale) {
2973 case 0: // No scale reg, "r+i", "r", or just "i".
2974 break;
2975 default: // No scaled addressing mode.
2976 return false;
2977 }
2978 return true;
2979}
2980
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002981/// Return true if folding a constant offset with the given GlobalAddress is
2982/// legal. It is frequently not legal in PIC relocation models.
2983bool HexagonTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA)
2984 const {
2985 return HTM.getRelocationModel() == Reloc::Static;
2986}
2987
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002988/// isLegalICmpImmediate - Return true if the specified immediate is legal
2989/// icmp immediate, that is the target has icmp instructions which can compare
2990/// a register against the immediate without having to materialize the
2991/// immediate into a register.
2992bool HexagonTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
2993 return Imm >= -512 && Imm <= 511;
2994}
2995
2996/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2997/// for tail call optimization. Targets which want to do tail call
2998/// optimization should implement this function.
2999bool HexagonTargetLowering::IsEligibleForTailCallOptimization(
3000 SDValue Callee,
3001 CallingConv::ID CalleeCC,
3002 bool isVarArg,
3003 bool isCalleeStructRet,
3004 bool isCallerStructRet,
3005 const SmallVectorImpl<ISD::OutputArg> &Outs,
3006 const SmallVectorImpl<SDValue> &OutVals,
3007 const SmallVectorImpl<ISD::InputArg> &Ins,
3008 SelectionDAG& DAG) const {
3009 const Function *CallerF = DAG.getMachineFunction().getFunction();
3010 CallingConv::ID CallerCC = CallerF->getCallingConv();
3011 bool CCMatch = CallerCC == CalleeCC;
3012
3013 // ***************************************************************************
3014 // Look for obvious safe cases to perform tail call optimization that do not
3015 // require ABI changes.
3016 // ***************************************************************************
3017
3018 // If this is a tail call via a function pointer, then don't do it!
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00003019 if (!isa<GlobalAddressSDNode>(Callee) &&
3020 !isa<ExternalSymbolSDNode>(Callee)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003021 return false;
3022 }
3023
Krzysztof Parzyszek0ba97542016-08-19 15:02:18 +00003024 // Do not optimize if the calling conventions do not match and the conventions
3025 // used are not C or Fast.
3026 if (!CCMatch) {
3027 bool R = (CallerCC == CallingConv::C || CallerCC == CallingConv::Fast);
3028 bool E = (CalleeCC == CallingConv::C || CalleeCC == CallingConv::Fast);
3029 // If R & E, then ok.
3030 if (!R || !E)
3031 return false;
3032 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003033
3034 // Do not tail call optimize vararg calls.
3035 if (isVarArg)
3036 return false;
3037
3038 // Also avoid tail call optimization if either caller or callee uses struct
3039 // return semantics.
3040 if (isCalleeStructRet || isCallerStructRet)
3041 return false;
3042
3043 // In addition to the cases above, we also disable Tail Call Optimization if
3044 // the calling convention code that at least one outgoing argument needs to
3045 // go on the stack. We cannot check that here because at this point that
3046 // information is not available.
3047 return true;
3048}
Colin LeMahieu025f8602014-12-08 21:19:18 +00003049
Krzysztof Parzyszek3e409e12016-08-02 18:34:31 +00003050/// Returns the target specific optimal type for load and store operations as
3051/// a result of memset, memcpy, and memmove lowering.
3052///
3053/// If DstAlign is zero that means it's safe to destination alignment can
3054/// satisfy any constraint. Similarly if SrcAlign is zero it means there isn't
3055/// a need to check it against alignment requirement, probably because the
3056/// source does not need to be loaded. If 'IsMemset' is true, that means it's
3057/// expanding a memset. If 'ZeroMemset' is true, that means it's a memset of
3058/// zero. 'MemcpyStrSrc' indicates whether the memcpy source is constant so it
3059/// does not need to be loaded. It returns EVT::Other if the type should be
3060/// determined using generic target-independent logic.
3061EVT HexagonTargetLowering::getOptimalMemOpType(uint64_t Size,
3062 unsigned DstAlign, unsigned SrcAlign, bool IsMemset, bool ZeroMemset,
3063 bool MemcpyStrSrc, MachineFunction &MF) const {
3064
3065 auto Aligned = [](unsigned GivenA, unsigned MinA) -> bool {
3066 return (GivenA % MinA) == 0;
3067 };
3068
3069 if (Size >= 8 && Aligned(DstAlign, 8) && (IsMemset || Aligned(SrcAlign, 8)))
3070 return MVT::i64;
3071 if (Size >= 4 && Aligned(DstAlign, 4) && (IsMemset || Aligned(SrcAlign, 4)))
3072 return MVT::i32;
3073 if (Size >= 2 && Aligned(DstAlign, 2) && (IsMemset || Aligned(SrcAlign, 2)))
3074 return MVT::i16;
3075
3076 return MVT::Other;
3077}
3078
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +00003079bool HexagonTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
3080 unsigned AS, unsigned Align, bool *Fast) const {
3081 if (Fast)
3082 *Fast = false;
3083
3084 switch (VT.getSimpleVT().SimpleTy) {
3085 default:
3086 return false;
3087 case MVT::v64i8:
3088 case MVT::v128i8:
3089 case MVT::v256i8:
3090 case MVT::v32i16:
3091 case MVT::v64i16:
3092 case MVT::v128i16:
3093 case MVT::v16i32:
3094 case MVT::v32i32:
3095 case MVT::v64i32:
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +00003096 return true;
3097 }
3098 return false;
3099}
3100
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003101std::pair<const TargetRegisterClass*, uint8_t>
3102HexagonTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
3103 MVT VT) const {
3104 const TargetRegisterClass *RRC = nullptr;
3105
3106 uint8_t Cost = 1;
3107 switch (VT.SimpleTy) {
3108 default:
3109 return TargetLowering::findRepresentativeClass(TRI, VT);
3110 case MVT::v64i8:
3111 case MVT::v32i16:
3112 case MVT::v16i32:
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00003113 RRC = &Hexagon::HvxVRRegClass;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003114 break;
3115 case MVT::v128i8:
3116 case MVT::v64i16:
3117 case MVT::v32i32:
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003118 if (Subtarget.hasV60TOps() && Subtarget.useHVXOps() &&
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +00003119 Subtarget.useHVX128BOps())
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00003120 RRC = &Hexagon::HvxVRRegClass;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003121 else
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00003122 RRC = &Hexagon::HvxWRRegClass;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003123 break;
3124 case MVT::v256i8:
3125 case MVT::v128i16:
3126 case MVT::v64i32:
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00003127 RRC = &Hexagon::HvxWRRegClass;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003128 break;
3129 }
3130 return std::make_pair(RRC, Cost);
3131}
3132
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003133Value *HexagonTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
3134 AtomicOrdering Ord) const {
3135 BasicBlock *BB = Builder.GetInsertBlock();
3136 Module *M = BB->getParent()->getParent();
3137 Type *Ty = cast<PointerType>(Addr->getType())->getElementType();
3138 unsigned SZ = Ty->getPrimitiveSizeInBits();
3139 assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic loads supported");
3140 Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_L2_loadw_locked
3141 : Intrinsic::hexagon_L4_loadd_locked;
3142 Value *Fn = Intrinsic::getDeclaration(M, IntID);
3143 return Builder.CreateCall(Fn, Addr, "larx");
3144}
3145
3146/// Perform a store-conditional operation to Addr. Return the status of the
3147/// store. This should be 0 if the store succeeded, non-zero otherwise.
3148Value *HexagonTargetLowering::emitStoreConditional(IRBuilder<> &Builder,
3149 Value *Val, Value *Addr, AtomicOrdering Ord) const {
3150 BasicBlock *BB = Builder.GetInsertBlock();
3151 Module *M = BB->getParent()->getParent();
3152 Type *Ty = Val->getType();
3153 unsigned SZ = Ty->getPrimitiveSizeInBits();
3154 assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic stores supported");
3155 Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_S2_storew_locked
3156 : Intrinsic::hexagon_S4_stored_locked;
3157 Value *Fn = Intrinsic::getDeclaration(M, IntID);
3158 Value *Call = Builder.CreateCall(Fn, {Addr, Val}, "stcx");
3159 Value *Cmp = Builder.CreateICmpEQ(Call, Builder.getInt32(0), "");
3160 Value *Ext = Builder.CreateZExt(Cmp, Type::getInt32Ty(M->getContext()));
3161 return Ext;
3162}
3163
Ahmed Bougacha52468672015-09-11 17:08:28 +00003164TargetLowering::AtomicExpansionKind
3165HexagonTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003166 // Do not expand loads and stores that don't exceed 64 bits.
Ahmed Bougacha52468672015-09-11 17:08:28 +00003167 return LI->getType()->getPrimitiveSizeInBits() > 64
Tim Northoverf520eff2015-12-02 18:12:57 +00003168 ? AtomicExpansionKind::LLOnly
Ahmed Bougacha52468672015-09-11 17:08:28 +00003169 : AtomicExpansionKind::None;
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003170}
3171
3172bool HexagonTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
3173 // Do not expand loads and stores that don't exceed 64 bits.
3174 return SI->getValueOperand()->getType()->getPrimitiveSizeInBits() > 64;
3175}
Krzysztof Parzyszekf228c952016-06-22 16:07:10 +00003176
3177bool HexagonTargetLowering::shouldExpandAtomicCmpXchgInIR(
3178 AtomicCmpXchgInst *AI) const {
3179 const DataLayout &DL = AI->getModule()->getDataLayout();
3180 unsigned Size = DL.getTypeStoreSize(AI->getCompareOperand()->getType());
3181 return Size >= 4 && Size <= 8;
3182}