blob: 861af94f1e387f6fe111de136160fa4b8d531fb1 [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
Eugene Zelenko58655bb2016-12-17 01:09:05 +000015#include "Hexagon.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000016#include "HexagonISelLowering.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"
29#include "llvm/CodeGen/RuntimeLibcalls.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000031#include "llvm/CodeGen/SelectionDAG.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000032#include "llvm/CodeGen/ValueTypes.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000033#include "llvm/IR/BasicBlock.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000034#include "llvm/IR/CallingConv.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000035#include "llvm/IR/DataLayout.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000036#include "llvm/IR/DerivedTypes.h"
37#include "llvm/IR/Function.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000038#include "llvm/IR/GlobalValue.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000039#include "llvm/IR/InlineAsm.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000040#include "llvm/IR/Instructions.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000041#include "llvm/IR/Intrinsics.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000042#include "llvm/IR/Module.h"
43#include "llvm/IR/Type.h"
44#include "llvm/IR/Value.h"
45#include "llvm/MC/MCRegisterInfo.h"
46#include "llvm/Support/Casting.h"
47#include "llvm/Support/CodeGen.h"
NAKAMURA Takumi54eed762012-04-21 15:31:36 +000048#include "llvm/Support/CommandLine.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000049#include "llvm/Support/Debug.h"
50#include "llvm/Support/ErrorHandling.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000051#include "llvm/Support/MathExtras.h"
NAKAMURA Takumie30303f2012-04-21 15:31:45 +000052#include "llvm/Support/raw_ostream.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000053#include "llvm/Target/TargetCallingConv.h"
54#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 Parzyszek317d42c2016-08-01 20:31:50 +0000132static bool isHvxVectorType(MVT ty);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000133
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000134static bool
135CC_Hexagon(unsigned ValNo, MVT ValVT,
136 MVT LocVT, CCValAssign::LocInfo LocInfo,
137 ISD::ArgFlagsTy ArgFlags, CCState &State);
138
139static bool
140CC_Hexagon32(unsigned ValNo, MVT ValVT,
141 MVT LocVT, CCValAssign::LocInfo LocInfo,
142 ISD::ArgFlagsTy ArgFlags, CCState &State);
143
144static bool
145CC_Hexagon64(unsigned ValNo, MVT ValVT,
146 MVT LocVT, CCValAssign::LocInfo LocInfo,
147 ISD::ArgFlagsTy ArgFlags, CCState &State);
148
149static bool
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000150CC_HexagonVector(unsigned ValNo, MVT ValVT,
151 MVT LocVT, CCValAssign::LocInfo LocInfo,
152 ISD::ArgFlagsTy ArgFlags, CCState &State);
153
154static bool
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000155RetCC_Hexagon(unsigned ValNo, MVT ValVT,
156 MVT LocVT, CCValAssign::LocInfo LocInfo,
157 ISD::ArgFlagsTy ArgFlags, CCState &State);
158
159static bool
160RetCC_Hexagon32(unsigned ValNo, MVT ValVT,
161 MVT LocVT, CCValAssign::LocInfo LocInfo,
162 ISD::ArgFlagsTy ArgFlags, CCState &State);
163
164static bool
165RetCC_Hexagon64(unsigned ValNo, MVT ValVT,
166 MVT LocVT, CCValAssign::LocInfo LocInfo,
167 ISD::ArgFlagsTy ArgFlags, CCState &State);
168
169static bool
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000170RetCC_HexagonVector(unsigned ValNo, MVT ValVT,
171 MVT LocVT, CCValAssign::LocInfo LocInfo,
172 ISD::ArgFlagsTy ArgFlags, CCState &State);
173
174static bool
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000175CC_Hexagon_VarArg (unsigned ValNo, MVT ValVT,
176 MVT LocVT, CCValAssign::LocInfo LocInfo,
177 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000178 HexagonCCState &HState = static_cast<HexagonCCState &>(State);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000179
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000180 if (ValNo < HState.getNumNamedVarArgParams()) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000181 // Deal with named arguments.
182 return CC_Hexagon(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State);
183 }
184
185 // Deal with un-named arguments.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000186 unsigned Offset;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000187 if (ArgFlags.isByVal()) {
188 // If pass-by-value, the size allocated on stack is decided
189 // by ArgFlags.getByValSize(), not by the size of LocVT.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000190 Offset = State.AllocateStack(ArgFlags.getByValSize(),
191 ArgFlags.getByValAlign());
192 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000193 return false;
194 }
Jyotsna Vermac7dcc2f2013-03-07 20:28:34 +0000195 if (LocVT == MVT::i1 || LocVT == MVT::i8 || LocVT == MVT::i16) {
196 LocVT = MVT::i32;
197 ValVT = MVT::i32;
198 if (ArgFlags.isSExt())
199 LocInfo = CCValAssign::SExt;
200 else if (ArgFlags.isZExt())
201 LocInfo = CCValAssign::ZExt;
202 else
203 LocInfo = CCValAssign::AExt;
204 }
Sirish Pande69295b82012-05-10 20:20:25 +0000205 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000206 Offset = State.AllocateStack(4, 4);
207 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000208 return false;
209 }
Sirish Pande69295b82012-05-10 20:20:25 +0000210 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000211 Offset = State.AllocateStack(8, 8);
212 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000213 return false;
214 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000215 if (LocVT == MVT::v2i64 || LocVT == MVT::v4i32 || LocVT == MVT::v8i16 ||
216 LocVT == MVT::v16i8) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000217 Offset = State.AllocateStack(16, 16);
218 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000219 return false;
220 }
221 if (LocVT == MVT::v4i64 || LocVT == MVT::v8i32 || LocVT == MVT::v16i16 ||
222 LocVT == MVT::v32i8) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000223 Offset = State.AllocateStack(32, 32);
224 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000225 return false;
226 }
227 if (LocVT == MVT::v8i64 || LocVT == MVT::v16i32 || LocVT == MVT::v32i16 ||
228 LocVT == MVT::v64i8 || LocVT == MVT::v512i1) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000229 Offset = State.AllocateStack(64, 64);
230 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000231 return false;
232 }
233 if (LocVT == MVT::v16i64 || LocVT == MVT::v32i32 || LocVT == MVT::v64i16 ||
234 LocVT == MVT::v128i8 || LocVT == MVT::v1024i1) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000235 Offset = State.AllocateStack(128, 128);
236 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000237 return false;
238 }
239 if (LocVT == MVT::v32i64 || LocVT == MVT::v64i32 || LocVT == MVT::v128i16 ||
240 LocVT == MVT::v256i8) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000241 Offset = State.AllocateStack(256, 256);
242 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000243 return false;
244 }
245
Craig Toppere73658d2014-04-28 04:05:08 +0000246 llvm_unreachable(nullptr);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000247}
248
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000249static bool CC_Hexagon (unsigned ValNo, MVT ValVT, MVT LocVT,
250 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, CCState &State) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000251 if (ArgFlags.isByVal()) {
252 // Passed on stack.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000253 unsigned Offset = State.AllocateStack(ArgFlags.getByValSize(),
254 ArgFlags.getByValAlign());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000255 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
256 return false;
257 }
258
Krzysztof Parzyszek8f23dd62017-03-01 17:30:10 +0000259 if (LocVT == MVT::i1) {
260 LocVT = MVT::i32;
261 } else if (LocVT == MVT::i8 || LocVT == MVT::i16) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000262 LocVT = MVT::i32;
263 ValVT = MVT::i32;
264 if (ArgFlags.isSExt())
265 LocInfo = CCValAssign::SExt;
266 else if (ArgFlags.isZExt())
267 LocInfo = CCValAssign::ZExt;
268 else
269 LocInfo = CCValAssign::AExt;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000270 } else if (LocVT == MVT::v4i8 || LocVT == MVT::v2i16) {
271 LocVT = MVT::i32;
272 LocInfo = CCValAssign::BCvt;
273 } else if (LocVT == MVT::v8i8 || LocVT == MVT::v4i16 || LocVT == MVT::v2i32) {
274 LocVT = MVT::i64;
275 LocInfo = CCValAssign::BCvt;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000276 }
277
Sirish Pande69295b82012-05-10 20:20:25 +0000278 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000279 if (!CC_Hexagon32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
280 return false;
281 }
282
Sirish Pande69295b82012-05-10 20:20:25 +0000283 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000284 if (!CC_Hexagon64(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
285 return false;
286 }
287
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000288 if (LocVT == MVT::v8i32 || LocVT == MVT::v16i16 || LocVT == MVT::v32i8) {
289 unsigned Offset = State.AllocateStack(ArgFlags.getByValSize(), 32);
290 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
291 return false;
292 }
293
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000294 if (isHvxVectorType(LocVT)) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000295 if (!CC_HexagonVector(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
296 return false;
297 }
298
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000299 return true; // CC didn't match.
300}
301
302
303static bool CC_Hexagon32(unsigned ValNo, MVT ValVT,
304 MVT LocVT, CCValAssign::LocInfo LocInfo,
305 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +0000306 static const MCPhysReg RegList[] = {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000307 Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3, Hexagon::R4,
308 Hexagon::R5
309 };
Tim Northover3b6b7ca2015-02-21 02:11:17 +0000310 if (unsigned Reg = State.AllocateReg(RegList)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000311 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
312 return false;
313 }
314
315 unsigned Offset = State.AllocateStack(4, 4);
316 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
317 return false;
318}
319
320static bool CC_Hexagon64(unsigned ValNo, MVT ValVT,
321 MVT LocVT, CCValAssign::LocInfo LocInfo,
322 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000323 if (unsigned Reg = State.AllocateReg(Hexagon::D0)) {
324 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
325 return false;
326 }
327
Craig Topper840beec2014-04-04 05:16:06 +0000328 static const MCPhysReg RegList1[] = {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000329 Hexagon::D1, Hexagon::D2
330 };
Craig Topper840beec2014-04-04 05:16:06 +0000331 static const MCPhysReg RegList2[] = {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000332 Hexagon::R1, Hexagon::R3
333 };
Tim Northover3b6b7ca2015-02-21 02:11:17 +0000334 if (unsigned Reg = State.AllocateReg(RegList1, RegList2)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000335 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
336 return false;
337 }
338
339 unsigned Offset = State.AllocateStack(8, 8, Hexagon::D2);
340 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
341 return false;
342}
343
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000344static bool CC_HexagonVector(unsigned ValNo, MVT ValVT,
345 MVT LocVT, CCValAssign::LocInfo LocInfo,
346 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000347 static const MCPhysReg VecLstS[] = {
348 Hexagon::V0, Hexagon::V1, Hexagon::V2, Hexagon::V3, Hexagon::V4,
349 Hexagon::V5, Hexagon::V6, Hexagon::V7, Hexagon::V8, Hexagon::V9,
350 Hexagon::V10, Hexagon::V11, Hexagon::V12, Hexagon::V13, Hexagon::V14,
351 Hexagon::V15
352 };
353 static const MCPhysReg VecLstD[] = {
354 Hexagon::W0, Hexagon::W1, Hexagon::W2, Hexagon::W3, Hexagon::W4,
355 Hexagon::W5, Hexagon::W6, Hexagon::W7
356 };
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000357 auto &MF = State.getMachineFunction();
358 auto &HST = MF.getSubtarget<HexagonSubtarget>();
359 bool UseHVX = HST.useHVXOps();
360 bool UseHVXDbl = HST.useHVXDblOps();
361
362 if ((UseHVX && !UseHVXDbl) &&
363 (LocVT == MVT::v8i64 || LocVT == MVT::v16i32 || LocVT == MVT::v32i16 ||
364 LocVT == MVT::v64i8 || LocVT == MVT::v512i1)) {
365 if (unsigned Reg = State.AllocateReg(VecLstS)) {
366 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
367 return false;
368 }
369 unsigned Offset = State.AllocateStack(64, 64);
370 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
371 return false;
372 }
373 if ((UseHVX && !UseHVXDbl) &&
374 (LocVT == MVT::v16i64 || LocVT == MVT::v32i32 || LocVT == MVT::v64i16 ||
375 LocVT == MVT::v128i8)) {
376 if (unsigned Reg = State.AllocateReg(VecLstD)) {
377 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
378 return false;
379 }
380 unsigned Offset = State.AllocateStack(128, 128);
381 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
382 return false;
383 }
384 // 128B Mode
385 if ((UseHVX && UseHVXDbl) &&
386 (LocVT == MVT::v32i64 || LocVT == MVT::v64i32 || LocVT == MVT::v128i16 ||
387 LocVT == MVT::v256i8)) {
388 if (unsigned Reg = State.AllocateReg(VecLstD)) {
389 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
390 return false;
391 }
392 unsigned Offset = State.AllocateStack(256, 256);
393 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
394 return false;
395 }
396 if ((UseHVX && UseHVXDbl) &&
397 (LocVT == MVT::v16i64 || LocVT == MVT::v32i32 || LocVT == MVT::v64i16 ||
398 LocVT == MVT::v128i8 || LocVT == MVT::v1024i1)) {
399 if (unsigned Reg = State.AllocateReg(VecLstS)) {
400 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
401 return false;
402 }
403 unsigned Offset = State.AllocateStack(128, 128);
404 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
405 return false;
406 }
407 return true;
408}
409
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000410static bool RetCC_Hexagon(unsigned ValNo, MVT ValVT,
411 MVT LocVT, CCValAssign::LocInfo LocInfo,
412 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000413 auto &MF = State.getMachineFunction();
414 auto &HST = MF.getSubtarget<HexagonSubtarget>();
415 bool UseHVX = HST.useHVXOps();
416 bool UseHVXDbl = HST.useHVXDblOps();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000417
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000418 if (LocVT == MVT::i1) {
419 // Return values of type MVT::i1 still need to be assigned to R0, but
420 // the value type needs to remain i1. LowerCallResult will deal with it,
421 // but it needs to recognize i1 as the value type.
422 LocVT = MVT::i32;
423 } else if (LocVT == MVT::i8 || LocVT == MVT::i16) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000424 LocVT = MVT::i32;
425 ValVT = MVT::i32;
426 if (ArgFlags.isSExt())
427 LocInfo = CCValAssign::SExt;
428 else if (ArgFlags.isZExt())
429 LocInfo = CCValAssign::ZExt;
430 else
431 LocInfo = CCValAssign::AExt;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000432 } else if (LocVT == MVT::v4i8 || LocVT == MVT::v2i16) {
433 LocVT = MVT::i32;
434 LocInfo = CCValAssign::BCvt;
435 } else if (LocVT == MVT::v8i8 || LocVT == MVT::v4i16 || LocVT == MVT::v2i32) {
436 LocVT = MVT::i64;
437 LocInfo = CCValAssign::BCvt;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000438 } else if (LocVT == MVT::v64i8 || LocVT == MVT::v32i16 ||
439 LocVT == MVT::v16i32 || LocVT == MVT::v8i64 ||
440 LocVT == MVT::v512i1) {
441 LocVT = MVT::v16i32;
442 ValVT = MVT::v16i32;
443 LocInfo = CCValAssign::Full;
444 } else if (LocVT == MVT::v128i8 || LocVT == MVT::v64i16 ||
445 LocVT == MVT::v32i32 || LocVT == MVT::v16i64 ||
446 (LocVT == MVT::v1024i1 && UseHVX && UseHVXDbl)) {
447 LocVT = MVT::v32i32;
448 ValVT = MVT::v32i32;
449 LocInfo = CCValAssign::Full;
450 } else if (LocVT == MVT::v256i8 || LocVT == MVT::v128i16 ||
451 LocVT == MVT::v64i32 || LocVT == MVT::v32i64) {
452 LocVT = MVT::v64i32;
453 ValVT = MVT::v64i32;
454 LocInfo = CCValAssign::Full;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000455 }
Sirish Pande69295b82012-05-10 20:20:25 +0000456 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000457 if (!RetCC_Hexagon32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000458 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000459 }
460
Sirish Pande69295b82012-05-10 20:20:25 +0000461 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000462 if (!RetCC_Hexagon64(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000463 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000464 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000465 if (LocVT == MVT::v16i32 || LocVT == MVT::v32i32 || LocVT == MVT::v64i32) {
466 if (!RetCC_HexagonVector(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000467 return false;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000468 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000469 return true; // CC didn't match.
470}
471
472static bool RetCC_Hexagon32(unsigned ValNo, MVT ValVT,
473 MVT LocVT, CCValAssign::LocInfo LocInfo,
474 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Sirish Pande69295b82012-05-10 20:20:25 +0000475 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Krzysztof Parzyszek14412ef2016-07-18 17:36:46 +0000476 // Note that use of registers beyond R1 is not ABI compliant. However there
477 // are (experimental) IR passes which generate internal functions that
478 // return structs using these additional registers.
479 static const uint16_t RegList[] = { Hexagon::R0, Hexagon::R1,
480 Hexagon::R2, Hexagon::R3,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000481 Hexagon::R4, Hexagon::R5 };
Krzysztof Parzyszek14412ef2016-07-18 17:36:46 +0000482 if (unsigned Reg = State.AllocateReg(RegList)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000483 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
484 return false;
485 }
486 }
487
Krzysztof Parzyszek56199522017-04-13 15:05:51 +0000488 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000489}
490
491static bool RetCC_Hexagon64(unsigned ValNo, MVT ValVT,
492 MVT LocVT, CCValAssign::LocInfo LocInfo,
493 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Sirish Pande69295b82012-05-10 20:20:25 +0000494 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000495 if (unsigned Reg = State.AllocateReg(Hexagon::D0)) {
496 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
497 return false;
498 }
499 }
500
Krzysztof Parzyszek56199522017-04-13 15:05:51 +0000501 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000502}
503
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000504static bool RetCC_HexagonVector(unsigned ValNo, MVT ValVT,
505 MVT LocVT, CCValAssign::LocInfo LocInfo,
506 ISD::ArgFlagsTy ArgFlags, CCState &State) {
507 auto &MF = State.getMachineFunction();
508 auto &HST = MF.getSubtarget<HexagonSubtarget>();
509 bool UseHVX = HST.useHVXOps();
510 bool UseHVXDbl = HST.useHVXDblOps();
511
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000512 if (LocVT == MVT::v16i32) {
513 if (unsigned Reg = State.AllocateReg(Hexagon::V0)) {
514 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
515 return false;
516 }
517 } else if (LocVT == MVT::v32i32) {
518 unsigned Req = (UseHVX && UseHVXDbl) ? Hexagon::V0 : Hexagon::W0;
519 if (unsigned Reg = State.AllocateReg(Req)) {
520 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
521 return false;
522 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000523 } else if (LocVT == MVT::v64i32) {
524 if (unsigned Reg = State.AllocateReg(Hexagon::W0)) {
525 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
526 return false;
527 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000528 }
529
Krzysztof Parzyszek56199522017-04-13 15:05:51 +0000530 return true;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000531}
532
Craig Topper18e69f42016-04-15 06:20:21 +0000533void HexagonTargetLowering::promoteLdStType(MVT VT, MVT PromotedLdStVT) {
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000534 if (VT != PromotedLdStVT) {
Craig Topper18e69f42016-04-15 06:20:21 +0000535 setOperationAction(ISD::LOAD, VT, Promote);
536 AddPromotedToType(ISD::LOAD, VT, PromotedLdStVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000537
Craig Topper18e69f42016-04-15 06:20:21 +0000538 setOperationAction(ISD::STORE, VT, Promote);
539 AddPromotedToType(ISD::STORE, VT, PromotedLdStVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000540 }
541}
542
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000543SDValue
544HexagonTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG)
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000545 const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000546 return SDValue();
547}
548
549/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
550/// by "Src" to address "Dst" of size "Size". Alignment information is
551/// specified by the specific parameter attribute. The copy will be passed as
552/// a byval function parameter. Sometimes what we are copying is the end of a
553/// larger object, the part that does not fit in registers.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000554static SDValue CreateCopyOfByValArgument(SDValue Src, SDValue Dst,
555 SDValue Chain, ISD::ArgFlagsTy Flags,
556 SelectionDAG &DAG, const SDLoc &dl) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000557 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000558 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
559 /*isVolatile=*/false, /*AlwaysInline=*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +0000560 /*isTailCall=*/false,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000561 MachinePointerInfo(), MachinePointerInfo());
562}
563
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000564static bool isHvxVectorType(MVT Ty) {
565 switch (Ty.SimpleTy) {
566 case MVT::v8i64:
567 case MVT::v16i32:
568 case MVT::v32i16:
569 case MVT::v64i8:
570 case MVT::v16i64:
571 case MVT::v32i32:
572 case MVT::v64i16:
573 case MVT::v128i8:
574 case MVT::v32i64:
575 case MVT::v64i32:
576 case MVT::v128i16:
577 case MVT::v256i8:
578 case MVT::v512i1:
579 case MVT::v1024i1:
580 return true;
581 default:
582 return false;
583 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000584}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000585
Krzysztof Parzyszek56199522017-04-13 15:05:51 +0000586bool
587HexagonTargetLowering::CanLowerReturn(
588 CallingConv::ID CallConv, MachineFunction &MF, bool isVarArg,
589 const SmallVectorImpl<ISD::OutputArg> &Outs,
590 LLVMContext &Context) const {
591 SmallVector<CCValAssign, 16> RVLocs;
592 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
593 return CCInfo.CheckReturn(Outs, RetCC_Hexagon);
594}
595
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000596// LowerReturn - Lower ISD::RET. If a struct is larger than 8 bytes and is
597// passed by value, the function prototype is modified to return void and
598// the value is stored in memory pointed by a pointer passed by caller.
599SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000600HexagonTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
601 bool isVarArg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000602 const SmallVectorImpl<ISD::OutputArg> &Outs,
603 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000604 const SDLoc &dl, SelectionDAG &DAG) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000605 // CCValAssign - represent the assignment of the return value to locations.
606 SmallVector<CCValAssign, 16> RVLocs;
607
608 // CCState - Info about the registers and stack slot.
Eric Christopherb5217502014-08-06 18:45:26 +0000609 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
610 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000611
612 // Analyze return values of ISD::RET
613 CCInfo.AnalyzeReturn(Outs, RetCC_Hexagon);
614
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000615 SDValue Flag;
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000616 SmallVector<SDValue, 4> RetOps(1, Chain);
617
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000618 // Copy the result values into the output registers.
619 for (unsigned i = 0; i != RVLocs.size(); ++i) {
620 CCValAssign &VA = RVLocs[i];
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000621
622 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), OutVals[i], Flag);
623
624 // Guarantee that all emitted copies are stuck together with flags.
625 Flag = Chain.getValue(1);
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000626 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000627 }
628
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000629 RetOps[0] = Chain; // Update chain.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000630
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000631 // Add the flag if we have it.
632 if (Flag.getNode())
633 RetOps.push_back(Flag);
634
Craig Topper48d114b2014-04-26 18:35:24 +0000635 return DAG.getNode(HexagonISD::RET_FLAG, dl, MVT::Other, RetOps);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000636}
637
Matt Arsenault31380752017-04-18 21:16:46 +0000638bool HexagonTargetLowering::mayBeEmittedAsTailCall(const CallInst *CI) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000639 // If either no tail call or told not to tail call at all, don't.
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000640 auto Attr =
641 CI->getParent()->getParent()->getFnAttribute("disable-tail-calls");
642 if (!CI->isTailCall() || Attr.getValueAsString() == "true")
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000643 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000644
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000645 return true;
646}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000647
648/// LowerCallResult - Lower the result values of an ISD::CALL into the
649/// appropriate copies out of appropriate physical registers. This assumes that
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000650/// Chain/Glue are the input chain/glue to use, and that TheCall is the call
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000651/// being lowered. Returns a SDNode with the same number of values as the
652/// ISD::CALL.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000653SDValue HexagonTargetLowering::LowerCallResult(
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000654 SDValue Chain, SDValue Glue, CallingConv::ID CallConv, bool isVarArg,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000655 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
656 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals,
657 const SmallVectorImpl<SDValue> &OutVals, SDValue Callee) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000658 // Assign locations to each value returned by this call.
659 SmallVector<CCValAssign, 16> RVLocs;
660
Eric Christopherb5217502014-08-06 18:45:26 +0000661 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
662 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000663
664 CCInfo.AnalyzeCallResult(Ins, RetCC_Hexagon);
665
666 // Copy all of the result registers out of their specified physreg.
667 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000668 SDValue RetVal;
669 if (RVLocs[i].getValVT() == MVT::i1) {
670 // Return values of type MVT::i1 require special handling. The reason
671 // is that MVT::i1 is associated with the PredRegs register class, but
672 // values of that type are still returned in R0. Generate an explicit
673 // copy into a predicate register from R0, and treat the value of the
674 // predicate register as the call result.
675 auto &MRI = DAG.getMachineFunction().getRegInfo();
676 SDValue FR0 = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000677 MVT::i32, Glue);
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000678 // FR0 = (Value, Chain, Glue)
679 unsigned PredR = MRI.createVirtualRegister(&Hexagon::PredRegsRegClass);
680 SDValue TPR = DAG.getCopyToReg(FR0.getValue(1), dl, PredR,
681 FR0.getValue(0), FR0.getValue(2));
682 // TPR = (Chain, Glue)
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000683 // Don't glue this CopyFromReg, because it copies from a virtual
684 // register. If it is glued to the call, InstrEmitter will add it
685 // as an implicit def to the call (EmitMachineNode).
686 RetVal = DAG.getCopyFromReg(TPR.getValue(0), dl, PredR, MVT::i1);
687 Glue = TPR.getValue(1);
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000688 } else {
689 RetVal = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000690 RVLocs[i].getValVT(), Glue);
691 Glue = RetVal.getValue(2);
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000692 }
693 InVals.push_back(RetVal.getValue(0));
694 Chain = RetVal.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000695 }
696
697 return Chain;
698}
699
700/// LowerCall - Functions arguments are copied from virtual regs to
701/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
702SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +0000703HexagonTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000704 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +0000705 SelectionDAG &DAG = CLI.DAG;
Craig Topperb94011f2013-07-14 04:42:23 +0000706 SDLoc &dl = CLI.DL;
707 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
708 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
709 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000710 SDValue Chain = CLI.Chain;
711 SDValue Callee = CLI.Callee;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000712 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000713 CallingConv::ID CallConv = CLI.CallConv;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000714 bool IsVarArg = CLI.IsVarArg;
715 bool DoesNotReturn = CLI.DoesNotReturn;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000716
717 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000718 MachineFunction &MF = DAG.getMachineFunction();
Mehdi Amini44ede332015-07-09 02:09:04 +0000719 auto PtrVT = getPointerTy(MF.getDataLayout());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000720
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000721 // Check for varargs.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000722 unsigned NumNamedVarArgParams = -1U;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000723 if (GlobalAddressSDNode *GAN = dyn_cast<GlobalAddressSDNode>(Callee)) {
724 const GlobalValue *GV = GAN->getGlobal();
725 Callee = DAG.getTargetGlobalAddress(GV, dl, MVT::i32);
726 if (const Function* F = dyn_cast<Function>(GV)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000727 // If a function has zero args and is a vararg function, that's
728 // disallowed so it must be an undeclared function. Do not assume
729 // varargs if the callee is undefined.
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000730 if (F->isVarArg() && F->getFunctionType()->getNumParams() != 0)
731 NumNamedVarArgParams = F->getFunctionType()->getNumParams();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000732 }
733 }
734
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000735 // Analyze operands of the call, assigning locations to each operand.
736 SmallVector<CCValAssign, 16> ArgLocs;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000737 HexagonCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
Eric Christopherb5217502014-08-06 18:45:26 +0000738 *DAG.getContext(), NumNamedVarArgParams);
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000739
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000740 if (IsVarArg)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000741 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon_VarArg);
742 else
743 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon);
744
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000745 auto Attr = MF.getFunction()->getFnAttribute("disable-tail-calls");
746 if (Attr.getValueAsString() == "true")
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000747 IsTailCall = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000748
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000749 if (IsTailCall) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000750 bool StructAttrFlag = MF.getFunction()->hasStructRetAttr();
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000751 IsTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
752 IsVarArg, IsStructRet,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000753 StructAttrFlag,
754 Outs, OutVals, Ins, DAG);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000755 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000756 CCValAssign &VA = ArgLocs[i];
757 if (VA.isMemLoc()) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000758 IsTailCall = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000759 break;
760 }
761 }
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000762 DEBUG(dbgs() << (IsTailCall ? "Eligible for Tail Call\n"
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000763 : "Argument must be passed on stack. "
764 "Not eligible for Tail Call\n"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000765 }
766 // Get a count of how many bytes are to be pushed on the stack.
767 unsigned NumBytes = CCInfo.getNextStackOffset();
768 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
769 SmallVector<SDValue, 8> MemOpChains;
770
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000771 auto &HRI = *Subtarget.getRegisterInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +0000772 SDValue StackPtr =
773 DAG.getCopyFromReg(Chain, dl, HRI.getStackRegister(), PtrVT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000774
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000775 bool NeedsArgAlign = false;
776 unsigned LargestAlignSeen = 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000777 // Walk the register/memloc assignments, inserting copies/loads.
778 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
779 CCValAssign &VA = ArgLocs[i];
780 SDValue Arg = OutVals[i];
781 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000782 // Record if we need > 8 byte alignment on an argument.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000783 bool ArgAlign = isHvxVectorType(VA.getValVT());
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000784 NeedsArgAlign |= ArgAlign;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000785
786 // Promote the value if needed.
787 switch (VA.getLocInfo()) {
788 default:
789 // Loc info must be one of Full, SExt, ZExt, or AExt.
Craig Toppere55c5562012-02-07 02:50:20 +0000790 llvm_unreachable("Unknown loc info!");
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000791 case CCValAssign::BCvt:
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000792 case CCValAssign::Full:
793 break;
794 case CCValAssign::SExt:
795 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
796 break;
797 case CCValAssign::ZExt:
798 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
799 break;
800 case CCValAssign::AExt:
801 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
802 break;
803 }
804
805 if (VA.isMemLoc()) {
806 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000807 SDValue MemAddr = DAG.getConstant(LocMemOffset, dl,
808 StackPtr.getValueType());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000809 MemAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, MemAddr);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000810 if (ArgAlign)
811 LargestAlignSeen = std::max(LargestAlignSeen,
812 VA.getLocVT().getStoreSizeInBits() >> 3);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000813 if (Flags.isByVal()) {
814 // The argument is a struct passed by value. According to LLVM, "Arg"
815 // is is pointer.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000816 MemOpChains.push_back(CreateCopyOfByValArgument(Arg, MemAddr, Chain,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000817 Flags, DAG, dl));
818 } else {
Alex Lorenze40c8a22015-08-11 23:09:45 +0000819 MachinePointerInfo LocPI = MachinePointerInfo::getStack(
820 DAG.getMachineFunction(), LocMemOffset);
Justin Lebar9c375812016-07-15 18:27:10 +0000821 SDValue S = DAG.getStore(Chain, dl, Arg, MemAddr, LocPI);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000822 MemOpChains.push_back(S);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000823 }
824 continue;
825 }
826
827 // Arguments that can be passed on register must be kept at RegsToPass
828 // vector.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000829 if (VA.isRegLoc())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000830 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000831 }
832
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000833 if (NeedsArgAlign && Subtarget.hasV60TOps()) {
834 DEBUG(dbgs() << "Function needs byte stack align due to call args\n");
Matthias Braun941a7052016-07-28 18:40:00 +0000835 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000836 // V6 vectors passed by value have 64 or 128 byte alignment depending
837 // on whether we are 64 byte vector mode or 128 byte.
838 bool UseHVXDbl = Subtarget.useHVXDblOps();
839 assert(Subtarget.useHVXOps());
840 const unsigned ObjAlign = UseHVXDbl ? 128 : 64;
841 LargestAlignSeen = std::max(LargestAlignSeen, ObjAlign);
Matthias Braun941a7052016-07-28 18:40:00 +0000842 MFI.ensureMaxAlignment(LargestAlignSeen);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000843 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000844 // Transform all store nodes into one single node because all store
845 // nodes are independent of each other.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000846 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000847 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000848
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000849 SDValue Glue;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000850 if (!IsTailCall) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000851 SDValue C = DAG.getConstant(NumBytes, dl, PtrVT, true);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000852 Chain = DAG.getCALLSEQ_START(Chain, C, dl);
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000853 Glue = Chain.getValue(1);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000854 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000855
856 // Build a sequence of copy-to-reg nodes chained together with token
857 // chain and flag operands which copy the outgoing args into registers.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000858 // The Glue is necessary since all emitted instructions must be
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000859 // stuck together.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000860 if (!IsTailCall) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000861 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
862 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000863 RegsToPass[i].second, Glue);
864 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000865 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000866 } else {
867 // For tail calls lower the arguments to the 'real' stack slot.
868 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000869 // Force all the incoming stack arguments to be loaded from the stack
870 // before any new outgoing arguments are stored to the stack, because the
871 // outgoing stack slots may alias the incoming argument stack slots, and
872 // the alias isn't otherwise explicit. This is slightly more conservative
873 // than necessary, because it means that each store effectively depends
874 // on every argument instead of just those arguments it would clobber.
875 //
Benjamin Kramerbde91762012-06-02 10:20:22 +0000876 // Do not flag preceding copytoreg stuff together with the following stuff.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000877 Glue = SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000878 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
879 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000880 RegsToPass[i].second, Glue);
881 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000882 }
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000883 Glue = SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000884 }
885
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000886 bool LongCalls = MF.getSubtarget<HexagonSubtarget>().useLongCalls();
887 unsigned Flags = LongCalls ? HexagonII::HMOTF_ConstExtended : 0;
888
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000889 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
890 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
891 // node so that legalize doesn't hack it.
Tobias Edler von Kochb51460c2015-12-16 17:29:37 +0000892 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000893 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, PtrVT, 0, Flags);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000894 } else if (ExternalSymbolSDNode *S =
895 dyn_cast<ExternalSymbolSDNode>(Callee)) {
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000896 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), PtrVT, Flags);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000897 }
898
899 // Returns a chain & a flag for retval copy to use.
900 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
901 SmallVector<SDValue, 8> Ops;
902 Ops.push_back(Chain);
903 Ops.push_back(Callee);
904
905 // Add argument registers to the end of the list so that they are
906 // known live into the call.
907 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
908 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
909 RegsToPass[i].second.getValueType()));
910 }
911
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000912 const uint32_t *Mask = HRI.getCallPreservedMask(MF, CallConv);
913 assert(Mask && "Missing call preserved mask for calling convention");
914 Ops.push_back(DAG.getRegisterMask(Mask));
915
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000916 if (Glue.getNode())
917 Ops.push_back(Glue);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000918
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000919 if (IsTailCall) {
Matthias Braun941a7052016-07-28 18:40:00 +0000920 MF.getFrameInfo().setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +0000921 return DAG.getNode(HexagonISD::TC_RETURN, dl, NodeTys, Ops);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +0000922 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000923
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +0000924 unsigned OpCode = DoesNotReturn ? HexagonISD::CALLnr : HexagonISD::CALL;
Colin LeMahieu2e3a26d2015-01-16 17:05:27 +0000925 Chain = DAG.getNode(OpCode, dl, NodeTys, Ops);
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000926 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000927
928 // Create the CALLSEQ_END node.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000929 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000930 DAG.getIntPtrConstant(0, dl, true), Glue, dl);
931 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000932
933 // Handle result values, copying them out of physregs into vregs that we
934 // return.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000935 return LowerCallResult(Chain, Glue, CallConv, IsVarArg, Ins, dl, DAG,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000936 InVals, OutVals, Callee);
937}
938
939static bool getIndexedAddressParts(SDNode *Ptr, EVT VT,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000940 SDValue &Base, SDValue &Offset,
941 bool &IsInc, SelectionDAG &DAG) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000942 if (Ptr->getOpcode() != ISD::ADD)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000943 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000944
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000945 auto &HST = static_cast<const HexagonSubtarget&>(DAG.getSubtarget());
946 bool UseHVX = HST.useHVXOps();
947 bool UseHVXDbl = HST.useHVXDblOps();
948
949 bool ValidHVXDblType =
950 (UseHVX && UseHVXDbl) && (VT == MVT::v32i32 || VT == MVT::v16i64 ||
951 VT == MVT::v64i16 || VT == MVT::v128i8);
952 bool ValidHVXType =
953 UseHVX && !UseHVXDbl && (VT == MVT::v16i32 || VT == MVT::v8i64 ||
954 VT == MVT::v32i16 || VT == MVT::v64i8);
955
956 if (ValidHVXDblType || ValidHVXType ||
957 VT == MVT::i64 || VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000958 IsInc = (Ptr->getOpcode() == ISD::ADD);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000959 Base = Ptr->getOperand(0);
960 Offset = Ptr->getOperand(1);
961 // Ensure that Offset is a constant.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000962 return isa<ConstantSDNode>(Offset);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000963 }
964
965 return false;
966}
967
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000968/// getPostIndexedAddressParts - returns true by value, base pointer and
969/// offset pointer and addressing mode by reference if this node can be
970/// combined with a load / store to form a post-indexed load / store.
971bool HexagonTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
972 SDValue &Base,
973 SDValue &Offset,
974 ISD::MemIndexedMode &AM,
975 SelectionDAG &DAG) const
976{
977 EVT VT;
978 SDValue Ptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000979
980 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
981 VT = LD->getMemoryVT();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000982 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
983 VT = ST->getMemoryVT();
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000984 if (ST->getValue().getValueType() == MVT::i64 && ST->isTruncatingStore())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000985 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000986 } else {
987 return false;
988 }
989
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000990 bool IsInc = false;
991 bool isLegal = getIndexedAddressParts(Op, VT, Base, Offset, IsInc, DAG);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000992 if (isLegal) {
993 auto &HII = *Subtarget.getInstrInfo();
994 int32_t OffsetVal = cast<ConstantSDNode>(Offset.getNode())->getSExtValue();
995 if (HII.isValidAutoIncImm(VT, OffsetVal)) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000996 AM = IsInc ? ISD::POST_INC : ISD::POST_DEC;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000997 return true;
998 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000999 }
1000
1001 return false;
1002}
1003
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001004SDValue
1005HexagonTargetLowering::LowerINLINEASM(SDValue Op, SelectionDAG &DAG) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001006 SDNode *Node = Op.getNode();
1007 MachineFunction &MF = DAG.getMachineFunction();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001008 auto &FuncInfo = *MF.getInfo<HexagonMachineFunctionInfo>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001009 switch (Node->getOpcode()) {
1010 case ISD::INLINEASM: {
1011 unsigned NumOps = Node->getNumOperands();
1012 if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue)
1013 --NumOps; // Ignore the flag operand.
1014
1015 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001016 if (FuncInfo.hasClobberLR())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001017 break;
1018 unsigned Flags =
1019 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
1020 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
1021 ++i; // Skip the ID value.
1022
1023 switch (InlineAsm::getKind(Flags)) {
1024 default: llvm_unreachable("Bad flags!");
1025 case InlineAsm::Kind_RegDef:
1026 case InlineAsm::Kind_RegUse:
1027 case InlineAsm::Kind_Imm:
1028 case InlineAsm::Kind_Clobber:
1029 case InlineAsm::Kind_Mem: {
1030 for (; NumVals; --NumVals, ++i) {}
1031 break;
1032 }
1033 case InlineAsm::Kind_RegDefEarlyClobber: {
1034 for (; NumVals; --NumVals, ++i) {
1035 unsigned Reg =
1036 cast<RegisterSDNode>(Node->getOperand(i))->getReg();
1037
1038 // Check it to be lr
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001039 const HexagonRegisterInfo *QRI = Subtarget.getRegisterInfo();
Eric Christopherdbe1cb02014-06-27 00:13:52 +00001040 if (Reg == QRI->getRARegister()) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001041 FuncInfo.setHasClobberLR(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001042 break;
1043 }
1044 }
1045 break;
1046 }
1047 }
1048 }
1049 }
1050 } // Node->getOpcode
1051 return Op;
1052}
1053
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00001054// Need to transform ISD::PREFETCH into something that doesn't inherit
1055// all of the properties of ISD::PREFETCH, specifically SDNPMayLoad and
1056// SDNPMayStore.
1057SDValue HexagonTargetLowering::LowerPREFETCH(SDValue Op,
1058 SelectionDAG &DAG) const {
1059 SDValue Chain = Op.getOperand(0);
1060 SDValue Addr = Op.getOperand(1);
1061 // Lower it to DCFETCH($reg, #0). A "pat" will try to merge the offset in,
1062 // if the "reg" is fed by an "add".
1063 SDLoc DL(Op);
1064 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
1065 return DAG.getNode(HexagonISD::DCFETCH, DL, MVT::Other, Chain, Addr, Zero);
1066}
1067
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +00001068// Custom-handle ISD::READCYCLECOUNTER because the target-independent SDNode
1069// is marked as having side-effects, while the register read on Hexagon does
1070// not have any. TableGen refuses to accept the direct pattern from that node
1071// to the A4_tfrcpp.
1072SDValue HexagonTargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
1073 SelectionDAG &DAG) const {
1074 SDValue Chain = Op.getOperand(0);
1075 SDLoc dl(Op);
1076 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
1077 return DAG.getNode(HexagonISD::READCYCLE, dl, VTs, Chain);
1078}
1079
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00001080SDValue HexagonTargetLowering::LowerINTRINSIC_VOID(SDValue Op,
1081 SelectionDAG &DAG) const {
1082 SDValue Chain = Op.getOperand(0);
1083 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1084 // Lower the hexagon_prefetch builtin to DCFETCH, as above.
1085 if (IntNo == Intrinsic::hexagon_prefetch) {
1086 SDValue Addr = Op.getOperand(2);
1087 SDLoc DL(Op);
1088 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
1089 return DAG.getNode(HexagonISD::DCFETCH, DL, MVT::Other, Chain, Addr, Zero);
1090 }
1091 return SDValue();
1092}
1093
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001094SDValue
1095HexagonTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
1096 SelectionDAG &DAG) const {
1097 SDValue Chain = Op.getOperand(0);
1098 SDValue Size = Op.getOperand(1);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001099 SDValue Align = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001100 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001101
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001102 ConstantSDNode *AlignConst = dyn_cast<ConstantSDNode>(Align);
1103 assert(AlignConst && "Non-constant Align in LowerDYNAMIC_STACKALLOC");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001104
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001105 unsigned A = AlignConst->getSExtValue();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001106 auto &HFI = *Subtarget.getFrameLowering();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001107 // "Zero" means natural stack alignment.
1108 if (A == 0)
1109 A = HFI.getStackAlignment();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001110
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001111 DEBUG({
Reid Kleckner40d72302016-10-20 00:22:23 +00001112 dbgs () << __func__ << " Align: " << A << " Size: ";
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001113 Size.getNode()->dump(&DAG);
1114 dbgs() << "\n";
1115 });
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001116
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001117 SDValue AC = DAG.getConstant(A, dl, MVT::i32);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001118 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00001119 SDValue AA = DAG.getNode(HexagonISD::ALLOCA, dl, VTs, Chain, Size, AC);
Nirav Davebfdb4832016-06-23 17:52:57 +00001120
1121 DAG.ReplaceAllUsesOfValueWith(Op, AA);
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00001122 return AA;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001123}
1124
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001125SDValue HexagonTargetLowering::LowerFormalArguments(
1126 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
1127 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
1128 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001129 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00001130 MachineFrameInfo &MFI = MF.getFrameInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001131 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001132 auto &FuncInfo = *MF.getInfo<HexagonMachineFunctionInfo>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001133
1134 // Assign locations to all of the incoming arguments.
1135 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001136 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1137 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001138
1139 CCInfo.AnalyzeFormalArguments(Ins, CC_Hexagon);
1140
1141 // For LLVM, in the case when returning a struct by value (>8byte),
1142 // the first argument is a pointer that points to the location on caller's
1143 // stack where the return value will be stored. For Hexagon, the location on
1144 // caller's stack is passed only when the struct size is smaller than (and
1145 // equal to) 8 bytes. If not, no address will be passed into callee and
1146 // callee return the result direclty through R0/R1.
1147
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001148 SmallVector<SDValue, 8> MemOps;
1149 bool UseHVX = Subtarget.useHVXOps(), UseHVXDbl = Subtarget.useHVXDblOps();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001150
1151 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1152 CCValAssign &VA = ArgLocs[i];
1153 ISD::ArgFlagsTy Flags = Ins[i].Flags;
1154 unsigned ObjSize;
1155 unsigned StackLocation;
1156 int FI;
1157
1158 if ( (VA.isRegLoc() && !Flags.isByVal())
1159 || (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() > 8)) {
1160 // Arguments passed in registers
1161 // 1. int, long long, ptr args that get allocated in register.
1162 // 2. Large struct that gets an register to put its address in.
1163 EVT RegVT = VA.getLocVT();
Sirish Pande69295b82012-05-10 20:20:25 +00001164 if (RegVT == MVT::i8 || RegVT == MVT::i16 ||
1165 RegVT == MVT::i32 || RegVT == MVT::f32) {
Krzysztof Parzyszek8f23dd62017-03-01 17:30:10 +00001166 unsigned VReg =
Craig Topperc7242e02012-04-20 07:30:17 +00001167 RegInfo.createVirtualRegister(&Hexagon::IntRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001168 RegInfo.addLiveIn(VA.getLocReg(), VReg);
Krzysztof Parzyszek8f23dd62017-03-01 17:30:10 +00001169 SDValue Copy = DAG.getCopyFromReg(Chain, dl, VReg, RegVT);
1170 // Treat values of type MVT::i1 specially: they are passed in
1171 // registers of type i32, but they need to remain as values of
1172 // type i1 for consistency of the argument lowering.
1173 if (VA.getValVT() == MVT::i1) {
1174 // Generate a copy into a predicate register and use the value
1175 // of the register as the "InVal".
1176 unsigned PReg =
1177 RegInfo.createVirtualRegister(&Hexagon::PredRegsRegClass);
1178 SDNode *T = DAG.getMachineNode(Hexagon::C2_tfrrp, dl, MVT::i1,
1179 Copy.getValue(0));
1180 Copy = DAG.getCopyToReg(Copy.getValue(1), dl, PReg, SDValue(T, 0));
1181 Copy = DAG.getCopyFromReg(Copy, dl, PReg, MVT::i1);
1182 }
1183 InVals.push_back(Copy);
1184 Chain = Copy.getValue(1);
Colin LeMahieu4379d102015-01-28 22:08:16 +00001185 } else if (RegVT == MVT::i64 || RegVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001186 unsigned VReg =
Craig Topperc7242e02012-04-20 07:30:17 +00001187 RegInfo.createVirtualRegister(&Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001188 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1189 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001190
1191 // Single Vector
1192 } else if ((RegVT == MVT::v8i64 || RegVT == MVT::v16i32 ||
1193 RegVT == MVT::v32i16 || RegVT == MVT::v64i8)) {
1194 unsigned VReg =
1195 RegInfo.createVirtualRegister(&Hexagon::VectorRegsRegClass);
1196 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1197 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1198 } else if (UseHVX && UseHVXDbl &&
1199 ((RegVT == MVT::v16i64 || RegVT == MVT::v32i32 ||
1200 RegVT == MVT::v64i16 || RegVT == MVT::v128i8))) {
1201 unsigned VReg =
1202 RegInfo.createVirtualRegister(&Hexagon::VectorRegs128BRegClass);
1203 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1204 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1205
1206 // Double Vector
1207 } else if ((RegVT == MVT::v16i64 || RegVT == MVT::v32i32 ||
1208 RegVT == MVT::v64i16 || RegVT == MVT::v128i8)) {
1209 unsigned VReg =
1210 RegInfo.createVirtualRegister(&Hexagon::VecDblRegsRegClass);
1211 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1212 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1213 } else if (UseHVX && UseHVXDbl &&
1214 ((RegVT == MVT::v32i64 || RegVT == MVT::v64i32 ||
1215 RegVT == MVT::v128i16 || RegVT == MVT::v256i8))) {
1216 unsigned VReg =
1217 RegInfo.createVirtualRegister(&Hexagon::VecDblRegs128BRegClass);
1218 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1219 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1220 } else if (RegVT == MVT::v512i1 || RegVT == MVT::v1024i1) {
1221 assert(0 && "need to support VecPred regs");
1222 unsigned VReg =
1223 RegInfo.createVirtualRegister(&Hexagon::VecPredRegsRegClass);
1224 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1225 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001226 } else {
1227 assert (0);
1228 }
1229 } else if (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() <= 8) {
1230 assert (0 && "ByValSize must be bigger than 8 bytes");
1231 } else {
1232 // Sanity check.
1233 assert(VA.isMemLoc());
1234
1235 if (Flags.isByVal()) {
1236 // If it's a byval parameter, then we need to compute the
1237 // "real" size, not the size of the pointer.
1238 ObjSize = Flags.getByValSize();
1239 } else {
1240 ObjSize = VA.getLocVT().getStoreSizeInBits() >> 3;
1241 }
1242
1243 StackLocation = HEXAGON_LRFP_SIZE + VA.getLocMemOffset();
1244 // Create the frame index object for this incoming parameter...
Matthias Braun941a7052016-07-28 18:40:00 +00001245 FI = MFI.CreateFixedObject(ObjSize, StackLocation, true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001246
1247 // Create the SelectionDAG nodes cordl, responding to a load
1248 // from this parameter.
1249 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
1250
1251 if (Flags.isByVal()) {
1252 // If it's a pass-by-value aggregate, then do not dereference the stack
1253 // location. Instead, we should generate a reference to the stack
1254 // location.
1255 InVals.push_back(FIN);
1256 } else {
Justin Lebar9c375812016-07-15 18:27:10 +00001257 InVals.push_back(
Krzysztof Parzyszek3e2046c2017-04-13 15:00:18 +00001258 DAG.getLoad(VA.getValVT(), dl, Chain, FIN, MachinePointerInfo()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001259 }
1260 }
1261 }
1262
1263 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001264 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001265
1266 if (isVarArg) {
1267 // This will point to the next argument passed via stack.
Matthias Braun941a7052016-07-28 18:40:00 +00001268 int FrameIndex = MFI.CreateFixedObject(Hexagon_PointerSize,
1269 HEXAGON_LRFP_SIZE +
1270 CCInfo.getNextStackOffset(),
1271 true);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001272 FuncInfo.setVarArgsFrameIndex(FrameIndex);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001273 }
1274
1275 return Chain;
1276}
1277
1278SDValue
1279HexagonTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
1280 // VASTART stores the address of the VarArgsFrameIndex slot into the
1281 // memory location argument.
1282 MachineFunction &MF = DAG.getMachineFunction();
1283 HexagonMachineFunctionInfo *QFI = MF.getInfo<HexagonMachineFunctionInfo>();
1284 SDValue Addr = DAG.getFrameIndex(QFI->getVarArgsFrameIndex(), MVT::i32);
1285 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Justin Lebar9c375812016-07-15 18:27:10 +00001286 return DAG.getStore(Op.getOperand(0), SDLoc(Op), Addr, Op.getOperand(1),
1287 MachinePointerInfo(SV));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001288}
1289
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001290// Creates a SPLAT instruction for a constant value VAL.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001291static SDValue createSplat(SelectionDAG &DAG, const SDLoc &dl, EVT VT,
1292 SDValue Val) {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001293 if (VT.getSimpleVT() == MVT::v4i8)
1294 return DAG.getNode(HexagonISD::VSPLATB, dl, VT, Val);
1295
1296 if (VT.getSimpleVT() == MVT::v4i16)
1297 return DAG.getNode(HexagonISD::VSPLATH, dl, VT, Val);
1298
1299 return SDValue();
1300}
1301
1302static bool isSExtFree(SDValue N) {
1303 // A sign-extend of a truncate of a sign-extend is free.
1304 if (N.getOpcode() == ISD::TRUNCATE &&
1305 N.getOperand(0).getOpcode() == ISD::AssertSext)
1306 return true;
1307 // We have sign-extended loads.
1308 if (N.getOpcode() == ISD::LOAD)
1309 return true;
1310 return false;
1311}
1312
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001313SDValue HexagonTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1314 SDLoc dl(Op);
1315
1316 SDValue LHS = Op.getOperand(0);
1317 SDValue RHS = Op.getOperand(1);
1318 SDValue Cmp = Op.getOperand(2);
1319 ISD::CondCode CC = cast<CondCodeSDNode>(Cmp)->get();
1320
1321 EVT VT = Op.getValueType();
1322 EVT LHSVT = LHS.getValueType();
1323 EVT RHSVT = RHS.getValueType();
1324
1325 if (LHSVT == MVT::v2i16) {
1326 assert(ISD::isSignedIntSetCC(CC) || ISD::isUnsignedIntSetCC(CC));
1327 unsigned ExtOpc = ISD::isSignedIntSetCC(CC) ? ISD::SIGN_EXTEND
1328 : ISD::ZERO_EXTEND;
1329 SDValue LX = DAG.getNode(ExtOpc, dl, MVT::v2i32, LHS);
1330 SDValue RX = DAG.getNode(ExtOpc, dl, MVT::v2i32, RHS);
1331 SDValue SC = DAG.getNode(ISD::SETCC, dl, MVT::v2i1, LX, RX, Cmp);
1332 return SC;
1333 }
1334
1335 // Treat all other vector types as legal.
1336 if (VT.isVector())
1337 return Op;
1338
1339 // Equals and not equals should use sign-extend, not zero-extend, since
1340 // we can represent small negative values in the compare instructions.
1341 // The LLVM default is to use zero-extend arbitrarily in these cases.
1342 if ((CC == ISD::SETEQ || CC == ISD::SETNE) &&
1343 (RHSVT == MVT::i8 || RHSVT == MVT::i16) &&
1344 (LHSVT == MVT::i8 || LHSVT == MVT::i16)) {
1345 ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS);
1346 if (C && C->getAPIntValue().isNegative()) {
1347 LHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, LHS);
1348 RHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, RHS);
1349 return DAG.getNode(ISD::SETCC, dl, Op.getValueType(),
1350 LHS, RHS, Op.getOperand(2));
1351 }
1352 if (isSExtFree(LHS) || isSExtFree(RHS)) {
1353 LHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, LHS);
1354 RHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, RHS);
1355 return DAG.getNode(ISD::SETCC, dl, Op.getValueType(),
1356 LHS, RHS, Op.getOperand(2));
1357 }
1358 }
1359 return SDValue();
1360}
1361
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001362SDValue
1363HexagonTargetLowering::LowerVSELECT(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001364 SDValue PredOp = Op.getOperand(0);
1365 SDValue Op1 = Op.getOperand(1), Op2 = Op.getOperand(2);
1366 EVT OpVT = Op1.getValueType();
1367 SDLoc DL(Op);
1368
1369 if (OpVT == MVT::v2i16) {
1370 SDValue X1 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op1);
1371 SDValue X2 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op2);
1372 SDValue SL = DAG.getNode(ISD::VSELECT, DL, MVT::v2i32, PredOp, X1, X2);
1373 SDValue TR = DAG.getNode(ISD::TRUNCATE, DL, MVT::v2i16, SL);
1374 return TR;
1375 }
1376
1377 return SDValue();
1378}
1379
1380// Handle only specific vector loads.
1381SDValue HexagonTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1382 EVT VT = Op.getValueType();
1383 SDLoc DL(Op);
1384 LoadSDNode *LoadNode = cast<LoadSDNode>(Op);
1385 SDValue Chain = LoadNode->getChain();
1386 SDValue Ptr = Op.getOperand(1);
1387 SDValue LoweredLoad;
1388 SDValue Result;
1389 SDValue Base = LoadNode->getBasePtr();
1390 ISD::LoadExtType Ext = LoadNode->getExtensionType();
1391 unsigned Alignment = LoadNode->getAlignment();
1392 SDValue LoadChain;
1393
1394 if(Ext == ISD::NON_EXTLOAD)
1395 Ext = ISD::ZEXTLOAD;
1396
1397 if (VT == MVT::v4i16) {
1398 if (Alignment == 2) {
1399 SDValue Loads[4];
1400 // Base load.
1401 Loads[0] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Base,
Justin Lebar9c375812016-07-15 18:27:10 +00001402 LoadNode->getPointerInfo(), MVT::i16, Alignment,
1403 LoadNode->getMemOperand()->getFlags());
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001404 // Base+2 load.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001405 SDValue Increment = DAG.getConstant(2, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001406 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1407 Loads[1] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
Justin Lebar9c375812016-07-15 18:27:10 +00001408 LoadNode->getPointerInfo(), MVT::i16, Alignment,
1409 LoadNode->getMemOperand()->getFlags());
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001410 // SHL 16, then OR base and base+2.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001411 SDValue ShiftAmount = DAG.getConstant(16, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001412 SDValue Tmp1 = DAG.getNode(ISD::SHL, DL, MVT::i32, Loads[1], ShiftAmount);
1413 SDValue Tmp2 = DAG.getNode(ISD::OR, DL, MVT::i32, Tmp1, Loads[0]);
1414 // Base + 4.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001415 Increment = DAG.getConstant(4, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001416 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1417 Loads[2] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
Justin Lebar9c375812016-07-15 18:27:10 +00001418 LoadNode->getPointerInfo(), MVT::i16, Alignment,
1419 LoadNode->getMemOperand()->getFlags());
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001420 // Base + 6.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001421 Increment = DAG.getConstant(6, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001422 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1423 Loads[3] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
Justin Lebar9c375812016-07-15 18:27:10 +00001424 LoadNode->getPointerInfo(), MVT::i16, Alignment,
1425 LoadNode->getMemOperand()->getFlags());
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001426 // SHL 16, then OR base+4 and base+6.
1427 Tmp1 = DAG.getNode(ISD::SHL, DL, MVT::i32, Loads[3], ShiftAmount);
1428 SDValue Tmp4 = DAG.getNode(ISD::OR, DL, MVT::i32, Tmp1, Loads[2]);
1429 // Combine to i64. This could be optimised out later if we can
1430 // affect reg allocation of this code.
1431 Result = DAG.getNode(HexagonISD::COMBINE, DL, MVT::i64, Tmp4, Tmp2);
1432 LoadChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
1433 Loads[0].getValue(1), Loads[1].getValue(1),
1434 Loads[2].getValue(1), Loads[3].getValue(1));
1435 } else {
1436 // Perform default type expansion.
1437 Result = DAG.getLoad(MVT::i64, DL, Chain, Ptr, LoadNode->getPointerInfo(),
Justin Lebar9c375812016-07-15 18:27:10 +00001438 LoadNode->getAlignment(),
1439 LoadNode->getMemOperand()->getFlags());
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001440 LoadChain = Result.getValue(1);
1441 }
1442 } else
1443 llvm_unreachable("Custom lowering unsupported load");
1444
1445 Result = DAG.getNode(ISD::BITCAST, DL, VT, Result);
1446 // Since we pretend to lower a load, we need the original chain
1447 // info attached to the result.
1448 SDValue Ops[] = { Result, LoadChain };
1449
1450 return DAG.getMergeValues(Ops, DL);
1451}
1452
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001453SDValue
Sirish Pande69295b82012-05-10 20:20:25 +00001454HexagonTargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
1455 EVT ValTy = Op.getValueType();
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001456 ConstantPoolSDNode *CPN = cast<ConstantPoolSDNode>(Op);
1457 unsigned Align = CPN->getAlignment();
Rafael Espindola405e25a2016-06-26 22:24:01 +00001458 bool IsPositionIndependent = isPositionIndependent();
1459 unsigned char TF = IsPositionIndependent ? HexagonII::MO_PCREL : 0;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001460
Ron Lieberman822ee882016-08-13 23:41:11 +00001461 unsigned Offset = 0;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001462 SDValue T;
1463 if (CPN->isMachineConstantPoolEntry())
Ron Lieberman822ee882016-08-13 23:41:11 +00001464 T = DAG.getTargetConstantPool(CPN->getMachineCPVal(), ValTy, Align, Offset,
1465 TF);
Sirish Pande69295b82012-05-10 20:20:25 +00001466 else
Ron Lieberman822ee882016-08-13 23:41:11 +00001467 T = DAG.getTargetConstantPool(CPN->getConstVal(), ValTy, Align, Offset,
1468 TF);
1469
1470 assert(cast<ConstantPoolSDNode>(T)->getTargetFlags() == TF &&
1471 "Inconsistent target flag encountered");
1472
Rafael Espindola405e25a2016-06-26 22:24:01 +00001473 if (IsPositionIndependent)
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001474 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), ValTy, T);
1475 return DAG.getNode(HexagonISD::CP, SDLoc(Op), ValTy, T);
1476}
1477
1478SDValue
1479HexagonTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
1480 EVT VT = Op.getValueType();
1481 int Idx = cast<JumpTableSDNode>(Op)->getIndex();
Rafael Espindola405e25a2016-06-26 22:24:01 +00001482 if (isPositionIndependent()) {
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001483 SDValue T = DAG.getTargetJumpTable(Idx, VT, HexagonII::MO_PCREL);
1484 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), VT, T);
1485 }
1486
1487 SDValue T = DAG.getTargetJumpTable(Idx, VT);
1488 return DAG.getNode(HexagonISD::JT, SDLoc(Op), VT, T);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001489}
1490
1491SDValue
1492HexagonTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001493 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001494 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00001495 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001496 MFI.setReturnAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001497
Bill Wendling908bf812014-01-06 00:43:20 +00001498 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001499 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001500
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001501 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001502 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001503 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1504 if (Depth) {
1505 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001506 SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001507 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
1508 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Justin Lebar9c375812016-07-15 18:27:10 +00001509 MachinePointerInfo());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001510 }
1511
1512 // Return LR, which contains the return address. Mark it an implicit live-in.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001513 unsigned Reg = MF.addLiveIn(HRI.getRARegister(), getRegClassFor(MVT::i32));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001514 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
1515}
1516
1517SDValue
1518HexagonTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001519 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Matthias Braun941a7052016-07-28 18:40:00 +00001520 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001521 MFI.setFrameAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001522
1523 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001524 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001525 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1526 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001527 HRI.getFrameRegister(), VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001528 while (Depth--)
1529 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00001530 MachinePointerInfo());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001531 return FrameAddr;
1532}
1533
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001534SDValue
1535HexagonTargetLowering::LowerATOMIC_FENCE(SDValue Op, SelectionDAG& DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001536 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001537 return DAG.getNode(HexagonISD::BARRIER, dl, MVT::Other, Op.getOperand(0));
1538}
1539
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001540SDValue
1541HexagonTargetLowering::LowerGLOBALADDRESS(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001542 SDLoc dl(Op);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001543 auto *GAN = cast<GlobalAddressSDNode>(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00001544 auto PtrVT = getPointerTy(DAG.getDataLayout());
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001545 auto *GV = GAN->getGlobal();
1546 int64_t Offset = GAN->getOffset();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001547
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001548 auto &HLOF = *HTM.getObjFileLowering();
1549 Reloc::Model RM = HTM.getRelocationModel();
1550
1551 if (RM == Reloc::Static) {
1552 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset);
Peter Collingbourne67335642016-10-24 19:23:39 +00001553 const GlobalObject *GO = GV->getBaseObject();
1554 if (GO && HLOF.isGlobalInSmallSection(GO, HTM))
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001555 return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, GA);
1556 return DAG.getNode(HexagonISD::CONST32, dl, PtrVT, GA);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001557 }
1558
Rafael Espindola3beef8d2016-06-27 23:15:57 +00001559 bool UsePCRel = getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001560 if (UsePCRel) {
1561 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset,
1562 HexagonII::MO_PCREL);
1563 return DAG.getNode(HexagonISD::AT_PCREL, dl, PtrVT, GA);
1564 }
1565
1566 // Use GOT index.
1567 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
1568 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, HexagonII::MO_GOT);
1569 SDValue Off = DAG.getConstant(Offset, dl, MVT::i32);
1570 return DAG.getNode(HexagonISD::AT_GOT, dl, PtrVT, GOT, GA, Off);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001571}
1572
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001573// Specifies that for loads and stores VT can be promoted to PromotedLdStVT.
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001574SDValue
1575HexagonTargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
1576 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001577 SDLoc dl(Op);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001578 EVT PtrVT = getPointerTy(DAG.getDataLayout());
1579
1580 Reloc::Model RM = HTM.getRelocationModel();
1581 if (RM == Reloc::Static) {
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001582 SDValue A = DAG.getTargetBlockAddress(BA, PtrVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001583 return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, A);
1584 }
1585
1586 SDValue A = DAG.getTargetBlockAddress(BA, PtrVT, 0, HexagonII::MO_PCREL);
1587 return DAG.getNode(HexagonISD::AT_PCREL, dl, PtrVT, A);
1588}
1589
1590SDValue
1591HexagonTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op, SelectionDAG &DAG)
1592 const {
1593 EVT PtrVT = getPointerTy(DAG.getDataLayout());
1594 SDValue GOTSym = DAG.getTargetExternalSymbol(HEXAGON_GOT_SYM_NAME, PtrVT,
1595 HexagonII::MO_PCREL);
1596 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), PtrVT, GOTSym);
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001597}
1598
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001599SDValue
1600HexagonTargetLowering::GetDynamicTLSAddr(SelectionDAG &DAG, SDValue Chain,
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001601 GlobalAddressSDNode *GA, SDValue Glue, EVT PtrVT, unsigned ReturnReg,
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001602 unsigned char OperandFlags) const {
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001603 MachineFunction &MF = DAG.getMachineFunction();
1604 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001605 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1606 SDLoc dl(GA);
1607 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
1608 GA->getValueType(0),
1609 GA->getOffset(),
1610 OperandFlags);
1611 // Create Operands for the call.The Operands should have the following:
1612 // 1. Chain SDValue
1613 // 2. Callee which in this case is the Global address value.
1614 // 3. Registers live into the call.In this case its R0, as we
1615 // have just one argument to be passed.
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001616 // 4. Glue.
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001617 // Note: The order is important.
1618
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001619 const auto &HRI = *Subtarget.getRegisterInfo();
1620 const uint32_t *Mask = HRI.getCallPreservedMask(MF, CallingConv::C);
1621 assert(Mask && "Missing call preserved mask for calling convention");
1622 SDValue Ops[] = { Chain, TGA, DAG.getRegister(Hexagon::R0, PtrVT),
1623 DAG.getRegisterMask(Mask), Glue };
1624 Chain = DAG.getNode(HexagonISD::CALL, dl, NodeTys, Ops);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001625
1626 // Inform MFI that function has calls.
Matthias Braun941a7052016-07-28 18:40:00 +00001627 MFI.setAdjustsStack(true);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001628
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001629 Glue = Chain.getValue(1);
1630 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Glue);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001631}
1632
1633//
1634// Lower using the intial executable model for TLS addresses
1635//
1636SDValue
1637HexagonTargetLowering::LowerToTLSInitialExecModel(GlobalAddressSDNode *GA,
1638 SelectionDAG &DAG) const {
1639 SDLoc dl(GA);
1640 int64_t Offset = GA->getOffset();
1641 auto PtrVT = getPointerTy(DAG.getDataLayout());
1642
1643 // Get the thread pointer.
1644 SDValue TP = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Hexagon::UGP, PtrVT);
1645
Rafael Espindola405e25a2016-06-26 22:24:01 +00001646 bool IsPositionIndependent = isPositionIndependent();
1647 unsigned char TF =
1648 IsPositionIndependent ? HexagonII::MO_IEGOT : HexagonII::MO_IE;
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001649
1650 // First generate the TLS symbol address
1651 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT,
1652 Offset, TF);
1653
1654 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1655
Rafael Espindola405e25a2016-06-26 22:24:01 +00001656 if (IsPositionIndependent) {
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001657 // Generate the GOT pointer in case of position independent code
1658 SDValue GOT = LowerGLOBAL_OFFSET_TABLE(Sym, DAG);
1659
1660 // Add the TLS Symbol address to GOT pointer.This gives
1661 // GOT relative relocation for the symbol.
1662 Sym = DAG.getNode(ISD::ADD, dl, PtrVT, GOT, Sym);
1663 }
1664
1665 // Load the offset value for TLS symbol.This offset is relative to
1666 // thread pointer.
Justin Lebar9c375812016-07-15 18:27:10 +00001667 SDValue LoadOffset =
1668 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Sym, MachinePointerInfo());
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001669
1670 // Address of the thread local variable is the add of thread
1671 // pointer and the offset of the variable.
1672 return DAG.getNode(ISD::ADD, dl, PtrVT, TP, LoadOffset);
1673}
1674
1675//
1676// Lower using the local executable model for TLS addresses
1677//
1678SDValue
1679HexagonTargetLowering::LowerToTLSLocalExecModel(GlobalAddressSDNode *GA,
1680 SelectionDAG &DAG) const {
1681 SDLoc dl(GA);
1682 int64_t Offset = GA->getOffset();
1683 auto PtrVT = getPointerTy(DAG.getDataLayout());
1684
1685 // Get the thread pointer.
1686 SDValue TP = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Hexagon::UGP, PtrVT);
1687 // Generate the TLS symbol address
1688 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT, Offset,
1689 HexagonII::MO_TPREL);
1690 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1691
1692 // Address of the thread local variable is the add of thread
1693 // pointer and the offset of the variable.
1694 return DAG.getNode(ISD::ADD, dl, PtrVT, TP, Sym);
1695}
1696
1697//
1698// Lower using the general dynamic model for TLS addresses
1699//
1700SDValue
1701HexagonTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
1702 SelectionDAG &DAG) const {
1703 SDLoc dl(GA);
1704 int64_t Offset = GA->getOffset();
1705 auto PtrVT = getPointerTy(DAG.getDataLayout());
1706
1707 // First generate the TLS symbol address
1708 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT, Offset,
1709 HexagonII::MO_GDGOT);
1710
1711 // Then, generate the GOT pointer
1712 SDValue GOT = LowerGLOBAL_OFFSET_TABLE(TGA, DAG);
1713
1714 // Add the TLS symbol and the GOT pointer
1715 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1716 SDValue Chain = DAG.getNode(ISD::ADD, dl, PtrVT, GOT, Sym);
1717
1718 // Copy over the argument to R0
1719 SDValue InFlag;
1720 Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, Hexagon::R0, Chain, InFlag);
1721 InFlag = Chain.getValue(1);
1722
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +00001723 unsigned Flags =
1724 static_cast<const HexagonSubtarget &>(DAG.getSubtarget()).useLongCalls()
1725 ? HexagonII::MO_GDPLT | HexagonII::HMOTF_ConstExtended
1726 : HexagonII::MO_GDPLT;
1727
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001728 return GetDynamicTLSAddr(DAG, Chain, GA, InFlag, PtrVT,
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +00001729 Hexagon::R0, Flags);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001730}
1731
1732//
1733// Lower TLS addresses.
1734//
1735// For now for dynamic models, we only support the general dynamic model.
1736//
1737SDValue
1738HexagonTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1739 SelectionDAG &DAG) const {
1740 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1741
1742 switch (HTM.getTLSModel(GA->getGlobal())) {
1743 case TLSModel::GeneralDynamic:
1744 case TLSModel::LocalDynamic:
1745 return LowerToTLSGeneralDynamicModel(GA, DAG);
1746 case TLSModel::InitialExec:
1747 return LowerToTLSInitialExecModel(GA, DAG);
1748 case TLSModel::LocalExec:
1749 return LowerToTLSLocalExecModel(GA, DAG);
1750 }
1751 llvm_unreachable("Bogus TLS model");
1752}
1753
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001754//===----------------------------------------------------------------------===//
1755// TargetLowering Implementation
1756//===----------------------------------------------------------------------===//
1757
Eric Christopherd737b762015-02-02 22:11:36 +00001758HexagonTargetLowering::HexagonTargetLowering(const TargetMachine &TM,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001759 const HexagonSubtarget &ST)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001760 : TargetLowering(TM), HTM(static_cast<const HexagonTargetMachine&>(TM)),
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001761 Subtarget(ST) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001762 bool IsV4 = !Subtarget.hasV5TOps();
1763 auto &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00001764 bool UseHVX = Subtarget.useHVXOps();
1765 bool UseHVXSgl = Subtarget.useHVXSglOps();
1766 bool UseHVXDbl = Subtarget.useHVXDblOps();
Sirish Pande69295b82012-05-10 20:20:25 +00001767
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001768 setPrefLoopAlignment(4);
1769 setPrefFunctionAlignment(4);
1770 setMinFunctionAlignment(2);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001771 setStackPointerRegisterToSaveRestore(HRI.getStackRegister());
1772
Krzysztof Parzyszekf228c952016-06-22 16:07:10 +00001773 setMaxAtomicSizeInBitsSupported(64);
1774 setMinCmpXchgSizeInBits(32);
1775
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001776 if (EnableHexSDNodeSched)
1777 setSchedulingPreference(Sched::VLIW);
1778 else
1779 setSchedulingPreference(Sched::Source);
1780
1781 // Limits for inline expansion of memcpy/memmove
1782 MaxStoresPerMemcpy = MaxStoresPerMemcpyCL;
1783 MaxStoresPerMemcpyOptSize = MaxStoresPerMemcpyOptSizeCL;
1784 MaxStoresPerMemmove = MaxStoresPerMemmoveCL;
1785 MaxStoresPerMemmoveOptSize = MaxStoresPerMemmoveOptSizeCL;
1786 MaxStoresPerMemset = MaxStoresPerMemsetCL;
1787 MaxStoresPerMemsetOptSize = MaxStoresPerMemsetOptSizeCL;
1788
1789 //
1790 // Set up register classes.
1791 //
1792
1793 addRegisterClass(MVT::i1, &Hexagon::PredRegsRegClass);
1794 addRegisterClass(MVT::v2i1, &Hexagon::PredRegsRegClass); // bbbbaaaa
1795 addRegisterClass(MVT::v4i1, &Hexagon::PredRegsRegClass); // ddccbbaa
1796 addRegisterClass(MVT::v8i1, &Hexagon::PredRegsRegClass); // hgfedcba
1797 addRegisterClass(MVT::i32, &Hexagon::IntRegsRegClass);
1798 addRegisterClass(MVT::v4i8, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001799 addRegisterClass(MVT::v2i16, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001800 addRegisterClass(MVT::i64, &Hexagon::DoubleRegsRegClass);
1801 addRegisterClass(MVT::v8i8, &Hexagon::DoubleRegsRegClass);
1802 addRegisterClass(MVT::v4i16, &Hexagon::DoubleRegsRegClass);
1803 addRegisterClass(MVT::v2i32, &Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001804
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001805 if (Subtarget.hasV5TOps()) {
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001806 addRegisterClass(MVT::f32, &Hexagon::IntRegsRegClass);
1807 addRegisterClass(MVT::f64, &Hexagon::DoubleRegsRegClass);
1808 }
Sirish Pande69295b82012-05-10 20:20:25 +00001809
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001810 if (Subtarget.hasV60TOps()) {
1811 if (Subtarget.useHVXSglOps()) {
1812 addRegisterClass(MVT::v64i8, &Hexagon::VectorRegsRegClass);
1813 addRegisterClass(MVT::v32i16, &Hexagon::VectorRegsRegClass);
1814 addRegisterClass(MVT::v16i32, &Hexagon::VectorRegsRegClass);
1815 addRegisterClass(MVT::v8i64, &Hexagon::VectorRegsRegClass);
1816 addRegisterClass(MVT::v128i8, &Hexagon::VecDblRegsRegClass);
1817 addRegisterClass(MVT::v64i16, &Hexagon::VecDblRegsRegClass);
1818 addRegisterClass(MVT::v32i32, &Hexagon::VecDblRegsRegClass);
1819 addRegisterClass(MVT::v16i64, &Hexagon::VecDblRegsRegClass);
1820 addRegisterClass(MVT::v512i1, &Hexagon::VecPredRegsRegClass);
1821 } else if (Subtarget.useHVXDblOps()) {
1822 addRegisterClass(MVT::v128i8, &Hexagon::VectorRegs128BRegClass);
1823 addRegisterClass(MVT::v64i16, &Hexagon::VectorRegs128BRegClass);
1824 addRegisterClass(MVT::v32i32, &Hexagon::VectorRegs128BRegClass);
1825 addRegisterClass(MVT::v16i64, &Hexagon::VectorRegs128BRegClass);
1826 addRegisterClass(MVT::v256i8, &Hexagon::VecDblRegs128BRegClass);
1827 addRegisterClass(MVT::v128i16, &Hexagon::VecDblRegs128BRegClass);
1828 addRegisterClass(MVT::v64i32, &Hexagon::VecDblRegs128BRegClass);
1829 addRegisterClass(MVT::v32i64, &Hexagon::VecDblRegs128BRegClass);
1830 addRegisterClass(MVT::v1024i1, &Hexagon::VecPredRegs128BRegClass);
1831 }
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001832 }
1833
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001834 //
1835 // Handling of scalar operations.
1836 //
1837 // All operations default to "legal", except:
1838 // - indexed loads and stores (pre-/post-incremented),
1839 // - ANY_EXTEND_VECTOR_INREG, ATOMIC_CMP_SWAP_WITH_SUCCESS, CONCAT_VECTORS,
1840 // ConstantFP, DEBUGTRAP, FCEIL, FCOPYSIGN, FEXP, FEXP2, FFLOOR, FGETSIGN,
1841 // FLOG, FLOG2, FLOG10, FMAXNUM, FMINNUM, FNEARBYINT, FRINT, FROUND, TRAP,
1842 // FTRUNC, PREFETCH, SIGN_EXTEND_VECTOR_INREG, ZERO_EXTEND_VECTOR_INREG,
1843 // which default to "expand" for at least one type.
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001844
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001845 // Misc operations.
1846 setOperationAction(ISD::ConstantFP, MVT::f32, Legal); // Default: expand
1847 setOperationAction(ISD::ConstantFP, MVT::f64, Legal); // Default: expand
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001848
1849 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001850 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001851 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001852 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
1853 setOperationAction(ISD::INLINEASM, MVT::Other, Custom);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00001854 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +00001855 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00001856 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001857 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001858 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001859 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001860 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001861
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001862 // Custom legalize GlobalAddress nodes into CONST32.
1863 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001864 setOperationAction(ISD::GlobalAddress, MVT::i8, Custom);
1865 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001866
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001867 // Hexagon needs to optimize cases with negative constants.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001868 setOperationAction(ISD::SETCC, MVT::i8, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001869 setOperationAction(ISD::SETCC, MVT::i16, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001870
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001871 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
1872 setOperationAction(ISD::VASTART, MVT::Other, Custom);
1873 setOperationAction(ISD::VAEND, MVT::Other, Expand);
1874 setOperationAction(ISD::VAARG, MVT::Other, Expand);
1875
1876 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
1877 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
1878 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
1879
1880 if (EmitJumpTables)
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001881 setMinimumJumpTableEntries(MinimumJumpTables);
Krzysztof Parzyszeka61f7da2016-01-13 21:43:13 +00001882 else
Eugene Zelenko58655bb2016-12-17 01:09:05 +00001883 setMinimumJumpTableEntries(std::numeric_limits<int>::max());
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001884 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001885
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001886 // Hexagon has instructions for add/sub with carry. The problem with
1887 // modeling these instructions is that they produce 2 results: Rdd and Px.
1888 // To model the update of Px, we will have to use Defs[p0..p3] which will
1889 // cause any predicate live range to spill. So, we pretend we dont't have
1890 // these instructions.
1891 setOperationAction(ISD::ADDE, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001892 setOperationAction(ISD::ADDE, MVT::i16, Expand);
1893 setOperationAction(ISD::ADDE, MVT::i32, Expand);
1894 setOperationAction(ISD::ADDE, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001895 setOperationAction(ISD::SUBE, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001896 setOperationAction(ISD::SUBE, MVT::i16, Expand);
1897 setOperationAction(ISD::SUBE, MVT::i32, Expand);
1898 setOperationAction(ISD::SUBE, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001899 setOperationAction(ISD::ADDC, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001900 setOperationAction(ISD::ADDC, MVT::i16, Expand);
1901 setOperationAction(ISD::ADDC, MVT::i32, Expand);
1902 setOperationAction(ISD::ADDC, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001903 setOperationAction(ISD::SUBC, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001904 setOperationAction(ISD::SUBC, MVT::i16, Expand);
1905 setOperationAction(ISD::SUBC, MVT::i32, Expand);
1906 setOperationAction(ISD::SUBC, MVT::i64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001907
Krzysztof Parzyszek2c4487d2015-04-13 20:37:01 +00001908 // Only add and sub that detect overflow are the saturating ones.
1909 for (MVT VT : MVT::integer_valuetypes()) {
1910 setOperationAction(ISD::UADDO, VT, Expand);
1911 setOperationAction(ISD::SADDO, VT, Expand);
1912 setOperationAction(ISD::USUBO, VT, Expand);
1913 setOperationAction(ISD::SSUBO, VT, Expand);
1914 }
1915
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001916 setOperationAction(ISD::CTLZ, MVT::i8, Promote);
1917 setOperationAction(ISD::CTLZ, MVT::i16, Promote);
1918 setOperationAction(ISD::CTTZ, MVT::i8, Promote);
1919 setOperationAction(ISD::CTTZ, MVT::i16, Promote);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001920
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001921 // In V5, popcount can count # of 1s in i64 but returns i32.
1922 // On V4 it will be expanded (set later).
1923 setOperationAction(ISD::CTPOP, MVT::i8, Promote);
1924 setOperationAction(ISD::CTPOP, MVT::i16, Promote);
1925 setOperationAction(ISD::CTPOP, MVT::i32, Promote);
Krzysztof Parzyszekaf5ff652017-02-23 15:02:09 +00001926 setOperationAction(ISD::CTPOP, MVT::i64, Legal);
1927
1928 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
1929 setOperationAction(ISD::BITREVERSE, MVT::i64, Legal);
1930 setOperationAction(ISD::BSWAP, MVT::i32, Legal);
1931 setOperationAction(ISD::BSWAP, MVT::i64, Legal);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001932
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001933 // We custom lower i64 to i64 mul, so that it is not considered as a legal
1934 // operation. There is a pattern that will match i64 mul and transform it
1935 // to a series of instructions.
1936 setOperationAction(ISD::MUL, MVT::i64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001937
Benjamin Kramer62460692015-04-25 14:46:53 +00001938 for (unsigned IntExpOp :
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001939 { ISD::SDIV, ISD::UDIV, ISD::SREM, ISD::UREM,
1940 ISD::SDIVREM, ISD::UDIVREM, ISD::ROTL, ISD::ROTR,
Krzysztof Parzyszekaf5ff652017-02-23 15:02:09 +00001941 ISD::SHL_PARTS, ISD::SRA_PARTS, ISD::SRL_PARTS,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001942 ISD::SMUL_LOHI, ISD::UMUL_LOHI }) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001943 setOperationAction(IntExpOp, MVT::i32, Expand);
1944 setOperationAction(IntExpOp, MVT::i64, Expand);
1945 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001946
Benjamin Kramer62460692015-04-25 14:46:53 +00001947 for (unsigned FPExpOp :
1948 {ISD::FDIV, ISD::FREM, ISD::FSQRT, ISD::FSIN, ISD::FCOS, ISD::FSINCOS,
1949 ISD::FPOW, ISD::FCOPYSIGN}) {
1950 setOperationAction(FPExpOp, MVT::f32, Expand);
1951 setOperationAction(FPExpOp, MVT::f64, Expand);
1952 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001953
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001954 // No extending loads from i32.
1955 for (MVT VT : MVT::integer_valuetypes()) {
1956 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
1957 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
1958 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
1959 }
1960 // Turn FP truncstore into trunc + store.
1961 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Michael Kuperstein2bc3d4d2016-08-18 20:08:15 +00001962 // Turn FP extload into load/fpextend.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001963 for (MVT VT : MVT::fp_valuetypes())
1964 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001965
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001966 // Expand BR_CC and SELECT_CC for all integer and fp types.
1967 for (MVT VT : MVT::integer_valuetypes()) {
1968 setOperationAction(ISD::BR_CC, VT, Expand);
1969 setOperationAction(ISD::SELECT_CC, VT, Expand);
1970 }
1971 for (MVT VT : MVT::fp_valuetypes()) {
1972 setOperationAction(ISD::BR_CC, VT, Expand);
1973 setOperationAction(ISD::SELECT_CC, VT, Expand);
1974 }
1975 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001976
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001977 //
1978 // Handling of vector operations.
1979 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001980
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001981 // Custom lower v4i16 load only. Let v4i16 store to be
1982 // promoted for now.
1983 promoteLdStType(MVT::v4i8, MVT::i32);
1984 promoteLdStType(MVT::v2i16, MVT::i32);
1985 promoteLdStType(MVT::v8i8, MVT::i64);
1986 promoteLdStType(MVT::v2i32, MVT::i64);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001987
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001988 setOperationAction(ISD::LOAD, MVT::v4i16, Custom);
1989 setOperationAction(ISD::STORE, MVT::v4i16, Promote);
1990 AddPromotedToType(ISD::LOAD, MVT::v4i16, MVT::i64);
1991 AddPromotedToType(ISD::STORE, MVT::v4i16, MVT::i64);
1992
1993 // Set the action for vector operations to "expand", then override it with
1994 // either "custom" or "legal" for specific cases.
Craig Topper26260942015-10-18 05:15:34 +00001995 static const unsigned VectExpOps[] = {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001996 // Integer arithmetic:
1997 ISD::ADD, ISD::SUB, ISD::MUL, ISD::SDIV, ISD::UDIV,
1998 ISD::SREM, ISD::UREM, ISD::SDIVREM, ISD::UDIVREM, ISD::ADDC,
1999 ISD::SUBC, ISD::SADDO, ISD::UADDO, ISD::SSUBO, ISD::USUBO,
2000 ISD::SMUL_LOHI, ISD::UMUL_LOHI,
2001 // Logical/bit:
2002 ISD::AND, ISD::OR, ISD::XOR, ISD::ROTL, ISD::ROTR,
Craig Topper33772c52016-04-28 03:34:31 +00002003 ISD::CTPOP, ISD::CTLZ, ISD::CTTZ,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002004 // Floating point arithmetic/math functions:
2005 ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FMA, ISD::FDIV,
2006 ISD::FREM, ISD::FNEG, ISD::FABS, ISD::FSQRT, ISD::FSIN,
2007 ISD::FCOS, ISD::FPOWI, ISD::FPOW, ISD::FLOG, ISD::FLOG2,
2008 ISD::FLOG10, ISD::FEXP, ISD::FEXP2, ISD::FCEIL, ISD::FTRUNC,
2009 ISD::FRINT, ISD::FNEARBYINT, ISD::FROUND, ISD::FFLOOR,
2010 ISD::FMINNUM, ISD::FMAXNUM, ISD::FSINCOS,
2011 // Misc:
Krzysztof Parzyszek046da742016-10-27 14:30:16 +00002012 ISD::BR_CC, ISD::SELECT_CC, ISD::ConstantPool,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002013 // Vector:
2014 ISD::BUILD_VECTOR, ISD::SCALAR_TO_VECTOR,
2015 ISD::EXTRACT_VECTOR_ELT, ISD::INSERT_VECTOR_ELT,
2016 ISD::EXTRACT_SUBVECTOR, ISD::INSERT_SUBVECTOR,
2017 ISD::CONCAT_VECTORS, ISD::VECTOR_SHUFFLE
2018 };
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002019
2020 for (MVT VT : MVT::vector_valuetypes()) {
Benjamin Kramer62460692015-04-25 14:46:53 +00002021 for (unsigned VectExpOp : VectExpOps)
2022 setOperationAction(VectExpOp, VT, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002023
Krzysztof Parzyszeka696b1b2016-09-08 17:42:14 +00002024 // Expand all extending loads and truncating stores:
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002025 for (MVT TargetVT : MVT::vector_valuetypes()) {
Krzysztof Parzyszeka696b1b2016-09-08 17:42:14 +00002026 if (TargetVT == VT)
2027 continue;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002028 setLoadExtAction(ISD::EXTLOAD, TargetVT, VT, Expand);
Krzysztof Parzyszeka696b1b2016-09-08 17:42:14 +00002029 setLoadExtAction(ISD::ZEXTLOAD, TargetVT, VT, Expand);
2030 setLoadExtAction(ISD::SEXTLOAD, TargetVT, VT, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002031 setTruncStoreAction(VT, TargetVT, Expand);
2032 }
2033
Krzysztof Parzyszek046da742016-10-27 14:30:16 +00002034 // Normalize all inputs to SELECT to be vectors of i32.
2035 if (VT.getVectorElementType() != MVT::i32) {
2036 MVT VT32 = MVT::getVectorVT(MVT::i32, VT.getSizeInBits()/32);
2037 setOperationAction(ISD::SELECT, VT, Promote);
2038 AddPromotedToType(ISD::SELECT, VT, VT32);
2039 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002040 setOperationAction(ISD::SRA, VT, Custom);
2041 setOperationAction(ISD::SHL, VT, Custom);
2042 setOperationAction(ISD::SRL, VT, Custom);
2043 }
2044
2045 // Types natively supported:
Benjamin Kramer62460692015-04-25 14:46:53 +00002046 for (MVT NativeVT : {MVT::v2i1, MVT::v4i1, MVT::v8i1, MVT::v32i1, MVT::v64i1,
2047 MVT::v4i8, MVT::v8i8, MVT::v2i16, MVT::v4i16, MVT::v1i32,
2048 MVT::v2i32, MVT::v1i64}) {
2049 setOperationAction(ISD::BUILD_VECTOR, NativeVT, Custom);
2050 setOperationAction(ISD::EXTRACT_VECTOR_ELT, NativeVT, Custom);
2051 setOperationAction(ISD::INSERT_VECTOR_ELT, NativeVT, Custom);
2052 setOperationAction(ISD::EXTRACT_SUBVECTOR, NativeVT, Custom);
2053 setOperationAction(ISD::INSERT_SUBVECTOR, NativeVT, Custom);
2054 setOperationAction(ISD::CONCAT_VECTORS, NativeVT, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002055
Benjamin Kramer62460692015-04-25 14:46:53 +00002056 setOperationAction(ISD::ADD, NativeVT, Legal);
2057 setOperationAction(ISD::SUB, NativeVT, Legal);
2058 setOperationAction(ISD::MUL, NativeVT, Legal);
2059 setOperationAction(ISD::AND, NativeVT, Legal);
2060 setOperationAction(ISD::OR, NativeVT, Legal);
2061 setOperationAction(ISD::XOR, NativeVT, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002062 }
2063
2064 setOperationAction(ISD::SETCC, MVT::v2i16, Custom);
2065 setOperationAction(ISD::VSELECT, MVT::v2i16, Custom);
2066 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
2067 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00002068
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002069 if (UseHVX) {
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002070 if (UseHVXSgl) {
2071 setOperationAction(ISD::CONCAT_VECTORS, MVT::v128i8, Custom);
2072 setOperationAction(ISD::CONCAT_VECTORS, MVT::v64i16, Custom);
2073 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i32, Custom);
2074 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i64, Custom);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002075 // We try to generate the vpack{e/o} instructions. If we fail
2076 // we fall back upon ExpandOp.
2077 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v64i8, Custom);
2078 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v32i16, Custom);
2079 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v64i8, Custom);
2080 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v32i16, Custom);
2081 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v16i32, Custom);
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002082 } else if (UseHVXDbl) {
2083 setOperationAction(ISD::CONCAT_VECTORS, MVT::v256i8, Custom);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002084 setOperationAction(ISD::CONCAT_VECTORS, MVT::v128i16, Custom);
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002085 setOperationAction(ISD::CONCAT_VECTORS, MVT::v64i32, Custom);
2086 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i64, Custom);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002087 // We try to generate the vpack{e/o} instructions. If we fail
2088 // we fall back upon ExpandOp.
2089 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v128i8, Custom);
2090 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v64i16, Custom);
2091 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v4i32, Custom);
2092 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v128i8, Custom);
2093 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v64i16, Custom);
2094 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v32i32, Custom);
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002095 } else {
2096 llvm_unreachable("Unrecognized HVX mode");
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002097 }
2098 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002099 // Subtarget-specific operation actions.
2100 //
2101 if (Subtarget.hasV5TOps()) {
2102 setOperationAction(ISD::FMA, MVT::f64, Expand);
2103 setOperationAction(ISD::FADD, MVT::f64, Expand);
2104 setOperationAction(ISD::FSUB, MVT::f64, Expand);
2105 setOperationAction(ISD::FMUL, MVT::f64, Expand);
2106
Krzysztof Parzyszekbd8ef4b2016-08-19 13:34:31 +00002107 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
2108 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
2109
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002110 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Promote);
2111 setOperationAction(ISD::FP_TO_UINT, MVT::i8, Promote);
2112 setOperationAction(ISD::FP_TO_UINT, MVT::i16, Promote);
2113 setOperationAction(ISD::FP_TO_SINT, MVT::i1, Promote);
2114 setOperationAction(ISD::FP_TO_SINT, MVT::i8, Promote);
2115 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
2116 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote);
2117 setOperationAction(ISD::UINT_TO_FP, MVT::i8, Promote);
2118 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote);
2119 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote);
2120 setOperationAction(ISD::SINT_TO_FP, MVT::i8, Promote);
2121 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002122 } else { // V4
2123 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
2124 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Expand);
2125 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
2126 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
2127 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Expand);
2128 setOperationAction(ISD::FP_TO_SINT, MVT::f32, Expand);
2129 setOperationAction(ISD::FP_EXTEND, MVT::f32, Expand);
2130 setOperationAction(ISD::FP_ROUND, MVT::f64, Expand);
2131 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
2132
2133 setOperationAction(ISD::CTPOP, MVT::i8, Expand);
2134 setOperationAction(ISD::CTPOP, MVT::i16, Expand);
2135 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
2136 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
2137
2138 // Expand these operations for both f32 and f64:
Benjamin Kramer62460692015-04-25 14:46:53 +00002139 for (unsigned FPExpOpV4 :
2140 {ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FABS, ISD::FNEG, ISD::FMA}) {
2141 setOperationAction(FPExpOpV4, MVT::f32, Expand);
2142 setOperationAction(FPExpOpV4, MVT::f64, Expand);
2143 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002144
Benjamin Kramer62460692015-04-25 14:46:53 +00002145 for (ISD::CondCode FPExpCCV4 :
2146 {ISD::SETOEQ, ISD::SETOGT, ISD::SETOLT, ISD::SETOGE, ISD::SETOLE,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002147 ISD::SETUO, ISD::SETO}) {
Benjamin Kramer62460692015-04-25 14:46:53 +00002148 setCondCodeAction(FPExpCCV4, MVT::f32, Expand);
2149 setCondCodeAction(FPExpCCV4, MVT::f64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002150 }
2151 }
2152
2153 // Handling of indexed loads/stores: default is "expand".
2154 //
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +00002155 for (MVT VT : {MVT::i8, MVT::i16, MVT::i32, MVT::i64}) {
2156 setIndexedLoadAction(ISD::POST_INC, VT, Legal);
2157 setIndexedStoreAction(ISD::POST_INC, VT, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002158 }
2159
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +00002160 if (UseHVXSgl) {
2161 for (MVT VT : {MVT::v64i8, MVT::v32i16, MVT::v16i32, MVT::v8i64,
2162 MVT::v128i8, MVT::v64i16, MVT::v32i32, MVT::v16i64}) {
2163 setIndexedLoadAction(ISD::POST_INC, VT, Legal);
2164 setIndexedStoreAction(ISD::POST_INC, VT, Legal);
2165 }
2166 } else if (UseHVXDbl) {
2167 for (MVT VT : {MVT::v128i8, MVT::v64i16, MVT::v32i32, MVT::v16i64,
2168 MVT::v256i8, MVT::v128i16, MVT::v64i32, MVT::v32i64}) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002169 setIndexedLoadAction(ISD::POST_INC, VT, Legal);
2170 setIndexedStoreAction(ISD::POST_INC, VT, Legal);
2171 }
2172 }
2173
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002174 computeRegisterProperties(&HRI);
2175
2176 //
2177 // Library calls for unsupported operations
2178 //
2179 bool FastMath = EnableFastMath;
2180
Benjamin Kramera37c8092015-04-25 14:46:46 +00002181 setLibcallName(RTLIB::SDIV_I32, "__hexagon_divsi3");
2182 setLibcallName(RTLIB::SDIV_I64, "__hexagon_divdi3");
2183 setLibcallName(RTLIB::UDIV_I32, "__hexagon_udivsi3");
2184 setLibcallName(RTLIB::UDIV_I64, "__hexagon_udivdi3");
2185 setLibcallName(RTLIB::SREM_I32, "__hexagon_modsi3");
2186 setLibcallName(RTLIB::SREM_I64, "__hexagon_moddi3");
2187 setLibcallName(RTLIB::UREM_I32, "__hexagon_umodsi3");
2188 setLibcallName(RTLIB::UREM_I64, "__hexagon_umoddi3");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002189
Benjamin Kramera37c8092015-04-25 14:46:46 +00002190 setLibcallName(RTLIB::SINTTOFP_I128_F64, "__hexagon_floattidf");
2191 setLibcallName(RTLIB::SINTTOFP_I128_F32, "__hexagon_floattisf");
2192 setLibcallName(RTLIB::FPTOUINT_F32_I128, "__hexagon_fixunssfti");
2193 setLibcallName(RTLIB::FPTOUINT_F64_I128, "__hexagon_fixunsdfti");
2194 setLibcallName(RTLIB::FPTOSINT_F32_I128, "__hexagon_fixsfti");
2195 setLibcallName(RTLIB::FPTOSINT_F64_I128, "__hexagon_fixdfti");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002196
2197 if (IsV4) {
2198 // Handle single-precision floating point operations on V4.
Benjamin Kramera37c8092015-04-25 14:46:46 +00002199 if (FastMath) {
2200 setLibcallName(RTLIB::ADD_F32, "__hexagon_fast_addsf3");
2201 setLibcallName(RTLIB::SUB_F32, "__hexagon_fast_subsf3");
2202 setLibcallName(RTLIB::MUL_F32, "__hexagon_fast_mulsf3");
2203 setLibcallName(RTLIB::OGT_F32, "__hexagon_fast_gtsf2");
2204 setLibcallName(RTLIB::OLT_F32, "__hexagon_fast_ltsf2");
2205 // Double-precision compares.
2206 setLibcallName(RTLIB::OGT_F64, "__hexagon_fast_gtdf2");
2207 setLibcallName(RTLIB::OLT_F64, "__hexagon_fast_ltdf2");
2208 } else {
2209 setLibcallName(RTLIB::ADD_F32, "__hexagon_addsf3");
2210 setLibcallName(RTLIB::SUB_F32, "__hexagon_subsf3");
2211 setLibcallName(RTLIB::MUL_F32, "__hexagon_mulsf3");
2212 setLibcallName(RTLIB::OGT_F32, "__hexagon_gtsf2");
2213 setLibcallName(RTLIB::OLT_F32, "__hexagon_ltsf2");
2214 // Double-precision compares.
2215 setLibcallName(RTLIB::OGT_F64, "__hexagon_gtdf2");
2216 setLibcallName(RTLIB::OLT_F64, "__hexagon_ltdf2");
2217 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002218 }
2219
2220 // This is the only fast library function for sqrtd.
2221 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00002222 setLibcallName(RTLIB::SQRT_F64, "__hexagon_fast2_sqrtdf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002223
Benjamin Kramera37c8092015-04-25 14:46:46 +00002224 // Prefix is: nothing for "slow-math",
2225 // "fast2_" for V4 fast-math and V5+ fast-math double-precision
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002226 // (actually, keep fast-math and fast-math2 separate for now)
Benjamin Kramera37c8092015-04-25 14:46:46 +00002227 if (FastMath) {
2228 setLibcallName(RTLIB::ADD_F64, "__hexagon_fast_adddf3");
2229 setLibcallName(RTLIB::SUB_F64, "__hexagon_fast_subdf3");
2230 setLibcallName(RTLIB::MUL_F64, "__hexagon_fast_muldf3");
2231 setLibcallName(RTLIB::DIV_F64, "__hexagon_fast_divdf3");
2232 // Calling __hexagon_fast2_divsf3 with fast-math on V5 (ok).
2233 setLibcallName(RTLIB::DIV_F32, "__hexagon_fast_divsf3");
2234 } else {
2235 setLibcallName(RTLIB::ADD_F64, "__hexagon_adddf3");
2236 setLibcallName(RTLIB::SUB_F64, "__hexagon_subdf3");
2237 setLibcallName(RTLIB::MUL_F64, "__hexagon_muldf3");
2238 setLibcallName(RTLIB::DIV_F64, "__hexagon_divdf3");
2239 setLibcallName(RTLIB::DIV_F32, "__hexagon_divsf3");
2240 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002241
2242 if (Subtarget.hasV5TOps()) {
2243 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00002244 setLibcallName(RTLIB::SQRT_F32, "__hexagon_fast2_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002245 else
Benjamin Kramera37c8092015-04-25 14:46:46 +00002246 setLibcallName(RTLIB::SQRT_F32, "__hexagon_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002247 } else {
2248 // V4
Benjamin Kramera37c8092015-04-25 14:46:46 +00002249 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__hexagon_floatsisf");
2250 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__hexagon_floatsidf");
2251 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__hexagon_floatdisf");
2252 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__hexagon_floatdidf");
2253 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__hexagon_floatunsisf");
2254 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__hexagon_floatunsidf");
2255 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__hexagon_floatundisf");
2256 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__hexagon_floatundidf");
2257 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__hexagon_fixunssfsi");
2258 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__hexagon_fixunssfdi");
2259 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__hexagon_fixunsdfsi");
2260 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__hexagon_fixunsdfdi");
2261 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__hexagon_fixsfsi");
2262 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__hexagon_fixsfdi");
2263 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__hexagon_fixdfsi");
2264 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__hexagon_fixdfdi");
2265 setLibcallName(RTLIB::FPEXT_F32_F64, "__hexagon_extendsfdf2");
2266 setLibcallName(RTLIB::FPROUND_F64_F32, "__hexagon_truncdfsf2");
2267 setLibcallName(RTLIB::OEQ_F32, "__hexagon_eqsf2");
2268 setLibcallName(RTLIB::OEQ_F64, "__hexagon_eqdf2");
2269 setLibcallName(RTLIB::OGE_F32, "__hexagon_gesf2");
2270 setLibcallName(RTLIB::OGE_F64, "__hexagon_gedf2");
2271 setLibcallName(RTLIB::OLE_F32, "__hexagon_lesf2");
2272 setLibcallName(RTLIB::OLE_F64, "__hexagon_ledf2");
2273 setLibcallName(RTLIB::UNE_F32, "__hexagon_nesf2");
2274 setLibcallName(RTLIB::UNE_F64, "__hexagon_nedf2");
2275 setLibcallName(RTLIB::UO_F32, "__hexagon_unordsf2");
2276 setLibcallName(RTLIB::UO_F64, "__hexagon_unorddf2");
2277 setLibcallName(RTLIB::O_F32, "__hexagon_unordsf2");
2278 setLibcallName(RTLIB::O_F64, "__hexagon_unorddf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002279 }
2280
2281 // These cause problems when the shift amount is non-constant.
2282 setLibcallName(RTLIB::SHL_I128, nullptr);
2283 setLibcallName(RTLIB::SRL_I128, nullptr);
2284 setLibcallName(RTLIB::SRA_I128, nullptr);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002285}
2286
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002287const char* HexagonTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00002288 switch ((HexagonISD::NodeType)Opcode) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002289 case HexagonISD::ALLOCA: return "HexagonISD::ALLOCA";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002290 case HexagonISD::AT_GOT: return "HexagonISD::AT_GOT";
2291 case HexagonISD::AT_PCREL: return "HexagonISD::AT_PCREL";
2292 case HexagonISD::BARRIER: return "HexagonISD::BARRIER";
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002293 case HexagonISD::CALL: return "HexagonISD::CALL";
2294 case HexagonISD::CALLnr: return "HexagonISD::CALLnr";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002295 case HexagonISD::CALLR: return "HexagonISD::CALLR";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002296 case HexagonISD::COMBINE: return "HexagonISD::COMBINE";
2297 case HexagonISD::CONST32_GP: return "HexagonISD::CONST32_GP";
2298 case HexagonISD::CONST32: return "HexagonISD::CONST32";
2299 case HexagonISD::CP: return "HexagonISD::CP";
2300 case HexagonISD::DCFETCH: return "HexagonISD::DCFETCH";
2301 case HexagonISD::EH_RETURN: return "HexagonISD::EH_RETURN";
2302 case HexagonISD::EXTRACTU: return "HexagonISD::EXTRACTU";
2303 case HexagonISD::EXTRACTURP: return "HexagonISD::EXTRACTURP";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002304 case HexagonISD::INSERT: return "HexagonISD::INSERT";
2305 case HexagonISD::INSERTRP: return "HexagonISD::INSERTRP";
2306 case HexagonISD::JT: return "HexagonISD::JT";
2307 case HexagonISD::PACKHL: return "HexagonISD::PACKHL";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002308 case HexagonISD::RET_FLAG: return "HexagonISD::RET_FLAG";
2309 case HexagonISD::SHUFFEB: return "HexagonISD::SHUFFEB";
2310 case HexagonISD::SHUFFEH: return "HexagonISD::SHUFFEH";
2311 case HexagonISD::SHUFFOB: return "HexagonISD::SHUFFOB";
2312 case HexagonISD::SHUFFOH: return "HexagonISD::SHUFFOH";
2313 case HexagonISD::TC_RETURN: return "HexagonISD::TC_RETURN";
2314 case HexagonISD::VCMPBEQ: return "HexagonISD::VCMPBEQ";
2315 case HexagonISD::VCMPBGT: return "HexagonISD::VCMPBGT";
2316 case HexagonISD::VCMPBGTU: return "HexagonISD::VCMPBGTU";
2317 case HexagonISD::VCMPHEQ: return "HexagonISD::VCMPHEQ";
2318 case HexagonISD::VCMPHGT: return "HexagonISD::VCMPHGT";
2319 case HexagonISD::VCMPHGTU: return "HexagonISD::VCMPHGTU";
2320 case HexagonISD::VCMPWEQ: return "HexagonISD::VCMPWEQ";
2321 case HexagonISD::VCMPWGT: return "HexagonISD::VCMPWGT";
2322 case HexagonISD::VCMPWGTU: return "HexagonISD::VCMPWGTU";
Krzysztof Parzyszekc168c012015-12-03 16:47:20 +00002323 case HexagonISD::VCOMBINE: return "HexagonISD::VCOMBINE";
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002324 case HexagonISD::VPACK: return "HexagonISD::VPACK";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002325 case HexagonISD::VSHLH: return "HexagonISD::VSHLH";
2326 case HexagonISD::VSHLW: return "HexagonISD::VSHLW";
2327 case HexagonISD::VSPLATB: return "HexagonISD::VSPLTB";
2328 case HexagonISD::VSPLATH: return "HexagonISD::VSPLATH";
2329 case HexagonISD::VSRAH: return "HexagonISD::VSRAH";
2330 case HexagonISD::VSRAW: return "HexagonISD::VSRAW";
2331 case HexagonISD::VSRLH: return "HexagonISD::VSRLH";
2332 case HexagonISD::VSRLW: return "HexagonISD::VSRLW";
2333 case HexagonISD::VSXTBH: return "HexagonISD::VSXTBH";
2334 case HexagonISD::VSXTBW: return "HexagonISD::VSXTBW";
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +00002335 case HexagonISD::READCYCLE: return "HexagonISD::READCYCLE";
Matthias Braund04893f2015-05-07 21:33:59 +00002336 case HexagonISD::OP_END: break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002337 }
Matthias Braund04893f2015-05-07 21:33:59 +00002338 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002339}
2340
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002341bool HexagonTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002342 EVT MTy1 = EVT::getEVT(Ty1);
2343 EVT MTy2 = EVT::getEVT(Ty2);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002344 if (!MTy1.isSimple() || !MTy2.isSimple())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002345 return false;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002346 return (MTy1.getSimpleVT() == MVT::i64) && (MTy2.getSimpleVT() == MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002347}
2348
2349bool HexagonTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002350 if (!VT1.isSimple() || !VT2.isSimple())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002351 return false;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002352 return (VT1.getSimpleVT() == MVT::i64) && (VT2.getSimpleVT() == MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002353}
2354
Krzysztof Parzyszekbd8ef4b2016-08-19 13:34:31 +00002355bool HexagonTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
2356 return isOperationLegalOrCustom(ISD::FMA, VT);
2357}
2358
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002359// Should we expand the build vector with shuffles?
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00002360bool HexagonTargetLowering::shouldExpandBuildVectorWithShuffles(EVT VT,
2361 unsigned DefinedValues) const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002362 // Hexagon vector shuffle operates on element sizes of bytes or halfwords
2363 EVT EltVT = VT.getVectorElementType();
2364 int EltBits = EltVT.getSizeInBits();
2365 if ((EltBits != 8) && (EltBits != 16))
2366 return false;
2367
2368 return TargetLowering::shouldExpandBuildVectorWithShuffles(VT, DefinedValues);
2369}
2370
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00002371static StridedLoadKind isStridedLoad(const ArrayRef<int> &Mask) {
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002372 int even_start = -2;
2373 int odd_start = -1;
2374 size_t mask_len = Mask.size();
2375 for (auto idx : Mask) {
2376 if ((idx - even_start) == 2)
2377 even_start = idx;
2378 else
2379 break;
2380 }
2381 if (even_start == (int)(mask_len * 2) - 2)
2382 return StridedLoadKind::Even;
2383 for (auto idx : Mask) {
2384 if ((idx - odd_start) == 2)
2385 odd_start = idx;
2386 else
2387 break;
2388 }
2389 if (odd_start == (int)(mask_len * 2) - 1)
2390 return StridedLoadKind::Odd;
2391
2392 return StridedLoadKind::NoPattern;
2393}
2394
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00002395bool HexagonTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &Mask,
2396 EVT VT) const {
2397 if (Subtarget.useHVXOps())
2398 return isStridedLoad(Mask) != StridedLoadKind::NoPattern;
2399 return true;
2400}
2401
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002402// Lower a vector shuffle (V1, V2, V3). V1 and V2 are the two vectors
2403// to select data from, V3 is the permutation.
2404SDValue
2405HexagonTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG)
2406 const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002407 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
2408 SDValue V1 = Op.getOperand(0);
2409 SDValue V2 = Op.getOperand(1);
2410 SDLoc dl(Op);
2411 EVT VT = Op.getValueType();
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002412 bool UseHVX = Subtarget.useHVXOps();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002413
Sanjay Patel57195842016-03-14 17:28:46 +00002414 if (V2.isUndef())
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002415 V2 = V1;
2416
2417 if (SVN->isSplat()) {
2418 int Lane = SVN->getSplatIndex();
2419 if (Lane == -1) Lane = 0;
2420
2421 // Test if V1 is a SCALAR_TO_VECTOR.
2422 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR)
2423 return createSplat(DAG, dl, VT, V1.getOperand(0));
2424
2425 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
2426 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
2427 // reaches it).
2428 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
2429 !isa<ConstantSDNode>(V1.getOperand(0))) {
2430 bool IsScalarToVector = true;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00002431 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i) {
Sanjay Patel75068522016-03-14 18:09:43 +00002432 if (!V1.getOperand(i).isUndef()) {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002433 IsScalarToVector = false;
2434 break;
2435 }
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00002436 }
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002437 if (IsScalarToVector)
2438 return createSplat(DAG, dl, VT, V1.getOperand(0));
2439 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002440 return createSplat(DAG, dl, VT, DAG.getConstant(Lane, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002441 }
2442
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002443 if (UseHVX) {
2444 ArrayRef<int> Mask = SVN->getMask();
2445 size_t MaskLen = Mask.size();
Sanjay Patel1ed771f2016-09-14 16:37:15 +00002446 int ElemSizeInBits = VT.getScalarSizeInBits();
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002447 if ((Subtarget.useHVXSglOps() && (ElemSizeInBits * MaskLen) == 64 * 8) ||
2448 (Subtarget.useHVXDblOps() && (ElemSizeInBits * MaskLen) == 128 * 8)) {
2449 // Return 1 for odd and 2 of even
2450 StridedLoadKind Pattern = isStridedLoad(Mask);
2451
2452 if (Pattern == StridedLoadKind::NoPattern)
2453 return SDValue();
2454
2455 SDValue Vec0 = Op.getOperand(0);
2456 SDValue Vec1 = Op.getOperand(1);
2457 SDValue StridePattern = DAG.getConstant(Pattern, dl, MVT::i32);
2458 SDValue Ops[] = { Vec1, Vec0, StridePattern };
2459 return DAG.getNode(HexagonISD::VPACK, dl, VT, Ops);
2460 }
2461 // We used to assert in the "else" part here, but that is bad for Halide
2462 // Halide creates intermediate double registers by interleaving two
2463 // concatenated vector registers. The interleaving requires vector_shuffle
2464 // nodes and we shouldn't barf on a double register result of a
2465 // vector_shuffle because it is most likely an intermediate result.
2466 }
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002467 // FIXME: We need to support more general vector shuffles. See
2468 // below the comment from the ARM backend that deals in the general
2469 // case with the vector shuffles. For now, let expand handle these.
2470 return SDValue();
2471
2472 // If the shuffle is not directly supported and it has 4 elements, use
2473 // the PerfectShuffle-generated table to synthesize it from other shuffles.
2474}
2475
2476// If BUILD_VECTOR has same base element repeated several times,
2477// report true.
2478static bool isCommonSplatElement(BuildVectorSDNode *BVN) {
2479 unsigned NElts = BVN->getNumOperands();
2480 SDValue V0 = BVN->getOperand(0);
2481
2482 for (unsigned i = 1, e = NElts; i != e; ++i) {
2483 if (BVN->getOperand(i) != V0)
2484 return false;
2485 }
2486 return true;
2487}
2488
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002489// Lower a vector shift. Try to convert
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002490// <VT> = SHL/SRA/SRL <VT> by <VT> to Hexagon specific
2491// <VT> = SHL/SRA/SRL <VT> by <IT/i32>.
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002492SDValue
2493HexagonTargetLowering::LowerVECTOR_SHIFT(SDValue Op, SelectionDAG &DAG) const {
Eugene Zelenko58655bb2016-12-17 01:09:05 +00002494 BuildVectorSDNode *BVN = nullptr;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002495 SDValue V1 = Op.getOperand(0);
2496 SDValue V2 = Op.getOperand(1);
2497 SDValue V3;
2498 SDLoc dl(Op);
2499 EVT VT = Op.getValueType();
2500
2501 if ((BVN = dyn_cast<BuildVectorSDNode>(V1.getNode())) &&
2502 isCommonSplatElement(BVN))
2503 V3 = V2;
2504 else if ((BVN = dyn_cast<BuildVectorSDNode>(V2.getNode())) &&
2505 isCommonSplatElement(BVN))
2506 V3 = V1;
2507 else
2508 return SDValue();
2509
2510 SDValue CommonSplat = BVN->getOperand(0);
2511 SDValue Result;
2512
2513 if (VT.getSimpleVT() == MVT::v4i16) {
2514 switch (Op.getOpcode()) {
2515 case ISD::SRA:
2516 Result = DAG.getNode(HexagonISD::VSRAH, dl, VT, V3, CommonSplat);
2517 break;
2518 case ISD::SHL:
2519 Result = DAG.getNode(HexagonISD::VSHLH, dl, VT, V3, CommonSplat);
2520 break;
2521 case ISD::SRL:
2522 Result = DAG.getNode(HexagonISD::VSRLH, dl, VT, V3, CommonSplat);
2523 break;
2524 default:
2525 return SDValue();
2526 }
2527 } else if (VT.getSimpleVT() == MVT::v2i32) {
2528 switch (Op.getOpcode()) {
2529 case ISD::SRA:
2530 Result = DAG.getNode(HexagonISD::VSRAW, dl, VT, V3, CommonSplat);
2531 break;
2532 case ISD::SHL:
2533 Result = DAG.getNode(HexagonISD::VSHLW, dl, VT, V3, CommonSplat);
2534 break;
2535 case ISD::SRL:
2536 Result = DAG.getNode(HexagonISD::VSRLW, dl, VT, V3, CommonSplat);
2537 break;
2538 default:
2539 return SDValue();
2540 }
2541 } else {
2542 return SDValue();
2543 }
2544
2545 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
2546}
2547
2548SDValue
2549HexagonTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
2550 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
2551 SDLoc dl(Op);
2552 EVT VT = Op.getValueType();
2553
2554 unsigned Size = VT.getSizeInBits();
2555
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002556 // Only handle vectors of 64 bits or shorter.
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002557 if (Size > 64)
2558 return SDValue();
2559
2560 APInt APSplatBits, APSplatUndef;
2561 unsigned SplatBitSize;
2562 bool HasAnyUndefs;
2563 unsigned NElts = BVN->getNumOperands();
2564
2565 // Try to generate a SPLAT instruction.
2566 if ((VT.getSimpleVT() == MVT::v4i8 || VT.getSimpleVT() == MVT::v4i16) &&
2567 (BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
2568 HasAnyUndefs, 0, true) && SplatBitSize <= 16)) {
2569 unsigned SplatBits = APSplatBits.getZExtValue();
2570 int32_t SextVal = ((int32_t) (SplatBits << (32 - SplatBitSize)) >>
2571 (32 - SplatBitSize));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002572 return createSplat(DAG, dl, VT, DAG.getConstant(SextVal, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002573 }
2574
2575 // Try to generate COMBINE to build v2i32 vectors.
2576 if (VT.getSimpleVT() == MVT::v2i32) {
2577 SDValue V0 = BVN->getOperand(0);
2578 SDValue V1 = BVN->getOperand(1);
2579
Sanjay Patel57195842016-03-14 17:28:46 +00002580 if (V0.isUndef())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002581 V0 = DAG.getConstant(0, dl, MVT::i32);
Sanjay Patel57195842016-03-14 17:28:46 +00002582 if (V1.isUndef())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002583 V1 = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002584
2585 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(V0);
2586 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(V1);
2587 // If the element isn't a constant, it is in a register:
2588 // generate a COMBINE Register Register instruction.
2589 if (!C0 || !C1)
2590 return DAG.getNode(HexagonISD::COMBINE, dl, VT, V1, V0);
2591
2592 // If one of the operands is an 8 bit integer constant, generate
2593 // a COMBINE Immediate Immediate instruction.
2594 if (isInt<8>(C0->getSExtValue()) ||
2595 isInt<8>(C1->getSExtValue()))
2596 return DAG.getNode(HexagonISD::COMBINE, dl, VT, V1, V0);
2597 }
2598
2599 // Try to generate a S2_packhl to build v2i16 vectors.
2600 if (VT.getSimpleVT() == MVT::v2i16) {
2601 for (unsigned i = 0, e = NElts; i != e; ++i) {
Sanjay Patel57195842016-03-14 17:28:46 +00002602 if (BVN->getOperand(i).isUndef())
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002603 continue;
2604 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(BVN->getOperand(i));
2605 // If the element isn't a constant, it is in a register:
2606 // generate a S2_packhl instruction.
2607 if (!Cst) {
2608 SDValue pack = DAG.getNode(HexagonISD::PACKHL, dl, MVT::v4i16,
2609 BVN->getOperand(1), BVN->getOperand(0));
2610
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00002611 return DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::v2i16,
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002612 pack);
2613 }
2614 }
2615 }
2616
2617 // In the general case, generate a CONST32 or a CONST64 for constant vectors,
2618 // and insert_vector_elt for all the other cases.
2619 uint64_t Res = 0;
2620 unsigned EltSize = Size / NElts;
2621 SDValue ConstVal;
2622 uint64_t Mask = ~uint64_t(0ULL) >> (64 - EltSize);
2623 bool HasNonConstantElements = false;
2624
2625 for (unsigned i = 0, e = NElts; i != e; ++i) {
2626 // LLVM's BUILD_VECTOR operands are in Little Endian mode, whereas Hexagon's
2627 // combine, const64, etc. are Big Endian.
2628 unsigned OpIdx = NElts - i - 1;
2629 SDValue Operand = BVN->getOperand(OpIdx);
Sanjay Patel57195842016-03-14 17:28:46 +00002630 if (Operand.isUndef())
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002631 continue;
2632
2633 int64_t Val = 0;
2634 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Operand))
2635 Val = Cst->getSExtValue();
2636 else
2637 HasNonConstantElements = true;
2638
2639 Val &= Mask;
2640 Res = (Res << EltSize) | Val;
2641 }
2642
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002643 if (Size > 64)
2644 return SDValue();
2645
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002646 if (Size == 64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002647 ConstVal = DAG.getConstant(Res, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002648 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002649 ConstVal = DAG.getConstant(Res, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002650
2651 // When there are non constant operands, add them with INSERT_VECTOR_ELT to
2652 // ConstVal, the constant part of the vector.
2653 if (HasNonConstantElements) {
2654 EVT EltVT = VT.getVectorElementType();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002655 SDValue Width = DAG.getConstant(EltVT.getSizeInBits(), dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002656 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002657 DAG.getConstant(32, dl, MVT::i64));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002658
2659 for (unsigned i = 0, e = NElts; i != e; ++i) {
2660 // LLVM's BUILD_VECTOR operands are in Little Endian mode, whereas Hexagon
2661 // is Big Endian.
2662 unsigned OpIdx = NElts - i - 1;
2663 SDValue Operand = BVN->getOperand(OpIdx);
Benjamin Kramer619c4e52015-04-10 11:24:51 +00002664 if (isa<ConstantSDNode>(Operand))
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002665 // This operand is already in ConstVal.
2666 continue;
2667
2668 if (VT.getSizeInBits() == 64 &&
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00002669 Operand.getValueSizeInBits() == 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002670 SDValue C = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002671 Operand = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, Operand);
2672 }
2673
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002674 SDValue Idx = DAG.getConstant(OpIdx, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002675 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i64, Idx, Width);
2676 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2677 const SDValue Ops[] = {ConstVal, Operand, Combined};
2678
2679 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002680 ConstVal = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002681 else
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002682 ConstVal = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002683 }
2684 }
2685
2686 return DAG.getNode(ISD::BITCAST, dl, VT, ConstVal);
2687}
2688
2689SDValue
2690HexagonTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
2691 SelectionDAG &DAG) const {
2692 SDLoc dl(Op);
Krzysztof Parzyszekc168c012015-12-03 16:47:20 +00002693 bool UseHVX = Subtarget.useHVXOps();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002694 EVT VT = Op.getValueType();
2695 unsigned NElts = Op.getNumOperands();
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002696 SDValue Vec0 = Op.getOperand(0);
2697 EVT VecVT = Vec0.getValueType();
2698 unsigned Width = VecVT.getSizeInBits();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002699
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002700 if (NElts == 2) {
2701 MVT ST = VecVT.getSimpleVT();
2702 // We are trying to concat two v2i16 to a single v4i16, or two v4i8
2703 // into a single v8i8.
2704 if (ST == MVT::v2i16 || ST == MVT::v4i8)
2705 return DAG.getNode(HexagonISD::COMBINE, dl, VT, Op.getOperand(1), Vec0);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002706
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002707 if (UseHVX) {
2708 assert((Width == 64*8 && Subtarget.useHVXSglOps()) ||
2709 (Width == 128*8 && Subtarget.useHVXDblOps()));
2710 SDValue Vec1 = Op.getOperand(1);
2711 MVT OpTy = Subtarget.useHVXSglOps() ? MVT::v16i32 : MVT::v32i32;
2712 MVT ReTy = Subtarget.useHVXSglOps() ? MVT::v32i32 : MVT::v64i32;
2713 SDValue B0 = DAG.getNode(ISD::BITCAST, dl, OpTy, Vec0);
2714 SDValue B1 = DAG.getNode(ISD::BITCAST, dl, OpTy, Vec1);
2715 SDValue VC = DAG.getNode(HexagonISD::VCOMBINE, dl, ReTy, B1, B0);
2716 return DAG.getNode(ISD::BITCAST, dl, VT, VC);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002717 }
2718 }
2719
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002720 if (VT.getSizeInBits() != 32 && VT.getSizeInBits() != 64)
2721 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002722
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002723 SDValue C0 = DAG.getConstant(0, dl, MVT::i64);
2724 SDValue C32 = DAG.getConstant(32, dl, MVT::i64);
2725 SDValue W = DAG.getConstant(Width, dl, MVT::i64);
2726 // Create the "width" part of the argument to insert_rp/insertp_rp.
2727 SDValue S = DAG.getNode(ISD::SHL, dl, MVT::i64, W, C32);
2728 SDValue V = C0;
2729
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002730 for (unsigned i = 0, e = NElts; i != e; ++i) {
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002731 unsigned N = NElts-i-1;
2732 SDValue OpN = Op.getOperand(N);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002733
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00002734 if (VT.getSizeInBits() == 64 && OpN.getValueSizeInBits() == 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002735 SDValue C = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002736 OpN = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, OpN);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002737 }
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002738 SDValue Idx = DAG.getConstant(N, dl, MVT::i64);
2739 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i64, Idx, W);
2740 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, S, Offset);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002741 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002742 V = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, {V, OpN, Or});
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002743 else if (VT.getSizeInBits() == 64)
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002744 V = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, {V, OpN, Or});
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002745 else
2746 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002747 }
2748
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002749 return DAG.getNode(ISD::BITCAST, dl, VT, V);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002750}
2751
2752SDValue
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002753HexagonTargetLowering::LowerEXTRACT_SUBVECTOR_HVX(SDValue Op,
2754 SelectionDAG &DAG) const {
2755 EVT VT = Op.getOperand(0).getValueType();
2756 SDLoc dl(Op);
2757 bool UseHVX = Subtarget.useHVXOps();
2758 bool UseHVXSgl = Subtarget.useHVXSglOps();
2759 // Just in case...
2760
2761 if (!VT.isVector() || !UseHVX)
2762 return SDValue();
2763
2764 EVT ResVT = Op.getValueType();
2765 unsigned ResSize = ResVT.getSizeInBits();
2766 unsigned VectorSizeInBits = UseHVXSgl ? (64 * 8) : (128 * 8);
2767 unsigned OpSize = VT.getSizeInBits();
2768
2769 // We deal only with cases where the result is the vector size
2770 // and the vector operand is a double register.
2771 if (!(ResVT.isByteSized() && ResSize == VectorSizeInBits) ||
2772 !(VT.isByteSized() && OpSize == 2 * VectorSizeInBits))
2773 return SDValue();
2774
2775 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Op.getOperand(1));
2776 if (!Cst)
2777 return SDValue();
2778 unsigned Val = Cst->getZExtValue();
2779
2780 // These two will get lowered to an appropriate EXTRACT_SUBREG in ISel.
2781 if (Val == 0) {
2782 SDValue Vec = Op.getOperand(0);
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00002783 return DAG.getTargetExtractSubreg(Hexagon::vsub_lo, dl, ResVT, Vec);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002784 }
2785
2786 if (ResVT.getVectorNumElements() == Val) {
2787 SDValue Vec = Op.getOperand(0);
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00002788 return DAG.getTargetExtractSubreg(Hexagon::vsub_hi, dl, ResVT, Vec);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002789 }
2790
2791 return SDValue();
2792}
2793
2794SDValue
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002795HexagonTargetLowering::LowerEXTRACT_VECTOR(SDValue Op,
2796 SelectionDAG &DAG) const {
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002797 // If we are dealing with EXTRACT_SUBVECTOR on a HVX type, we may
2798 // be able to simplify it to an EXTRACT_SUBREG.
2799 if (Op.getOpcode() == ISD::EXTRACT_SUBVECTOR && Subtarget.useHVXOps() &&
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00002800 isHvxVectorType(Op.getValueType().getSimpleVT()))
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002801 return LowerEXTRACT_SUBVECTOR_HVX(Op, DAG);
2802
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002803 EVT VT = Op.getValueType();
2804 int VTN = VT.isVector() ? VT.getVectorNumElements() : 1;
2805 SDLoc dl(Op);
2806 SDValue Idx = Op.getOperand(1);
2807 SDValue Vec = Op.getOperand(0);
2808 EVT VecVT = Vec.getValueType();
2809 EVT EltVT = VecVT.getVectorElementType();
2810 int EltSize = EltVT.getSizeInBits();
2811 SDValue Width = DAG.getConstant(Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT ?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002812 EltSize : VTN * EltSize, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002813
2814 // Constant element number.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002815 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Idx)) {
2816 uint64_t X = CI->getZExtValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002817 SDValue Offset = DAG.getConstant(X * EltSize, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002818 const SDValue Ops[] = {Vec, Width, Offset};
2819
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002820 ConstantSDNode *CW = dyn_cast<ConstantSDNode>(Width);
2821 assert(CW && "Non constant width in LowerEXTRACT_VECTOR");
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002822
2823 SDValue N;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002824 MVT SVT = VecVT.getSimpleVT();
2825 uint64_t W = CW->getZExtValue();
2826
2827 if (W == 32) {
2828 // Translate this node into EXTRACT_SUBREG.
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00002829 unsigned Subreg = (X == 0) ? Hexagon::isub_lo : 0;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002830
2831 if (X == 0)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00002832 Subreg = Hexagon::isub_lo;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002833 else if (SVT == MVT::v2i32 && X == 1)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00002834 Subreg = Hexagon::isub_hi;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002835 else if (SVT == MVT::v4i16 && X == 2)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00002836 Subreg = Hexagon::isub_hi;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002837 else if (SVT == MVT::v8i8 && X == 4)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00002838 Subreg = Hexagon::isub_hi;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002839 else
2840 llvm_unreachable("Bad offset");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002841 N = DAG.getTargetExtractSubreg(Subreg, dl, MVT::i32, Vec);
2842
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002843 } else if (SVT.getSizeInBits() == 32) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002844 N = DAG.getNode(HexagonISD::EXTRACTU, dl, MVT::i32, Ops);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002845 } else if (SVT.getSizeInBits() == 64) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002846 N = DAG.getNode(HexagonISD::EXTRACTU, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002847 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00002848 N = DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, N);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002849 } else
2850 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002851
2852 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2853 }
2854
2855 // Variable element number.
2856 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i32, Idx,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002857 DAG.getConstant(EltSize, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002858 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002859 DAG.getConstant(32, dl, MVT::i64));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002860 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2861
2862 const SDValue Ops[] = {Vec, Combined};
2863
2864 SDValue N;
2865 if (VecVT.getSizeInBits() == 32) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002866 N = DAG.getNode(HexagonISD::EXTRACTURP, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002867 } else {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002868 N = DAG.getNode(HexagonISD::EXTRACTURP, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002869 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00002870 N = DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, N);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002871 }
2872 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2873}
2874
2875SDValue
2876HexagonTargetLowering::LowerINSERT_VECTOR(SDValue Op,
2877 SelectionDAG &DAG) const {
2878 EVT VT = Op.getValueType();
2879 int VTN = VT.isVector() ? VT.getVectorNumElements() : 1;
2880 SDLoc dl(Op);
2881 SDValue Vec = Op.getOperand(0);
2882 SDValue Val = Op.getOperand(1);
2883 SDValue Idx = Op.getOperand(2);
2884 EVT VecVT = Vec.getValueType();
2885 EVT EltVT = VecVT.getVectorElementType();
2886 int EltSize = EltVT.getSizeInBits();
2887 SDValue Width = DAG.getConstant(Op.getOpcode() == ISD::INSERT_VECTOR_ELT ?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002888 EltSize : VTN * EltSize, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002889
2890 if (ConstantSDNode *C = cast<ConstantSDNode>(Idx)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002891 SDValue Offset = DAG.getConstant(C->getSExtValue() * EltSize, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002892 const SDValue Ops[] = {Vec, Val, Width, Offset};
2893
2894 SDValue N;
2895 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002896 N = DAG.getNode(HexagonISD::INSERT, dl, MVT::i32, Ops);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002897 else if (VT.getSizeInBits() == 64)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002898 N = DAG.getNode(HexagonISD::INSERT, dl, MVT::i64, Ops);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002899 else
2900 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002901
2902 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2903 }
2904
2905 // Variable element number.
2906 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i32, Idx,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002907 DAG.getConstant(EltSize, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002908 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002909 DAG.getConstant(32, dl, MVT::i64));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002910 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2911
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00002912 if (VT.getSizeInBits() == 64 && Val.getValueSizeInBits() == 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002913 SDValue C = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002914 Val = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, Val);
2915 }
2916
2917 const SDValue Ops[] = {Vec, Val, Combined};
2918
2919 SDValue N;
2920 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002921 N = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, Ops);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002922 else if (VT.getSizeInBits() == 64)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002923 N = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, Ops);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002924 else
2925 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002926
2927 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2928}
2929
Tim Northovera4415852013-08-06 09:12:35 +00002930bool
2931HexagonTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
2932 // Assuming the caller does not have either a signext or zeroext modifier, and
2933 // only one value is accepted, any reasonable truncation is allowed.
2934 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
2935 return false;
2936
2937 // FIXME: in principle up to 64-bit could be made safe, but it would be very
2938 // fragile at the moment: any support for multiple value returns would be
2939 // liable to disallow tail calls involving i64 -> iN truncation in many cases.
2940 return Ty1->getPrimitiveSizeInBits() <= 32;
2941}
2942
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002943SDValue
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002944HexagonTargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
2945 SDValue Chain = Op.getOperand(0);
2946 SDValue Offset = Op.getOperand(1);
2947 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002948 SDLoc dl(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002949 auto PtrVT = getPointerTy(DAG.getDataLayout());
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002950
2951 // Mark function as containing a call to EH_RETURN.
2952 HexagonMachineFunctionInfo *FuncInfo =
2953 DAG.getMachineFunction().getInfo<HexagonMachineFunctionInfo>();
2954 FuncInfo->setHasEHReturn();
2955
2956 unsigned OffsetReg = Hexagon::R28;
2957
Mehdi Amini44ede332015-07-09 02:09:04 +00002958 SDValue StoreAddr =
2959 DAG.getNode(ISD::ADD, dl, PtrVT, DAG.getRegister(Hexagon::R30, PtrVT),
2960 DAG.getIntPtrConstant(4, dl));
Justin Lebar9c375812016-07-15 18:27:10 +00002961 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo());
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002962 Chain = DAG.getCopyToReg(Chain, dl, OffsetReg, Offset);
2963
2964 // Not needed we already use it as explict input to EH_RETURN.
2965 // MF.getRegInfo().addLiveOut(OffsetReg);
2966
2967 return DAG.getNode(HexagonISD::EH_RETURN, dl, MVT::Other, Chain);
2968}
2969
2970SDValue
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002971HexagonTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002972 unsigned Opc = Op.getOpcode();
2973 switch (Opc) {
2974 default:
2975#ifndef NDEBUG
2976 Op.getNode()->dumpr(&DAG);
2977 if (Opc > HexagonISD::OP_BEGIN && Opc < HexagonISD::OP_END)
2978 errs() << "Check for a non-legal type in this operation\n";
2979#endif
2980 llvm_unreachable("Should not custom lower this!");
2981 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
2982 case ISD::INSERT_SUBVECTOR: return LowerINSERT_VECTOR(Op, DAG);
2983 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR(Op, DAG);
2984 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_VECTOR(Op, DAG);
2985 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR(Op, DAG);
2986 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2987 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002988 case ISD::SRA:
2989 case ISD::SHL:
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002990 case ISD::SRL: return LowerVECTOR_SHIFT(Op, DAG);
2991 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002992 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002993 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
2994 // Frame & Return address. Currently unimplemented.
2995 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
2996 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00002997 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002998 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
2999 case ISD::GlobalAddress: return LowerGLOBALADDRESS(Op, DAG);
3000 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00003001 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00003002 case ISD::VASTART: return LowerVASTART(Op, DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00003003 // Custom lower some vector loads.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00003004 case ISD::LOAD: return LowerLOAD(Op, DAG);
3005 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
3006 case ISD::SETCC: return LowerSETCC(Op, DAG);
3007 case ISD::VSELECT: return LowerVSELECT(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00003008 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00003009 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00003010 case ISD::INLINEASM: return LowerINLINEASM(Op, DAG);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00003011 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG);
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +00003012 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003013 }
3014}
3015
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00003016/// Returns relocation base for the given PIC jumptable.
3017SDValue
3018HexagonTargetLowering::getPICJumpTableRelocBase(SDValue Table,
3019 SelectionDAG &DAG) const {
3020 int Idx = cast<JumpTableSDNode>(Table)->getIndex();
3021 EVT VT = Table.getValueType();
3022 SDValue T = DAG.getTargetJumpTable(Idx, VT, HexagonII::MO_PCREL);
3023 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Table), VT, T);
3024}
3025
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003026//===----------------------------------------------------------------------===//
3027// Inline Assembly Support
3028//===----------------------------------------------------------------------===//
3029
Krzysztof Parzyszekca3b5322016-05-18 14:34:51 +00003030TargetLowering::ConstraintType
3031HexagonTargetLowering::getConstraintType(StringRef Constraint) const {
3032 if (Constraint.size() == 1) {
3033 switch (Constraint[0]) {
3034 case 'q':
3035 case 'v':
3036 if (Subtarget.useHVXOps())
3037 return C_Register;
3038 break;
3039 }
3040 }
3041 return TargetLowering::getConstraintType(Constraint);
3042}
3043
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003044std::pair<unsigned, const TargetRegisterClass*>
Eric Christopher11e4df72015-02-26 22:38:43 +00003045HexagonTargetLowering::getRegForInlineAsmConstraint(
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003046 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003047 bool UseHVX = Subtarget.useHVXOps(), UseHVXDbl = Subtarget.useHVXDblOps();
3048
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003049 if (Constraint.size() == 1) {
3050 switch (Constraint[0]) {
3051 case 'r': // R0-R31
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003052 switch (VT.SimpleTy) {
3053 default:
3054 llvm_unreachable("getRegForInlineAsmConstraint Unhandled data type");
3055 case MVT::i1:
3056 case MVT::i8:
3057 case MVT::i16:
3058 case MVT::i32:
3059 case MVT::f32:
3060 return std::make_pair(0U, &Hexagon::IntRegsRegClass);
3061 case MVT::i64:
3062 case MVT::f64:
3063 return std::make_pair(0U, &Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003064 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003065 case 'q': // q0-q3
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00003066 switch (VT.getSizeInBits()) {
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003067 default:
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00003068 llvm_unreachable("getRegForInlineAsmConstraint Unhandled vector size");
3069 case 512:
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003070 return std::make_pair(0U, &Hexagon::VecPredRegsRegClass);
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00003071 case 1024:
3072 return std::make_pair(0U, &Hexagon::VecPredRegs128BRegClass);
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003073 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003074 case 'v': // V0-V31
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00003075 switch (VT.getSizeInBits()) {
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003076 default:
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00003077 llvm_unreachable("getRegForInlineAsmConstraint Unhandled vector size");
3078 case 512:
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003079 return std::make_pair(0U, &Hexagon::VectorRegsRegClass);
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00003080 case 1024:
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003081 if (Subtarget.hasV60TOps() && UseHVX && UseHVXDbl)
3082 return std::make_pair(0U, &Hexagon::VectorRegs128BRegClass);
3083 return std::make_pair(0U, &Hexagon::VecDblRegsRegClass);
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00003084 case 2048:
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003085 return std::make_pair(0U, &Hexagon::VecDblRegs128BRegClass);
3086 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003087
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003088 default:
Craig Toppere55c5562012-02-07 02:50:20 +00003089 llvm_unreachable("Unknown asm register class");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003090 }
3091 }
3092
Eric Christopher11e4df72015-02-26 22:38:43 +00003093 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003094}
3095
Sirish Pande69295b82012-05-10 20:20:25 +00003096/// isFPImmLegal - Returns true if the target can instruction select the
3097/// specified FP immediate natively. If false, the legalizer will
3098/// materialize the FP immediate as a load from a constant pool.
3099bool HexagonTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00003100 return Subtarget.hasV5TOps();
Sirish Pande69295b82012-05-10 20:20:25 +00003101}
3102
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003103/// isLegalAddressingMode - Return true if the addressing mode represented by
3104/// AM is legal for this target, for a load/store of the specified type.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +00003105bool HexagonTargetLowering::isLegalAddressingMode(const DataLayout &DL,
3106 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +00003107 unsigned AS) const {
Krzysztof Parzyszeked4e7822016-08-03 15:06:18 +00003108 if (Ty->isSized()) {
3109 // When LSR detects uses of the same base address to access different
3110 // types (e.g. unions), it will assume a conservative type for these
3111 // uses:
3112 // LSR Use: Kind=Address of void in addrspace(4294967295), ...
3113 // The type Ty passed here would then be "void". Skip the alignment
3114 // checks, but do not return false right away, since that confuses
3115 // LSR into crashing.
3116 unsigned A = DL.getABITypeAlignment(Ty);
3117 // The base offset must be a multiple of the alignment.
3118 if ((AM.BaseOffs % A) != 0)
3119 return false;
3120 // The shifted offset must fit in 11 bits.
3121 if (!isInt<11>(AM.BaseOffs >> Log2_32(A)))
3122 return false;
3123 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003124
3125 // No global is ever allowed as a base.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00003126 if (AM.BaseGV)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003127 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003128
3129 int Scale = AM.Scale;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00003130 if (Scale < 0)
3131 Scale = -Scale;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003132 switch (Scale) {
3133 case 0: // No scale reg, "r+i", "r", or just "i".
3134 break;
3135 default: // No scaled addressing mode.
3136 return false;
3137 }
3138 return true;
3139}
3140
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00003141/// Return true if folding a constant offset with the given GlobalAddress is
3142/// legal. It is frequently not legal in PIC relocation models.
3143bool HexagonTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA)
3144 const {
3145 return HTM.getRelocationModel() == Reloc::Static;
3146}
3147
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003148/// isLegalICmpImmediate - Return true if the specified immediate is legal
3149/// icmp immediate, that is the target has icmp instructions which can compare
3150/// a register against the immediate without having to materialize the
3151/// immediate into a register.
3152bool HexagonTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
3153 return Imm >= -512 && Imm <= 511;
3154}
3155
3156/// IsEligibleForTailCallOptimization - Check whether the call is eligible
3157/// for tail call optimization. Targets which want to do tail call
3158/// optimization should implement this function.
3159bool HexagonTargetLowering::IsEligibleForTailCallOptimization(
3160 SDValue Callee,
3161 CallingConv::ID CalleeCC,
3162 bool isVarArg,
3163 bool isCalleeStructRet,
3164 bool isCallerStructRet,
3165 const SmallVectorImpl<ISD::OutputArg> &Outs,
3166 const SmallVectorImpl<SDValue> &OutVals,
3167 const SmallVectorImpl<ISD::InputArg> &Ins,
3168 SelectionDAG& DAG) const {
3169 const Function *CallerF = DAG.getMachineFunction().getFunction();
3170 CallingConv::ID CallerCC = CallerF->getCallingConv();
3171 bool CCMatch = CallerCC == CalleeCC;
3172
3173 // ***************************************************************************
3174 // Look for obvious safe cases to perform tail call optimization that do not
3175 // require ABI changes.
3176 // ***************************************************************************
3177
3178 // If this is a tail call via a function pointer, then don't do it!
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00003179 if (!isa<GlobalAddressSDNode>(Callee) &&
3180 !isa<ExternalSymbolSDNode>(Callee)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003181 return false;
3182 }
3183
Krzysztof Parzyszek0ba97542016-08-19 15:02:18 +00003184 // Do not optimize if the calling conventions do not match and the conventions
3185 // used are not C or Fast.
3186 if (!CCMatch) {
3187 bool R = (CallerCC == CallingConv::C || CallerCC == CallingConv::Fast);
3188 bool E = (CalleeCC == CallingConv::C || CalleeCC == CallingConv::Fast);
3189 // If R & E, then ok.
3190 if (!R || !E)
3191 return false;
3192 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003193
3194 // Do not tail call optimize vararg calls.
3195 if (isVarArg)
3196 return false;
3197
3198 // Also avoid tail call optimization if either caller or callee uses struct
3199 // return semantics.
3200 if (isCalleeStructRet || isCallerStructRet)
3201 return false;
3202
3203 // In addition to the cases above, we also disable Tail Call Optimization if
3204 // the calling convention code that at least one outgoing argument needs to
3205 // go on the stack. We cannot check that here because at this point that
3206 // information is not available.
3207 return true;
3208}
Colin LeMahieu025f8602014-12-08 21:19:18 +00003209
Krzysztof Parzyszek3e409e12016-08-02 18:34:31 +00003210/// Returns the target specific optimal type for load and store operations as
3211/// a result of memset, memcpy, and memmove lowering.
3212///
3213/// If DstAlign is zero that means it's safe to destination alignment can
3214/// satisfy any constraint. Similarly if SrcAlign is zero it means there isn't
3215/// a need to check it against alignment requirement, probably because the
3216/// source does not need to be loaded. If 'IsMemset' is true, that means it's
3217/// expanding a memset. If 'ZeroMemset' is true, that means it's a memset of
3218/// zero. 'MemcpyStrSrc' indicates whether the memcpy source is constant so it
3219/// does not need to be loaded. It returns EVT::Other if the type should be
3220/// determined using generic target-independent logic.
3221EVT HexagonTargetLowering::getOptimalMemOpType(uint64_t Size,
3222 unsigned DstAlign, unsigned SrcAlign, bool IsMemset, bool ZeroMemset,
3223 bool MemcpyStrSrc, MachineFunction &MF) const {
3224
3225 auto Aligned = [](unsigned GivenA, unsigned MinA) -> bool {
3226 return (GivenA % MinA) == 0;
3227 };
3228
3229 if (Size >= 8 && Aligned(DstAlign, 8) && (IsMemset || Aligned(SrcAlign, 8)))
3230 return MVT::i64;
3231 if (Size >= 4 && Aligned(DstAlign, 4) && (IsMemset || Aligned(SrcAlign, 4)))
3232 return MVT::i32;
3233 if (Size >= 2 && Aligned(DstAlign, 2) && (IsMemset || Aligned(SrcAlign, 2)))
3234 return MVT::i16;
3235
3236 return MVT::Other;
3237}
3238
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +00003239bool HexagonTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
3240 unsigned AS, unsigned Align, bool *Fast) const {
3241 if (Fast)
3242 *Fast = false;
3243
3244 switch (VT.getSimpleVT().SimpleTy) {
3245 default:
3246 return false;
3247 case MVT::v64i8:
3248 case MVT::v128i8:
3249 case MVT::v256i8:
3250 case MVT::v32i16:
3251 case MVT::v64i16:
3252 case MVT::v128i16:
3253 case MVT::v16i32:
3254 case MVT::v32i32:
3255 case MVT::v64i32:
3256 case MVT::v8i64:
3257 case MVT::v16i64:
3258 case MVT::v32i64:
3259 return true;
3260 }
3261 return false;
3262}
3263
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003264std::pair<const TargetRegisterClass*, uint8_t>
3265HexagonTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
3266 MVT VT) const {
3267 const TargetRegisterClass *RRC = nullptr;
3268
3269 uint8_t Cost = 1;
3270 switch (VT.SimpleTy) {
3271 default:
3272 return TargetLowering::findRepresentativeClass(TRI, VT);
3273 case MVT::v64i8:
3274 case MVT::v32i16:
3275 case MVT::v16i32:
3276 case MVT::v8i64:
3277 RRC = &Hexagon::VectorRegsRegClass;
3278 break;
3279 case MVT::v128i8:
3280 case MVT::v64i16:
3281 case MVT::v32i32:
3282 case MVT::v16i64:
3283 if (Subtarget.hasV60TOps() && Subtarget.useHVXOps() &&
3284 Subtarget.useHVXDblOps())
3285 RRC = &Hexagon::VectorRegs128BRegClass;
3286 else
3287 RRC = &Hexagon::VecDblRegsRegClass;
3288 break;
3289 case MVT::v256i8:
3290 case MVT::v128i16:
3291 case MVT::v64i32:
3292 case MVT::v32i64:
3293 RRC = &Hexagon::VecDblRegs128BRegClass;
3294 break;
3295 }
3296 return std::make_pair(RRC, Cost);
3297}
3298
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003299Value *HexagonTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
3300 AtomicOrdering Ord) const {
3301 BasicBlock *BB = Builder.GetInsertBlock();
3302 Module *M = BB->getParent()->getParent();
3303 Type *Ty = cast<PointerType>(Addr->getType())->getElementType();
3304 unsigned SZ = Ty->getPrimitiveSizeInBits();
3305 assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic loads supported");
3306 Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_L2_loadw_locked
3307 : Intrinsic::hexagon_L4_loadd_locked;
3308 Value *Fn = Intrinsic::getDeclaration(M, IntID);
3309 return Builder.CreateCall(Fn, Addr, "larx");
3310}
3311
3312/// Perform a store-conditional operation to Addr. Return the status of the
3313/// store. This should be 0 if the store succeeded, non-zero otherwise.
3314Value *HexagonTargetLowering::emitStoreConditional(IRBuilder<> &Builder,
3315 Value *Val, Value *Addr, AtomicOrdering Ord) const {
3316 BasicBlock *BB = Builder.GetInsertBlock();
3317 Module *M = BB->getParent()->getParent();
3318 Type *Ty = Val->getType();
3319 unsigned SZ = Ty->getPrimitiveSizeInBits();
3320 assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic stores supported");
3321 Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_S2_storew_locked
3322 : Intrinsic::hexagon_S4_stored_locked;
3323 Value *Fn = Intrinsic::getDeclaration(M, IntID);
3324 Value *Call = Builder.CreateCall(Fn, {Addr, Val}, "stcx");
3325 Value *Cmp = Builder.CreateICmpEQ(Call, Builder.getInt32(0), "");
3326 Value *Ext = Builder.CreateZExt(Cmp, Type::getInt32Ty(M->getContext()));
3327 return Ext;
3328}
3329
Ahmed Bougacha52468672015-09-11 17:08:28 +00003330TargetLowering::AtomicExpansionKind
3331HexagonTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003332 // Do not expand loads and stores that don't exceed 64 bits.
Ahmed Bougacha52468672015-09-11 17:08:28 +00003333 return LI->getType()->getPrimitiveSizeInBits() > 64
Tim Northoverf520eff2015-12-02 18:12:57 +00003334 ? AtomicExpansionKind::LLOnly
Ahmed Bougacha52468672015-09-11 17:08:28 +00003335 : AtomicExpansionKind::None;
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003336}
3337
3338bool HexagonTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
3339 // Do not expand loads and stores that don't exceed 64 bits.
3340 return SI->getValueOperand()->getType()->getPrimitiveSizeInBits() > 64;
3341}
Krzysztof Parzyszekf228c952016-06-22 16:07:10 +00003342
3343bool HexagonTargetLowering::shouldExpandAtomicCmpXchgInIR(
3344 AtomicCmpXchgInst *AI) const {
3345 const DataLayout &DL = AI->getModule()->getDataLayout();
3346 unsigned Size = DL.getTypeStoreSize(AI->getCompareOperand()->getType());
3347 return Size >= 4 && Size <= 8;
3348}