blob: f6d0239e450d7f9d5c0c782d32c36f02d47a3a53 [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 Parzyszek7d37dd82017-12-06 16:40:37 +0000132static const MVT LegalV64[] = {
133 MVT::v64i8, MVT::v32i16, MVT::v16i32, MVT::v8i64
134};
135static const MVT LegalW64[] = {
136 MVT::v128i8, MVT::v64i16, MVT::v32i32, MVT::v16i64
137};
138static const MVT LegalV128[] = {
139 MVT::v128i8, MVT::v64i16, MVT::v32i32, MVT::v16i64
140};
141static const MVT LegalW128[] = {
142 MVT::v256i8, MVT::v128i16, MVT::v64i32, MVT::v32i64
143};
144
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000145static bool
146CC_Hexagon(unsigned ValNo, MVT ValVT,
147 MVT LocVT, CCValAssign::LocInfo LocInfo,
148 ISD::ArgFlagsTy ArgFlags, CCState &State);
149
150static bool
151CC_Hexagon32(unsigned ValNo, MVT ValVT,
152 MVT LocVT, CCValAssign::LocInfo LocInfo,
153 ISD::ArgFlagsTy ArgFlags, CCState &State);
154
155static bool
156CC_Hexagon64(unsigned ValNo, MVT ValVT,
157 MVT LocVT, CCValAssign::LocInfo LocInfo,
158 ISD::ArgFlagsTy ArgFlags, CCState &State);
159
160static bool
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000161CC_HexagonVector(unsigned ValNo, MVT ValVT,
162 MVT LocVT, CCValAssign::LocInfo LocInfo,
163 ISD::ArgFlagsTy ArgFlags, CCState &State);
164
165static bool
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000166RetCC_Hexagon(unsigned ValNo, MVT ValVT,
167 MVT LocVT, CCValAssign::LocInfo LocInfo,
168 ISD::ArgFlagsTy ArgFlags, CCState &State);
169
170static bool
171RetCC_Hexagon32(unsigned ValNo, MVT ValVT,
172 MVT LocVT, CCValAssign::LocInfo LocInfo,
173 ISD::ArgFlagsTy ArgFlags, CCState &State);
174
175static bool
176RetCC_Hexagon64(unsigned ValNo, MVT ValVT,
177 MVT LocVT, CCValAssign::LocInfo LocInfo,
178 ISD::ArgFlagsTy ArgFlags, CCState &State);
179
180static bool
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000181RetCC_HexagonVector(unsigned ValNo, MVT ValVT,
182 MVT LocVT, CCValAssign::LocInfo LocInfo,
183 ISD::ArgFlagsTy ArgFlags, CCState &State);
184
185static bool
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000186CC_Hexagon_VarArg (unsigned ValNo, MVT ValVT,
187 MVT LocVT, CCValAssign::LocInfo LocInfo,
188 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000189 HexagonCCState &HState = static_cast<HexagonCCState &>(State);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000190
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000191 if (ValNo < HState.getNumNamedVarArgParams()) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000192 // Deal with named arguments.
193 return CC_Hexagon(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State);
194 }
195
196 // Deal with un-named arguments.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000197 unsigned Offset;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000198 if (ArgFlags.isByVal()) {
199 // If pass-by-value, the size allocated on stack is decided
200 // by ArgFlags.getByValSize(), not by the size of LocVT.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000201 Offset = State.AllocateStack(ArgFlags.getByValSize(),
202 ArgFlags.getByValAlign());
203 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000204 return false;
205 }
Jyotsna Vermac7dcc2f2013-03-07 20:28:34 +0000206 if (LocVT == MVT::i1 || LocVT == MVT::i8 || LocVT == MVT::i16) {
207 LocVT = MVT::i32;
208 ValVT = MVT::i32;
209 if (ArgFlags.isSExt())
210 LocInfo = CCValAssign::SExt;
211 else if (ArgFlags.isZExt())
212 LocInfo = CCValAssign::ZExt;
213 else
214 LocInfo = CCValAssign::AExt;
215 }
Sirish Pande69295b82012-05-10 20:20:25 +0000216 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000217 Offset = State.AllocateStack(4, 4);
218 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000219 return false;
220 }
Sirish Pande69295b82012-05-10 20:20:25 +0000221 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000222 Offset = State.AllocateStack(8, 8);
223 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000224 return false;
225 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000226 if (LocVT == MVT::v2i64 || LocVT == MVT::v4i32 || LocVT == MVT::v8i16 ||
227 LocVT == MVT::v16i8) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000228 Offset = State.AllocateStack(16, 16);
229 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000230 return false;
231 }
232 if (LocVT == MVT::v4i64 || LocVT == MVT::v8i32 || LocVT == MVT::v16i16 ||
233 LocVT == MVT::v32i8) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000234 Offset = State.AllocateStack(32, 32);
235 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000236 return false;
237 }
238 if (LocVT == MVT::v8i64 || LocVT == MVT::v16i32 || LocVT == MVT::v32i16 ||
239 LocVT == MVT::v64i8 || LocVT == MVT::v512i1) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000240 Offset = State.AllocateStack(64, 64);
241 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000242 return false;
243 }
244 if (LocVT == MVT::v16i64 || LocVT == MVT::v32i32 || LocVT == MVT::v64i16 ||
245 LocVT == MVT::v128i8 || LocVT == MVT::v1024i1) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000246 Offset = State.AllocateStack(128, 128);
247 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000248 return false;
249 }
250 if (LocVT == MVT::v32i64 || LocVT == MVT::v64i32 || LocVT == MVT::v128i16 ||
251 LocVT == MVT::v256i8) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000252 Offset = State.AllocateStack(256, 256);
253 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000254 return false;
255 }
256
Craig Toppere73658d2014-04-28 04:05:08 +0000257 llvm_unreachable(nullptr);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000258}
259
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000260static bool CC_Hexagon (unsigned ValNo, MVT ValVT, MVT LocVT,
261 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, CCState &State) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000262 if (ArgFlags.isByVal()) {
263 // Passed on stack.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000264 unsigned Offset = State.AllocateStack(ArgFlags.getByValSize(),
265 ArgFlags.getByValAlign());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000266 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
267 return false;
268 }
269
Krzysztof Parzyszek8f23dd62017-03-01 17:30:10 +0000270 if (LocVT == MVT::i1) {
271 LocVT = MVT::i32;
272 } else if (LocVT == MVT::i8 || LocVT == MVT::i16) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000273 LocVT = MVT::i32;
274 ValVT = MVT::i32;
275 if (ArgFlags.isSExt())
276 LocInfo = CCValAssign::SExt;
277 else if (ArgFlags.isZExt())
278 LocInfo = CCValAssign::ZExt;
279 else
280 LocInfo = CCValAssign::AExt;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000281 } else if (LocVT == MVT::v4i8 || LocVT == MVT::v2i16) {
282 LocVT = MVT::i32;
283 LocInfo = CCValAssign::BCvt;
284 } else if (LocVT == MVT::v8i8 || LocVT == MVT::v4i16 || LocVT == MVT::v2i32) {
285 LocVT = MVT::i64;
286 LocInfo = CCValAssign::BCvt;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000287 }
288
Sirish Pande69295b82012-05-10 20:20:25 +0000289 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000290 if (!CC_Hexagon32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
291 return false;
292 }
293
Sirish Pande69295b82012-05-10 20:20:25 +0000294 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000295 if (!CC_Hexagon64(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
296 return false;
297 }
298
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000299 if (LocVT == MVT::v8i32 || LocVT == MVT::v16i16 || LocVT == MVT::v32i8) {
300 unsigned Offset = State.AllocateStack(ArgFlags.getByValSize(), 32);
301 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
302 return false;
303 }
304
Krzysztof Parzyszekac1966e2017-11-27 18:12:16 +0000305 auto &HST = State.getMachineFunction().getSubtarget<HexagonSubtarget>();
306 if (HST.isHVXVectorType(LocVT)) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000307 if (!CC_HexagonVector(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
308 return false;
309 }
310
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000311 return true; // CC didn't match.
312}
313
314
315static bool CC_Hexagon32(unsigned ValNo, MVT ValVT,
316 MVT LocVT, CCValAssign::LocInfo LocInfo,
317 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +0000318 static const MCPhysReg RegList[] = {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000319 Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3, Hexagon::R4,
320 Hexagon::R5
321 };
Tim Northover3b6b7ca2015-02-21 02:11:17 +0000322 if (unsigned Reg = State.AllocateReg(RegList)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000323 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
324 return false;
325 }
326
327 unsigned Offset = State.AllocateStack(4, 4);
328 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
329 return false;
330}
331
332static bool CC_Hexagon64(unsigned ValNo, MVT ValVT,
333 MVT LocVT, CCValAssign::LocInfo LocInfo,
334 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000335 if (unsigned Reg = State.AllocateReg(Hexagon::D0)) {
336 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
337 return false;
338 }
339
Craig Topper840beec2014-04-04 05:16:06 +0000340 static const MCPhysReg RegList1[] = {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000341 Hexagon::D1, Hexagon::D2
342 };
Craig Topper840beec2014-04-04 05:16:06 +0000343 static const MCPhysReg RegList2[] = {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000344 Hexagon::R1, Hexagon::R3
345 };
Tim Northover3b6b7ca2015-02-21 02:11:17 +0000346 if (unsigned Reg = State.AllocateReg(RegList1, RegList2)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000347 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
348 return false;
349 }
350
351 unsigned Offset = State.AllocateStack(8, 8, Hexagon::D2);
352 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
353 return false;
354}
355
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000356static bool CC_HexagonVector(unsigned ValNo, MVT ValVT,
357 MVT LocVT, CCValAssign::LocInfo LocInfo,
358 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000359 static const MCPhysReg VecLstS[] = {
360 Hexagon::V0, Hexagon::V1, Hexagon::V2, Hexagon::V3, Hexagon::V4,
361 Hexagon::V5, Hexagon::V6, Hexagon::V7, Hexagon::V8, Hexagon::V9,
362 Hexagon::V10, Hexagon::V11, Hexagon::V12, Hexagon::V13, Hexagon::V14,
363 Hexagon::V15
364 };
365 static const MCPhysReg VecLstD[] = {
366 Hexagon::W0, Hexagon::W1, Hexagon::W2, Hexagon::W3, Hexagon::W4,
367 Hexagon::W5, Hexagon::W6, Hexagon::W7
368 };
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000369 auto &MF = State.getMachineFunction();
370 auto &HST = MF.getSubtarget<HexagonSubtarget>();
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000371
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +0000372 if (HST.useHVX64BOps() &&
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000373 (LocVT == MVT::v8i64 || LocVT == MVT::v16i32 || LocVT == MVT::v32i16 ||
374 LocVT == MVT::v64i8 || LocVT == MVT::v512i1)) {
375 if (unsigned Reg = State.AllocateReg(VecLstS)) {
376 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
377 return false;
378 }
379 unsigned Offset = State.AllocateStack(64, 64);
380 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
381 return false;
382 }
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +0000383 if (HST.useHVX64BOps() && (LocVT == MVT::v16i64 || LocVT == MVT::v32i32 ||
Sumanth Gundapaneni9d954c42017-10-18 17:45:22 +0000384 LocVT == MVT::v64i16 || LocVT == MVT::v128i8)) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000385 if (unsigned Reg = State.AllocateReg(VecLstD)) {
386 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
387 return false;
388 }
389 unsigned Offset = State.AllocateStack(128, 128);
390 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
391 return false;
392 }
Sumanth Gundapaneni9d954c42017-10-18 17:45:22 +0000393 // 128B Mode
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +0000394 if (HST.useHVX128BOps() && (LocVT == MVT::v32i64 || LocVT == MVT::v64i32 ||
395 LocVT == MVT::v128i16 || LocVT == MVT::v256i8)) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000396 if (unsigned Reg = State.AllocateReg(VecLstD)) {
397 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
398 return false;
399 }
400 unsigned Offset = State.AllocateStack(256, 256);
401 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
402 return false;
403 }
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +0000404 if (HST.useHVX128BOps() &&
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000405 (LocVT == MVT::v16i64 || LocVT == MVT::v32i32 || LocVT == MVT::v64i16 ||
406 LocVT == MVT::v128i8 || LocVT == MVT::v1024i1)) {
407 if (unsigned Reg = State.AllocateReg(VecLstS)) {
408 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
409 return false;
410 }
411 unsigned Offset = State.AllocateStack(128, 128);
412 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
413 return false;
414 }
415 return true;
416}
417
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000418static bool RetCC_Hexagon(unsigned ValNo, MVT ValVT,
419 MVT LocVT, CCValAssign::LocInfo LocInfo,
420 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000421 auto &MF = State.getMachineFunction();
422 auto &HST = MF.getSubtarget<HexagonSubtarget>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000423
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000424 if (LocVT == MVT::i1) {
425 // Return values of type MVT::i1 still need to be assigned to R0, but
426 // the value type needs to remain i1. LowerCallResult will deal with it,
427 // but it needs to recognize i1 as the value type.
428 LocVT = MVT::i32;
429 } else if (LocVT == MVT::i8 || LocVT == MVT::i16) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000430 LocVT = MVT::i32;
431 ValVT = MVT::i32;
432 if (ArgFlags.isSExt())
433 LocInfo = CCValAssign::SExt;
434 else if (ArgFlags.isZExt())
435 LocInfo = CCValAssign::ZExt;
436 else
437 LocInfo = CCValAssign::AExt;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000438 } else if (LocVT == MVT::v4i8 || LocVT == MVT::v2i16) {
439 LocVT = MVT::i32;
440 LocInfo = CCValAssign::BCvt;
441 } else if (LocVT == MVT::v8i8 || LocVT == MVT::v4i16 || LocVT == MVT::v2i32) {
442 LocVT = MVT::i64;
443 LocInfo = CCValAssign::BCvt;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000444 } else if (LocVT == MVT::v64i8 || LocVT == MVT::v32i16 ||
445 LocVT == MVT::v16i32 || LocVT == MVT::v8i64 ||
446 LocVT == MVT::v512i1) {
447 LocVT = MVT::v16i32;
448 ValVT = MVT::v16i32;
449 LocInfo = CCValAssign::Full;
450 } else if (LocVT == MVT::v128i8 || LocVT == MVT::v64i16 ||
451 LocVT == MVT::v32i32 || LocVT == MVT::v16i64 ||
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +0000452 (LocVT == MVT::v1024i1 && HST.useHVX128BOps())) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000453 LocVT = MVT::v32i32;
454 ValVT = MVT::v32i32;
455 LocInfo = CCValAssign::Full;
456 } else if (LocVT == MVT::v256i8 || LocVT == MVT::v128i16 ||
457 LocVT == MVT::v64i32 || LocVT == MVT::v32i64) {
458 LocVT = MVT::v64i32;
459 ValVT = MVT::v64i32;
460 LocInfo = CCValAssign::Full;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000461 }
Sirish Pande69295b82012-05-10 20:20:25 +0000462 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000463 if (!RetCC_Hexagon32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000464 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000465 }
466
Sirish Pande69295b82012-05-10 20:20:25 +0000467 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000468 if (!RetCC_Hexagon64(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000469 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000470 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000471 if (LocVT == MVT::v16i32 || LocVT == MVT::v32i32 || LocVT == MVT::v64i32) {
472 if (!RetCC_HexagonVector(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000473 return false;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000474 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000475 return true; // CC didn't match.
476}
477
478static bool RetCC_Hexagon32(unsigned ValNo, MVT ValVT,
479 MVT LocVT, CCValAssign::LocInfo LocInfo,
480 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Sirish Pande69295b82012-05-10 20:20:25 +0000481 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Krzysztof Parzyszek14412ef2016-07-18 17:36:46 +0000482 // Note that use of registers beyond R1 is not ABI compliant. However there
483 // are (experimental) IR passes which generate internal functions that
484 // return structs using these additional registers.
485 static const uint16_t RegList[] = { Hexagon::R0, Hexagon::R1,
486 Hexagon::R2, Hexagon::R3,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000487 Hexagon::R4, Hexagon::R5 };
Krzysztof Parzyszek14412ef2016-07-18 17:36:46 +0000488 if (unsigned Reg = State.AllocateReg(RegList)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000489 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
490 return false;
491 }
492 }
493
Krzysztof Parzyszek56199522017-04-13 15:05:51 +0000494 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000495}
496
497static bool RetCC_Hexagon64(unsigned ValNo, MVT ValVT,
498 MVT LocVT, CCValAssign::LocInfo LocInfo,
499 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Sirish Pande69295b82012-05-10 20:20:25 +0000500 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000501 if (unsigned Reg = State.AllocateReg(Hexagon::D0)) {
502 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
503 return false;
504 }
505 }
506
Krzysztof Parzyszek56199522017-04-13 15:05:51 +0000507 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000508}
509
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000510static bool RetCC_HexagonVector(unsigned ValNo, MVT ValVT,
511 MVT LocVT, CCValAssign::LocInfo LocInfo,
512 ISD::ArgFlagsTy ArgFlags, CCState &State) {
513 auto &MF = State.getMachineFunction();
514 auto &HST = MF.getSubtarget<HexagonSubtarget>();
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000515
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000516 if (LocVT == MVT::v16i32) {
517 if (unsigned Reg = State.AllocateReg(Hexagon::V0)) {
518 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
519 return false;
520 }
521 } else if (LocVT == MVT::v32i32) {
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +0000522 unsigned Req = HST.useHVX128BOps() ? Hexagon::V0 : Hexagon::W0;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000523 if (unsigned Reg = State.AllocateReg(Req)) {
524 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
525 return false;
526 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000527 } else if (LocVT == MVT::v64i32) {
528 if (unsigned Reg = State.AllocateReg(Hexagon::W0)) {
529 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
530 return false;
531 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000532 }
533
Krzysztof Parzyszek56199522017-04-13 15:05:51 +0000534 return true;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000535}
536
Craig Topper18e69f42016-04-15 06:20:21 +0000537void HexagonTargetLowering::promoteLdStType(MVT VT, MVT PromotedLdStVT) {
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000538 if (VT != PromotedLdStVT) {
Craig Topper18e69f42016-04-15 06:20:21 +0000539 setOperationAction(ISD::LOAD, VT, Promote);
540 AddPromotedToType(ISD::LOAD, VT, PromotedLdStVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000541
Craig Topper18e69f42016-04-15 06:20:21 +0000542 setOperationAction(ISD::STORE, VT, Promote);
543 AddPromotedToType(ISD::STORE, VT, PromotedLdStVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000544 }
545}
546
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000547SDValue
548HexagonTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG)
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000549 const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000550 return SDValue();
551}
552
553/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
554/// by "Src" to address "Dst" of size "Size". Alignment information is
555/// specified by the specific parameter attribute. The copy will be passed as
556/// a byval function parameter. Sometimes what we are copying is the end of a
557/// larger object, the part that does not fit in registers.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000558static SDValue CreateCopyOfByValArgument(SDValue Src, SDValue Dst,
559 SDValue Chain, ISD::ArgFlagsTy Flags,
560 SelectionDAG &DAG, const SDLoc &dl) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000561 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000562 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
563 /*isVolatile=*/false, /*AlwaysInline=*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +0000564 /*isTailCall=*/false,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000565 MachinePointerInfo(), MachinePointerInfo());
566}
567
Krzysztof Parzyszek56199522017-04-13 15:05:51 +0000568bool
569HexagonTargetLowering::CanLowerReturn(
570 CallingConv::ID CallConv, MachineFunction &MF, bool isVarArg,
571 const SmallVectorImpl<ISD::OutputArg> &Outs,
572 LLVMContext &Context) const {
573 SmallVector<CCValAssign, 16> RVLocs;
574 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
575 return CCInfo.CheckReturn(Outs, RetCC_Hexagon);
576}
577
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000578// LowerReturn - Lower ISD::RET. If a struct is larger than 8 bytes and is
579// passed by value, the function prototype is modified to return void and
580// the value is stored in memory pointed by a pointer passed by caller.
581SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000582HexagonTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
583 bool isVarArg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000584 const SmallVectorImpl<ISD::OutputArg> &Outs,
585 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000586 const SDLoc &dl, SelectionDAG &DAG) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000587 // CCValAssign - represent the assignment of the return value to locations.
588 SmallVector<CCValAssign, 16> RVLocs;
589
590 // CCState - Info about the registers and stack slot.
Eric Christopherb5217502014-08-06 18:45:26 +0000591 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
592 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000593
594 // Analyze return values of ISD::RET
595 CCInfo.AnalyzeReturn(Outs, RetCC_Hexagon);
596
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000597 SDValue Flag;
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000598 SmallVector<SDValue, 4> RetOps(1, Chain);
599
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000600 // Copy the result values into the output registers.
601 for (unsigned i = 0; i != RVLocs.size(); ++i) {
602 CCValAssign &VA = RVLocs[i];
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000603
604 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), OutVals[i], Flag);
605
606 // Guarantee that all emitted copies are stuck together with flags.
607 Flag = Chain.getValue(1);
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000608 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000609 }
610
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000611 RetOps[0] = Chain; // Update chain.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000612
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000613 // Add the flag if we have it.
614 if (Flag.getNode())
615 RetOps.push_back(Flag);
616
Craig Topper48d114b2014-04-26 18:35:24 +0000617 return DAG.getNode(HexagonISD::RET_FLAG, dl, MVT::Other, RetOps);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000618}
619
Matt Arsenault31380752017-04-18 21:16:46 +0000620bool HexagonTargetLowering::mayBeEmittedAsTailCall(const CallInst *CI) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000621 // If either no tail call or told not to tail call at all, don't.
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000622 auto Attr =
623 CI->getParent()->getParent()->getFnAttribute("disable-tail-calls");
624 if (!CI->isTailCall() || Attr.getValueAsString() == "true")
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000625 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000626
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000627 return true;
628}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000629
630/// LowerCallResult - Lower the result values of an ISD::CALL into the
631/// appropriate copies out of appropriate physical registers. This assumes that
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000632/// Chain/Glue are the input chain/glue to use, and that TheCall is the call
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000633/// being lowered. Returns a SDNode with the same number of values as the
634/// ISD::CALL.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000635SDValue HexagonTargetLowering::LowerCallResult(
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000636 SDValue Chain, SDValue Glue, CallingConv::ID CallConv, bool isVarArg,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000637 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
638 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals,
639 const SmallVectorImpl<SDValue> &OutVals, SDValue Callee) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000640 // Assign locations to each value returned by this call.
641 SmallVector<CCValAssign, 16> RVLocs;
642
Eric Christopherb5217502014-08-06 18:45:26 +0000643 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
644 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000645
646 CCInfo.AnalyzeCallResult(Ins, RetCC_Hexagon);
647
648 // Copy all of the result registers out of their specified physreg.
649 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000650 SDValue RetVal;
651 if (RVLocs[i].getValVT() == MVT::i1) {
652 // Return values of type MVT::i1 require special handling. The reason
653 // is that MVT::i1 is associated with the PredRegs register class, but
654 // values of that type are still returned in R0. Generate an explicit
655 // copy into a predicate register from R0, and treat the value of the
656 // predicate register as the call result.
657 auto &MRI = DAG.getMachineFunction().getRegInfo();
658 SDValue FR0 = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000659 MVT::i32, Glue);
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000660 // FR0 = (Value, Chain, Glue)
661 unsigned PredR = MRI.createVirtualRegister(&Hexagon::PredRegsRegClass);
662 SDValue TPR = DAG.getCopyToReg(FR0.getValue(1), dl, PredR,
663 FR0.getValue(0), FR0.getValue(2));
664 // TPR = (Chain, Glue)
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000665 // Don't glue this CopyFromReg, because it copies from a virtual
666 // register. If it is glued to the call, InstrEmitter will add it
667 // as an implicit def to the call (EmitMachineNode).
668 RetVal = DAG.getCopyFromReg(TPR.getValue(0), dl, PredR, MVT::i1);
669 Glue = TPR.getValue(1);
Krzysztof Parzyszek6f06b6e2017-10-23 19:35:25 +0000670 Chain = TPR.getValue(0);
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000671 } else {
672 RetVal = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000673 RVLocs[i].getValVT(), Glue);
674 Glue = RetVal.getValue(2);
Krzysztof Parzyszek6f06b6e2017-10-23 19:35:25 +0000675 Chain = RetVal.getValue(1);
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000676 }
677 InVals.push_back(RetVal.getValue(0));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000678 }
679
680 return Chain;
681}
682
683/// LowerCall - Functions arguments are copied from virtual regs to
684/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
685SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +0000686HexagonTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000687 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +0000688 SelectionDAG &DAG = CLI.DAG;
Craig Topperb94011f2013-07-14 04:42:23 +0000689 SDLoc &dl = CLI.DL;
690 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
691 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
692 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000693 SDValue Chain = CLI.Chain;
694 SDValue Callee = CLI.Callee;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000695 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000696 CallingConv::ID CallConv = CLI.CallConv;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000697 bool IsVarArg = CLI.IsVarArg;
698 bool DoesNotReturn = CLI.DoesNotReturn;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000699
700 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000701 MachineFunction &MF = DAG.getMachineFunction();
Krzysztof Parzyszekf67cd822017-07-11 17:11:54 +0000702 MachineFrameInfo &MFI = MF.getFrameInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +0000703 auto PtrVT = getPointerTy(MF.getDataLayout());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000704
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000705 // Check for varargs.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000706 unsigned NumNamedVarArgParams = -1U;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000707 if (GlobalAddressSDNode *GAN = dyn_cast<GlobalAddressSDNode>(Callee)) {
708 const GlobalValue *GV = GAN->getGlobal();
709 Callee = DAG.getTargetGlobalAddress(GV, dl, MVT::i32);
710 if (const Function* F = dyn_cast<Function>(GV)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000711 // If a function has zero args and is a vararg function, that's
712 // disallowed so it must be an undeclared function. Do not assume
713 // varargs if the callee is undefined.
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000714 if (F->isVarArg() && F->getFunctionType()->getNumParams() != 0)
715 NumNamedVarArgParams = F->getFunctionType()->getNumParams();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000716 }
717 }
718
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000719 // Analyze operands of the call, assigning locations to each operand.
720 SmallVector<CCValAssign, 16> ArgLocs;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000721 HexagonCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
Eric Christopherb5217502014-08-06 18:45:26 +0000722 *DAG.getContext(), NumNamedVarArgParams);
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000723
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000724 if (IsVarArg)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000725 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon_VarArg);
726 else
727 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon);
728
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000729 auto Attr = MF.getFunction()->getFnAttribute("disable-tail-calls");
730 if (Attr.getValueAsString() == "true")
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000731 IsTailCall = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000732
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000733 if (IsTailCall) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000734 bool StructAttrFlag = MF.getFunction()->hasStructRetAttr();
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000735 IsTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
736 IsVarArg, IsStructRet,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000737 StructAttrFlag,
738 Outs, OutVals, Ins, DAG);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000739 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000740 CCValAssign &VA = ArgLocs[i];
741 if (VA.isMemLoc()) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000742 IsTailCall = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000743 break;
744 }
745 }
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000746 DEBUG(dbgs() << (IsTailCall ? "Eligible for Tail Call\n"
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000747 : "Argument must be passed on stack. "
748 "Not eligible for Tail Call\n"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000749 }
750 // Get a count of how many bytes are to be pushed on the stack.
751 unsigned NumBytes = CCInfo.getNextStackOffset();
752 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
753 SmallVector<SDValue, 8> MemOpChains;
754
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000755 auto &HRI = *Subtarget.getRegisterInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +0000756 SDValue StackPtr =
757 DAG.getCopyFromReg(Chain, dl, HRI.getStackRegister(), PtrVT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000758
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000759 bool NeedsArgAlign = false;
760 unsigned LargestAlignSeen = 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000761 // Walk the register/memloc assignments, inserting copies/loads.
762 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
763 CCValAssign &VA = ArgLocs[i];
764 SDValue Arg = OutVals[i];
765 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000766 // Record if we need > 8 byte alignment on an argument.
Krzysztof Parzyszekac1966e2017-11-27 18:12:16 +0000767 bool ArgAlign = Subtarget.isHVXVectorType(VA.getValVT());
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000768 NeedsArgAlign |= ArgAlign;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000769
770 // Promote the value if needed.
771 switch (VA.getLocInfo()) {
772 default:
773 // Loc info must be one of Full, SExt, ZExt, or AExt.
Craig Toppere55c5562012-02-07 02:50:20 +0000774 llvm_unreachable("Unknown loc info!");
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000775 case CCValAssign::BCvt:
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000776 case CCValAssign::Full:
777 break;
778 case CCValAssign::SExt:
779 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
780 break;
781 case CCValAssign::ZExt:
782 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
783 break;
784 case CCValAssign::AExt:
785 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
786 break;
787 }
788
789 if (VA.isMemLoc()) {
790 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000791 SDValue MemAddr = DAG.getConstant(LocMemOffset, dl,
792 StackPtr.getValueType());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000793 MemAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, MemAddr);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000794 if (ArgAlign)
795 LargestAlignSeen = std::max(LargestAlignSeen,
796 VA.getLocVT().getStoreSizeInBits() >> 3);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000797 if (Flags.isByVal()) {
798 // The argument is a struct passed by value. According to LLVM, "Arg"
799 // is is pointer.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000800 MemOpChains.push_back(CreateCopyOfByValArgument(Arg, MemAddr, Chain,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000801 Flags, DAG, dl));
802 } else {
Alex Lorenze40c8a22015-08-11 23:09:45 +0000803 MachinePointerInfo LocPI = MachinePointerInfo::getStack(
804 DAG.getMachineFunction(), LocMemOffset);
Justin Lebar9c375812016-07-15 18:27:10 +0000805 SDValue S = DAG.getStore(Chain, dl, Arg, MemAddr, LocPI);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000806 MemOpChains.push_back(S);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000807 }
808 continue;
809 }
810
811 // Arguments that can be passed on register must be kept at RegsToPass
812 // vector.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000813 if (VA.isRegLoc())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000814 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000815 }
816
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000817 if (NeedsArgAlign && Subtarget.hasV60TOps()) {
818 DEBUG(dbgs() << "Function needs byte stack align due to call args\n");
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000819 // V6 vectors passed by value have 64 or 128 byte alignment depending
820 // on whether we are 64 byte vector mode or 128 byte.
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +0000821 bool UseHVX128B = Subtarget.useHVX128BOps();
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000822 assert(Subtarget.useHVXOps());
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +0000823 const unsigned ObjAlign = UseHVX128B ? 128 : 64;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000824 LargestAlignSeen = std::max(LargestAlignSeen, ObjAlign);
Matthias Braun941a7052016-07-28 18:40:00 +0000825 MFI.ensureMaxAlignment(LargestAlignSeen);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000826 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000827 // Transform all store nodes into one single node because all store
828 // nodes are independent of each other.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000829 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000830 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000831
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000832 SDValue Glue;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000833 if (!IsTailCall) {
Serge Pavlovd526b132017-05-09 13:35:13 +0000834 Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, dl);
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000835 Glue = Chain.getValue(1);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000836 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000837
838 // Build a sequence of copy-to-reg nodes chained together with token
839 // chain and flag operands which copy the outgoing args into registers.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000840 // The Glue is necessary since all emitted instructions must be
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000841 // stuck together.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000842 if (!IsTailCall) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000843 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
844 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000845 RegsToPass[i].second, Glue);
846 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000847 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000848 } else {
849 // For tail calls lower the arguments to the 'real' stack slot.
850 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000851 // Force all the incoming stack arguments to be loaded from the stack
852 // before any new outgoing arguments are stored to the stack, because the
853 // outgoing stack slots may alias the incoming argument stack slots, and
854 // the alias isn't otherwise explicit. This is slightly more conservative
855 // than necessary, because it means that each store effectively depends
856 // on every argument instead of just those arguments it would clobber.
857 //
Benjamin Kramerbde91762012-06-02 10:20:22 +0000858 // Do not flag preceding copytoreg stuff together with the following stuff.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000859 Glue = SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000860 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
861 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000862 RegsToPass[i].second, Glue);
863 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000864 }
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000865 Glue = SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000866 }
867
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000868 bool LongCalls = MF.getSubtarget<HexagonSubtarget>().useLongCalls();
869 unsigned Flags = LongCalls ? HexagonII::HMOTF_ConstExtended : 0;
870
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000871 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
872 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
873 // node so that legalize doesn't hack it.
Tobias Edler von Kochb51460c2015-12-16 17:29:37 +0000874 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000875 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, PtrVT, 0, Flags);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000876 } else if (ExternalSymbolSDNode *S =
877 dyn_cast<ExternalSymbolSDNode>(Callee)) {
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000878 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), PtrVT, Flags);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000879 }
880
881 // Returns a chain & a flag for retval copy to use.
882 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
883 SmallVector<SDValue, 8> Ops;
884 Ops.push_back(Chain);
885 Ops.push_back(Callee);
886
887 // Add argument registers to the end of the list so that they are
888 // known live into the call.
889 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
890 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
891 RegsToPass[i].second.getValueType()));
892 }
893
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000894 const uint32_t *Mask = HRI.getCallPreservedMask(MF, CallConv);
895 assert(Mask && "Missing call preserved mask for calling convention");
896 Ops.push_back(DAG.getRegisterMask(Mask));
897
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000898 if (Glue.getNode())
899 Ops.push_back(Glue);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000900
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000901 if (IsTailCall) {
Krzysztof Parzyszekf67cd822017-07-11 17:11:54 +0000902 MFI.setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +0000903 return DAG.getNode(HexagonISD::TC_RETURN, dl, NodeTys, Ops);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +0000904 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000905
Krzysztof Parzyszekf67cd822017-07-11 17:11:54 +0000906 // Set this here because we need to know this for "hasFP" in frame lowering.
907 // The target-independent code calls getFrameRegister before setting it, and
908 // getFrameRegister uses hasFP to determine whether the function has FP.
909 MFI.setHasCalls(true);
910
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +0000911 unsigned OpCode = DoesNotReturn ? HexagonISD::CALLnr : HexagonISD::CALL;
Colin LeMahieu2e3a26d2015-01-16 17:05:27 +0000912 Chain = DAG.getNode(OpCode, dl, NodeTys, Ops);
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000913 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000914
915 // Create the CALLSEQ_END node.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000916 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000917 DAG.getIntPtrConstant(0, dl, true), Glue, dl);
918 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000919
920 // Handle result values, copying them out of physregs into vregs that we
921 // return.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000922 return LowerCallResult(Chain, Glue, CallConv, IsVarArg, Ins, dl, DAG,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000923 InVals, OutVals, Callee);
924}
925
926static bool getIndexedAddressParts(SDNode *Ptr, EVT VT,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000927 SDValue &Base, SDValue &Offset,
928 bool &IsInc, SelectionDAG &DAG) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000929 if (Ptr->getOpcode() != ISD::ADD)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000930 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000931
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000932 auto &HST = static_cast<const HexagonSubtarget&>(DAG.getSubtarget());
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000933
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +0000934 bool ValidHVX128BType =
935 HST.useHVX128BOps() && (VT == MVT::v32i32 || VT == MVT::v16i64 ||
936 VT == MVT::v64i16 || VT == MVT::v128i8);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000937 bool ValidHVXType =
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +0000938 HST.useHVX64BOps() && (VT == MVT::v16i32 || VT == MVT::v8i64 ||
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000939 VT == MVT::v32i16 || VT == MVT::v64i8);
940
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +0000941 if (ValidHVX128BType || ValidHVXType || VT == MVT::i64 || VT == MVT::i32 ||
942 VT == MVT::i16 || VT == MVT::i8) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000943 IsInc = (Ptr->getOpcode() == ISD::ADD);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000944 Base = Ptr->getOperand(0);
945 Offset = Ptr->getOperand(1);
946 // Ensure that Offset is a constant.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000947 return isa<ConstantSDNode>(Offset);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000948 }
949
950 return false;
951}
952
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000953/// getPostIndexedAddressParts - returns true by value, base pointer and
954/// offset pointer and addressing mode by reference if this node can be
955/// combined with a load / store to form a post-indexed load / store.
956bool HexagonTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
957 SDValue &Base,
958 SDValue &Offset,
959 ISD::MemIndexedMode &AM,
960 SelectionDAG &DAG) const
961{
962 EVT VT;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000963
964 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
965 VT = LD->getMemoryVT();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000966 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
967 VT = ST->getMemoryVT();
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000968 if (ST->getValue().getValueType() == MVT::i64 && ST->isTruncatingStore())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000969 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000970 } else {
971 return false;
972 }
973
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000974 bool IsInc = false;
975 bool isLegal = getIndexedAddressParts(Op, VT, Base, Offset, IsInc, DAG);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000976 if (isLegal) {
977 auto &HII = *Subtarget.getInstrInfo();
978 int32_t OffsetVal = cast<ConstantSDNode>(Offset.getNode())->getSExtValue();
979 if (HII.isValidAutoIncImm(VT, OffsetVal)) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000980 AM = IsInc ? ISD::POST_INC : ISD::POST_DEC;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000981 return true;
982 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000983 }
984
985 return false;
986}
987
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000988SDValue
989HexagonTargetLowering::LowerINLINEASM(SDValue Op, SelectionDAG &DAG) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000990 MachineFunction &MF = DAG.getMachineFunction();
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000991 auto &HMFI = *MF.getInfo<HexagonMachineFunctionInfo>();
992 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
993 unsigned LR = HRI.getRARegister();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000994
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000995 if (Op.getOpcode() != ISD::INLINEASM || HMFI.hasClobberLR())
996 return Op;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000997
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000998 unsigned NumOps = Op.getNumOperands();
999 if (Op.getOperand(NumOps-1).getValueType() == MVT::Glue)
1000 --NumOps; // Ignore the flag operand.
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001001
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +00001002 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
1003 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(i))->getZExtValue();
1004 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
1005 ++i; // Skip the ID value.
1006
1007 switch (InlineAsm::getKind(Flags)) {
1008 default:
1009 llvm_unreachable("Bad flags!");
1010 case InlineAsm::Kind_RegUse:
1011 case InlineAsm::Kind_Imm:
1012 case InlineAsm::Kind_Mem:
1013 i += NumVals;
1014 break;
1015 case InlineAsm::Kind_Clobber:
1016 case InlineAsm::Kind_RegDef:
1017 case InlineAsm::Kind_RegDefEarlyClobber: {
1018 for (; NumVals; --NumVals, ++i) {
1019 unsigned Reg = cast<RegisterSDNode>(Op.getOperand(i))->getReg();
1020 if (Reg != LR)
1021 continue;
1022 HMFI.setHasClobberLR(true);
1023 return Op;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001024 }
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +00001025 break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001026 }
1027 }
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +00001028 }
1029
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001030 return Op;
1031}
1032
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00001033// Need to transform ISD::PREFETCH into something that doesn't inherit
1034// all of the properties of ISD::PREFETCH, specifically SDNPMayLoad and
1035// SDNPMayStore.
1036SDValue HexagonTargetLowering::LowerPREFETCH(SDValue Op,
1037 SelectionDAG &DAG) const {
1038 SDValue Chain = Op.getOperand(0);
1039 SDValue Addr = Op.getOperand(1);
1040 // Lower it to DCFETCH($reg, #0). A "pat" will try to merge the offset in,
1041 // if the "reg" is fed by an "add".
1042 SDLoc DL(Op);
1043 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
1044 return DAG.getNode(HexagonISD::DCFETCH, DL, MVT::Other, Chain, Addr, Zero);
1045}
1046
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +00001047// Custom-handle ISD::READCYCLECOUNTER because the target-independent SDNode
1048// is marked as having side-effects, while the register read on Hexagon does
1049// not have any. TableGen refuses to accept the direct pattern from that node
1050// to the A4_tfrcpp.
1051SDValue HexagonTargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
1052 SelectionDAG &DAG) const {
1053 SDValue Chain = Op.getOperand(0);
1054 SDLoc dl(Op);
1055 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
1056 return DAG.getNode(HexagonISD::READCYCLE, dl, VTs, Chain);
1057}
1058
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00001059SDValue HexagonTargetLowering::LowerINTRINSIC_VOID(SDValue Op,
1060 SelectionDAG &DAG) const {
1061 SDValue Chain = Op.getOperand(0);
1062 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1063 // Lower the hexagon_prefetch builtin to DCFETCH, as above.
1064 if (IntNo == Intrinsic::hexagon_prefetch) {
1065 SDValue Addr = Op.getOperand(2);
1066 SDLoc DL(Op);
1067 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
1068 return DAG.getNode(HexagonISD::DCFETCH, DL, MVT::Other, Chain, Addr, Zero);
1069 }
1070 return SDValue();
1071}
1072
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001073SDValue
1074HexagonTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
1075 SelectionDAG &DAG) const {
1076 SDValue Chain = Op.getOperand(0);
1077 SDValue Size = Op.getOperand(1);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001078 SDValue Align = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001079 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001080
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001081 ConstantSDNode *AlignConst = dyn_cast<ConstantSDNode>(Align);
1082 assert(AlignConst && "Non-constant Align in LowerDYNAMIC_STACKALLOC");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001083
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001084 unsigned A = AlignConst->getSExtValue();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001085 auto &HFI = *Subtarget.getFrameLowering();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001086 // "Zero" means natural stack alignment.
1087 if (A == 0)
1088 A = HFI.getStackAlignment();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001089
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001090 DEBUG({
Reid Kleckner40d72302016-10-20 00:22:23 +00001091 dbgs () << __func__ << " Align: " << A << " Size: ";
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001092 Size.getNode()->dump(&DAG);
1093 dbgs() << "\n";
1094 });
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001095
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001096 SDValue AC = DAG.getConstant(A, dl, MVT::i32);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001097 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00001098 SDValue AA = DAG.getNode(HexagonISD::ALLOCA, dl, VTs, Chain, Size, AC);
Nirav Davebfdb4832016-06-23 17:52:57 +00001099
1100 DAG.ReplaceAllUsesOfValueWith(Op, AA);
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00001101 return AA;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001102}
1103
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001104SDValue HexagonTargetLowering::LowerFormalArguments(
1105 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
1106 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
1107 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001108 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00001109 MachineFrameInfo &MFI = MF.getFrameInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001110 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001111 auto &FuncInfo = *MF.getInfo<HexagonMachineFunctionInfo>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001112
1113 // Assign locations to all of the incoming arguments.
1114 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001115 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1116 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001117
1118 CCInfo.AnalyzeFormalArguments(Ins, CC_Hexagon);
1119
1120 // For LLVM, in the case when returning a struct by value (>8byte),
1121 // the first argument is a pointer that points to the location on caller's
1122 // stack where the return value will be stored. For Hexagon, the location on
1123 // caller's stack is passed only when the struct size is smaller than (and
1124 // equal to) 8 bytes. If not, no address will be passed into callee and
1125 // callee return the result direclty through R0/R1.
1126
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001127 SmallVector<SDValue, 8> MemOps;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001128
1129 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1130 CCValAssign &VA = ArgLocs[i];
1131 ISD::ArgFlagsTy Flags = Ins[i].Flags;
1132 unsigned ObjSize;
1133 unsigned StackLocation;
1134 int FI;
1135
1136 if ( (VA.isRegLoc() && !Flags.isByVal())
1137 || (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() > 8)) {
1138 // Arguments passed in registers
1139 // 1. int, long long, ptr args that get allocated in register.
1140 // 2. Large struct that gets an register to put its address in.
1141 EVT RegVT = VA.getLocVT();
Sirish Pande69295b82012-05-10 20:20:25 +00001142 if (RegVT == MVT::i8 || RegVT == MVT::i16 ||
1143 RegVT == MVT::i32 || RegVT == MVT::f32) {
Krzysztof Parzyszek6acecc92017-11-22 20:43:00 +00001144 unsigned VReg =
Craig Topperc7242e02012-04-20 07:30:17 +00001145 RegInfo.createVirtualRegister(&Hexagon::IntRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001146 RegInfo.addLiveIn(VA.getLocReg(), VReg);
Krzysztof Parzyszek8f23dd62017-03-01 17:30:10 +00001147 SDValue Copy = DAG.getCopyFromReg(Chain, dl, VReg, RegVT);
1148 // Treat values of type MVT::i1 specially: they are passed in
1149 // registers of type i32, but they need to remain as values of
1150 // type i1 for consistency of the argument lowering.
1151 if (VA.getValVT() == MVT::i1) {
1152 // Generate a copy into a predicate register and use the value
1153 // of the register as the "InVal".
1154 unsigned PReg =
1155 RegInfo.createVirtualRegister(&Hexagon::PredRegsRegClass);
1156 SDNode *T = DAG.getMachineNode(Hexagon::C2_tfrrp, dl, MVT::i1,
1157 Copy.getValue(0));
1158 Copy = DAG.getCopyToReg(Copy.getValue(1), dl, PReg, SDValue(T, 0));
1159 Copy = DAG.getCopyFromReg(Copy, dl, PReg, MVT::i1);
1160 }
1161 InVals.push_back(Copy);
1162 Chain = Copy.getValue(1);
Colin LeMahieu4379d102015-01-28 22:08:16 +00001163 } else if (RegVT == MVT::i64 || RegVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001164 unsigned VReg =
Craig Topperc7242e02012-04-20 07:30:17 +00001165 RegInfo.createVirtualRegister(&Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001166 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1167 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001168
1169 // Single Vector
1170 } else if ((RegVT == MVT::v8i64 || RegVT == MVT::v16i32 ||
1171 RegVT == MVT::v32i16 || RegVT == MVT::v64i8)) {
1172 unsigned VReg =
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001173 RegInfo.createVirtualRegister(&Hexagon::HvxVRRegClass);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001174 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1175 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +00001176 } else if (Subtarget.useHVX128BOps() &&
Sumanth Gundapaneni9d954c42017-10-18 17:45:22 +00001177 ((RegVT == MVT::v16i64 || RegVT == MVT::v32i32 ||
1178 RegVT == MVT::v64i16 || RegVT == MVT::v128i8))) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001179 unsigned VReg =
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001180 RegInfo.createVirtualRegister(&Hexagon::HvxVRRegClass);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001181 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1182 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1183
1184 // Double Vector
1185 } else if ((RegVT == MVT::v16i64 || RegVT == MVT::v32i32 ||
1186 RegVT == MVT::v64i16 || RegVT == MVT::v128i8)) {
1187 unsigned VReg =
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001188 RegInfo.createVirtualRegister(&Hexagon::HvxWRRegClass);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001189 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1190 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +00001191 } else if (Subtarget.useHVX128BOps() &&
Sumanth Gundapaneni9d954c42017-10-18 17:45:22 +00001192 ((RegVT == MVT::v32i64 || RegVT == MVT::v64i32 ||
1193 RegVT == MVT::v128i16 || RegVT == MVT::v256i8))) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001194 unsigned VReg =
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001195 RegInfo.createVirtualRegister(&Hexagon::HvxWRRegClass);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001196 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1197 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1198 } else if (RegVT == MVT::v512i1 || RegVT == MVT::v1024i1) {
1199 assert(0 && "need to support VecPred regs");
1200 unsigned VReg =
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001201 RegInfo.createVirtualRegister(&Hexagon::HvxQRRegClass);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001202 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1203 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001204 } else {
1205 assert (0);
1206 }
1207 } else if (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() <= 8) {
1208 assert (0 && "ByValSize must be bigger than 8 bytes");
1209 } else {
1210 // Sanity check.
1211 assert(VA.isMemLoc());
1212
1213 if (Flags.isByVal()) {
1214 // If it's a byval parameter, then we need to compute the
1215 // "real" size, not the size of the pointer.
1216 ObjSize = Flags.getByValSize();
1217 } else {
1218 ObjSize = VA.getLocVT().getStoreSizeInBits() >> 3;
1219 }
1220
1221 StackLocation = HEXAGON_LRFP_SIZE + VA.getLocMemOffset();
1222 // Create the frame index object for this incoming parameter...
Matthias Braun941a7052016-07-28 18:40:00 +00001223 FI = MFI.CreateFixedObject(ObjSize, StackLocation, true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001224
1225 // Create the SelectionDAG nodes cordl, responding to a load
1226 // from this parameter.
1227 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
1228
1229 if (Flags.isByVal()) {
1230 // If it's a pass-by-value aggregate, then do not dereference the stack
1231 // location. Instead, we should generate a reference to the stack
1232 // location.
1233 InVals.push_back(FIN);
1234 } else {
Justin Lebar9c375812016-07-15 18:27:10 +00001235 InVals.push_back(
Krzysztof Parzyszek3e2046c2017-04-13 15:00:18 +00001236 DAG.getLoad(VA.getValVT(), dl, Chain, FIN, MachinePointerInfo()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001237 }
1238 }
1239 }
1240
1241 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001242 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001243
1244 if (isVarArg) {
1245 // This will point to the next argument passed via stack.
Matthias Braun941a7052016-07-28 18:40:00 +00001246 int FrameIndex = MFI.CreateFixedObject(Hexagon_PointerSize,
1247 HEXAGON_LRFP_SIZE +
1248 CCInfo.getNextStackOffset(),
1249 true);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001250 FuncInfo.setVarArgsFrameIndex(FrameIndex);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001251 }
1252
1253 return Chain;
1254}
1255
1256SDValue
1257HexagonTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
1258 // VASTART stores the address of the VarArgsFrameIndex slot into the
1259 // memory location argument.
1260 MachineFunction &MF = DAG.getMachineFunction();
1261 HexagonMachineFunctionInfo *QFI = MF.getInfo<HexagonMachineFunctionInfo>();
1262 SDValue Addr = DAG.getFrameIndex(QFI->getVarArgsFrameIndex(), MVT::i32);
1263 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Justin Lebar9c375812016-07-15 18:27:10 +00001264 return DAG.getStore(Op.getOperand(0), SDLoc(Op), Addr, Op.getOperand(1),
1265 MachinePointerInfo(SV));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001266}
1267
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001268static bool isSExtFree(SDValue N) {
1269 // A sign-extend of a truncate of a sign-extend is free.
1270 if (N.getOpcode() == ISD::TRUNCATE &&
1271 N.getOperand(0).getOpcode() == ISD::AssertSext)
1272 return true;
1273 // We have sign-extended loads.
1274 if (N.getOpcode() == ISD::LOAD)
1275 return true;
1276 return false;
1277}
1278
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001279SDValue HexagonTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1280 SDLoc dl(Op);
1281
1282 SDValue LHS = Op.getOperand(0);
1283 SDValue RHS = Op.getOperand(1);
1284 SDValue Cmp = Op.getOperand(2);
1285 ISD::CondCode CC = cast<CondCodeSDNode>(Cmp)->get();
1286
1287 EVT VT = Op.getValueType();
1288 EVT LHSVT = LHS.getValueType();
1289 EVT RHSVT = RHS.getValueType();
1290
1291 if (LHSVT == MVT::v2i16) {
1292 assert(ISD::isSignedIntSetCC(CC) || ISD::isUnsignedIntSetCC(CC));
1293 unsigned ExtOpc = ISD::isSignedIntSetCC(CC) ? ISD::SIGN_EXTEND
1294 : ISD::ZERO_EXTEND;
1295 SDValue LX = DAG.getNode(ExtOpc, dl, MVT::v2i32, LHS);
1296 SDValue RX = DAG.getNode(ExtOpc, dl, MVT::v2i32, RHS);
1297 SDValue SC = DAG.getNode(ISD::SETCC, dl, MVT::v2i1, LX, RX, Cmp);
1298 return SC;
1299 }
1300
1301 // Treat all other vector types as legal.
1302 if (VT.isVector())
1303 return Op;
1304
1305 // Equals and not equals should use sign-extend, not zero-extend, since
1306 // we can represent small negative values in the compare instructions.
1307 // The LLVM default is to use zero-extend arbitrarily in these cases.
1308 if ((CC == ISD::SETEQ || CC == ISD::SETNE) &&
1309 (RHSVT == MVT::i8 || RHSVT == MVT::i16) &&
1310 (LHSVT == MVT::i8 || LHSVT == MVT::i16)) {
1311 ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS);
1312 if (C && C->getAPIntValue().isNegative()) {
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 if (isSExtFree(LHS) || isSExtFree(RHS)) {
1319 LHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, LHS);
1320 RHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, RHS);
1321 return DAG.getNode(ISD::SETCC, dl, Op.getValueType(),
1322 LHS, RHS, Op.getOperand(2));
1323 }
1324 }
1325 return SDValue();
1326}
1327
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001328SDValue
1329HexagonTargetLowering::LowerVSELECT(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001330 SDValue PredOp = Op.getOperand(0);
1331 SDValue Op1 = Op.getOperand(1), Op2 = Op.getOperand(2);
1332 EVT OpVT = Op1.getValueType();
1333 SDLoc DL(Op);
1334
1335 if (OpVT == MVT::v2i16) {
1336 SDValue X1 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op1);
1337 SDValue X2 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op2);
1338 SDValue SL = DAG.getNode(ISD::VSELECT, DL, MVT::v2i32, PredOp, X1, X2);
1339 SDValue TR = DAG.getNode(ISD::TRUNCATE, DL, MVT::v2i16, SL);
1340 return TR;
1341 }
1342
1343 return SDValue();
1344}
1345
Krzysztof Parzyszek91ff5c62017-08-01 13:12:53 +00001346static Constant *convert_i1_to_i8(const Constant *ConstVal) {
1347 SmallVector<Constant *, 128> NewConst;
1348 const ConstantVector *CV = dyn_cast<ConstantVector>(ConstVal);
1349 if (!CV)
1350 return nullptr;
1351
1352 LLVMContext &Ctx = ConstVal->getContext();
1353 IRBuilder<> IRB(Ctx);
1354 unsigned NumVectorElements = CV->getNumOperands();
1355 assert(isPowerOf2_32(NumVectorElements) &&
1356 "conversion only supported for pow2 VectorSize!");
1357
1358 for (unsigned i = 0; i < NumVectorElements / 8; ++i) {
1359 uint8_t x = 0;
1360 for (unsigned j = 0; j < 8; ++j) {
1361 uint8_t y = CV->getOperand(i * 8 + j)->getUniqueInteger().getZExtValue();
1362 x |= y << (7 - j);
1363 }
1364 assert((x == 0 || x == 255) && "Either all 0's or all 1's expected!");
1365 NewConst.push_back(IRB.getInt8(x));
1366 }
1367 return ConstantVector::get(NewConst);
1368}
1369
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001370SDValue
Sirish Pande69295b82012-05-10 20:20:25 +00001371HexagonTargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
1372 EVT ValTy = Op.getValueType();
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001373 ConstantPoolSDNode *CPN = cast<ConstantPoolSDNode>(Op);
Krzysztof Parzyszek91ff5c62017-08-01 13:12:53 +00001374 Constant *CVal = nullptr;
1375 bool isVTi1Type = false;
1376 if (const Constant *ConstVal = dyn_cast<Constant>(CPN->getConstVal())) {
1377 Type *CValTy = ConstVal->getType();
1378 if (CValTy->isVectorTy() &&
1379 CValTy->getVectorElementType()->isIntegerTy(1)) {
1380 CVal = convert_i1_to_i8(ConstVal);
1381 isVTi1Type = (CVal != nullptr);
1382 }
1383 }
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001384 unsigned Align = CPN->getAlignment();
Rafael Espindola405e25a2016-06-26 22:24:01 +00001385 bool IsPositionIndependent = isPositionIndependent();
1386 unsigned char TF = IsPositionIndependent ? HexagonII::MO_PCREL : 0;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001387
Ron Lieberman822ee882016-08-13 23:41:11 +00001388 unsigned Offset = 0;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001389 SDValue T;
1390 if (CPN->isMachineConstantPoolEntry())
Ron Lieberman822ee882016-08-13 23:41:11 +00001391 T = DAG.getTargetConstantPool(CPN->getMachineCPVal(), ValTy, Align, Offset,
1392 TF);
Krzysztof Parzyszek91ff5c62017-08-01 13:12:53 +00001393 else if (isVTi1Type)
1394 T = DAG.getTargetConstantPool(CVal, ValTy, Align, Offset, TF);
Sirish Pande69295b82012-05-10 20:20:25 +00001395 else
Ron Lieberman822ee882016-08-13 23:41:11 +00001396 T = DAG.getTargetConstantPool(CPN->getConstVal(), ValTy, Align, Offset,
1397 TF);
1398
1399 assert(cast<ConstantPoolSDNode>(T)->getTargetFlags() == TF &&
1400 "Inconsistent target flag encountered");
1401
Rafael Espindola405e25a2016-06-26 22:24:01 +00001402 if (IsPositionIndependent)
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001403 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), ValTy, T);
1404 return DAG.getNode(HexagonISD::CP, SDLoc(Op), ValTy, T);
1405}
1406
1407SDValue
1408HexagonTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
1409 EVT VT = Op.getValueType();
1410 int Idx = cast<JumpTableSDNode>(Op)->getIndex();
Rafael Espindola405e25a2016-06-26 22:24:01 +00001411 if (isPositionIndependent()) {
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001412 SDValue T = DAG.getTargetJumpTable(Idx, VT, HexagonII::MO_PCREL);
1413 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), VT, T);
1414 }
1415
1416 SDValue T = DAG.getTargetJumpTable(Idx, VT);
1417 return DAG.getNode(HexagonISD::JT, SDLoc(Op), VT, T);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001418}
1419
1420SDValue
1421HexagonTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001422 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001423 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00001424 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001425 MFI.setReturnAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001426
Bill Wendling908bf812014-01-06 00:43:20 +00001427 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001428 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001429
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001430 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001431 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001432 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1433 if (Depth) {
1434 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001435 SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001436 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
1437 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Justin Lebar9c375812016-07-15 18:27:10 +00001438 MachinePointerInfo());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001439 }
1440
1441 // Return LR, which contains the return address. Mark it an implicit live-in.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001442 unsigned Reg = MF.addLiveIn(HRI.getRARegister(), getRegClassFor(MVT::i32));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001443 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
1444}
1445
1446SDValue
1447HexagonTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001448 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Matthias Braun941a7052016-07-28 18:40:00 +00001449 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001450 MFI.setFrameAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001451
1452 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001453 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001454 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1455 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001456 HRI.getFrameRegister(), VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001457 while (Depth--)
1458 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00001459 MachinePointerInfo());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001460 return FrameAddr;
1461}
1462
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001463SDValue
1464HexagonTargetLowering::LowerATOMIC_FENCE(SDValue Op, SelectionDAG& DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001465 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001466 return DAG.getNode(HexagonISD::BARRIER, dl, MVT::Other, Op.getOperand(0));
1467}
1468
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001469SDValue
1470HexagonTargetLowering::LowerGLOBALADDRESS(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001471 SDLoc dl(Op);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001472 auto *GAN = cast<GlobalAddressSDNode>(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00001473 auto PtrVT = getPointerTy(DAG.getDataLayout());
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001474 auto *GV = GAN->getGlobal();
1475 int64_t Offset = GAN->getOffset();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001476
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001477 auto &HLOF = *HTM.getObjFileLowering();
1478 Reloc::Model RM = HTM.getRelocationModel();
1479
1480 if (RM == Reloc::Static) {
1481 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset);
Peter Collingbourne67335642016-10-24 19:23:39 +00001482 const GlobalObject *GO = GV->getBaseObject();
1483 if (GO && HLOF.isGlobalInSmallSection(GO, HTM))
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001484 return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, GA);
1485 return DAG.getNode(HexagonISD::CONST32, dl, PtrVT, GA);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001486 }
1487
Rafael Espindola3beef8d2016-06-27 23:15:57 +00001488 bool UsePCRel = getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001489 if (UsePCRel) {
1490 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset,
1491 HexagonII::MO_PCREL);
1492 return DAG.getNode(HexagonISD::AT_PCREL, dl, PtrVT, GA);
1493 }
1494
1495 // Use GOT index.
1496 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
1497 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, HexagonII::MO_GOT);
1498 SDValue Off = DAG.getConstant(Offset, dl, MVT::i32);
1499 return DAG.getNode(HexagonISD::AT_GOT, dl, PtrVT, GOT, GA, Off);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001500}
1501
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001502// Specifies that for loads and stores VT can be promoted to PromotedLdStVT.
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001503SDValue
1504HexagonTargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
1505 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001506 SDLoc dl(Op);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001507 EVT PtrVT = getPointerTy(DAG.getDataLayout());
1508
1509 Reloc::Model RM = HTM.getRelocationModel();
1510 if (RM == Reloc::Static) {
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001511 SDValue A = DAG.getTargetBlockAddress(BA, PtrVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001512 return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, A);
1513 }
1514
1515 SDValue A = DAG.getTargetBlockAddress(BA, PtrVT, 0, HexagonII::MO_PCREL);
1516 return DAG.getNode(HexagonISD::AT_PCREL, dl, PtrVT, A);
1517}
1518
1519SDValue
1520HexagonTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op, SelectionDAG &DAG)
1521 const {
1522 EVT PtrVT = getPointerTy(DAG.getDataLayout());
1523 SDValue GOTSym = DAG.getTargetExternalSymbol(HEXAGON_GOT_SYM_NAME, PtrVT,
1524 HexagonII::MO_PCREL);
1525 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), PtrVT, GOTSym);
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001526}
1527
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001528SDValue
1529HexagonTargetLowering::GetDynamicTLSAddr(SelectionDAG &DAG, SDValue Chain,
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001530 GlobalAddressSDNode *GA, SDValue Glue, EVT PtrVT, unsigned ReturnReg,
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001531 unsigned char OperandFlags) const {
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001532 MachineFunction &MF = DAG.getMachineFunction();
1533 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001534 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1535 SDLoc dl(GA);
1536 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
1537 GA->getValueType(0),
1538 GA->getOffset(),
1539 OperandFlags);
1540 // Create Operands for the call.The Operands should have the following:
1541 // 1. Chain SDValue
1542 // 2. Callee which in this case is the Global address value.
1543 // 3. Registers live into the call.In this case its R0, as we
1544 // have just one argument to be passed.
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001545 // 4. Glue.
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001546 // Note: The order is important.
1547
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001548 const auto &HRI = *Subtarget.getRegisterInfo();
1549 const uint32_t *Mask = HRI.getCallPreservedMask(MF, CallingConv::C);
1550 assert(Mask && "Missing call preserved mask for calling convention");
1551 SDValue Ops[] = { Chain, TGA, DAG.getRegister(Hexagon::R0, PtrVT),
1552 DAG.getRegisterMask(Mask), Glue };
1553 Chain = DAG.getNode(HexagonISD::CALL, dl, NodeTys, Ops);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001554
1555 // Inform MFI that function has calls.
Matthias Braun941a7052016-07-28 18:40:00 +00001556 MFI.setAdjustsStack(true);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001557
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001558 Glue = Chain.getValue(1);
1559 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Glue);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001560}
1561
1562//
1563// Lower using the intial executable model for TLS addresses
1564//
1565SDValue
1566HexagonTargetLowering::LowerToTLSInitialExecModel(GlobalAddressSDNode *GA,
1567 SelectionDAG &DAG) const {
1568 SDLoc dl(GA);
1569 int64_t Offset = GA->getOffset();
1570 auto PtrVT = getPointerTy(DAG.getDataLayout());
1571
1572 // Get the thread pointer.
1573 SDValue TP = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Hexagon::UGP, PtrVT);
1574
Rafael Espindola405e25a2016-06-26 22:24:01 +00001575 bool IsPositionIndependent = isPositionIndependent();
1576 unsigned char TF =
1577 IsPositionIndependent ? HexagonII::MO_IEGOT : HexagonII::MO_IE;
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001578
1579 // First generate the TLS symbol address
1580 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT,
1581 Offset, TF);
1582
1583 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1584
Rafael Espindola405e25a2016-06-26 22:24:01 +00001585 if (IsPositionIndependent) {
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001586 // Generate the GOT pointer in case of position independent code
1587 SDValue GOT = LowerGLOBAL_OFFSET_TABLE(Sym, DAG);
1588
1589 // Add the TLS Symbol address to GOT pointer.This gives
1590 // GOT relative relocation for the symbol.
1591 Sym = DAG.getNode(ISD::ADD, dl, PtrVT, GOT, Sym);
1592 }
1593
1594 // Load the offset value for TLS symbol.This offset is relative to
1595 // thread pointer.
Justin Lebar9c375812016-07-15 18:27:10 +00001596 SDValue LoadOffset =
1597 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Sym, MachinePointerInfo());
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001598
1599 // Address of the thread local variable is the add of thread
1600 // pointer and the offset of the variable.
1601 return DAG.getNode(ISD::ADD, dl, PtrVT, TP, LoadOffset);
1602}
1603
1604//
1605// Lower using the local executable model for TLS addresses
1606//
1607SDValue
1608HexagonTargetLowering::LowerToTLSLocalExecModel(GlobalAddressSDNode *GA,
1609 SelectionDAG &DAG) const {
1610 SDLoc dl(GA);
1611 int64_t Offset = GA->getOffset();
1612 auto PtrVT = getPointerTy(DAG.getDataLayout());
1613
1614 // Get the thread pointer.
1615 SDValue TP = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Hexagon::UGP, PtrVT);
1616 // Generate the TLS symbol address
1617 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT, Offset,
1618 HexagonII::MO_TPREL);
1619 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1620
1621 // Address of the thread local variable is the add of thread
1622 // pointer and the offset of the variable.
1623 return DAG.getNode(ISD::ADD, dl, PtrVT, TP, Sym);
1624}
1625
1626//
1627// Lower using the general dynamic model for TLS addresses
1628//
1629SDValue
1630HexagonTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
1631 SelectionDAG &DAG) const {
1632 SDLoc dl(GA);
1633 int64_t Offset = GA->getOffset();
1634 auto PtrVT = getPointerTy(DAG.getDataLayout());
1635
1636 // First generate the TLS symbol address
1637 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT, Offset,
1638 HexagonII::MO_GDGOT);
1639
1640 // Then, generate the GOT pointer
1641 SDValue GOT = LowerGLOBAL_OFFSET_TABLE(TGA, DAG);
1642
1643 // Add the TLS symbol and the GOT pointer
1644 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1645 SDValue Chain = DAG.getNode(ISD::ADD, dl, PtrVT, GOT, Sym);
1646
1647 // Copy over the argument to R0
1648 SDValue InFlag;
1649 Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, Hexagon::R0, Chain, InFlag);
1650 InFlag = Chain.getValue(1);
1651
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +00001652 unsigned Flags =
1653 static_cast<const HexagonSubtarget &>(DAG.getSubtarget()).useLongCalls()
1654 ? HexagonII::MO_GDPLT | HexagonII::HMOTF_ConstExtended
1655 : HexagonII::MO_GDPLT;
1656
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001657 return GetDynamicTLSAddr(DAG, Chain, GA, InFlag, PtrVT,
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +00001658 Hexagon::R0, Flags);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001659}
1660
1661//
1662// Lower TLS addresses.
1663//
1664// For now for dynamic models, we only support the general dynamic model.
1665//
1666SDValue
1667HexagonTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1668 SelectionDAG &DAG) const {
1669 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1670
1671 switch (HTM.getTLSModel(GA->getGlobal())) {
1672 case TLSModel::GeneralDynamic:
1673 case TLSModel::LocalDynamic:
1674 return LowerToTLSGeneralDynamicModel(GA, DAG);
1675 case TLSModel::InitialExec:
1676 return LowerToTLSInitialExecModel(GA, DAG);
1677 case TLSModel::LocalExec:
1678 return LowerToTLSLocalExecModel(GA, DAG);
1679 }
1680 llvm_unreachable("Bogus TLS model");
1681}
1682
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001683//===----------------------------------------------------------------------===//
1684// TargetLowering Implementation
1685//===----------------------------------------------------------------------===//
1686
Eric Christopherd737b762015-02-02 22:11:36 +00001687HexagonTargetLowering::HexagonTargetLowering(const TargetMachine &TM,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001688 const HexagonSubtarget &ST)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001689 : TargetLowering(TM), HTM(static_cast<const HexagonTargetMachine&>(TM)),
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001690 Subtarget(ST) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001691 bool IsV4 = !Subtarget.hasV5TOps();
1692 auto &HRI = *Subtarget.getRegisterInfo();
Sirish Pande69295b82012-05-10 20:20:25 +00001693
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001694 setPrefLoopAlignment(4);
1695 setPrefFunctionAlignment(4);
1696 setMinFunctionAlignment(2);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001697 setStackPointerRegisterToSaveRestore(HRI.getStackRegister());
1698
Krzysztof Parzyszekf228c952016-06-22 16:07:10 +00001699 setMaxAtomicSizeInBitsSupported(64);
1700 setMinCmpXchgSizeInBits(32);
1701
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001702 if (EnableHexSDNodeSched)
1703 setSchedulingPreference(Sched::VLIW);
1704 else
1705 setSchedulingPreference(Sched::Source);
1706
1707 // Limits for inline expansion of memcpy/memmove
1708 MaxStoresPerMemcpy = MaxStoresPerMemcpyCL;
1709 MaxStoresPerMemcpyOptSize = MaxStoresPerMemcpyOptSizeCL;
1710 MaxStoresPerMemmove = MaxStoresPerMemmoveCL;
1711 MaxStoresPerMemmoveOptSize = MaxStoresPerMemmoveOptSizeCL;
1712 MaxStoresPerMemset = MaxStoresPerMemsetCL;
1713 MaxStoresPerMemsetOptSize = MaxStoresPerMemsetOptSizeCL;
1714
1715 //
1716 // Set up register classes.
1717 //
1718
1719 addRegisterClass(MVT::i1, &Hexagon::PredRegsRegClass);
1720 addRegisterClass(MVT::v2i1, &Hexagon::PredRegsRegClass); // bbbbaaaa
1721 addRegisterClass(MVT::v4i1, &Hexagon::PredRegsRegClass); // ddccbbaa
1722 addRegisterClass(MVT::v8i1, &Hexagon::PredRegsRegClass); // hgfedcba
1723 addRegisterClass(MVT::i32, &Hexagon::IntRegsRegClass);
1724 addRegisterClass(MVT::v4i8, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001725 addRegisterClass(MVT::v2i16, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001726 addRegisterClass(MVT::i64, &Hexagon::DoubleRegsRegClass);
1727 addRegisterClass(MVT::v8i8, &Hexagon::DoubleRegsRegClass);
1728 addRegisterClass(MVT::v4i16, &Hexagon::DoubleRegsRegClass);
1729 addRegisterClass(MVT::v2i32, &Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001730
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001731 if (Subtarget.hasV5TOps()) {
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001732 addRegisterClass(MVT::f32, &Hexagon::IntRegsRegClass);
1733 addRegisterClass(MVT::f64, &Hexagon::DoubleRegsRegClass);
1734 }
Sirish Pande69295b82012-05-10 20:20:25 +00001735
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001736 if (Subtarget.hasV60TOps()) {
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +00001737 if (Subtarget.useHVX64BOps()) {
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001738 addRegisterClass(MVT::v64i8, &Hexagon::HvxVRRegClass);
1739 addRegisterClass(MVT::v32i16, &Hexagon::HvxVRRegClass);
1740 addRegisterClass(MVT::v16i32, &Hexagon::HvxVRRegClass);
1741 addRegisterClass(MVT::v8i64, &Hexagon::HvxVRRegClass);
1742 addRegisterClass(MVT::v128i8, &Hexagon::HvxWRRegClass);
1743 addRegisterClass(MVT::v64i16, &Hexagon::HvxWRRegClass);
1744 addRegisterClass(MVT::v32i32, &Hexagon::HvxWRRegClass);
1745 addRegisterClass(MVT::v16i64, &Hexagon::HvxWRRegClass);
1746 addRegisterClass(MVT::v512i1, &Hexagon::HvxQRRegClass);
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +00001747 } else if (Subtarget.useHVX128BOps()) {
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001748 addRegisterClass(MVT::v128i8, &Hexagon::HvxVRRegClass);
1749 addRegisterClass(MVT::v64i16, &Hexagon::HvxVRRegClass);
1750 addRegisterClass(MVT::v32i32, &Hexagon::HvxVRRegClass);
1751 addRegisterClass(MVT::v16i64, &Hexagon::HvxVRRegClass);
1752 addRegisterClass(MVT::v256i8, &Hexagon::HvxWRRegClass);
1753 addRegisterClass(MVT::v128i16, &Hexagon::HvxWRRegClass);
1754 addRegisterClass(MVT::v64i32, &Hexagon::HvxWRRegClass);
1755 addRegisterClass(MVT::v32i64, &Hexagon::HvxWRRegClass);
1756 addRegisterClass(MVT::v1024i1, &Hexagon::HvxQRRegClass);
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001757 }
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001758 }
1759
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001760 //
1761 // Handling of scalar operations.
1762 //
1763 // All operations default to "legal", except:
1764 // - indexed loads and stores (pre-/post-incremented),
1765 // - ANY_EXTEND_VECTOR_INREG, ATOMIC_CMP_SWAP_WITH_SUCCESS, CONCAT_VECTORS,
1766 // ConstantFP, DEBUGTRAP, FCEIL, FCOPYSIGN, FEXP, FEXP2, FFLOOR, FGETSIGN,
1767 // FLOG, FLOG2, FLOG10, FMAXNUM, FMINNUM, FNEARBYINT, FRINT, FROUND, TRAP,
1768 // FTRUNC, PREFETCH, SIGN_EXTEND_VECTOR_INREG, ZERO_EXTEND_VECTOR_INREG,
1769 // which default to "expand" for at least one type.
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001770
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001771 // Misc operations.
1772 setOperationAction(ISD::ConstantFP, MVT::f32, Legal); // Default: expand
1773 setOperationAction(ISD::ConstantFP, MVT::f64, Legal); // Default: expand
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001774
1775 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001776 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001777 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001778 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
1779 setOperationAction(ISD::INLINEASM, MVT::Other, Custom);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00001780 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +00001781 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00001782 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001783 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001784 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001785 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001786 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001787
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001788 // Custom legalize GlobalAddress nodes into CONST32.
1789 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001790 setOperationAction(ISD::GlobalAddress, MVT::i8, Custom);
1791 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001792
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001793 // Hexagon needs to optimize cases with negative constants.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001794 setOperationAction(ISD::SETCC, MVT::i8, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001795 setOperationAction(ISD::SETCC, MVT::i16, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001796
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001797 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
1798 setOperationAction(ISD::VASTART, MVT::Other, Custom);
1799 setOperationAction(ISD::VAEND, MVT::Other, Expand);
1800 setOperationAction(ISD::VAARG, MVT::Other, Expand);
1801
1802 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
1803 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
1804 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
1805
1806 if (EmitJumpTables)
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001807 setMinimumJumpTableEntries(MinimumJumpTables);
Krzysztof Parzyszeka61f7da2016-01-13 21:43:13 +00001808 else
Eugene Zelenko58655bb2016-12-17 01:09:05 +00001809 setMinimumJumpTableEntries(std::numeric_limits<int>::max());
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001810 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001811
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001812 // Hexagon has instructions for add/sub with carry. The problem with
1813 // modeling these instructions is that they produce 2 results: Rdd and Px.
1814 // To model the update of Px, we will have to use Defs[p0..p3] which will
1815 // cause any predicate live range to spill. So, we pretend we dont't have
1816 // these instructions.
1817 setOperationAction(ISD::ADDE, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001818 setOperationAction(ISD::ADDE, MVT::i16, Expand);
1819 setOperationAction(ISD::ADDE, MVT::i32, Expand);
1820 setOperationAction(ISD::ADDE, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001821 setOperationAction(ISD::SUBE, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001822 setOperationAction(ISD::SUBE, MVT::i16, Expand);
1823 setOperationAction(ISD::SUBE, MVT::i32, Expand);
1824 setOperationAction(ISD::SUBE, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001825 setOperationAction(ISD::ADDC, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001826 setOperationAction(ISD::ADDC, MVT::i16, Expand);
1827 setOperationAction(ISD::ADDC, MVT::i32, Expand);
1828 setOperationAction(ISD::ADDC, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001829 setOperationAction(ISD::SUBC, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001830 setOperationAction(ISD::SUBC, MVT::i16, Expand);
1831 setOperationAction(ISD::SUBC, MVT::i32, Expand);
1832 setOperationAction(ISD::SUBC, MVT::i64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001833
Krzysztof Parzyszek2c4487d2015-04-13 20:37:01 +00001834 // Only add and sub that detect overflow are the saturating ones.
1835 for (MVT VT : MVT::integer_valuetypes()) {
1836 setOperationAction(ISD::UADDO, VT, Expand);
1837 setOperationAction(ISD::SADDO, VT, Expand);
1838 setOperationAction(ISD::USUBO, VT, Expand);
1839 setOperationAction(ISD::SSUBO, VT, Expand);
1840 }
1841
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001842 setOperationAction(ISD::CTLZ, MVT::i8, Promote);
1843 setOperationAction(ISD::CTLZ, MVT::i16, Promote);
1844 setOperationAction(ISD::CTTZ, MVT::i8, Promote);
1845 setOperationAction(ISD::CTTZ, MVT::i16, Promote);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001846
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001847 // In V5, popcount can count # of 1s in i64 but returns i32.
1848 // On V4 it will be expanded (set later).
1849 setOperationAction(ISD::CTPOP, MVT::i8, Promote);
1850 setOperationAction(ISD::CTPOP, MVT::i16, Promote);
1851 setOperationAction(ISD::CTPOP, MVT::i32, Promote);
Krzysztof Parzyszekaf5ff652017-02-23 15:02:09 +00001852 setOperationAction(ISD::CTPOP, MVT::i64, Legal);
1853
1854 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
1855 setOperationAction(ISD::BITREVERSE, MVT::i64, Legal);
1856 setOperationAction(ISD::BSWAP, MVT::i32, Legal);
1857 setOperationAction(ISD::BSWAP, MVT::i64, Legal);
Krzysztof Parzyszekef580172017-05-30 17:47:51 +00001858 setOperationAction(ISD::MUL, MVT::i64, Legal);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001859
Benjamin Kramer62460692015-04-25 14:46:53 +00001860 for (unsigned IntExpOp :
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001861 { ISD::SDIV, ISD::UDIV, ISD::SREM, ISD::UREM,
1862 ISD::SDIVREM, ISD::UDIVREM, ISD::ROTL, ISD::ROTR,
Krzysztof Parzyszekaf5ff652017-02-23 15:02:09 +00001863 ISD::SHL_PARTS, ISD::SRA_PARTS, ISD::SRL_PARTS,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001864 ISD::SMUL_LOHI, ISD::UMUL_LOHI }) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001865 setOperationAction(IntExpOp, MVT::i32, Expand);
1866 setOperationAction(IntExpOp, MVT::i64, Expand);
1867 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001868
Benjamin Kramer62460692015-04-25 14:46:53 +00001869 for (unsigned FPExpOp :
1870 {ISD::FDIV, ISD::FREM, ISD::FSQRT, ISD::FSIN, ISD::FCOS, ISD::FSINCOS,
1871 ISD::FPOW, ISD::FCOPYSIGN}) {
1872 setOperationAction(FPExpOp, MVT::f32, Expand);
1873 setOperationAction(FPExpOp, MVT::f64, Expand);
1874 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001875
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001876 // No extending loads from i32.
1877 for (MVT VT : MVT::integer_valuetypes()) {
1878 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
1879 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
1880 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
1881 }
1882 // Turn FP truncstore into trunc + store.
1883 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Michael Kuperstein2bc3d4d2016-08-18 20:08:15 +00001884 // Turn FP extload into load/fpextend.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001885 for (MVT VT : MVT::fp_valuetypes())
1886 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001887
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001888 // Expand BR_CC and SELECT_CC for all integer and fp types.
1889 for (MVT VT : MVT::integer_valuetypes()) {
1890 setOperationAction(ISD::BR_CC, VT, Expand);
1891 setOperationAction(ISD::SELECT_CC, VT, Expand);
1892 }
1893 for (MVT VT : MVT::fp_valuetypes()) {
1894 setOperationAction(ISD::BR_CC, VT, Expand);
1895 setOperationAction(ISD::SELECT_CC, VT, Expand);
1896 }
1897 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001898
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001899 //
1900 // Handling of vector operations.
1901 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001902
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001903 promoteLdStType(MVT::v4i8, MVT::i32);
1904 promoteLdStType(MVT::v2i16, MVT::i32);
1905 promoteLdStType(MVT::v8i8, MVT::i64);
Krzysztof Parzyszek5eef92e2017-07-17 15:45:45 +00001906 promoteLdStType(MVT::v4i16, MVT::i64);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001907 promoteLdStType(MVT::v2i32, MVT::i64);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001908
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001909 // Set the action for vector operations to "expand", then override it with
1910 // either "custom" or "legal" for specific cases.
Craig Topper26260942015-10-18 05:15:34 +00001911 static const unsigned VectExpOps[] = {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001912 // Integer arithmetic:
1913 ISD::ADD, ISD::SUB, ISD::MUL, ISD::SDIV, ISD::UDIV,
1914 ISD::SREM, ISD::UREM, ISD::SDIVREM, ISD::UDIVREM, ISD::ADDC,
1915 ISD::SUBC, ISD::SADDO, ISD::UADDO, ISD::SSUBO, ISD::USUBO,
1916 ISD::SMUL_LOHI, ISD::UMUL_LOHI,
1917 // Logical/bit:
1918 ISD::AND, ISD::OR, ISD::XOR, ISD::ROTL, ISD::ROTR,
Craig Topper33772c52016-04-28 03:34:31 +00001919 ISD::CTPOP, ISD::CTLZ, ISD::CTTZ,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001920 // Floating point arithmetic/math functions:
1921 ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FMA, ISD::FDIV,
1922 ISD::FREM, ISD::FNEG, ISD::FABS, ISD::FSQRT, ISD::FSIN,
Craig Topperf6d4dc52017-05-30 15:27:55 +00001923 ISD::FCOS, ISD::FPOW, ISD::FLOG, ISD::FLOG2,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001924 ISD::FLOG10, ISD::FEXP, ISD::FEXP2, ISD::FCEIL, ISD::FTRUNC,
1925 ISD::FRINT, ISD::FNEARBYINT, ISD::FROUND, ISD::FFLOOR,
1926 ISD::FMINNUM, ISD::FMAXNUM, ISD::FSINCOS,
1927 // Misc:
Krzysztof Parzyszek046da742016-10-27 14:30:16 +00001928 ISD::BR_CC, ISD::SELECT_CC, ISD::ConstantPool,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001929 // Vector:
1930 ISD::BUILD_VECTOR, ISD::SCALAR_TO_VECTOR,
1931 ISD::EXTRACT_VECTOR_ELT, ISD::INSERT_VECTOR_ELT,
1932 ISD::EXTRACT_SUBVECTOR, ISD::INSERT_SUBVECTOR,
1933 ISD::CONCAT_VECTORS, ISD::VECTOR_SHUFFLE
1934 };
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001935
1936 for (MVT VT : MVT::vector_valuetypes()) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001937 for (unsigned VectExpOp : VectExpOps)
1938 setOperationAction(VectExpOp, VT, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001939
Krzysztof Parzyszeka696b1b2016-09-08 17:42:14 +00001940 // Expand all extending loads and truncating stores:
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001941 for (MVT TargetVT : MVT::vector_valuetypes()) {
Krzysztof Parzyszeka696b1b2016-09-08 17:42:14 +00001942 if (TargetVT == VT)
1943 continue;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001944 setLoadExtAction(ISD::EXTLOAD, TargetVT, VT, Expand);
Krzysztof Parzyszeka696b1b2016-09-08 17:42:14 +00001945 setLoadExtAction(ISD::ZEXTLOAD, TargetVT, VT, Expand);
1946 setLoadExtAction(ISD::SEXTLOAD, TargetVT, VT, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001947 setTruncStoreAction(VT, TargetVT, Expand);
1948 }
1949
Krzysztof Parzyszek046da742016-10-27 14:30:16 +00001950 // Normalize all inputs to SELECT to be vectors of i32.
1951 if (VT.getVectorElementType() != MVT::i32) {
1952 MVT VT32 = MVT::getVectorVT(MVT::i32, VT.getSizeInBits()/32);
1953 setOperationAction(ISD::SELECT, VT, Promote);
1954 AddPromotedToType(ISD::SELECT, VT, VT32);
1955 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001956 setOperationAction(ISD::SRA, VT, Custom);
1957 setOperationAction(ISD::SHL, VT, Custom);
1958 setOperationAction(ISD::SRL, VT, Custom);
1959 }
1960
Krzysztof Parzyszek64e5d7d2017-10-20 19:33:12 +00001961 // Extending loads from (native) vectors of i8 into (native) vectors of i16
1962 // are legal.
1963 setLoadExtAction(ISD::EXTLOAD, MVT::v2i16, MVT::v2i8, Legal);
1964 setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i16, MVT::v2i8, Legal);
1965 setLoadExtAction(ISD::SEXTLOAD, MVT::v2i16, MVT::v2i8, Legal);
1966 setLoadExtAction(ISD::EXTLOAD, MVT::v4i16, MVT::v4i8, Legal);
1967 setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i16, MVT::v4i8, Legal);
1968 setLoadExtAction(ISD::SEXTLOAD, MVT::v4i16, MVT::v4i8, Legal);
1969
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001970 // Types natively supported:
Benjamin Kramer62460692015-04-25 14:46:53 +00001971 for (MVT NativeVT : {MVT::v2i1, MVT::v4i1, MVT::v8i1, MVT::v32i1, MVT::v64i1,
1972 MVT::v4i8, MVT::v8i8, MVT::v2i16, MVT::v4i16, MVT::v1i32,
1973 MVT::v2i32, MVT::v1i64}) {
1974 setOperationAction(ISD::BUILD_VECTOR, NativeVT, Custom);
1975 setOperationAction(ISD::EXTRACT_VECTOR_ELT, NativeVT, Custom);
1976 setOperationAction(ISD::INSERT_VECTOR_ELT, NativeVT, Custom);
1977 setOperationAction(ISD::EXTRACT_SUBVECTOR, NativeVT, Custom);
1978 setOperationAction(ISD::INSERT_SUBVECTOR, NativeVT, Custom);
1979 setOperationAction(ISD::CONCAT_VECTORS, NativeVT, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001980
Benjamin Kramer62460692015-04-25 14:46:53 +00001981 setOperationAction(ISD::ADD, NativeVT, Legal);
1982 setOperationAction(ISD::SUB, NativeVT, Legal);
1983 setOperationAction(ISD::MUL, NativeVT, Legal);
1984 setOperationAction(ISD::AND, NativeVT, Legal);
1985 setOperationAction(ISD::OR, NativeVT, Legal);
1986 setOperationAction(ISD::XOR, NativeVT, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001987 }
1988
1989 setOperationAction(ISD::SETCC, MVT::v2i16, Custom);
1990 setOperationAction(ISD::VSELECT, MVT::v2i16, Custom);
1991 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
1992 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00001993
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001994 auto setPromoteTo = [this] (unsigned Opc, MVT FromTy, MVT ToTy) {
1995 setOperationAction(Opc, FromTy, Promote);
1996 AddPromotedToType(Opc, FromTy, ToTy);
1997 };
1998
Sumanth Gundapaneni9d954c42017-10-18 17:45:22 +00001999 if (Subtarget.useHVXOps()) {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002000 bool Use64b = Subtarget.useHVX64BOps();
2001 ArrayRef<MVT> LegalV = Use64b ? LegalV64 : LegalV128;
2002 ArrayRef<MVT> LegalW = Use64b ? LegalW64 : LegalW128;
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00002003 MVT ByteV = Use64b ? MVT::v64i8 : MVT::v128i8;
2004 MVT ByteW = Use64b ? MVT::v128i8 : MVT::v256i8;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002005
2006 setOperationAction(ISD::VECTOR_SHUFFLE, ByteV, Legal);
2007 setOperationAction(ISD::VECTOR_SHUFFLE, ByteW, Legal);
2008 setOperationAction(ISD::CONCAT_VECTORS, ByteW, Legal);
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00002009 setOperationAction(ISD::AND, ByteV, Legal);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002010 setOperationAction(ISD::OR, ByteV, Legal);
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00002011 setOperationAction(ISD::XOR, ByteV, Legal);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002012
2013 for (MVT T : LegalV) {
2014 setIndexedLoadAction(ISD::POST_INC, T, Legal);
2015 setIndexedStoreAction(ISD::POST_INC, T, Legal);
2016
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00002017 setOperationAction(ISD::ADD, T, Legal);
2018 setOperationAction(ISD::SUB, T, Legal);
2019 setOperationAction(ISD::MUL, T, Custom);
2020
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002021 setOperationAction(ISD::BUILD_VECTOR, T, Custom);
2022 setOperationAction(ISD::INSERT_SUBVECTOR, T, Custom);
2023 setOperationAction(ISD::INSERT_VECTOR_ELT, T, Custom);
2024 setOperationAction(ISD::EXTRACT_SUBVECTOR, T, Custom);
2025 setOperationAction(ISD::EXTRACT_VECTOR_ELT, T, Custom);
2026 }
2027
2028 for (MVT T : LegalV) {
2029 if (T == ByteV)
2030 continue;
2031 // Promote all shuffles and concats to operate on vectors of bytes.
2032 setPromoteTo(ISD::VECTOR_SHUFFLE, T, ByteV);
2033 setPromoteTo(ISD::CONCAT_VECTORS, T, ByteV);
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00002034 setPromoteTo(ISD::AND, T, ByteV);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002035 setPromoteTo(ISD::OR, T, ByteV);
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00002036 setPromoteTo(ISD::XOR, T, ByteV);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002037 }
2038
2039 for (MVT T : LegalW) {
2040 if (T == ByteW)
2041 continue;
2042 // Promote all shuffles and concats to operate on vectors of bytes.
2043 setPromoteTo(ISD::VECTOR_SHUFFLE, T, ByteW);
2044 setPromoteTo(ISD::CONCAT_VECTORS, T, ByteW);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002045 }
2046 }
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002047
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002048 // Subtarget-specific operation actions.
2049 //
2050 if (Subtarget.hasV5TOps()) {
2051 setOperationAction(ISD::FMA, MVT::f64, Expand);
2052 setOperationAction(ISD::FADD, MVT::f64, Expand);
2053 setOperationAction(ISD::FSUB, MVT::f64, Expand);
2054 setOperationAction(ISD::FMUL, MVT::f64, Expand);
2055
Krzysztof Parzyszekbd8ef4b2016-08-19 13:34:31 +00002056 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
2057 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
2058
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002059 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Promote);
2060 setOperationAction(ISD::FP_TO_UINT, MVT::i8, Promote);
2061 setOperationAction(ISD::FP_TO_UINT, MVT::i16, Promote);
2062 setOperationAction(ISD::FP_TO_SINT, MVT::i1, Promote);
2063 setOperationAction(ISD::FP_TO_SINT, MVT::i8, Promote);
2064 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
2065 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote);
2066 setOperationAction(ISD::UINT_TO_FP, MVT::i8, Promote);
2067 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote);
2068 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote);
2069 setOperationAction(ISD::SINT_TO_FP, MVT::i8, Promote);
2070 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002071 } else { // V4
2072 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
2073 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Expand);
2074 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
2075 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
2076 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Expand);
2077 setOperationAction(ISD::FP_TO_SINT, MVT::f32, Expand);
2078 setOperationAction(ISD::FP_EXTEND, MVT::f32, Expand);
2079 setOperationAction(ISD::FP_ROUND, MVT::f64, Expand);
2080 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
2081
2082 setOperationAction(ISD::CTPOP, MVT::i8, Expand);
2083 setOperationAction(ISD::CTPOP, MVT::i16, Expand);
2084 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
2085 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
2086
2087 // Expand these operations for both f32 and f64:
Benjamin Kramer62460692015-04-25 14:46:53 +00002088 for (unsigned FPExpOpV4 :
2089 {ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FABS, ISD::FNEG, ISD::FMA}) {
2090 setOperationAction(FPExpOpV4, MVT::f32, Expand);
2091 setOperationAction(FPExpOpV4, MVT::f64, Expand);
2092 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002093
Benjamin Kramer62460692015-04-25 14:46:53 +00002094 for (ISD::CondCode FPExpCCV4 :
2095 {ISD::SETOEQ, ISD::SETOGT, ISD::SETOLT, ISD::SETOGE, ISD::SETOLE,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002096 ISD::SETUO, ISD::SETO}) {
Benjamin Kramer62460692015-04-25 14:46:53 +00002097 setCondCodeAction(FPExpCCV4, MVT::f32, Expand);
2098 setCondCodeAction(FPExpCCV4, MVT::f64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002099 }
2100 }
2101
2102 // Handling of indexed loads/stores: default is "expand".
2103 //
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +00002104 for (MVT VT : {MVT::i8, MVT::i16, MVT::i32, MVT::i64}) {
2105 setIndexedLoadAction(ISD::POST_INC, VT, Legal);
2106 setIndexedStoreAction(ISD::POST_INC, VT, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002107 }
2108
2109 computeRegisterProperties(&HRI);
2110
2111 //
2112 // Library calls for unsupported operations
2113 //
2114 bool FastMath = EnableFastMath;
2115
Benjamin Kramera37c8092015-04-25 14:46:46 +00002116 setLibcallName(RTLIB::SDIV_I32, "__hexagon_divsi3");
2117 setLibcallName(RTLIB::SDIV_I64, "__hexagon_divdi3");
2118 setLibcallName(RTLIB::UDIV_I32, "__hexagon_udivsi3");
2119 setLibcallName(RTLIB::UDIV_I64, "__hexagon_udivdi3");
2120 setLibcallName(RTLIB::SREM_I32, "__hexagon_modsi3");
2121 setLibcallName(RTLIB::SREM_I64, "__hexagon_moddi3");
2122 setLibcallName(RTLIB::UREM_I32, "__hexagon_umodsi3");
2123 setLibcallName(RTLIB::UREM_I64, "__hexagon_umoddi3");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002124
Benjamin Kramera37c8092015-04-25 14:46:46 +00002125 setLibcallName(RTLIB::SINTTOFP_I128_F64, "__hexagon_floattidf");
2126 setLibcallName(RTLIB::SINTTOFP_I128_F32, "__hexagon_floattisf");
2127 setLibcallName(RTLIB::FPTOUINT_F32_I128, "__hexagon_fixunssfti");
2128 setLibcallName(RTLIB::FPTOUINT_F64_I128, "__hexagon_fixunsdfti");
2129 setLibcallName(RTLIB::FPTOSINT_F32_I128, "__hexagon_fixsfti");
2130 setLibcallName(RTLIB::FPTOSINT_F64_I128, "__hexagon_fixdfti");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002131
2132 if (IsV4) {
2133 // Handle single-precision floating point operations on V4.
Benjamin Kramera37c8092015-04-25 14:46:46 +00002134 if (FastMath) {
2135 setLibcallName(RTLIB::ADD_F32, "__hexagon_fast_addsf3");
2136 setLibcallName(RTLIB::SUB_F32, "__hexagon_fast_subsf3");
2137 setLibcallName(RTLIB::MUL_F32, "__hexagon_fast_mulsf3");
2138 setLibcallName(RTLIB::OGT_F32, "__hexagon_fast_gtsf2");
2139 setLibcallName(RTLIB::OLT_F32, "__hexagon_fast_ltsf2");
2140 // Double-precision compares.
2141 setLibcallName(RTLIB::OGT_F64, "__hexagon_fast_gtdf2");
2142 setLibcallName(RTLIB::OLT_F64, "__hexagon_fast_ltdf2");
2143 } else {
2144 setLibcallName(RTLIB::ADD_F32, "__hexagon_addsf3");
2145 setLibcallName(RTLIB::SUB_F32, "__hexagon_subsf3");
2146 setLibcallName(RTLIB::MUL_F32, "__hexagon_mulsf3");
2147 setLibcallName(RTLIB::OGT_F32, "__hexagon_gtsf2");
2148 setLibcallName(RTLIB::OLT_F32, "__hexagon_ltsf2");
2149 // Double-precision compares.
2150 setLibcallName(RTLIB::OGT_F64, "__hexagon_gtdf2");
2151 setLibcallName(RTLIB::OLT_F64, "__hexagon_ltdf2");
2152 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002153 }
2154
2155 // This is the only fast library function for sqrtd.
2156 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00002157 setLibcallName(RTLIB::SQRT_F64, "__hexagon_fast2_sqrtdf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002158
Benjamin Kramera37c8092015-04-25 14:46:46 +00002159 // Prefix is: nothing for "slow-math",
2160 // "fast2_" for V4 fast-math and V5+ fast-math double-precision
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002161 // (actually, keep fast-math and fast-math2 separate for now)
Benjamin Kramera37c8092015-04-25 14:46:46 +00002162 if (FastMath) {
2163 setLibcallName(RTLIB::ADD_F64, "__hexagon_fast_adddf3");
2164 setLibcallName(RTLIB::SUB_F64, "__hexagon_fast_subdf3");
2165 setLibcallName(RTLIB::MUL_F64, "__hexagon_fast_muldf3");
2166 setLibcallName(RTLIB::DIV_F64, "__hexagon_fast_divdf3");
2167 // Calling __hexagon_fast2_divsf3 with fast-math on V5 (ok).
2168 setLibcallName(RTLIB::DIV_F32, "__hexagon_fast_divsf3");
2169 } else {
2170 setLibcallName(RTLIB::ADD_F64, "__hexagon_adddf3");
2171 setLibcallName(RTLIB::SUB_F64, "__hexagon_subdf3");
2172 setLibcallName(RTLIB::MUL_F64, "__hexagon_muldf3");
2173 setLibcallName(RTLIB::DIV_F64, "__hexagon_divdf3");
2174 setLibcallName(RTLIB::DIV_F32, "__hexagon_divsf3");
2175 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002176
2177 if (Subtarget.hasV5TOps()) {
2178 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00002179 setLibcallName(RTLIB::SQRT_F32, "__hexagon_fast2_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002180 else
Benjamin Kramera37c8092015-04-25 14:46:46 +00002181 setLibcallName(RTLIB::SQRT_F32, "__hexagon_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002182 } else {
2183 // V4
Benjamin Kramera37c8092015-04-25 14:46:46 +00002184 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__hexagon_floatsisf");
2185 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__hexagon_floatsidf");
2186 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__hexagon_floatdisf");
2187 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__hexagon_floatdidf");
2188 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__hexagon_floatunsisf");
2189 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__hexagon_floatunsidf");
2190 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__hexagon_floatundisf");
2191 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__hexagon_floatundidf");
2192 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__hexagon_fixunssfsi");
2193 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__hexagon_fixunssfdi");
2194 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__hexagon_fixunsdfsi");
2195 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__hexagon_fixunsdfdi");
2196 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__hexagon_fixsfsi");
2197 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__hexagon_fixsfdi");
2198 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__hexagon_fixdfsi");
2199 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__hexagon_fixdfdi");
2200 setLibcallName(RTLIB::FPEXT_F32_F64, "__hexagon_extendsfdf2");
2201 setLibcallName(RTLIB::FPROUND_F64_F32, "__hexagon_truncdfsf2");
2202 setLibcallName(RTLIB::OEQ_F32, "__hexagon_eqsf2");
2203 setLibcallName(RTLIB::OEQ_F64, "__hexagon_eqdf2");
2204 setLibcallName(RTLIB::OGE_F32, "__hexagon_gesf2");
2205 setLibcallName(RTLIB::OGE_F64, "__hexagon_gedf2");
2206 setLibcallName(RTLIB::OLE_F32, "__hexagon_lesf2");
2207 setLibcallName(RTLIB::OLE_F64, "__hexagon_ledf2");
2208 setLibcallName(RTLIB::UNE_F32, "__hexagon_nesf2");
2209 setLibcallName(RTLIB::UNE_F64, "__hexagon_nedf2");
2210 setLibcallName(RTLIB::UO_F32, "__hexagon_unordsf2");
2211 setLibcallName(RTLIB::UO_F64, "__hexagon_unorddf2");
2212 setLibcallName(RTLIB::O_F32, "__hexagon_unordsf2");
2213 setLibcallName(RTLIB::O_F64, "__hexagon_unorddf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002214 }
2215
2216 // These cause problems when the shift amount is non-constant.
2217 setLibcallName(RTLIB::SHL_I128, nullptr);
2218 setLibcallName(RTLIB::SRL_I128, nullptr);
2219 setLibcallName(RTLIB::SRA_I128, nullptr);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002220}
2221
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002222const char* HexagonTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00002223 switch ((HexagonISD::NodeType)Opcode) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002224 case HexagonISD::ALLOCA: return "HexagonISD::ALLOCA";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002225 case HexagonISD::AT_GOT: return "HexagonISD::AT_GOT";
2226 case HexagonISD::AT_PCREL: return "HexagonISD::AT_PCREL";
2227 case HexagonISD::BARRIER: return "HexagonISD::BARRIER";
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002228 case HexagonISD::CALL: return "HexagonISD::CALL";
2229 case HexagonISD::CALLnr: return "HexagonISD::CALLnr";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002230 case HexagonISD::CALLR: return "HexagonISD::CALLR";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002231 case HexagonISD::COMBINE: return "HexagonISD::COMBINE";
2232 case HexagonISD::CONST32_GP: return "HexagonISD::CONST32_GP";
2233 case HexagonISD::CONST32: return "HexagonISD::CONST32";
2234 case HexagonISD::CP: return "HexagonISD::CP";
2235 case HexagonISD::DCFETCH: return "HexagonISD::DCFETCH";
2236 case HexagonISD::EH_RETURN: return "HexagonISD::EH_RETURN";
2237 case HexagonISD::EXTRACTU: return "HexagonISD::EXTRACTU";
2238 case HexagonISD::EXTRACTURP: return "HexagonISD::EXTRACTURP";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002239 case HexagonISD::INSERT: return "HexagonISD::INSERT";
2240 case HexagonISD::INSERTRP: return "HexagonISD::INSERTRP";
2241 case HexagonISD::JT: return "HexagonISD::JT";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002242 case HexagonISD::RET_FLAG: return "HexagonISD::RET_FLAG";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002243 case HexagonISD::TC_RETURN: return "HexagonISD::TC_RETURN";
Krzysztof Parzyszekc168c012015-12-03 16:47:20 +00002244 case HexagonISD::VCOMBINE: return "HexagonISD::VCOMBINE";
Krzysztof Parzyszek302a9d42017-07-14 19:02:32 +00002245 case HexagonISD::VPACKE: return "HexagonISD::VPACKE";
2246 case HexagonISD::VPACKO: return "HexagonISD::VPACKO";
Krzysztof Parzyszekf85dd9f2017-07-10 20:16:44 +00002247 case HexagonISD::VASL: return "HexagonISD::VASL";
2248 case HexagonISD::VASR: return "HexagonISD::VASR";
2249 case HexagonISD::VLSR: return "HexagonISD::VLSR";
2250 case HexagonISD::VSPLAT: return "HexagonISD::VSPLAT";
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002251 case HexagonISD::VEXTRACTW: return "HexagonISD::VEXTRACTW";
2252 case HexagonISD::VINSERTW0: return "HexagonISD::VINSERTW0";
2253 case HexagonISD::VROR: return "HexagonISD::VROR";
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +00002254 case HexagonISD::READCYCLE: return "HexagonISD::READCYCLE";
Matthias Braund04893f2015-05-07 21:33:59 +00002255 case HexagonISD::OP_END: break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002256 }
Matthias Braund04893f2015-05-07 21:33:59 +00002257 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002258}
2259
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002260bool HexagonTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002261 EVT MTy1 = EVT::getEVT(Ty1);
2262 EVT MTy2 = EVT::getEVT(Ty2);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002263 if (!MTy1.isSimple() || !MTy2.isSimple())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002264 return false;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002265 return (MTy1.getSimpleVT() == MVT::i64) && (MTy2.getSimpleVT() == MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002266}
2267
2268bool HexagonTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002269 if (!VT1.isSimple() || !VT2.isSimple())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002270 return false;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002271 return (VT1.getSimpleVT() == MVT::i64) && (VT2.getSimpleVT() == MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002272}
2273
Krzysztof Parzyszekbd8ef4b2016-08-19 13:34:31 +00002274bool HexagonTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
2275 return isOperationLegalOrCustom(ISD::FMA, VT);
2276}
2277
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002278// Should we expand the build vector with shuffles?
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00002279bool HexagonTargetLowering::shouldExpandBuildVectorWithShuffles(EVT VT,
2280 unsigned DefinedValues) const {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002281 return false;
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002282}
2283
Zvi Rackover1b736822017-07-26 08:06:58 +00002284bool HexagonTargetLowering::isShuffleMaskLegal(ArrayRef<int> Mask,
2285 EVT VT) const {
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00002286 return true;
2287}
2288
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002289// Lower a vector shuffle (V1, V2, V3). V1 and V2 are the two vectors
2290// to select data from, V3 is the permutation.
2291SDValue
2292HexagonTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG)
2293 const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002294 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
2295 SDValue V1 = Op.getOperand(0);
2296 SDValue V2 = Op.getOperand(1);
2297 SDLoc dl(Op);
2298 EVT VT = Op.getValueType();
2299
Sanjay Patel57195842016-03-14 17:28:46 +00002300 if (V2.isUndef())
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002301 V2 = V1;
2302
2303 if (SVN->isSplat()) {
2304 int Lane = SVN->getSplatIndex();
2305 if (Lane == -1) Lane = 0;
2306
2307 // Test if V1 is a SCALAR_TO_VECTOR.
2308 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR)
Krzysztof Parzyszek89b2d7c2017-07-13 18:17:58 +00002309 return DAG.getNode(HexagonISD::VSPLAT, dl, VT, V1.getOperand(0));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002310
2311 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
2312 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
2313 // reaches it).
2314 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
2315 !isa<ConstantSDNode>(V1.getOperand(0))) {
2316 bool IsScalarToVector = true;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00002317 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i) {
Sanjay Patel75068522016-03-14 18:09:43 +00002318 if (!V1.getOperand(i).isUndef()) {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002319 IsScalarToVector = false;
2320 break;
2321 }
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00002322 }
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002323 if (IsScalarToVector)
Krzysztof Parzyszek89b2d7c2017-07-13 18:17:58 +00002324 return DAG.getNode(HexagonISD::VSPLAT, dl, VT, V1.getOperand(0));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002325 }
Krzysztof Parzyszek89b2d7c2017-07-13 18:17:58 +00002326 return DAG.getNode(HexagonISD::VSPLAT, dl, VT,
2327 DAG.getConstant(Lane, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002328 }
2329
2330 // FIXME: We need to support more general vector shuffles. See
2331 // below the comment from the ARM backend that deals in the general
2332 // case with the vector shuffles. For now, let expand handle these.
2333 return SDValue();
2334
2335 // If the shuffle is not directly supported and it has 4 elements, use
2336 // the PerfectShuffle-generated table to synthesize it from other shuffles.
2337}
2338
2339// If BUILD_VECTOR has same base element repeated several times,
2340// report true.
2341static bool isCommonSplatElement(BuildVectorSDNode *BVN) {
2342 unsigned NElts = BVN->getNumOperands();
2343 SDValue V0 = BVN->getOperand(0);
2344
2345 for (unsigned i = 1, e = NElts; i != e; ++i) {
2346 if (BVN->getOperand(i) != V0)
2347 return false;
2348 }
2349 return true;
2350}
2351
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002352// Lower a vector shift. Try to convert
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002353// <VT> = SHL/SRA/SRL <VT> by <VT> to Hexagon specific
2354// <VT> = SHL/SRA/SRL <VT> by <IT/i32>.
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002355SDValue
2356HexagonTargetLowering::LowerVECTOR_SHIFT(SDValue Op, SelectionDAG &DAG) const {
Eugene Zelenko58655bb2016-12-17 01:09:05 +00002357 BuildVectorSDNode *BVN = nullptr;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002358 SDValue V1 = Op.getOperand(0);
2359 SDValue V2 = Op.getOperand(1);
2360 SDValue V3;
2361 SDLoc dl(Op);
2362 EVT VT = Op.getValueType();
2363
2364 if ((BVN = dyn_cast<BuildVectorSDNode>(V1.getNode())) &&
2365 isCommonSplatElement(BVN))
2366 V3 = V2;
2367 else if ((BVN = dyn_cast<BuildVectorSDNode>(V2.getNode())) &&
2368 isCommonSplatElement(BVN))
2369 V3 = V1;
2370 else
2371 return SDValue();
2372
2373 SDValue CommonSplat = BVN->getOperand(0);
2374 SDValue Result;
2375
2376 if (VT.getSimpleVT() == MVT::v4i16) {
2377 switch (Op.getOpcode()) {
2378 case ISD::SRA:
Krzysztof Parzyszekf85dd9f2017-07-10 20:16:44 +00002379 Result = DAG.getNode(HexagonISD::VASR, dl, VT, V3, CommonSplat);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002380 break;
2381 case ISD::SHL:
Krzysztof Parzyszekf85dd9f2017-07-10 20:16:44 +00002382 Result = DAG.getNode(HexagonISD::VASL, dl, VT, V3, CommonSplat);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002383 break;
2384 case ISD::SRL:
Krzysztof Parzyszekf85dd9f2017-07-10 20:16:44 +00002385 Result = DAG.getNode(HexagonISD::VLSR, dl, VT, V3, CommonSplat);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002386 break;
2387 default:
2388 return SDValue();
2389 }
2390 } else if (VT.getSimpleVT() == MVT::v2i32) {
2391 switch (Op.getOpcode()) {
2392 case ISD::SRA:
Krzysztof Parzyszekf85dd9f2017-07-10 20:16:44 +00002393 Result = DAG.getNode(HexagonISD::VASR, dl, VT, V3, CommonSplat);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002394 break;
2395 case ISD::SHL:
Krzysztof Parzyszekf85dd9f2017-07-10 20:16:44 +00002396 Result = DAG.getNode(HexagonISD::VASL, dl, VT, V3, CommonSplat);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002397 break;
2398 case ISD::SRL:
Krzysztof Parzyszekf85dd9f2017-07-10 20:16:44 +00002399 Result = DAG.getNode(HexagonISD::VLSR, dl, VT, V3, CommonSplat);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002400 break;
2401 default:
2402 return SDValue();
2403 }
2404 } else {
2405 return SDValue();
2406 }
2407
2408 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
2409}
2410
2411SDValue
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002412HexagonTargetLowering::buildVector32(ArrayRef<SDValue> Elem, const SDLoc &dl,
2413 MVT VecTy, SelectionDAG &DAG) const {
2414 MVT ElemTy = VecTy.getVectorElementType();
2415 assert(VecTy.getVectorNumElements() == Elem.size());
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002416
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002417 SmallVector<ConstantSDNode*,4> Consts;
2418 bool AllConst = true;
2419 for (SDValue V : Elem) {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002420 if (isUndef(V))
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002421 V = DAG.getConstant(0, dl, ElemTy);
2422 auto *C = dyn_cast<ConstantSDNode>(V.getNode());
2423 Consts.push_back(C);
2424 AllConst = AllConst && C != nullptr;
2425 }
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002426
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002427 unsigned First, Num = Elem.size();
2428 for (First = 0; First != Num; ++First)
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002429 if (!isUndef(Elem[First]))
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002430 break;
2431 if (First == Num)
2432 return DAG.getUNDEF(VecTy);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002433
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002434 if (ElemTy == MVT::i16) {
2435 assert(Elem.size() == 2);
2436 if (AllConst) {
2437 uint32_t V = (Consts[0]->getZExtValue() & 0xFFFF) |
2438 Consts[1]->getZExtValue() << 16;
2439 return DAG.getBitcast(MVT::v2i16, DAG.getConstant(V, dl, MVT::i32));
Krzysztof Parzyszek89b2d7c2017-07-13 18:17:58 +00002440 }
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002441 SDValue N = getNode(Hexagon::A2_combine_ll, dl, MVT::i32,
2442 {Elem[1], Elem[0]}, DAG);
2443 return DAG.getBitcast(MVT::v2i16, N);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002444 }
2445
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002446 // First try generating a constant.
2447 assert(ElemTy == MVT::i8 && Num == 4);
2448 if (AllConst) {
2449 int32_t V = (Consts[0]->getZExtValue() & 0xFF) |
2450 (Consts[1]->getZExtValue() & 0xFF) << 8 |
2451 (Consts[1]->getZExtValue() & 0xFF) << 16 |
2452 Consts[2]->getZExtValue() << 24;
2453 return DAG.getBitcast(MVT::v4i8, DAG.getConstant(V, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002454 }
2455
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002456 // Then try splat.
2457 bool IsSplat = true;
2458 for (unsigned i = 0; i != Num; ++i) {
2459 if (i == First)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002460 continue;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002461 if (Elem[i] == Elem[First] || isUndef(Elem[i]))
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002462 continue;
2463 IsSplat = false;
2464 break;
2465 }
2466 if (IsSplat)
2467 return DAG.getNode(HexagonISD::VSPLAT, dl, VecTy, Elem[First]);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002468
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002469 // Generate
2470 // (zxtb(Elem[0]) | (zxtb(Elem[1]) << 8)) |
2471 // (zxtb(Elem[2]) | (zxtb(Elem[3]) << 8)) << 16
2472 SDValue S8 = DAG.getConstant(8, dl, MVT::i32);
Krzysztof Parzyszek081e4582017-11-28 19:13:17 +00002473 SDValue V0 = DAG.getZeroExtendInReg(Elem[0], dl, MVT::i8);
2474 SDValue V1 = DAG.getZeroExtendInReg(Elem[1], dl, MVT::i8);
2475 SDValue V2 = DAG.getZeroExtendInReg(Elem[2], dl, MVT::i8);
2476 SDValue V3 = DAG.getZeroExtendInReg(Elem[3], dl, MVT::i8);
2477
2478 SDValue V4 = DAG.getNode(ISD::SHL, dl, MVT::i32, {V1, S8});
2479 SDValue V5 = DAG.getNode(ISD::SHL, dl, MVT::i32, {V3, S8});
2480 SDValue V6 = DAG.getNode(ISD::OR, dl, MVT::i32, {V0, V4});
2481 SDValue V7 = DAG.getNode(ISD::OR, dl, MVT::i32, {V2, V5});
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002482
2483 SDValue T0 = getNode(Hexagon::A2_combine_ll, dl, MVT::i32, {V7, V6}, DAG);
2484 return DAG.getBitcast(MVT::v4i8, T0);
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002485}
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002486
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002487SDValue
2488HexagonTargetLowering::buildVector64(ArrayRef<SDValue> Elem, const SDLoc &dl,
2489 MVT VecTy, SelectionDAG &DAG) const {
2490 MVT ElemTy = VecTy.getVectorElementType();
2491 assert(VecTy.getVectorNumElements() == Elem.size());
2492
2493 SmallVector<ConstantSDNode*,8> Consts;
2494 bool AllConst = true;
2495 for (SDValue V : Elem) {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002496 if (isUndef(V))
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002497 V = DAG.getConstant(0, dl, ElemTy);
2498 auto *C = dyn_cast<ConstantSDNode>(V.getNode());
2499 Consts.push_back(C);
2500 AllConst = AllConst && C != nullptr;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002501 }
2502
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002503 unsigned First, Num = Elem.size();
2504 for (First = 0; First != Num; ++First)
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002505 if (!isUndef(Elem[First]))
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002506 break;
2507 if (First == Num)
2508 return DAG.getUNDEF(VecTy);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002509
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002510 // First try splat if possible.
2511 if (ElemTy == MVT::i16) {
2512 bool IsSplat = true;
2513 for (unsigned i = 0; i != Num; ++i) {
2514 if (i == First)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002515 continue;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002516 if (Elem[i] == Elem[First] || isUndef(Elem[i]))
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002517 continue;
2518 IsSplat = false;
2519 break;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002520 }
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002521 if (IsSplat)
2522 return DAG.getNode(HexagonISD::VSPLAT, dl, VecTy, Elem[First]);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002523 }
2524
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002525 // Then try constant.
2526 if (AllConst) {
2527 uint64_t Val = 0;
2528 unsigned W = ElemTy.getSizeInBits();
2529 uint64_t Mask = (ElemTy == MVT::i8) ? 0xFFull
2530 : (ElemTy == MVT::i16) ? 0xFFFFull : 0xFFFFFFFFull;
2531 for (unsigned i = 0; i != Num; ++i)
2532 Val = (Val << W) | (Consts[i]->getZExtValue() & Mask);
2533 SDValue V0 = DAG.getConstant(Val, dl, MVT::i64);
2534 return DAG.getBitcast(VecTy, V0);
2535 }
2536
2537 // Build two 32-bit vectors and concatenate.
2538 MVT HalfTy = MVT::getVectorVT(ElemTy, Num/2);
2539 SDValue L = (ElemTy == MVT::i32)
2540 ? Elem[0]
2541 : buildVector32({Elem.data(), Num/2}, dl, HalfTy, DAG);
2542 SDValue H = (ElemTy == MVT::i32)
2543 ? Elem[1]
2544 : buildVector32({Elem.data()+Num/2, Num/2}, dl, HalfTy, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002545 return DAG.getNode(HexagonISD::COMBINE, dl, VecTy, {H, L});
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002546}
2547
2548SDValue
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002549HexagonTargetLowering::extractVector(SDValue VecV, SDValue IdxV,
2550 const SDLoc &dl, MVT ValTy, MVT ResTy,
2551 SelectionDAG &DAG) const {
2552 MVT VecTy = ty(VecV);
2553 assert(!ValTy.isVector() ||
2554 VecTy.getVectorElementType() == ValTy.getVectorElementType());
2555 unsigned VecWidth = VecTy.getSizeInBits();
2556 unsigned ValWidth = ValTy.getSizeInBits();
2557 unsigned ElemWidth = VecTy.getVectorElementType().getSizeInBits();
2558 assert(VecWidth == 32 || VecWidth == 64);
2559 assert((VecWidth % ElemWidth) == 0);
2560
2561 // Cast everything to scalar integer types.
2562 MVT ScalarTy = tyScalar(VecTy);
2563 VecV = DAG.getBitcast(ScalarTy, VecV);
2564
2565 SDValue WidthV = DAG.getConstant(ValWidth, dl, MVT::i32);
2566 SDValue ExtV;
2567
2568 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(IdxV)) {
2569 unsigned Off = C->getZExtValue() * ElemWidth;
2570 if (VecWidth == 64 && ValWidth == 32) {
2571 assert(Off == 0 || Off == 32);
2572 unsigned SubIdx = Off == 0 ? Hexagon::isub_lo : Hexagon::isub_hi;
2573 ExtV = DAG.getTargetExtractSubreg(SubIdx, dl, MVT::i32, VecV);
2574 } else if (Off == 0 && (ValWidth % 8) == 0) {
2575 ExtV = DAG.getZeroExtendInReg(VecV, dl, tyScalar(ValTy));
2576 } else {
2577 SDValue OffV = DAG.getConstant(Off, dl, MVT::i32);
2578 // The return type of EXTRACTU must be the same as the type of the
2579 // input vector.
2580 ExtV = DAG.getNode(HexagonISD::EXTRACTU, dl, ScalarTy,
2581 {VecV, WidthV, OffV});
2582 }
2583 } else {
2584 if (ty(IdxV) != MVT::i32)
2585 IdxV = DAG.getZExtOrTrunc(IdxV, dl, MVT::i32);
2586 SDValue OffV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
2587 DAG.getConstant(ElemWidth, dl, MVT::i32));
2588 // EXTRACTURP takes width/offset in a 64-bit pair.
2589 SDValue CombV = DAG.getNode(HexagonISD::COMBINE, dl, MVT::i64,
2590 {WidthV, OffV});
2591 ExtV = DAG.getNode(HexagonISD::EXTRACTURP, dl, ScalarTy,
2592 {VecV, CombV});
2593 }
2594
2595 // Cast ExtV to the requested result type.
2596 ExtV = DAG.getZExtOrTrunc(ExtV, dl, tyScalar(ResTy));
2597 ExtV = DAG.getBitcast(ResTy, ExtV);
2598 return ExtV;
2599}
2600
2601SDValue
2602HexagonTargetLowering::insertVector(SDValue VecV, SDValue ValV, SDValue IdxV,
2603 const SDLoc &dl, MVT ValTy,
2604 SelectionDAG &DAG) const {
2605 MVT VecTy = ty(VecV);
2606 unsigned VecWidth = VecTy.getSizeInBits();
2607 unsigned ValWidth = ValTy.getSizeInBits();
2608 assert(VecWidth == 32 || VecWidth == 64);
2609 assert((VecWidth % ValWidth) == 0);
2610
2611 // Cast everything to scalar integer types.
2612 MVT ScalarTy = MVT::getIntegerVT(VecWidth);
2613 // The actual type of ValV may be different than ValTy (which is related
2614 // to the vector type).
2615 unsigned VW = ty(ValV).getSizeInBits();
2616 ValV = DAG.getBitcast(MVT::getIntegerVT(VW), ValV);
2617 VecV = DAG.getBitcast(ScalarTy, VecV);
2618 if (VW != VecWidth)
2619 ValV = DAG.getAnyExtOrTrunc(ValV, dl, ScalarTy);
2620
2621 SDValue WidthV = DAG.getConstant(ValWidth, dl, MVT::i32);
2622 SDValue InsV;
2623
2624 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(IdxV)) {
2625 unsigned W = C->getZExtValue() * ValWidth;
2626 SDValue OffV = DAG.getConstant(W, dl, MVT::i32);
2627 InsV = DAG.getNode(HexagonISD::INSERT, dl, ScalarTy,
2628 {VecV, ValV, WidthV, OffV});
2629 } else {
2630 if (ty(IdxV) != MVT::i32)
2631 IdxV = DAG.getZExtOrTrunc(IdxV, dl, MVT::i32);
2632 SDValue OffV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV, WidthV);
2633 // INSERTRP takes width/offset in a 64-bit pair.
2634 SDValue CombV = DAG.getNode(HexagonISD::COMBINE, dl, MVT::i64,
2635 {WidthV, OffV});
2636 InsV = DAG.getNode(HexagonISD::INSERTRP, dl, ScalarTy,
2637 {VecV, ValV, CombV});
2638 }
2639
2640 return DAG.getNode(ISD::BITCAST, dl, VecTy, InsV);
2641}
2642
2643SDValue
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002644HexagonTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002645 MVT VecTy = ty(Op);
2646 unsigned BW = VecTy.getSizeInBits();
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002647 if (BW == 32 || BW == 64) {
2648 SmallVector<SDValue,8> Ops;
2649 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i)
2650 Ops.push_back(Op.getOperand(i));
2651 if (BW == 32)
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002652 return buildVector32(Ops, SDLoc(Op), VecTy, DAG);
2653 return buildVector64(Ops, SDLoc(Op), VecTy, DAG);
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002654 }
2655
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002656 if (Subtarget.useHVXOps() && Subtarget.isHVXVectorType(VecTy))
2657 return LowerHvxBuildVector(Op, DAG);
2658
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002659 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002660}
2661
2662SDValue
2663HexagonTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
2664 SelectionDAG &DAG) const {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002665 MVT VecTy = ty(Op);
2666 assert(!Subtarget.useHVXOps() || !Subtarget.isHVXVectorType(VecTy));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002667
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002668 if (VecTy.getSizeInBits() == 64) {
2669 assert(Op.getNumOperands() == 2);
2670 return DAG.getNode(HexagonISD::COMBINE, SDLoc(Op), VecTy, Op.getOperand(1),
2671 Op.getOperand(0));
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002672 }
2673
2674 return SDValue();
2675}
2676
2677SDValue
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002678HexagonTargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
2679 SelectionDAG &DAG) const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002680 SDValue Vec = Op.getOperand(0);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002681 MVT VecTy = ty(Vec);
2682 if (Subtarget.useHVXOps() && Subtarget.isHVXVectorType(VecTy))
2683 return LowerHvxExtractElement(Op, DAG);
2684
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002685 MVT ElemTy = ty(Vec).getVectorElementType();
2686 return extractVector(Vec, Op.getOperand(1), SDLoc(Op), ElemTy, ty(Op), DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002687}
2688
2689SDValue
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002690HexagonTargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op,
2691 SelectionDAG &DAG) const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002692 SDValue Vec = Op.getOperand(0);
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002693 MVT VecTy = ty(Vec);
2694 if (Subtarget.useHVXOps() && Subtarget.isHVXVectorType(VecTy))
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002695 return LowerHvxExtractSubvector(Op, DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002696
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002697 return extractVector(Vec, Op.getOperand(1), SDLoc(Op), ty(Op), ty(Op), DAG);
2698}
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002699
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002700SDValue
2701HexagonTargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op,
2702 SelectionDAG &DAG) const {
2703 MVT VecTy = ty(Op);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002704 if (Subtarget.useHVXOps() && Subtarget.isHVXVectorType(VecTy))
2705 return LowerHvxInsertElement(Op, DAG);
2706
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002707 return insertVector(Op.getOperand(0), Op.getOperand(1), Op.getOperand(2),
2708 SDLoc(Op), VecTy.getVectorElementType(), DAG);
2709}
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002710
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002711SDValue
2712HexagonTargetLowering::LowerINSERT_SUBVECTOR(SDValue Op,
2713 SelectionDAG &DAG) const {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002714 if (Subtarget.useHVXOps() && Subtarget.isHVXVectorType(ty(Op)))
2715 return LowerHvxInsertSubvector(Op, DAG);
2716
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002717 SDValue ValV = Op.getOperand(1);
2718 return insertVector(Op.getOperand(0), ValV, Op.getOperand(2),
2719 SDLoc(Op), ty(ValV), DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002720}
2721
Tim Northovera4415852013-08-06 09:12:35 +00002722bool
2723HexagonTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
2724 // Assuming the caller does not have either a signext or zeroext modifier, and
2725 // only one value is accepted, any reasonable truncation is allowed.
2726 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
2727 return false;
2728
2729 // FIXME: in principle up to 64-bit could be made safe, but it would be very
2730 // fragile at the moment: any support for multiple value returns would be
2731 // liable to disallow tail calls involving i64 -> iN truncation in many cases.
2732 return Ty1->getPrimitiveSizeInBits() <= 32;
2733}
2734
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002735SDValue
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002736HexagonTargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
2737 SDValue Chain = Op.getOperand(0);
2738 SDValue Offset = Op.getOperand(1);
2739 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002740 SDLoc dl(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002741 auto PtrVT = getPointerTy(DAG.getDataLayout());
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002742
2743 // Mark function as containing a call to EH_RETURN.
2744 HexagonMachineFunctionInfo *FuncInfo =
2745 DAG.getMachineFunction().getInfo<HexagonMachineFunctionInfo>();
2746 FuncInfo->setHasEHReturn();
2747
2748 unsigned OffsetReg = Hexagon::R28;
2749
Mehdi Amini44ede332015-07-09 02:09:04 +00002750 SDValue StoreAddr =
2751 DAG.getNode(ISD::ADD, dl, PtrVT, DAG.getRegister(Hexagon::R30, PtrVT),
2752 DAG.getIntPtrConstant(4, dl));
Justin Lebar9c375812016-07-15 18:27:10 +00002753 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo());
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002754 Chain = DAG.getCopyToReg(Chain, dl, OffsetReg, Offset);
2755
2756 // Not needed we already use it as explict input to EH_RETURN.
2757 // MF.getRegInfo().addLiveOut(OffsetReg);
2758
2759 return DAG.getNode(HexagonISD::EH_RETURN, dl, MVT::Other, Chain);
2760}
2761
2762SDValue
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002763HexagonTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002764 unsigned Opc = Op.getOpcode();
2765 switch (Opc) {
2766 default:
2767#ifndef NDEBUG
2768 Op.getNode()->dumpr(&DAG);
2769 if (Opc > HexagonISD::OP_BEGIN && Opc < HexagonISD::OP_END)
2770 errs() << "Check for a non-legal type in this operation\n";
2771#endif
2772 llvm_unreachable("Should not custom lower this!");
2773 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002774 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
2775 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
2776 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
2777 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002778 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2779 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002780 case ISD::SRA:
2781 case ISD::SHL:
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002782 case ISD::SRL: return LowerVECTOR_SHIFT(Op, DAG);
2783 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002784 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002785 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
2786 // Frame & Return address. Currently unimplemented.
2787 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
2788 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00002789 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002790 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
2791 case ISD::GlobalAddress: return LowerGLOBALADDRESS(Op, DAG);
2792 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002793 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002794 case ISD::VASTART: return LowerVASTART(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002795 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
2796 case ISD::SETCC: return LowerSETCC(Op, DAG);
2797 case ISD::VSELECT: return LowerVSELECT(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002798 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00002799 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002800 case ISD::INLINEASM: return LowerINLINEASM(Op, DAG);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00002801 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG);
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +00002802 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Krzysztof Parzyszek039d4d92017-12-07 17:37:28 +00002803 case ISD::MUL:
2804 if (Subtarget.useHVXOps())
2805 return LowerHvxMul(Op, DAG);
2806 break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002807 }
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002808 return SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002809}
2810
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002811/// Returns relocation base for the given PIC jumptable.
2812SDValue
2813HexagonTargetLowering::getPICJumpTableRelocBase(SDValue Table,
2814 SelectionDAG &DAG) const {
2815 int Idx = cast<JumpTableSDNode>(Table)->getIndex();
2816 EVT VT = Table.getValueType();
2817 SDValue T = DAG.getTargetJumpTable(Idx, VT, HexagonII::MO_PCREL);
2818 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Table), VT, T);
2819}
2820
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002821//===----------------------------------------------------------------------===//
2822// Inline Assembly Support
2823//===----------------------------------------------------------------------===//
2824
Krzysztof Parzyszekca3b5322016-05-18 14:34:51 +00002825TargetLowering::ConstraintType
2826HexagonTargetLowering::getConstraintType(StringRef Constraint) const {
2827 if (Constraint.size() == 1) {
2828 switch (Constraint[0]) {
2829 case 'q':
2830 case 'v':
2831 if (Subtarget.useHVXOps())
Krzysztof Parzyszek3ad0d012017-07-21 17:51:27 +00002832 return C_RegisterClass;
2833 break;
2834 case 'a':
2835 return C_RegisterClass;
2836 default:
Krzysztof Parzyszekca3b5322016-05-18 14:34:51 +00002837 break;
2838 }
2839 }
2840 return TargetLowering::getConstraintType(Constraint);
2841}
2842
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002843std::pair<unsigned, const TargetRegisterClass*>
Eric Christopher11e4df72015-02-26 22:38:43 +00002844HexagonTargetLowering::getRegForInlineAsmConstraint(
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00002845 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002846
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002847 if (Constraint.size() == 1) {
2848 switch (Constraint[0]) {
2849 case 'r': // R0-R31
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002850 switch (VT.SimpleTy) {
2851 default:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002852 return {0u, nullptr};
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002853 case MVT::i1:
2854 case MVT::i8:
2855 case MVT::i16:
2856 case MVT::i32:
2857 case MVT::f32:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002858 return {0u, &Hexagon::IntRegsRegClass};
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002859 case MVT::i64:
2860 case MVT::f64:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002861 return {0u, &Hexagon::DoubleRegsRegClass};
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002862 }
Krzysztof Parzyszek3ad0d012017-07-21 17:51:27 +00002863 break;
2864 case 'a': // M0-M1
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002865 if (VT != MVT::i32)
2866 return {0u, nullptr};
2867 return {0u, &Hexagon::ModRegsRegClass};
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002868 case 'q': // q0-q3
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002869 switch (VT.getSizeInBits()) {
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002870 default:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002871 return {0u, nullptr};
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002872 case 512:
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002873 case 1024:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002874 return {0u, &Hexagon::HvxQRRegClass};
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002875 }
Krzysztof Parzyszek3ad0d012017-07-21 17:51:27 +00002876 break;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002877 case 'v': // V0-V31
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002878 switch (VT.getSizeInBits()) {
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002879 default:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002880 return {0u, nullptr};
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002881 case 512:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002882 return {0u, &Hexagon::HvxVRRegClass};
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002883 case 1024:
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +00002884 if (Subtarget.hasV60TOps() && Subtarget.useHVX128BOps())
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002885 return {0u, &Hexagon::HvxVRRegClass};
2886 return {0u, &Hexagon::HvxWRRegClass};
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002887 case 2048:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002888 return {0u, &Hexagon::HvxWRRegClass};
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002889 }
Krzysztof Parzyszek3ad0d012017-07-21 17:51:27 +00002890 break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002891 default:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002892 return {0u, nullptr};
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002893 }
2894 }
2895
Eric Christopher11e4df72015-02-26 22:38:43 +00002896 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002897}
2898
Sirish Pande69295b82012-05-10 20:20:25 +00002899/// isFPImmLegal - Returns true if the target can instruction select the
2900/// specified FP immediate natively. If false, the legalizer will
2901/// materialize the FP immediate as a load from a constant pool.
2902bool HexagonTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002903 return Subtarget.hasV5TOps();
Sirish Pande69295b82012-05-10 20:20:25 +00002904}
2905
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002906/// isLegalAddressingMode - Return true if the addressing mode represented by
2907/// AM is legal for this target, for a load/store of the specified type.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +00002908bool HexagonTargetLowering::isLegalAddressingMode(const DataLayout &DL,
2909 const AddrMode &AM, Type *Ty,
Jonas Paulsson024e3192017-07-21 11:59:37 +00002910 unsigned AS, Instruction *I) const {
Krzysztof Parzyszeked4e7822016-08-03 15:06:18 +00002911 if (Ty->isSized()) {
2912 // When LSR detects uses of the same base address to access different
2913 // types (e.g. unions), it will assume a conservative type for these
2914 // uses:
2915 // LSR Use: Kind=Address of void in addrspace(4294967295), ...
2916 // The type Ty passed here would then be "void". Skip the alignment
2917 // checks, but do not return false right away, since that confuses
2918 // LSR into crashing.
2919 unsigned A = DL.getABITypeAlignment(Ty);
2920 // The base offset must be a multiple of the alignment.
2921 if ((AM.BaseOffs % A) != 0)
2922 return false;
2923 // The shifted offset must fit in 11 bits.
2924 if (!isInt<11>(AM.BaseOffs >> Log2_32(A)))
2925 return false;
2926 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002927
2928 // No global is ever allowed as a base.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002929 if (AM.BaseGV)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002930 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002931
2932 int Scale = AM.Scale;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00002933 if (Scale < 0)
2934 Scale = -Scale;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002935 switch (Scale) {
2936 case 0: // No scale reg, "r+i", "r", or just "i".
2937 break;
2938 default: // No scaled addressing mode.
2939 return false;
2940 }
2941 return true;
2942}
2943
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002944/// Return true if folding a constant offset with the given GlobalAddress is
2945/// legal. It is frequently not legal in PIC relocation models.
2946bool HexagonTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA)
2947 const {
2948 return HTM.getRelocationModel() == Reloc::Static;
2949}
2950
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002951/// isLegalICmpImmediate - Return true if the specified immediate is legal
2952/// icmp immediate, that is the target has icmp instructions which can compare
2953/// a register against the immediate without having to materialize the
2954/// immediate into a register.
2955bool HexagonTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
2956 return Imm >= -512 && Imm <= 511;
2957}
2958
2959/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2960/// for tail call optimization. Targets which want to do tail call
2961/// optimization should implement this function.
2962bool HexagonTargetLowering::IsEligibleForTailCallOptimization(
2963 SDValue Callee,
2964 CallingConv::ID CalleeCC,
2965 bool isVarArg,
2966 bool isCalleeStructRet,
2967 bool isCallerStructRet,
2968 const SmallVectorImpl<ISD::OutputArg> &Outs,
2969 const SmallVectorImpl<SDValue> &OutVals,
2970 const SmallVectorImpl<ISD::InputArg> &Ins,
2971 SelectionDAG& DAG) const {
2972 const Function *CallerF = DAG.getMachineFunction().getFunction();
2973 CallingConv::ID CallerCC = CallerF->getCallingConv();
2974 bool CCMatch = CallerCC == CalleeCC;
2975
2976 // ***************************************************************************
2977 // Look for obvious safe cases to perform tail call optimization that do not
2978 // require ABI changes.
2979 // ***************************************************************************
2980
2981 // If this is a tail call via a function pointer, then don't do it!
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00002982 if (!isa<GlobalAddressSDNode>(Callee) &&
2983 !isa<ExternalSymbolSDNode>(Callee)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002984 return false;
2985 }
2986
Krzysztof Parzyszek0ba97542016-08-19 15:02:18 +00002987 // Do not optimize if the calling conventions do not match and the conventions
2988 // used are not C or Fast.
2989 if (!CCMatch) {
2990 bool R = (CallerCC == CallingConv::C || CallerCC == CallingConv::Fast);
2991 bool E = (CalleeCC == CallingConv::C || CalleeCC == CallingConv::Fast);
2992 // If R & E, then ok.
2993 if (!R || !E)
2994 return false;
2995 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002996
2997 // Do not tail call optimize vararg calls.
2998 if (isVarArg)
2999 return false;
3000
3001 // Also avoid tail call optimization if either caller or callee uses struct
3002 // return semantics.
3003 if (isCalleeStructRet || isCallerStructRet)
3004 return false;
3005
3006 // In addition to the cases above, we also disable Tail Call Optimization if
3007 // the calling convention code that at least one outgoing argument needs to
3008 // go on the stack. We cannot check that here because at this point that
3009 // information is not available.
3010 return true;
3011}
Colin LeMahieu025f8602014-12-08 21:19:18 +00003012
Krzysztof Parzyszek3e409e12016-08-02 18:34:31 +00003013/// Returns the target specific optimal type for load and store operations as
3014/// a result of memset, memcpy, and memmove lowering.
3015///
3016/// If DstAlign is zero that means it's safe to destination alignment can
3017/// satisfy any constraint. Similarly if SrcAlign is zero it means there isn't
3018/// a need to check it against alignment requirement, probably because the
3019/// source does not need to be loaded. If 'IsMemset' is true, that means it's
3020/// expanding a memset. If 'ZeroMemset' is true, that means it's a memset of
3021/// zero. 'MemcpyStrSrc' indicates whether the memcpy source is constant so it
3022/// does not need to be loaded. It returns EVT::Other if the type should be
3023/// determined using generic target-independent logic.
3024EVT HexagonTargetLowering::getOptimalMemOpType(uint64_t Size,
3025 unsigned DstAlign, unsigned SrcAlign, bool IsMemset, bool ZeroMemset,
3026 bool MemcpyStrSrc, MachineFunction &MF) const {
3027
3028 auto Aligned = [](unsigned GivenA, unsigned MinA) -> bool {
3029 return (GivenA % MinA) == 0;
3030 };
3031
3032 if (Size >= 8 && Aligned(DstAlign, 8) && (IsMemset || Aligned(SrcAlign, 8)))
3033 return MVT::i64;
3034 if (Size >= 4 && Aligned(DstAlign, 4) && (IsMemset || Aligned(SrcAlign, 4)))
3035 return MVT::i32;
3036 if (Size >= 2 && Aligned(DstAlign, 2) && (IsMemset || Aligned(SrcAlign, 2)))
3037 return MVT::i16;
3038
3039 return MVT::Other;
3040}
3041
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +00003042bool HexagonTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
3043 unsigned AS, unsigned Align, bool *Fast) const {
3044 if (Fast)
3045 *Fast = false;
3046
3047 switch (VT.getSimpleVT().SimpleTy) {
3048 default:
3049 return false;
3050 case MVT::v64i8:
3051 case MVT::v128i8:
3052 case MVT::v256i8:
3053 case MVT::v32i16:
3054 case MVT::v64i16:
3055 case MVT::v128i16:
3056 case MVT::v16i32:
3057 case MVT::v32i32:
3058 case MVT::v64i32:
3059 case MVT::v8i64:
3060 case MVT::v16i64:
3061 case MVT::v32i64:
3062 return true;
3063 }
3064 return false;
3065}
3066
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003067std::pair<const TargetRegisterClass*, uint8_t>
3068HexagonTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
3069 MVT VT) const {
3070 const TargetRegisterClass *RRC = nullptr;
3071
3072 uint8_t Cost = 1;
3073 switch (VT.SimpleTy) {
3074 default:
3075 return TargetLowering::findRepresentativeClass(TRI, VT);
3076 case MVT::v64i8:
3077 case MVT::v32i16:
3078 case MVT::v16i32:
3079 case MVT::v8i64:
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00003080 RRC = &Hexagon::HvxVRRegClass;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003081 break;
3082 case MVT::v128i8:
3083 case MVT::v64i16:
3084 case MVT::v32i32:
3085 case MVT::v16i64:
3086 if (Subtarget.hasV60TOps() && Subtarget.useHVXOps() &&
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +00003087 Subtarget.useHVX128BOps())
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00003088 RRC = &Hexagon::HvxVRRegClass;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003089 else
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00003090 RRC = &Hexagon::HvxWRRegClass;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003091 break;
3092 case MVT::v256i8:
3093 case MVT::v128i16:
3094 case MVT::v64i32:
3095 case MVT::v32i64:
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00003096 RRC = &Hexagon::HvxWRRegClass;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003097 break;
3098 }
3099 return std::make_pair(RRC, Cost);
3100}
3101
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003102Value *HexagonTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
3103 AtomicOrdering Ord) const {
3104 BasicBlock *BB = Builder.GetInsertBlock();
3105 Module *M = BB->getParent()->getParent();
3106 Type *Ty = cast<PointerType>(Addr->getType())->getElementType();
3107 unsigned SZ = Ty->getPrimitiveSizeInBits();
3108 assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic loads supported");
3109 Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_L2_loadw_locked
3110 : Intrinsic::hexagon_L4_loadd_locked;
3111 Value *Fn = Intrinsic::getDeclaration(M, IntID);
3112 return Builder.CreateCall(Fn, Addr, "larx");
3113}
3114
3115/// Perform a store-conditional operation to Addr. Return the status of the
3116/// store. This should be 0 if the store succeeded, non-zero otherwise.
3117Value *HexagonTargetLowering::emitStoreConditional(IRBuilder<> &Builder,
3118 Value *Val, Value *Addr, AtomicOrdering Ord) const {
3119 BasicBlock *BB = Builder.GetInsertBlock();
3120 Module *M = BB->getParent()->getParent();
3121 Type *Ty = Val->getType();
3122 unsigned SZ = Ty->getPrimitiveSizeInBits();
3123 assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic stores supported");
3124 Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_S2_storew_locked
3125 : Intrinsic::hexagon_S4_stored_locked;
3126 Value *Fn = Intrinsic::getDeclaration(M, IntID);
3127 Value *Call = Builder.CreateCall(Fn, {Addr, Val}, "stcx");
3128 Value *Cmp = Builder.CreateICmpEQ(Call, Builder.getInt32(0), "");
3129 Value *Ext = Builder.CreateZExt(Cmp, Type::getInt32Ty(M->getContext()));
3130 return Ext;
3131}
3132
Ahmed Bougacha52468672015-09-11 17:08:28 +00003133TargetLowering::AtomicExpansionKind
3134HexagonTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003135 // Do not expand loads and stores that don't exceed 64 bits.
Ahmed Bougacha52468672015-09-11 17:08:28 +00003136 return LI->getType()->getPrimitiveSizeInBits() > 64
Tim Northoverf520eff2015-12-02 18:12:57 +00003137 ? AtomicExpansionKind::LLOnly
Ahmed Bougacha52468672015-09-11 17:08:28 +00003138 : AtomicExpansionKind::None;
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003139}
3140
3141bool HexagonTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
3142 // Do not expand loads and stores that don't exceed 64 bits.
3143 return SI->getValueOperand()->getType()->getPrimitiveSizeInBits() > 64;
3144}
Krzysztof Parzyszekf228c952016-06-22 16:07:10 +00003145
3146bool HexagonTargetLowering::shouldExpandAtomicCmpXchgInIR(
3147 AtomicCmpXchgInst *AI) const {
3148 const DataLayout &DL = AI->getModule()->getDataLayout();
3149 unsigned Size = DL.getTypeStoreSize(AI->getCompareOperand()->getType());
3150 return Size >= 4 && Size <= 8;
3151}