blob: 88fb1bee27dbb427c1ea05f2a878272b77a29190 [file] [log] [blame]
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001//===-- HexagonISelLowering.cpp - Hexagon DAG Lowering Implementation -----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the interfaces that Hexagon uses to lower LLVM code
11// into a selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "HexagonISelLowering.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "Hexagon.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000017#include "HexagonMachineFunctionInfo.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000018#include "HexagonRegisterInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000019#include "HexagonSubtarget.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "HexagonTargetMachine.h"
21#include "HexagonTargetObjectFile.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000022#include "llvm/ADT/APInt.h"
23#include "llvm/ADT/ArrayRef.h"
24#include "llvm/ADT/SmallVector.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000025#include "llvm/CodeGen/CallingConvLower.h"
26#include "llvm/CodeGen/MachineFrameInfo.h"
27#include "llvm/CodeGen/MachineFunction.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000028#include "llvm/CodeGen/MachineMemOperand.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000030#include "llvm/CodeGen/RuntimeLibcalls.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000031#include "llvm/CodeGen/SelectionDAG.h"
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();
Krzysztof Parzyszekf67cd822017-07-11 17:11:54 +0000719 MachineFrameInfo &MFI = MF.getFrameInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +0000720 auto PtrVT = getPointerTy(MF.getDataLayout());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000721
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000722 // Check for varargs.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000723 unsigned NumNamedVarArgParams = -1U;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000724 if (GlobalAddressSDNode *GAN = dyn_cast<GlobalAddressSDNode>(Callee)) {
725 const GlobalValue *GV = GAN->getGlobal();
726 Callee = DAG.getTargetGlobalAddress(GV, dl, MVT::i32);
727 if (const Function* F = dyn_cast<Function>(GV)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000728 // If a function has zero args and is a vararg function, that's
729 // disallowed so it must be an undeclared function. Do not assume
730 // varargs if the callee is undefined.
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000731 if (F->isVarArg() && F->getFunctionType()->getNumParams() != 0)
732 NumNamedVarArgParams = F->getFunctionType()->getNumParams();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000733 }
734 }
735
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000736 // Analyze operands of the call, assigning locations to each operand.
737 SmallVector<CCValAssign, 16> ArgLocs;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000738 HexagonCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
Eric Christopherb5217502014-08-06 18:45:26 +0000739 *DAG.getContext(), NumNamedVarArgParams);
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000740
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000741 if (IsVarArg)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000742 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon_VarArg);
743 else
744 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon);
745
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000746 auto Attr = MF.getFunction()->getFnAttribute("disable-tail-calls");
747 if (Attr.getValueAsString() == "true")
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000748 IsTailCall = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000749
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000750 if (IsTailCall) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000751 bool StructAttrFlag = MF.getFunction()->hasStructRetAttr();
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000752 IsTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
753 IsVarArg, IsStructRet,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000754 StructAttrFlag,
755 Outs, OutVals, Ins, DAG);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000756 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000757 CCValAssign &VA = ArgLocs[i];
758 if (VA.isMemLoc()) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000759 IsTailCall = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000760 break;
761 }
762 }
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000763 DEBUG(dbgs() << (IsTailCall ? "Eligible for Tail Call\n"
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000764 : "Argument must be passed on stack. "
765 "Not eligible for Tail Call\n"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000766 }
767 // Get a count of how many bytes are to be pushed on the stack.
768 unsigned NumBytes = CCInfo.getNextStackOffset();
769 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
770 SmallVector<SDValue, 8> MemOpChains;
771
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000772 auto &HRI = *Subtarget.getRegisterInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +0000773 SDValue StackPtr =
774 DAG.getCopyFromReg(Chain, dl, HRI.getStackRegister(), PtrVT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000775
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000776 bool NeedsArgAlign = false;
777 unsigned LargestAlignSeen = 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000778 // Walk the register/memloc assignments, inserting copies/loads.
779 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
780 CCValAssign &VA = ArgLocs[i];
781 SDValue Arg = OutVals[i];
782 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000783 // Record if we need > 8 byte alignment on an argument.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000784 bool ArgAlign = isHvxVectorType(VA.getValVT());
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000785 NeedsArgAlign |= ArgAlign;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000786
787 // Promote the value if needed.
788 switch (VA.getLocInfo()) {
789 default:
790 // Loc info must be one of Full, SExt, ZExt, or AExt.
Craig Toppere55c5562012-02-07 02:50:20 +0000791 llvm_unreachable("Unknown loc info!");
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000792 case CCValAssign::BCvt:
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000793 case CCValAssign::Full:
794 break;
795 case CCValAssign::SExt:
796 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
797 break;
798 case CCValAssign::ZExt:
799 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
800 break;
801 case CCValAssign::AExt:
802 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
803 break;
804 }
805
806 if (VA.isMemLoc()) {
807 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000808 SDValue MemAddr = DAG.getConstant(LocMemOffset, dl,
809 StackPtr.getValueType());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000810 MemAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, MemAddr);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000811 if (ArgAlign)
812 LargestAlignSeen = std::max(LargestAlignSeen,
813 VA.getLocVT().getStoreSizeInBits() >> 3);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000814 if (Flags.isByVal()) {
815 // The argument is a struct passed by value. According to LLVM, "Arg"
816 // is is pointer.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000817 MemOpChains.push_back(CreateCopyOfByValArgument(Arg, MemAddr, Chain,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000818 Flags, DAG, dl));
819 } else {
Alex Lorenze40c8a22015-08-11 23:09:45 +0000820 MachinePointerInfo LocPI = MachinePointerInfo::getStack(
821 DAG.getMachineFunction(), LocMemOffset);
Justin Lebar9c375812016-07-15 18:27:10 +0000822 SDValue S = DAG.getStore(Chain, dl, Arg, MemAddr, LocPI);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000823 MemOpChains.push_back(S);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000824 }
825 continue;
826 }
827
828 // Arguments that can be passed on register must be kept at RegsToPass
829 // vector.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000830 if (VA.isRegLoc())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000831 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000832 }
833
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000834 if (NeedsArgAlign && Subtarget.hasV60TOps()) {
835 DEBUG(dbgs() << "Function needs byte stack align due to call args\n");
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) {
Serge Pavlovd526b132017-05-09 13:35:13 +0000851 Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, dl);
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000852 Glue = Chain.getValue(1);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000853 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000854
855 // Build a sequence of copy-to-reg nodes chained together with token
856 // chain and flag operands which copy the outgoing args into registers.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000857 // The Glue is necessary since all emitted instructions must be
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000858 // stuck together.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000859 if (!IsTailCall) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000860 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
861 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000862 RegsToPass[i].second, Glue);
863 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000864 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000865 } else {
866 // For tail calls lower the arguments to the 'real' stack slot.
867 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000868 // Force all the incoming stack arguments to be loaded from the stack
869 // before any new outgoing arguments are stored to the stack, because the
870 // outgoing stack slots may alias the incoming argument stack slots, and
871 // the alias isn't otherwise explicit. This is slightly more conservative
872 // than necessary, because it means that each store effectively depends
873 // on every argument instead of just those arguments it would clobber.
874 //
Benjamin Kramerbde91762012-06-02 10:20:22 +0000875 // Do not flag preceding copytoreg stuff together with the following stuff.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000876 Glue = SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000877 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
878 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000879 RegsToPass[i].second, Glue);
880 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000881 }
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000882 Glue = SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000883 }
884
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000885 bool LongCalls = MF.getSubtarget<HexagonSubtarget>().useLongCalls();
886 unsigned Flags = LongCalls ? HexagonII::HMOTF_ConstExtended : 0;
887
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000888 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
889 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
890 // node so that legalize doesn't hack it.
Tobias Edler von Kochb51460c2015-12-16 17:29:37 +0000891 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000892 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, PtrVT, 0, Flags);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000893 } else if (ExternalSymbolSDNode *S =
894 dyn_cast<ExternalSymbolSDNode>(Callee)) {
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000895 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), PtrVT, Flags);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000896 }
897
898 // Returns a chain & a flag for retval copy to use.
899 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
900 SmallVector<SDValue, 8> Ops;
901 Ops.push_back(Chain);
902 Ops.push_back(Callee);
903
904 // Add argument registers to the end of the list so that they are
905 // known live into the call.
906 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
907 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
908 RegsToPass[i].second.getValueType()));
909 }
910
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000911 const uint32_t *Mask = HRI.getCallPreservedMask(MF, CallConv);
912 assert(Mask && "Missing call preserved mask for calling convention");
913 Ops.push_back(DAG.getRegisterMask(Mask));
914
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000915 if (Glue.getNode())
916 Ops.push_back(Glue);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000917
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000918 if (IsTailCall) {
Krzysztof Parzyszekf67cd822017-07-11 17:11:54 +0000919 MFI.setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +0000920 return DAG.getNode(HexagonISD::TC_RETURN, dl, NodeTys, Ops);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +0000921 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000922
Krzysztof Parzyszekf67cd822017-07-11 17:11:54 +0000923 // Set this here because we need to know this for "hasFP" in frame lowering.
924 // The target-independent code calls getFrameRegister before setting it, and
925 // getFrameRegister uses hasFP to determine whether the function has FP.
926 MFI.setHasCalls(true);
927
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +0000928 unsigned OpCode = DoesNotReturn ? HexagonISD::CALLnr : HexagonISD::CALL;
Colin LeMahieu2e3a26d2015-01-16 17:05:27 +0000929 Chain = DAG.getNode(OpCode, dl, NodeTys, Ops);
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000930 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000931
932 // Create the CALLSEQ_END node.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000933 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000934 DAG.getIntPtrConstant(0, dl, true), Glue, dl);
935 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000936
937 // Handle result values, copying them out of physregs into vregs that we
938 // return.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000939 return LowerCallResult(Chain, Glue, CallConv, IsVarArg, Ins, dl, DAG,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000940 InVals, OutVals, Callee);
941}
942
943static bool getIndexedAddressParts(SDNode *Ptr, EVT VT,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000944 SDValue &Base, SDValue &Offset,
945 bool &IsInc, SelectionDAG &DAG) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000946 if (Ptr->getOpcode() != ISD::ADD)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000947 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000948
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000949 auto &HST = static_cast<const HexagonSubtarget&>(DAG.getSubtarget());
950 bool UseHVX = HST.useHVXOps();
951 bool UseHVXDbl = HST.useHVXDblOps();
952
953 bool ValidHVXDblType =
954 (UseHVX && UseHVXDbl) && (VT == MVT::v32i32 || VT == MVT::v16i64 ||
955 VT == MVT::v64i16 || VT == MVT::v128i8);
956 bool ValidHVXType =
957 UseHVX && !UseHVXDbl && (VT == MVT::v16i32 || VT == MVT::v8i64 ||
958 VT == MVT::v32i16 || VT == MVT::v64i8);
959
960 if (ValidHVXDblType || ValidHVXType ||
961 VT == MVT::i64 || VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000962 IsInc = (Ptr->getOpcode() == ISD::ADD);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000963 Base = Ptr->getOperand(0);
964 Offset = Ptr->getOperand(1);
965 // Ensure that Offset is a constant.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000966 return isa<ConstantSDNode>(Offset);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000967 }
968
969 return false;
970}
971
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000972/// getPostIndexedAddressParts - returns true by value, base pointer and
973/// offset pointer and addressing mode by reference if this node can be
974/// combined with a load / store to form a post-indexed load / store.
975bool HexagonTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
976 SDValue &Base,
977 SDValue &Offset,
978 ISD::MemIndexedMode &AM,
979 SelectionDAG &DAG) const
980{
981 EVT VT;
982 SDValue Ptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000983
984 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
985 VT = LD->getMemoryVT();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000986 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
987 VT = ST->getMemoryVT();
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000988 if (ST->getValue().getValueType() == MVT::i64 && ST->isTruncatingStore())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000989 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000990 } else {
991 return false;
992 }
993
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000994 bool IsInc = false;
995 bool isLegal = getIndexedAddressParts(Op, VT, Base, Offset, IsInc, DAG);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000996 if (isLegal) {
997 auto &HII = *Subtarget.getInstrInfo();
998 int32_t OffsetVal = cast<ConstantSDNode>(Offset.getNode())->getSExtValue();
999 if (HII.isValidAutoIncImm(VT, OffsetVal)) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00001000 AM = IsInc ? ISD::POST_INC : ISD::POST_DEC;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001001 return true;
1002 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001003 }
1004
1005 return false;
1006}
1007
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001008SDValue
1009HexagonTargetLowering::LowerINLINEASM(SDValue Op, SelectionDAG &DAG) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001010 MachineFunction &MF = DAG.getMachineFunction();
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +00001011 auto &HMFI = *MF.getInfo<HexagonMachineFunctionInfo>();
1012 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
1013 unsigned LR = HRI.getRARegister();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001014
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +00001015 if (Op.getOpcode() != ISD::INLINEASM || HMFI.hasClobberLR())
1016 return Op;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001017
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +00001018 unsigned NumOps = Op.getNumOperands();
1019 if (Op.getOperand(NumOps-1).getValueType() == MVT::Glue)
1020 --NumOps; // Ignore the flag operand.
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001021
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +00001022 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
1023 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(i))->getZExtValue();
1024 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
1025 ++i; // Skip the ID value.
1026
1027 switch (InlineAsm::getKind(Flags)) {
1028 default:
1029 llvm_unreachable("Bad flags!");
1030 case InlineAsm::Kind_RegUse:
1031 case InlineAsm::Kind_Imm:
1032 case InlineAsm::Kind_Mem:
1033 i += NumVals;
1034 break;
1035 case InlineAsm::Kind_Clobber:
1036 case InlineAsm::Kind_RegDef:
1037 case InlineAsm::Kind_RegDefEarlyClobber: {
1038 for (; NumVals; --NumVals, ++i) {
1039 unsigned Reg = cast<RegisterSDNode>(Op.getOperand(i))->getReg();
1040 if (Reg != LR)
1041 continue;
1042 HMFI.setHasClobberLR(true);
1043 return Op;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001044 }
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +00001045 break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001046 }
1047 }
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +00001048 }
1049
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001050 return Op;
1051}
1052
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00001053// Need to transform ISD::PREFETCH into something that doesn't inherit
1054// all of the properties of ISD::PREFETCH, specifically SDNPMayLoad and
1055// SDNPMayStore.
1056SDValue HexagonTargetLowering::LowerPREFETCH(SDValue Op,
1057 SelectionDAG &DAG) const {
1058 SDValue Chain = Op.getOperand(0);
1059 SDValue Addr = Op.getOperand(1);
1060 // Lower it to DCFETCH($reg, #0). A "pat" will try to merge the offset in,
1061 // if the "reg" is fed by an "add".
1062 SDLoc DL(Op);
1063 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
1064 return DAG.getNode(HexagonISD::DCFETCH, DL, MVT::Other, Chain, Addr, Zero);
1065}
1066
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +00001067// Custom-handle ISD::READCYCLECOUNTER because the target-independent SDNode
1068// is marked as having side-effects, while the register read on Hexagon does
1069// not have any. TableGen refuses to accept the direct pattern from that node
1070// to the A4_tfrcpp.
1071SDValue HexagonTargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
1072 SelectionDAG &DAG) const {
1073 SDValue Chain = Op.getOperand(0);
1074 SDLoc dl(Op);
1075 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
1076 return DAG.getNode(HexagonISD::READCYCLE, dl, VTs, Chain);
1077}
1078
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00001079SDValue HexagonTargetLowering::LowerINTRINSIC_VOID(SDValue Op,
1080 SelectionDAG &DAG) const {
1081 SDValue Chain = Op.getOperand(0);
1082 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1083 // Lower the hexagon_prefetch builtin to DCFETCH, as above.
1084 if (IntNo == Intrinsic::hexagon_prefetch) {
1085 SDValue Addr = Op.getOperand(2);
1086 SDLoc DL(Op);
1087 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
1088 return DAG.getNode(HexagonISD::DCFETCH, DL, MVT::Other, Chain, Addr, Zero);
1089 }
1090 return SDValue();
1091}
1092
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001093SDValue
1094HexagonTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
1095 SelectionDAG &DAG) const {
1096 SDValue Chain = Op.getOperand(0);
1097 SDValue Size = Op.getOperand(1);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001098 SDValue Align = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001099 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001100
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001101 ConstantSDNode *AlignConst = dyn_cast<ConstantSDNode>(Align);
1102 assert(AlignConst && "Non-constant Align in LowerDYNAMIC_STACKALLOC");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001103
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001104 unsigned A = AlignConst->getSExtValue();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001105 auto &HFI = *Subtarget.getFrameLowering();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001106 // "Zero" means natural stack alignment.
1107 if (A == 0)
1108 A = HFI.getStackAlignment();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001109
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001110 DEBUG({
Reid Kleckner40d72302016-10-20 00:22:23 +00001111 dbgs () << __func__ << " Align: " << A << " Size: ";
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001112 Size.getNode()->dump(&DAG);
1113 dbgs() << "\n";
1114 });
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001115
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001116 SDValue AC = DAG.getConstant(A, dl, MVT::i32);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001117 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00001118 SDValue AA = DAG.getNode(HexagonISD::ALLOCA, dl, VTs, Chain, Size, AC);
Nirav Davebfdb4832016-06-23 17:52:57 +00001119
1120 DAG.ReplaceAllUsesOfValueWith(Op, AA);
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00001121 return AA;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001122}
1123
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001124SDValue HexagonTargetLowering::LowerFormalArguments(
1125 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
1126 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
1127 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001128 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00001129 MachineFrameInfo &MFI = MF.getFrameInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001130 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001131 auto &FuncInfo = *MF.getInfo<HexagonMachineFunctionInfo>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001132
1133 // Assign locations to all of the incoming arguments.
1134 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001135 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1136 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001137
1138 CCInfo.AnalyzeFormalArguments(Ins, CC_Hexagon);
1139
1140 // For LLVM, in the case when returning a struct by value (>8byte),
1141 // the first argument is a pointer that points to the location on caller's
1142 // stack where the return value will be stored. For Hexagon, the location on
1143 // caller's stack is passed only when the struct size is smaller than (and
1144 // equal to) 8 bytes. If not, no address will be passed into callee and
1145 // callee return the result direclty through R0/R1.
1146
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001147 SmallVector<SDValue, 8> MemOps;
1148 bool UseHVX = Subtarget.useHVXOps(), UseHVXDbl = Subtarget.useHVXDblOps();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001149
1150 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1151 CCValAssign &VA = ArgLocs[i];
1152 ISD::ArgFlagsTy Flags = Ins[i].Flags;
1153 unsigned ObjSize;
1154 unsigned StackLocation;
1155 int FI;
1156
1157 if ( (VA.isRegLoc() && !Flags.isByVal())
1158 || (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() > 8)) {
1159 // Arguments passed in registers
1160 // 1. int, long long, ptr args that get allocated in register.
1161 // 2. Large struct that gets an register to put its address in.
1162 EVT RegVT = VA.getLocVT();
Sirish Pande69295b82012-05-10 20:20:25 +00001163 if (RegVT == MVT::i8 || RegVT == MVT::i16 ||
1164 RegVT == MVT::i32 || RegVT == MVT::f32) {
Krzysztof Parzyszek8f23dd62017-03-01 17:30:10 +00001165 unsigned VReg =
Craig Topperc7242e02012-04-20 07:30:17 +00001166 RegInfo.createVirtualRegister(&Hexagon::IntRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001167 RegInfo.addLiveIn(VA.getLocReg(), VReg);
Krzysztof Parzyszek8f23dd62017-03-01 17:30:10 +00001168 SDValue Copy = DAG.getCopyFromReg(Chain, dl, VReg, RegVT);
1169 // Treat values of type MVT::i1 specially: they are passed in
1170 // registers of type i32, but they need to remain as values of
1171 // type i1 for consistency of the argument lowering.
1172 if (VA.getValVT() == MVT::i1) {
1173 // Generate a copy into a predicate register and use the value
1174 // of the register as the "InVal".
1175 unsigned PReg =
1176 RegInfo.createVirtualRegister(&Hexagon::PredRegsRegClass);
1177 SDNode *T = DAG.getMachineNode(Hexagon::C2_tfrrp, dl, MVT::i1,
1178 Copy.getValue(0));
1179 Copy = DAG.getCopyToReg(Copy.getValue(1), dl, PReg, SDValue(T, 0));
1180 Copy = DAG.getCopyFromReg(Copy, dl, PReg, MVT::i1);
1181 }
1182 InVals.push_back(Copy);
1183 Chain = Copy.getValue(1);
Colin LeMahieu4379d102015-01-28 22:08:16 +00001184 } else if (RegVT == MVT::i64 || RegVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001185 unsigned VReg =
Craig Topperc7242e02012-04-20 07:30:17 +00001186 RegInfo.createVirtualRegister(&Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001187 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1188 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001189
1190 // Single Vector
1191 } else if ((RegVT == MVT::v8i64 || RegVT == MVT::v16i32 ||
1192 RegVT == MVT::v32i16 || RegVT == MVT::v64i8)) {
1193 unsigned VReg =
1194 RegInfo.createVirtualRegister(&Hexagon::VectorRegsRegClass);
1195 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1196 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1197 } else if (UseHVX && UseHVXDbl &&
1198 ((RegVT == MVT::v16i64 || RegVT == MVT::v32i32 ||
1199 RegVT == MVT::v64i16 || RegVT == MVT::v128i8))) {
1200 unsigned VReg =
1201 RegInfo.createVirtualRegister(&Hexagon::VectorRegs128BRegClass);
1202 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1203 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1204
1205 // Double Vector
1206 } else if ((RegVT == MVT::v16i64 || RegVT == MVT::v32i32 ||
1207 RegVT == MVT::v64i16 || RegVT == MVT::v128i8)) {
1208 unsigned VReg =
1209 RegInfo.createVirtualRegister(&Hexagon::VecDblRegsRegClass);
1210 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1211 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1212 } else if (UseHVX && UseHVXDbl &&
1213 ((RegVT == MVT::v32i64 || RegVT == MVT::v64i32 ||
1214 RegVT == MVT::v128i16 || RegVT == MVT::v256i8))) {
1215 unsigned VReg =
1216 RegInfo.createVirtualRegister(&Hexagon::VecDblRegs128BRegClass);
1217 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1218 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1219 } else if (RegVT == MVT::v512i1 || RegVT == MVT::v1024i1) {
1220 assert(0 && "need to support VecPred regs");
1221 unsigned VReg =
1222 RegInfo.createVirtualRegister(&Hexagon::VecPredRegsRegClass);
1223 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1224 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001225 } else {
1226 assert (0);
1227 }
1228 } else if (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() <= 8) {
1229 assert (0 && "ByValSize must be bigger than 8 bytes");
1230 } else {
1231 // Sanity check.
1232 assert(VA.isMemLoc());
1233
1234 if (Flags.isByVal()) {
1235 // If it's a byval parameter, then we need to compute the
1236 // "real" size, not the size of the pointer.
1237 ObjSize = Flags.getByValSize();
1238 } else {
1239 ObjSize = VA.getLocVT().getStoreSizeInBits() >> 3;
1240 }
1241
1242 StackLocation = HEXAGON_LRFP_SIZE + VA.getLocMemOffset();
1243 // Create the frame index object for this incoming parameter...
Matthias Braun941a7052016-07-28 18:40:00 +00001244 FI = MFI.CreateFixedObject(ObjSize, StackLocation, true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001245
1246 // Create the SelectionDAG nodes cordl, responding to a load
1247 // from this parameter.
1248 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
1249
1250 if (Flags.isByVal()) {
1251 // If it's a pass-by-value aggregate, then do not dereference the stack
1252 // location. Instead, we should generate a reference to the stack
1253 // location.
1254 InVals.push_back(FIN);
1255 } else {
Justin Lebar9c375812016-07-15 18:27:10 +00001256 InVals.push_back(
Krzysztof Parzyszek3e2046c2017-04-13 15:00:18 +00001257 DAG.getLoad(VA.getValVT(), dl, Chain, FIN, MachinePointerInfo()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001258 }
1259 }
1260 }
1261
1262 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001263 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001264
1265 if (isVarArg) {
1266 // This will point to the next argument passed via stack.
Matthias Braun941a7052016-07-28 18:40:00 +00001267 int FrameIndex = MFI.CreateFixedObject(Hexagon_PointerSize,
1268 HEXAGON_LRFP_SIZE +
1269 CCInfo.getNextStackOffset(),
1270 true);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001271 FuncInfo.setVarArgsFrameIndex(FrameIndex);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001272 }
1273
1274 return Chain;
1275}
1276
1277SDValue
1278HexagonTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
1279 // VASTART stores the address of the VarArgsFrameIndex slot into the
1280 // memory location argument.
1281 MachineFunction &MF = DAG.getMachineFunction();
1282 HexagonMachineFunctionInfo *QFI = MF.getInfo<HexagonMachineFunctionInfo>();
1283 SDValue Addr = DAG.getFrameIndex(QFI->getVarArgsFrameIndex(), MVT::i32);
1284 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Justin Lebar9c375812016-07-15 18:27:10 +00001285 return DAG.getStore(Op.getOperand(0), SDLoc(Op), Addr, Op.getOperand(1),
1286 MachinePointerInfo(SV));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001287}
1288
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001289static bool isSExtFree(SDValue N) {
1290 // A sign-extend of a truncate of a sign-extend is free.
1291 if (N.getOpcode() == ISD::TRUNCATE &&
1292 N.getOperand(0).getOpcode() == ISD::AssertSext)
1293 return true;
1294 // We have sign-extended loads.
1295 if (N.getOpcode() == ISD::LOAD)
1296 return true;
1297 return false;
1298}
1299
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001300SDValue HexagonTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1301 SDLoc dl(Op);
1302
1303 SDValue LHS = Op.getOperand(0);
1304 SDValue RHS = Op.getOperand(1);
1305 SDValue Cmp = Op.getOperand(2);
1306 ISD::CondCode CC = cast<CondCodeSDNode>(Cmp)->get();
1307
1308 EVT VT = Op.getValueType();
1309 EVT LHSVT = LHS.getValueType();
1310 EVT RHSVT = RHS.getValueType();
1311
1312 if (LHSVT == MVT::v2i16) {
1313 assert(ISD::isSignedIntSetCC(CC) || ISD::isUnsignedIntSetCC(CC));
1314 unsigned ExtOpc = ISD::isSignedIntSetCC(CC) ? ISD::SIGN_EXTEND
1315 : ISD::ZERO_EXTEND;
1316 SDValue LX = DAG.getNode(ExtOpc, dl, MVT::v2i32, LHS);
1317 SDValue RX = DAG.getNode(ExtOpc, dl, MVT::v2i32, RHS);
1318 SDValue SC = DAG.getNode(ISD::SETCC, dl, MVT::v2i1, LX, RX, Cmp);
1319 return SC;
1320 }
1321
1322 // Treat all other vector types as legal.
1323 if (VT.isVector())
1324 return Op;
1325
1326 // Equals and not equals should use sign-extend, not zero-extend, since
1327 // we can represent small negative values in the compare instructions.
1328 // The LLVM default is to use zero-extend arbitrarily in these cases.
1329 if ((CC == ISD::SETEQ || CC == ISD::SETNE) &&
1330 (RHSVT == MVT::i8 || RHSVT == MVT::i16) &&
1331 (LHSVT == MVT::i8 || LHSVT == MVT::i16)) {
1332 ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS);
1333 if (C && C->getAPIntValue().isNegative()) {
1334 LHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, LHS);
1335 RHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, RHS);
1336 return DAG.getNode(ISD::SETCC, dl, Op.getValueType(),
1337 LHS, RHS, Op.getOperand(2));
1338 }
1339 if (isSExtFree(LHS) || isSExtFree(RHS)) {
1340 LHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, LHS);
1341 RHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, RHS);
1342 return DAG.getNode(ISD::SETCC, dl, Op.getValueType(),
1343 LHS, RHS, Op.getOperand(2));
1344 }
1345 }
1346 return SDValue();
1347}
1348
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001349SDValue
1350HexagonTargetLowering::LowerVSELECT(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001351 SDValue PredOp = Op.getOperand(0);
1352 SDValue Op1 = Op.getOperand(1), Op2 = Op.getOperand(2);
1353 EVT OpVT = Op1.getValueType();
1354 SDLoc DL(Op);
1355
1356 if (OpVT == MVT::v2i16) {
1357 SDValue X1 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op1);
1358 SDValue X2 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op2);
1359 SDValue SL = DAG.getNode(ISD::VSELECT, DL, MVT::v2i32, PredOp, X1, X2);
1360 SDValue TR = DAG.getNode(ISD::TRUNCATE, DL, MVT::v2i16, SL);
1361 return TR;
1362 }
1363
1364 return SDValue();
1365}
1366
1367// Handle only specific vector loads.
1368SDValue HexagonTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1369 EVT VT = Op.getValueType();
1370 SDLoc DL(Op);
1371 LoadSDNode *LoadNode = cast<LoadSDNode>(Op);
1372 SDValue Chain = LoadNode->getChain();
1373 SDValue Ptr = Op.getOperand(1);
1374 SDValue LoweredLoad;
1375 SDValue Result;
1376 SDValue Base = LoadNode->getBasePtr();
1377 ISD::LoadExtType Ext = LoadNode->getExtensionType();
1378 unsigned Alignment = LoadNode->getAlignment();
1379 SDValue LoadChain;
1380
1381 if(Ext == ISD::NON_EXTLOAD)
1382 Ext = ISD::ZEXTLOAD;
1383
1384 if (VT == MVT::v4i16) {
1385 if (Alignment == 2) {
1386 SDValue Loads[4];
1387 // Base load.
1388 Loads[0] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Base,
Justin Lebar9c375812016-07-15 18:27:10 +00001389 LoadNode->getPointerInfo(), MVT::i16, Alignment,
1390 LoadNode->getMemOperand()->getFlags());
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001391 // Base+2 load.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001392 SDValue Increment = DAG.getConstant(2, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001393 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1394 Loads[1] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
Justin Lebar9c375812016-07-15 18:27:10 +00001395 LoadNode->getPointerInfo(), MVT::i16, Alignment,
1396 LoadNode->getMemOperand()->getFlags());
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001397 // SHL 16, then OR base and base+2.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001398 SDValue ShiftAmount = DAG.getConstant(16, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001399 SDValue Tmp1 = DAG.getNode(ISD::SHL, DL, MVT::i32, Loads[1], ShiftAmount);
1400 SDValue Tmp2 = DAG.getNode(ISD::OR, DL, MVT::i32, Tmp1, Loads[0]);
1401 // Base + 4.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001402 Increment = DAG.getConstant(4, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001403 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1404 Loads[2] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
Justin Lebar9c375812016-07-15 18:27:10 +00001405 LoadNode->getPointerInfo(), MVT::i16, Alignment,
1406 LoadNode->getMemOperand()->getFlags());
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001407 // Base + 6.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001408 Increment = DAG.getConstant(6, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001409 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1410 Loads[3] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
Justin Lebar9c375812016-07-15 18:27:10 +00001411 LoadNode->getPointerInfo(), MVT::i16, Alignment,
1412 LoadNode->getMemOperand()->getFlags());
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001413 // SHL 16, then OR base+4 and base+6.
1414 Tmp1 = DAG.getNode(ISD::SHL, DL, MVT::i32, Loads[3], ShiftAmount);
1415 SDValue Tmp4 = DAG.getNode(ISD::OR, DL, MVT::i32, Tmp1, Loads[2]);
1416 // Combine to i64. This could be optimised out later if we can
1417 // affect reg allocation of this code.
1418 Result = DAG.getNode(HexagonISD::COMBINE, DL, MVT::i64, Tmp4, Tmp2);
1419 LoadChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
1420 Loads[0].getValue(1), Loads[1].getValue(1),
1421 Loads[2].getValue(1), Loads[3].getValue(1));
1422 } else {
1423 // Perform default type expansion.
1424 Result = DAG.getLoad(MVT::i64, DL, Chain, Ptr, LoadNode->getPointerInfo(),
Justin Lebar9c375812016-07-15 18:27:10 +00001425 LoadNode->getAlignment(),
1426 LoadNode->getMemOperand()->getFlags());
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001427 LoadChain = Result.getValue(1);
1428 }
1429 } else
1430 llvm_unreachable("Custom lowering unsupported load");
1431
1432 Result = DAG.getNode(ISD::BITCAST, DL, VT, Result);
1433 // Since we pretend to lower a load, we need the original chain
1434 // info attached to the result.
1435 SDValue Ops[] = { Result, LoadChain };
1436
1437 return DAG.getMergeValues(Ops, DL);
1438}
1439
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001440SDValue
Sirish Pande69295b82012-05-10 20:20:25 +00001441HexagonTargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
1442 EVT ValTy = Op.getValueType();
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001443 ConstantPoolSDNode *CPN = cast<ConstantPoolSDNode>(Op);
1444 unsigned Align = CPN->getAlignment();
Rafael Espindola405e25a2016-06-26 22:24:01 +00001445 bool IsPositionIndependent = isPositionIndependent();
1446 unsigned char TF = IsPositionIndependent ? HexagonII::MO_PCREL : 0;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001447
Ron Lieberman822ee882016-08-13 23:41:11 +00001448 unsigned Offset = 0;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001449 SDValue T;
1450 if (CPN->isMachineConstantPoolEntry())
Ron Lieberman822ee882016-08-13 23:41:11 +00001451 T = DAG.getTargetConstantPool(CPN->getMachineCPVal(), ValTy, Align, Offset,
1452 TF);
Sirish Pande69295b82012-05-10 20:20:25 +00001453 else
Ron Lieberman822ee882016-08-13 23:41:11 +00001454 T = DAG.getTargetConstantPool(CPN->getConstVal(), ValTy, Align, Offset,
1455 TF);
1456
1457 assert(cast<ConstantPoolSDNode>(T)->getTargetFlags() == TF &&
1458 "Inconsistent target flag encountered");
1459
Rafael Espindola405e25a2016-06-26 22:24:01 +00001460 if (IsPositionIndependent)
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001461 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), ValTy, T);
1462 return DAG.getNode(HexagonISD::CP, SDLoc(Op), ValTy, T);
1463}
1464
1465SDValue
1466HexagonTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
1467 EVT VT = Op.getValueType();
1468 int Idx = cast<JumpTableSDNode>(Op)->getIndex();
Rafael Espindola405e25a2016-06-26 22:24:01 +00001469 if (isPositionIndependent()) {
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001470 SDValue T = DAG.getTargetJumpTable(Idx, VT, HexagonII::MO_PCREL);
1471 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), VT, T);
1472 }
1473
1474 SDValue T = DAG.getTargetJumpTable(Idx, VT);
1475 return DAG.getNode(HexagonISD::JT, SDLoc(Op), VT, T);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001476}
1477
1478SDValue
1479HexagonTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001480 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001481 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00001482 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001483 MFI.setReturnAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001484
Bill Wendling908bf812014-01-06 00:43:20 +00001485 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001486 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001487
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001488 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001489 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001490 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1491 if (Depth) {
1492 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001493 SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001494 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
1495 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Justin Lebar9c375812016-07-15 18:27:10 +00001496 MachinePointerInfo());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001497 }
1498
1499 // Return LR, which contains the return address. Mark it an implicit live-in.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001500 unsigned Reg = MF.addLiveIn(HRI.getRARegister(), getRegClassFor(MVT::i32));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001501 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
1502}
1503
1504SDValue
1505HexagonTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001506 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Matthias Braun941a7052016-07-28 18:40:00 +00001507 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001508 MFI.setFrameAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001509
1510 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001511 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001512 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1513 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001514 HRI.getFrameRegister(), VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001515 while (Depth--)
1516 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00001517 MachinePointerInfo());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001518 return FrameAddr;
1519}
1520
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001521SDValue
1522HexagonTargetLowering::LowerATOMIC_FENCE(SDValue Op, SelectionDAG& DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001523 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001524 return DAG.getNode(HexagonISD::BARRIER, dl, MVT::Other, Op.getOperand(0));
1525}
1526
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001527SDValue
1528HexagonTargetLowering::LowerGLOBALADDRESS(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001529 SDLoc dl(Op);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001530 auto *GAN = cast<GlobalAddressSDNode>(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00001531 auto PtrVT = getPointerTy(DAG.getDataLayout());
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001532 auto *GV = GAN->getGlobal();
1533 int64_t Offset = GAN->getOffset();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001534
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001535 auto &HLOF = *HTM.getObjFileLowering();
1536 Reloc::Model RM = HTM.getRelocationModel();
1537
1538 if (RM == Reloc::Static) {
1539 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset);
Peter Collingbourne67335642016-10-24 19:23:39 +00001540 const GlobalObject *GO = GV->getBaseObject();
1541 if (GO && HLOF.isGlobalInSmallSection(GO, HTM))
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001542 return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, GA);
1543 return DAG.getNode(HexagonISD::CONST32, dl, PtrVT, GA);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001544 }
1545
Rafael Espindola3beef8d2016-06-27 23:15:57 +00001546 bool UsePCRel = getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001547 if (UsePCRel) {
1548 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset,
1549 HexagonII::MO_PCREL);
1550 return DAG.getNode(HexagonISD::AT_PCREL, dl, PtrVT, GA);
1551 }
1552
1553 // Use GOT index.
1554 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
1555 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, HexagonII::MO_GOT);
1556 SDValue Off = DAG.getConstant(Offset, dl, MVT::i32);
1557 return DAG.getNode(HexagonISD::AT_GOT, dl, PtrVT, GOT, GA, Off);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001558}
1559
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001560// Specifies that for loads and stores VT can be promoted to PromotedLdStVT.
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001561SDValue
1562HexagonTargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
1563 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001564 SDLoc dl(Op);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001565 EVT PtrVT = getPointerTy(DAG.getDataLayout());
1566
1567 Reloc::Model RM = HTM.getRelocationModel();
1568 if (RM == Reloc::Static) {
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001569 SDValue A = DAG.getTargetBlockAddress(BA, PtrVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001570 return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, A);
1571 }
1572
1573 SDValue A = DAG.getTargetBlockAddress(BA, PtrVT, 0, HexagonII::MO_PCREL);
1574 return DAG.getNode(HexagonISD::AT_PCREL, dl, PtrVT, A);
1575}
1576
1577SDValue
1578HexagonTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op, SelectionDAG &DAG)
1579 const {
1580 EVT PtrVT = getPointerTy(DAG.getDataLayout());
1581 SDValue GOTSym = DAG.getTargetExternalSymbol(HEXAGON_GOT_SYM_NAME, PtrVT,
1582 HexagonII::MO_PCREL);
1583 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), PtrVT, GOTSym);
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001584}
1585
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001586SDValue
1587HexagonTargetLowering::GetDynamicTLSAddr(SelectionDAG &DAG, SDValue Chain,
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001588 GlobalAddressSDNode *GA, SDValue Glue, EVT PtrVT, unsigned ReturnReg,
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001589 unsigned char OperandFlags) const {
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001590 MachineFunction &MF = DAG.getMachineFunction();
1591 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001592 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1593 SDLoc dl(GA);
1594 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
1595 GA->getValueType(0),
1596 GA->getOffset(),
1597 OperandFlags);
1598 // Create Operands for the call.The Operands should have the following:
1599 // 1. Chain SDValue
1600 // 2. Callee which in this case is the Global address value.
1601 // 3. Registers live into the call.In this case its R0, as we
1602 // have just one argument to be passed.
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001603 // 4. Glue.
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001604 // Note: The order is important.
1605
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001606 const auto &HRI = *Subtarget.getRegisterInfo();
1607 const uint32_t *Mask = HRI.getCallPreservedMask(MF, CallingConv::C);
1608 assert(Mask && "Missing call preserved mask for calling convention");
1609 SDValue Ops[] = { Chain, TGA, DAG.getRegister(Hexagon::R0, PtrVT),
1610 DAG.getRegisterMask(Mask), Glue };
1611 Chain = DAG.getNode(HexagonISD::CALL, dl, NodeTys, Ops);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001612
1613 // Inform MFI that function has calls.
Matthias Braun941a7052016-07-28 18:40:00 +00001614 MFI.setAdjustsStack(true);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001615
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001616 Glue = Chain.getValue(1);
1617 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Glue);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001618}
1619
1620//
1621// Lower using the intial executable model for TLS addresses
1622//
1623SDValue
1624HexagonTargetLowering::LowerToTLSInitialExecModel(GlobalAddressSDNode *GA,
1625 SelectionDAG &DAG) const {
1626 SDLoc dl(GA);
1627 int64_t Offset = GA->getOffset();
1628 auto PtrVT = getPointerTy(DAG.getDataLayout());
1629
1630 // Get the thread pointer.
1631 SDValue TP = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Hexagon::UGP, PtrVT);
1632
Rafael Espindola405e25a2016-06-26 22:24:01 +00001633 bool IsPositionIndependent = isPositionIndependent();
1634 unsigned char TF =
1635 IsPositionIndependent ? HexagonII::MO_IEGOT : HexagonII::MO_IE;
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001636
1637 // First generate the TLS symbol address
1638 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT,
1639 Offset, TF);
1640
1641 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1642
Rafael Espindola405e25a2016-06-26 22:24:01 +00001643 if (IsPositionIndependent) {
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001644 // Generate the GOT pointer in case of position independent code
1645 SDValue GOT = LowerGLOBAL_OFFSET_TABLE(Sym, DAG);
1646
1647 // Add the TLS Symbol address to GOT pointer.This gives
1648 // GOT relative relocation for the symbol.
1649 Sym = DAG.getNode(ISD::ADD, dl, PtrVT, GOT, Sym);
1650 }
1651
1652 // Load the offset value for TLS symbol.This offset is relative to
1653 // thread pointer.
Justin Lebar9c375812016-07-15 18:27:10 +00001654 SDValue LoadOffset =
1655 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Sym, MachinePointerInfo());
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001656
1657 // Address of the thread local variable is the add of thread
1658 // pointer and the offset of the variable.
1659 return DAG.getNode(ISD::ADD, dl, PtrVT, TP, LoadOffset);
1660}
1661
1662//
1663// Lower using the local executable model for TLS addresses
1664//
1665SDValue
1666HexagonTargetLowering::LowerToTLSLocalExecModel(GlobalAddressSDNode *GA,
1667 SelectionDAG &DAG) const {
1668 SDLoc dl(GA);
1669 int64_t Offset = GA->getOffset();
1670 auto PtrVT = getPointerTy(DAG.getDataLayout());
1671
1672 // Get the thread pointer.
1673 SDValue TP = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Hexagon::UGP, PtrVT);
1674 // Generate the TLS symbol address
1675 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT, Offset,
1676 HexagonII::MO_TPREL);
1677 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1678
1679 // Address of the thread local variable is the add of thread
1680 // pointer and the offset of the variable.
1681 return DAG.getNode(ISD::ADD, dl, PtrVT, TP, Sym);
1682}
1683
1684//
1685// Lower using the general dynamic model for TLS addresses
1686//
1687SDValue
1688HexagonTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
1689 SelectionDAG &DAG) const {
1690 SDLoc dl(GA);
1691 int64_t Offset = GA->getOffset();
1692 auto PtrVT = getPointerTy(DAG.getDataLayout());
1693
1694 // First generate the TLS symbol address
1695 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT, Offset,
1696 HexagonII::MO_GDGOT);
1697
1698 // Then, generate the GOT pointer
1699 SDValue GOT = LowerGLOBAL_OFFSET_TABLE(TGA, DAG);
1700
1701 // Add the TLS symbol and the GOT pointer
1702 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1703 SDValue Chain = DAG.getNode(ISD::ADD, dl, PtrVT, GOT, Sym);
1704
1705 // Copy over the argument to R0
1706 SDValue InFlag;
1707 Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, Hexagon::R0, Chain, InFlag);
1708 InFlag = Chain.getValue(1);
1709
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +00001710 unsigned Flags =
1711 static_cast<const HexagonSubtarget &>(DAG.getSubtarget()).useLongCalls()
1712 ? HexagonII::MO_GDPLT | HexagonII::HMOTF_ConstExtended
1713 : HexagonII::MO_GDPLT;
1714
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001715 return GetDynamicTLSAddr(DAG, Chain, GA, InFlag, PtrVT,
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +00001716 Hexagon::R0, Flags);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001717}
1718
1719//
1720// Lower TLS addresses.
1721//
1722// For now for dynamic models, we only support the general dynamic model.
1723//
1724SDValue
1725HexagonTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1726 SelectionDAG &DAG) const {
1727 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1728
1729 switch (HTM.getTLSModel(GA->getGlobal())) {
1730 case TLSModel::GeneralDynamic:
1731 case TLSModel::LocalDynamic:
1732 return LowerToTLSGeneralDynamicModel(GA, DAG);
1733 case TLSModel::InitialExec:
1734 return LowerToTLSInitialExecModel(GA, DAG);
1735 case TLSModel::LocalExec:
1736 return LowerToTLSLocalExecModel(GA, DAG);
1737 }
1738 llvm_unreachable("Bogus TLS model");
1739}
1740
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001741//===----------------------------------------------------------------------===//
1742// TargetLowering Implementation
1743//===----------------------------------------------------------------------===//
1744
Eric Christopherd737b762015-02-02 22:11:36 +00001745HexagonTargetLowering::HexagonTargetLowering(const TargetMachine &TM,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001746 const HexagonSubtarget &ST)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001747 : TargetLowering(TM), HTM(static_cast<const HexagonTargetMachine&>(TM)),
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001748 Subtarget(ST) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001749 bool IsV4 = !Subtarget.hasV5TOps();
1750 auto &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00001751 bool UseHVX = Subtarget.useHVXOps();
1752 bool UseHVXSgl = Subtarget.useHVXSglOps();
1753 bool UseHVXDbl = Subtarget.useHVXDblOps();
Sirish Pande69295b82012-05-10 20:20:25 +00001754
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001755 setPrefLoopAlignment(4);
1756 setPrefFunctionAlignment(4);
1757 setMinFunctionAlignment(2);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001758 setStackPointerRegisterToSaveRestore(HRI.getStackRegister());
1759
Krzysztof Parzyszekf228c952016-06-22 16:07:10 +00001760 setMaxAtomicSizeInBitsSupported(64);
1761 setMinCmpXchgSizeInBits(32);
1762
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001763 if (EnableHexSDNodeSched)
1764 setSchedulingPreference(Sched::VLIW);
1765 else
1766 setSchedulingPreference(Sched::Source);
1767
1768 // Limits for inline expansion of memcpy/memmove
1769 MaxStoresPerMemcpy = MaxStoresPerMemcpyCL;
1770 MaxStoresPerMemcpyOptSize = MaxStoresPerMemcpyOptSizeCL;
1771 MaxStoresPerMemmove = MaxStoresPerMemmoveCL;
1772 MaxStoresPerMemmoveOptSize = MaxStoresPerMemmoveOptSizeCL;
1773 MaxStoresPerMemset = MaxStoresPerMemsetCL;
1774 MaxStoresPerMemsetOptSize = MaxStoresPerMemsetOptSizeCL;
1775
1776 //
1777 // Set up register classes.
1778 //
1779
1780 addRegisterClass(MVT::i1, &Hexagon::PredRegsRegClass);
1781 addRegisterClass(MVT::v2i1, &Hexagon::PredRegsRegClass); // bbbbaaaa
1782 addRegisterClass(MVT::v4i1, &Hexagon::PredRegsRegClass); // ddccbbaa
1783 addRegisterClass(MVT::v8i1, &Hexagon::PredRegsRegClass); // hgfedcba
1784 addRegisterClass(MVT::i32, &Hexagon::IntRegsRegClass);
1785 addRegisterClass(MVT::v4i8, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001786 addRegisterClass(MVT::v2i16, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001787 addRegisterClass(MVT::i64, &Hexagon::DoubleRegsRegClass);
1788 addRegisterClass(MVT::v8i8, &Hexagon::DoubleRegsRegClass);
1789 addRegisterClass(MVT::v4i16, &Hexagon::DoubleRegsRegClass);
1790 addRegisterClass(MVT::v2i32, &Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001791
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001792 if (Subtarget.hasV5TOps()) {
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001793 addRegisterClass(MVT::f32, &Hexagon::IntRegsRegClass);
1794 addRegisterClass(MVT::f64, &Hexagon::DoubleRegsRegClass);
1795 }
Sirish Pande69295b82012-05-10 20:20:25 +00001796
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001797 if (Subtarget.hasV60TOps()) {
1798 if (Subtarget.useHVXSglOps()) {
1799 addRegisterClass(MVT::v64i8, &Hexagon::VectorRegsRegClass);
1800 addRegisterClass(MVT::v32i16, &Hexagon::VectorRegsRegClass);
1801 addRegisterClass(MVT::v16i32, &Hexagon::VectorRegsRegClass);
1802 addRegisterClass(MVT::v8i64, &Hexagon::VectorRegsRegClass);
1803 addRegisterClass(MVT::v128i8, &Hexagon::VecDblRegsRegClass);
1804 addRegisterClass(MVT::v64i16, &Hexagon::VecDblRegsRegClass);
1805 addRegisterClass(MVT::v32i32, &Hexagon::VecDblRegsRegClass);
1806 addRegisterClass(MVT::v16i64, &Hexagon::VecDblRegsRegClass);
1807 addRegisterClass(MVT::v512i1, &Hexagon::VecPredRegsRegClass);
1808 } else if (Subtarget.useHVXDblOps()) {
1809 addRegisterClass(MVT::v128i8, &Hexagon::VectorRegs128BRegClass);
1810 addRegisterClass(MVT::v64i16, &Hexagon::VectorRegs128BRegClass);
1811 addRegisterClass(MVT::v32i32, &Hexagon::VectorRegs128BRegClass);
1812 addRegisterClass(MVT::v16i64, &Hexagon::VectorRegs128BRegClass);
1813 addRegisterClass(MVT::v256i8, &Hexagon::VecDblRegs128BRegClass);
1814 addRegisterClass(MVT::v128i16, &Hexagon::VecDblRegs128BRegClass);
1815 addRegisterClass(MVT::v64i32, &Hexagon::VecDblRegs128BRegClass);
1816 addRegisterClass(MVT::v32i64, &Hexagon::VecDblRegs128BRegClass);
1817 addRegisterClass(MVT::v1024i1, &Hexagon::VecPredRegs128BRegClass);
1818 }
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001819 }
1820
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001821 //
1822 // Handling of scalar operations.
1823 //
1824 // All operations default to "legal", except:
1825 // - indexed loads and stores (pre-/post-incremented),
1826 // - ANY_EXTEND_VECTOR_INREG, ATOMIC_CMP_SWAP_WITH_SUCCESS, CONCAT_VECTORS,
1827 // ConstantFP, DEBUGTRAP, FCEIL, FCOPYSIGN, FEXP, FEXP2, FFLOOR, FGETSIGN,
1828 // FLOG, FLOG2, FLOG10, FMAXNUM, FMINNUM, FNEARBYINT, FRINT, FROUND, TRAP,
1829 // FTRUNC, PREFETCH, SIGN_EXTEND_VECTOR_INREG, ZERO_EXTEND_VECTOR_INREG,
1830 // which default to "expand" for at least one type.
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001831
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001832 // Misc operations.
1833 setOperationAction(ISD::ConstantFP, MVT::f32, Legal); // Default: expand
1834 setOperationAction(ISD::ConstantFP, MVT::f64, Legal); // Default: expand
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001835
1836 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001837 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001838 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001839 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
1840 setOperationAction(ISD::INLINEASM, MVT::Other, Custom);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00001841 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +00001842 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00001843 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001844 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001845 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001846 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001847 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001848
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001849 // Custom legalize GlobalAddress nodes into CONST32.
1850 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001851 setOperationAction(ISD::GlobalAddress, MVT::i8, Custom);
1852 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001853
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001854 // Hexagon needs to optimize cases with negative constants.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001855 setOperationAction(ISD::SETCC, MVT::i8, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001856 setOperationAction(ISD::SETCC, MVT::i16, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001857
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001858 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
1859 setOperationAction(ISD::VASTART, MVT::Other, Custom);
1860 setOperationAction(ISD::VAEND, MVT::Other, Expand);
1861 setOperationAction(ISD::VAARG, MVT::Other, Expand);
1862
1863 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
1864 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
1865 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
1866
1867 if (EmitJumpTables)
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001868 setMinimumJumpTableEntries(MinimumJumpTables);
Krzysztof Parzyszeka61f7da2016-01-13 21:43:13 +00001869 else
Eugene Zelenko58655bb2016-12-17 01:09:05 +00001870 setMinimumJumpTableEntries(std::numeric_limits<int>::max());
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001871 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001872
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001873 // Hexagon has instructions for add/sub with carry. The problem with
1874 // modeling these instructions is that they produce 2 results: Rdd and Px.
1875 // To model the update of Px, we will have to use Defs[p0..p3] which will
1876 // cause any predicate live range to spill. So, we pretend we dont't have
1877 // these instructions.
1878 setOperationAction(ISD::ADDE, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001879 setOperationAction(ISD::ADDE, MVT::i16, Expand);
1880 setOperationAction(ISD::ADDE, MVT::i32, Expand);
1881 setOperationAction(ISD::ADDE, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001882 setOperationAction(ISD::SUBE, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001883 setOperationAction(ISD::SUBE, MVT::i16, Expand);
1884 setOperationAction(ISD::SUBE, MVT::i32, Expand);
1885 setOperationAction(ISD::SUBE, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001886 setOperationAction(ISD::ADDC, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001887 setOperationAction(ISD::ADDC, MVT::i16, Expand);
1888 setOperationAction(ISD::ADDC, MVT::i32, Expand);
1889 setOperationAction(ISD::ADDC, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001890 setOperationAction(ISD::SUBC, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001891 setOperationAction(ISD::SUBC, MVT::i16, Expand);
1892 setOperationAction(ISD::SUBC, MVT::i32, Expand);
1893 setOperationAction(ISD::SUBC, MVT::i64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001894
Krzysztof Parzyszek2c4487d2015-04-13 20:37:01 +00001895 // Only add and sub that detect overflow are the saturating ones.
1896 for (MVT VT : MVT::integer_valuetypes()) {
1897 setOperationAction(ISD::UADDO, VT, Expand);
1898 setOperationAction(ISD::SADDO, VT, Expand);
1899 setOperationAction(ISD::USUBO, VT, Expand);
1900 setOperationAction(ISD::SSUBO, VT, Expand);
1901 }
1902
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001903 setOperationAction(ISD::CTLZ, MVT::i8, Promote);
1904 setOperationAction(ISD::CTLZ, MVT::i16, Promote);
1905 setOperationAction(ISD::CTTZ, MVT::i8, Promote);
1906 setOperationAction(ISD::CTTZ, MVT::i16, Promote);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001907
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001908 // In V5, popcount can count # of 1s in i64 but returns i32.
1909 // On V4 it will be expanded (set later).
1910 setOperationAction(ISD::CTPOP, MVT::i8, Promote);
1911 setOperationAction(ISD::CTPOP, MVT::i16, Promote);
1912 setOperationAction(ISD::CTPOP, MVT::i32, Promote);
Krzysztof Parzyszekaf5ff652017-02-23 15:02:09 +00001913 setOperationAction(ISD::CTPOP, MVT::i64, Legal);
1914
1915 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
1916 setOperationAction(ISD::BITREVERSE, MVT::i64, Legal);
1917 setOperationAction(ISD::BSWAP, MVT::i32, Legal);
1918 setOperationAction(ISD::BSWAP, MVT::i64, Legal);
Krzysztof Parzyszekef580172017-05-30 17:47:51 +00001919 setOperationAction(ISD::MUL, MVT::i64, Legal);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001920
Benjamin Kramer62460692015-04-25 14:46:53 +00001921 for (unsigned IntExpOp :
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001922 { ISD::SDIV, ISD::UDIV, ISD::SREM, ISD::UREM,
1923 ISD::SDIVREM, ISD::UDIVREM, ISD::ROTL, ISD::ROTR,
Krzysztof Parzyszekaf5ff652017-02-23 15:02:09 +00001924 ISD::SHL_PARTS, ISD::SRA_PARTS, ISD::SRL_PARTS,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001925 ISD::SMUL_LOHI, ISD::UMUL_LOHI }) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001926 setOperationAction(IntExpOp, MVT::i32, Expand);
1927 setOperationAction(IntExpOp, MVT::i64, Expand);
1928 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001929
Benjamin Kramer62460692015-04-25 14:46:53 +00001930 for (unsigned FPExpOp :
1931 {ISD::FDIV, ISD::FREM, ISD::FSQRT, ISD::FSIN, ISD::FCOS, ISD::FSINCOS,
1932 ISD::FPOW, ISD::FCOPYSIGN}) {
1933 setOperationAction(FPExpOp, MVT::f32, Expand);
1934 setOperationAction(FPExpOp, MVT::f64, Expand);
1935 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001936
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001937 // No extending loads from i32.
1938 for (MVT VT : MVT::integer_valuetypes()) {
1939 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
1940 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
1941 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
1942 }
1943 // Turn FP truncstore into trunc + store.
1944 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Michael Kuperstein2bc3d4d2016-08-18 20:08:15 +00001945 // Turn FP extload into load/fpextend.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001946 for (MVT VT : MVT::fp_valuetypes())
1947 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001948
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001949 // Expand BR_CC and SELECT_CC for all integer and fp types.
1950 for (MVT VT : MVT::integer_valuetypes()) {
1951 setOperationAction(ISD::BR_CC, VT, Expand);
1952 setOperationAction(ISD::SELECT_CC, VT, Expand);
1953 }
1954 for (MVT VT : MVT::fp_valuetypes()) {
1955 setOperationAction(ISD::BR_CC, VT, Expand);
1956 setOperationAction(ISD::SELECT_CC, VT, Expand);
1957 }
1958 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001959
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001960 //
1961 // Handling of vector operations.
1962 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001963
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001964 // Custom lower v4i16 load only. Let v4i16 store to be
1965 // promoted for now.
1966 promoteLdStType(MVT::v4i8, MVT::i32);
1967 promoteLdStType(MVT::v2i16, MVT::i32);
1968 promoteLdStType(MVT::v8i8, MVT::i64);
1969 promoteLdStType(MVT::v2i32, MVT::i64);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001970
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001971 setOperationAction(ISD::LOAD, MVT::v4i16, Custom);
1972 setOperationAction(ISD::STORE, MVT::v4i16, Promote);
1973 AddPromotedToType(ISD::LOAD, MVT::v4i16, MVT::i64);
1974 AddPromotedToType(ISD::STORE, MVT::v4i16, MVT::i64);
1975
1976 // Set the action for vector operations to "expand", then override it with
1977 // either "custom" or "legal" for specific cases.
Craig Topper26260942015-10-18 05:15:34 +00001978 static const unsigned VectExpOps[] = {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001979 // Integer arithmetic:
1980 ISD::ADD, ISD::SUB, ISD::MUL, ISD::SDIV, ISD::UDIV,
1981 ISD::SREM, ISD::UREM, ISD::SDIVREM, ISD::UDIVREM, ISD::ADDC,
1982 ISD::SUBC, ISD::SADDO, ISD::UADDO, ISD::SSUBO, ISD::USUBO,
1983 ISD::SMUL_LOHI, ISD::UMUL_LOHI,
1984 // Logical/bit:
1985 ISD::AND, ISD::OR, ISD::XOR, ISD::ROTL, ISD::ROTR,
Craig Topper33772c52016-04-28 03:34:31 +00001986 ISD::CTPOP, ISD::CTLZ, ISD::CTTZ,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001987 // Floating point arithmetic/math functions:
1988 ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FMA, ISD::FDIV,
1989 ISD::FREM, ISD::FNEG, ISD::FABS, ISD::FSQRT, ISD::FSIN,
Craig Topperf6d4dc52017-05-30 15:27:55 +00001990 ISD::FCOS, ISD::FPOW, ISD::FLOG, ISD::FLOG2,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001991 ISD::FLOG10, ISD::FEXP, ISD::FEXP2, ISD::FCEIL, ISD::FTRUNC,
1992 ISD::FRINT, ISD::FNEARBYINT, ISD::FROUND, ISD::FFLOOR,
1993 ISD::FMINNUM, ISD::FMAXNUM, ISD::FSINCOS,
1994 // Misc:
Krzysztof Parzyszek046da742016-10-27 14:30:16 +00001995 ISD::BR_CC, ISD::SELECT_CC, ISD::ConstantPool,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001996 // Vector:
1997 ISD::BUILD_VECTOR, ISD::SCALAR_TO_VECTOR,
1998 ISD::EXTRACT_VECTOR_ELT, ISD::INSERT_VECTOR_ELT,
1999 ISD::EXTRACT_SUBVECTOR, ISD::INSERT_SUBVECTOR,
2000 ISD::CONCAT_VECTORS, ISD::VECTOR_SHUFFLE
2001 };
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002002
2003 for (MVT VT : MVT::vector_valuetypes()) {
Benjamin Kramer62460692015-04-25 14:46:53 +00002004 for (unsigned VectExpOp : VectExpOps)
2005 setOperationAction(VectExpOp, VT, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002006
Krzysztof Parzyszeka696b1b2016-09-08 17:42:14 +00002007 // Expand all extending loads and truncating stores:
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002008 for (MVT TargetVT : MVT::vector_valuetypes()) {
Krzysztof Parzyszeka696b1b2016-09-08 17:42:14 +00002009 if (TargetVT == VT)
2010 continue;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002011 setLoadExtAction(ISD::EXTLOAD, TargetVT, VT, Expand);
Krzysztof Parzyszeka696b1b2016-09-08 17:42:14 +00002012 setLoadExtAction(ISD::ZEXTLOAD, TargetVT, VT, Expand);
2013 setLoadExtAction(ISD::SEXTLOAD, TargetVT, VT, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002014 setTruncStoreAction(VT, TargetVT, Expand);
2015 }
2016
Krzysztof Parzyszek046da742016-10-27 14:30:16 +00002017 // Normalize all inputs to SELECT to be vectors of i32.
2018 if (VT.getVectorElementType() != MVT::i32) {
2019 MVT VT32 = MVT::getVectorVT(MVT::i32, VT.getSizeInBits()/32);
2020 setOperationAction(ISD::SELECT, VT, Promote);
2021 AddPromotedToType(ISD::SELECT, VT, VT32);
2022 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002023 setOperationAction(ISD::SRA, VT, Custom);
2024 setOperationAction(ISD::SHL, VT, Custom);
2025 setOperationAction(ISD::SRL, VT, Custom);
2026 }
2027
2028 // Types natively supported:
Benjamin Kramer62460692015-04-25 14:46:53 +00002029 for (MVT NativeVT : {MVT::v2i1, MVT::v4i1, MVT::v8i1, MVT::v32i1, MVT::v64i1,
2030 MVT::v4i8, MVT::v8i8, MVT::v2i16, MVT::v4i16, MVT::v1i32,
2031 MVT::v2i32, MVT::v1i64}) {
2032 setOperationAction(ISD::BUILD_VECTOR, NativeVT, Custom);
2033 setOperationAction(ISD::EXTRACT_VECTOR_ELT, NativeVT, Custom);
2034 setOperationAction(ISD::INSERT_VECTOR_ELT, NativeVT, Custom);
2035 setOperationAction(ISD::EXTRACT_SUBVECTOR, NativeVT, Custom);
2036 setOperationAction(ISD::INSERT_SUBVECTOR, NativeVT, Custom);
2037 setOperationAction(ISD::CONCAT_VECTORS, NativeVT, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002038
Benjamin Kramer62460692015-04-25 14:46:53 +00002039 setOperationAction(ISD::ADD, NativeVT, Legal);
2040 setOperationAction(ISD::SUB, NativeVT, Legal);
2041 setOperationAction(ISD::MUL, NativeVT, Legal);
2042 setOperationAction(ISD::AND, NativeVT, Legal);
2043 setOperationAction(ISD::OR, NativeVT, Legal);
2044 setOperationAction(ISD::XOR, NativeVT, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002045 }
2046
2047 setOperationAction(ISD::SETCC, MVT::v2i16, Custom);
2048 setOperationAction(ISD::VSELECT, MVT::v2i16, Custom);
2049 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
2050 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00002051
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002052 if (UseHVX) {
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002053 if (UseHVXSgl) {
2054 setOperationAction(ISD::CONCAT_VECTORS, MVT::v128i8, Custom);
2055 setOperationAction(ISD::CONCAT_VECTORS, MVT::v64i16, Custom);
2056 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i32, Custom);
2057 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i64, Custom);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002058 // We try to generate the vpack{e/o} instructions. If we fail
2059 // we fall back upon ExpandOp.
2060 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v64i8, Custom);
2061 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v32i16, Custom);
2062 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v64i8, Custom);
2063 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v32i16, Custom);
2064 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v16i32, Custom);
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002065 } else if (UseHVXDbl) {
2066 setOperationAction(ISD::CONCAT_VECTORS, MVT::v256i8, Custom);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002067 setOperationAction(ISD::CONCAT_VECTORS, MVT::v128i16, Custom);
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002068 setOperationAction(ISD::CONCAT_VECTORS, MVT::v64i32, Custom);
2069 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i64, Custom);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002070 // We try to generate the vpack{e/o} instructions. If we fail
2071 // we fall back upon ExpandOp.
2072 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v128i8, Custom);
2073 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v64i16, Custom);
2074 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v4i32, Custom);
2075 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v128i8, Custom);
2076 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v64i16, Custom);
2077 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v32i32, Custom);
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002078 } else {
2079 llvm_unreachable("Unrecognized HVX mode");
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002080 }
2081 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002082 // Subtarget-specific operation actions.
2083 //
2084 if (Subtarget.hasV5TOps()) {
2085 setOperationAction(ISD::FMA, MVT::f64, Expand);
2086 setOperationAction(ISD::FADD, MVT::f64, Expand);
2087 setOperationAction(ISD::FSUB, MVT::f64, Expand);
2088 setOperationAction(ISD::FMUL, MVT::f64, Expand);
2089
Krzysztof Parzyszekbd8ef4b2016-08-19 13:34:31 +00002090 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
2091 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
2092
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002093 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Promote);
2094 setOperationAction(ISD::FP_TO_UINT, MVT::i8, Promote);
2095 setOperationAction(ISD::FP_TO_UINT, MVT::i16, Promote);
2096 setOperationAction(ISD::FP_TO_SINT, MVT::i1, Promote);
2097 setOperationAction(ISD::FP_TO_SINT, MVT::i8, Promote);
2098 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
2099 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote);
2100 setOperationAction(ISD::UINT_TO_FP, MVT::i8, Promote);
2101 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote);
2102 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote);
2103 setOperationAction(ISD::SINT_TO_FP, MVT::i8, Promote);
2104 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002105 } else { // V4
2106 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
2107 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Expand);
2108 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
2109 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
2110 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Expand);
2111 setOperationAction(ISD::FP_TO_SINT, MVT::f32, Expand);
2112 setOperationAction(ISD::FP_EXTEND, MVT::f32, Expand);
2113 setOperationAction(ISD::FP_ROUND, MVT::f64, Expand);
2114 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
2115
2116 setOperationAction(ISD::CTPOP, MVT::i8, Expand);
2117 setOperationAction(ISD::CTPOP, MVT::i16, Expand);
2118 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
2119 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
2120
2121 // Expand these operations for both f32 and f64:
Benjamin Kramer62460692015-04-25 14:46:53 +00002122 for (unsigned FPExpOpV4 :
2123 {ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FABS, ISD::FNEG, ISD::FMA}) {
2124 setOperationAction(FPExpOpV4, MVT::f32, Expand);
2125 setOperationAction(FPExpOpV4, MVT::f64, Expand);
2126 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002127
Benjamin Kramer62460692015-04-25 14:46:53 +00002128 for (ISD::CondCode FPExpCCV4 :
2129 {ISD::SETOEQ, ISD::SETOGT, ISD::SETOLT, ISD::SETOGE, ISD::SETOLE,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002130 ISD::SETUO, ISD::SETO}) {
Benjamin Kramer62460692015-04-25 14:46:53 +00002131 setCondCodeAction(FPExpCCV4, MVT::f32, Expand);
2132 setCondCodeAction(FPExpCCV4, MVT::f64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002133 }
2134 }
2135
2136 // Handling of indexed loads/stores: default is "expand".
2137 //
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +00002138 for (MVT VT : {MVT::i8, MVT::i16, MVT::i32, MVT::i64}) {
2139 setIndexedLoadAction(ISD::POST_INC, VT, Legal);
2140 setIndexedStoreAction(ISD::POST_INC, VT, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002141 }
2142
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +00002143 if (UseHVXSgl) {
2144 for (MVT VT : {MVT::v64i8, MVT::v32i16, MVT::v16i32, MVT::v8i64,
2145 MVT::v128i8, MVT::v64i16, MVT::v32i32, MVT::v16i64}) {
2146 setIndexedLoadAction(ISD::POST_INC, VT, Legal);
2147 setIndexedStoreAction(ISD::POST_INC, VT, Legal);
2148 }
2149 } else if (UseHVXDbl) {
2150 for (MVT VT : {MVT::v128i8, MVT::v64i16, MVT::v32i32, MVT::v16i64,
2151 MVT::v256i8, MVT::v128i16, MVT::v64i32, MVT::v32i64}) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002152 setIndexedLoadAction(ISD::POST_INC, VT, Legal);
2153 setIndexedStoreAction(ISD::POST_INC, VT, Legal);
2154 }
2155 }
2156
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002157 computeRegisterProperties(&HRI);
2158
2159 //
2160 // Library calls for unsupported operations
2161 //
2162 bool FastMath = EnableFastMath;
2163
Benjamin Kramera37c8092015-04-25 14:46:46 +00002164 setLibcallName(RTLIB::SDIV_I32, "__hexagon_divsi3");
2165 setLibcallName(RTLIB::SDIV_I64, "__hexagon_divdi3");
2166 setLibcallName(RTLIB::UDIV_I32, "__hexagon_udivsi3");
2167 setLibcallName(RTLIB::UDIV_I64, "__hexagon_udivdi3");
2168 setLibcallName(RTLIB::SREM_I32, "__hexagon_modsi3");
2169 setLibcallName(RTLIB::SREM_I64, "__hexagon_moddi3");
2170 setLibcallName(RTLIB::UREM_I32, "__hexagon_umodsi3");
2171 setLibcallName(RTLIB::UREM_I64, "__hexagon_umoddi3");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002172
Benjamin Kramera37c8092015-04-25 14:46:46 +00002173 setLibcallName(RTLIB::SINTTOFP_I128_F64, "__hexagon_floattidf");
2174 setLibcallName(RTLIB::SINTTOFP_I128_F32, "__hexagon_floattisf");
2175 setLibcallName(RTLIB::FPTOUINT_F32_I128, "__hexagon_fixunssfti");
2176 setLibcallName(RTLIB::FPTOUINT_F64_I128, "__hexagon_fixunsdfti");
2177 setLibcallName(RTLIB::FPTOSINT_F32_I128, "__hexagon_fixsfti");
2178 setLibcallName(RTLIB::FPTOSINT_F64_I128, "__hexagon_fixdfti");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002179
2180 if (IsV4) {
2181 // Handle single-precision floating point operations on V4.
Benjamin Kramera37c8092015-04-25 14:46:46 +00002182 if (FastMath) {
2183 setLibcallName(RTLIB::ADD_F32, "__hexagon_fast_addsf3");
2184 setLibcallName(RTLIB::SUB_F32, "__hexagon_fast_subsf3");
2185 setLibcallName(RTLIB::MUL_F32, "__hexagon_fast_mulsf3");
2186 setLibcallName(RTLIB::OGT_F32, "__hexagon_fast_gtsf2");
2187 setLibcallName(RTLIB::OLT_F32, "__hexagon_fast_ltsf2");
2188 // Double-precision compares.
2189 setLibcallName(RTLIB::OGT_F64, "__hexagon_fast_gtdf2");
2190 setLibcallName(RTLIB::OLT_F64, "__hexagon_fast_ltdf2");
2191 } else {
2192 setLibcallName(RTLIB::ADD_F32, "__hexagon_addsf3");
2193 setLibcallName(RTLIB::SUB_F32, "__hexagon_subsf3");
2194 setLibcallName(RTLIB::MUL_F32, "__hexagon_mulsf3");
2195 setLibcallName(RTLIB::OGT_F32, "__hexagon_gtsf2");
2196 setLibcallName(RTLIB::OLT_F32, "__hexagon_ltsf2");
2197 // Double-precision compares.
2198 setLibcallName(RTLIB::OGT_F64, "__hexagon_gtdf2");
2199 setLibcallName(RTLIB::OLT_F64, "__hexagon_ltdf2");
2200 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002201 }
2202
2203 // This is the only fast library function for sqrtd.
2204 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00002205 setLibcallName(RTLIB::SQRT_F64, "__hexagon_fast2_sqrtdf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002206
Benjamin Kramera37c8092015-04-25 14:46:46 +00002207 // Prefix is: nothing for "slow-math",
2208 // "fast2_" for V4 fast-math and V5+ fast-math double-precision
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002209 // (actually, keep fast-math and fast-math2 separate for now)
Benjamin Kramera37c8092015-04-25 14:46:46 +00002210 if (FastMath) {
2211 setLibcallName(RTLIB::ADD_F64, "__hexagon_fast_adddf3");
2212 setLibcallName(RTLIB::SUB_F64, "__hexagon_fast_subdf3");
2213 setLibcallName(RTLIB::MUL_F64, "__hexagon_fast_muldf3");
2214 setLibcallName(RTLIB::DIV_F64, "__hexagon_fast_divdf3");
2215 // Calling __hexagon_fast2_divsf3 with fast-math on V5 (ok).
2216 setLibcallName(RTLIB::DIV_F32, "__hexagon_fast_divsf3");
2217 } else {
2218 setLibcallName(RTLIB::ADD_F64, "__hexagon_adddf3");
2219 setLibcallName(RTLIB::SUB_F64, "__hexagon_subdf3");
2220 setLibcallName(RTLIB::MUL_F64, "__hexagon_muldf3");
2221 setLibcallName(RTLIB::DIV_F64, "__hexagon_divdf3");
2222 setLibcallName(RTLIB::DIV_F32, "__hexagon_divsf3");
2223 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002224
2225 if (Subtarget.hasV5TOps()) {
2226 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00002227 setLibcallName(RTLIB::SQRT_F32, "__hexagon_fast2_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002228 else
Benjamin Kramera37c8092015-04-25 14:46:46 +00002229 setLibcallName(RTLIB::SQRT_F32, "__hexagon_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002230 } else {
2231 // V4
Benjamin Kramera37c8092015-04-25 14:46:46 +00002232 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__hexagon_floatsisf");
2233 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__hexagon_floatsidf");
2234 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__hexagon_floatdisf");
2235 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__hexagon_floatdidf");
2236 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__hexagon_floatunsisf");
2237 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__hexagon_floatunsidf");
2238 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__hexagon_floatundisf");
2239 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__hexagon_floatundidf");
2240 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__hexagon_fixunssfsi");
2241 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__hexagon_fixunssfdi");
2242 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__hexagon_fixunsdfsi");
2243 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__hexagon_fixunsdfdi");
2244 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__hexagon_fixsfsi");
2245 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__hexagon_fixsfdi");
2246 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__hexagon_fixdfsi");
2247 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__hexagon_fixdfdi");
2248 setLibcallName(RTLIB::FPEXT_F32_F64, "__hexagon_extendsfdf2");
2249 setLibcallName(RTLIB::FPROUND_F64_F32, "__hexagon_truncdfsf2");
2250 setLibcallName(RTLIB::OEQ_F32, "__hexagon_eqsf2");
2251 setLibcallName(RTLIB::OEQ_F64, "__hexagon_eqdf2");
2252 setLibcallName(RTLIB::OGE_F32, "__hexagon_gesf2");
2253 setLibcallName(RTLIB::OGE_F64, "__hexagon_gedf2");
2254 setLibcallName(RTLIB::OLE_F32, "__hexagon_lesf2");
2255 setLibcallName(RTLIB::OLE_F64, "__hexagon_ledf2");
2256 setLibcallName(RTLIB::UNE_F32, "__hexagon_nesf2");
2257 setLibcallName(RTLIB::UNE_F64, "__hexagon_nedf2");
2258 setLibcallName(RTLIB::UO_F32, "__hexagon_unordsf2");
2259 setLibcallName(RTLIB::UO_F64, "__hexagon_unorddf2");
2260 setLibcallName(RTLIB::O_F32, "__hexagon_unordsf2");
2261 setLibcallName(RTLIB::O_F64, "__hexagon_unorddf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002262 }
2263
2264 // These cause problems when the shift amount is non-constant.
2265 setLibcallName(RTLIB::SHL_I128, nullptr);
2266 setLibcallName(RTLIB::SRL_I128, nullptr);
2267 setLibcallName(RTLIB::SRA_I128, nullptr);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002268}
2269
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002270const char* HexagonTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00002271 switch ((HexagonISD::NodeType)Opcode) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002272 case HexagonISD::ALLOCA: return "HexagonISD::ALLOCA";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002273 case HexagonISD::AT_GOT: return "HexagonISD::AT_GOT";
2274 case HexagonISD::AT_PCREL: return "HexagonISD::AT_PCREL";
2275 case HexagonISD::BARRIER: return "HexagonISD::BARRIER";
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002276 case HexagonISD::CALL: return "HexagonISD::CALL";
2277 case HexagonISD::CALLnr: return "HexagonISD::CALLnr";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002278 case HexagonISD::CALLR: return "HexagonISD::CALLR";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002279 case HexagonISD::COMBINE: return "HexagonISD::COMBINE";
2280 case HexagonISD::CONST32_GP: return "HexagonISD::CONST32_GP";
2281 case HexagonISD::CONST32: return "HexagonISD::CONST32";
2282 case HexagonISD::CP: return "HexagonISD::CP";
2283 case HexagonISD::DCFETCH: return "HexagonISD::DCFETCH";
2284 case HexagonISD::EH_RETURN: return "HexagonISD::EH_RETURN";
2285 case HexagonISD::EXTRACTU: return "HexagonISD::EXTRACTU";
2286 case HexagonISD::EXTRACTURP: return "HexagonISD::EXTRACTURP";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002287 case HexagonISD::INSERT: return "HexagonISD::INSERT";
2288 case HexagonISD::INSERTRP: return "HexagonISD::INSERTRP";
2289 case HexagonISD::JT: return "HexagonISD::JT";
2290 case HexagonISD::PACKHL: return "HexagonISD::PACKHL";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002291 case HexagonISD::RET_FLAG: return "HexagonISD::RET_FLAG";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002292 case HexagonISD::TC_RETURN: return "HexagonISD::TC_RETURN";
Krzysztof Parzyszekc168c012015-12-03 16:47:20 +00002293 case HexagonISD::VCOMBINE: return "HexagonISD::VCOMBINE";
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002294 case HexagonISD::VPACK: return "HexagonISD::VPACK";
Krzysztof Parzyszekf85dd9f2017-07-10 20:16:44 +00002295 case HexagonISD::VASL: return "HexagonISD::VASL";
2296 case HexagonISD::VASR: return "HexagonISD::VASR";
2297 case HexagonISD::VLSR: return "HexagonISD::VLSR";
2298 case HexagonISD::VSPLAT: return "HexagonISD::VSPLAT";
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +00002299 case HexagonISD::READCYCLE: return "HexagonISD::READCYCLE";
Matthias Braund04893f2015-05-07 21:33:59 +00002300 case HexagonISD::OP_END: break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002301 }
Matthias Braund04893f2015-05-07 21:33:59 +00002302 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002303}
2304
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002305bool HexagonTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002306 EVT MTy1 = EVT::getEVT(Ty1);
2307 EVT MTy2 = EVT::getEVT(Ty2);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002308 if (!MTy1.isSimple() || !MTy2.isSimple())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002309 return false;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002310 return (MTy1.getSimpleVT() == MVT::i64) && (MTy2.getSimpleVT() == MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002311}
2312
2313bool HexagonTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002314 if (!VT1.isSimple() || !VT2.isSimple())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002315 return false;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002316 return (VT1.getSimpleVT() == MVT::i64) && (VT2.getSimpleVT() == MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002317}
2318
Krzysztof Parzyszekbd8ef4b2016-08-19 13:34:31 +00002319bool HexagonTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
2320 return isOperationLegalOrCustom(ISD::FMA, VT);
2321}
2322
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002323// Should we expand the build vector with shuffles?
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00002324bool HexagonTargetLowering::shouldExpandBuildVectorWithShuffles(EVT VT,
2325 unsigned DefinedValues) const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002326 // Hexagon vector shuffle operates on element sizes of bytes or halfwords
2327 EVT EltVT = VT.getVectorElementType();
2328 int EltBits = EltVT.getSizeInBits();
2329 if ((EltBits != 8) && (EltBits != 16))
2330 return false;
2331
2332 return TargetLowering::shouldExpandBuildVectorWithShuffles(VT, DefinedValues);
2333}
2334
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00002335static StridedLoadKind isStridedLoad(const ArrayRef<int> &Mask) {
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002336 int even_start = -2;
2337 int odd_start = -1;
2338 size_t mask_len = Mask.size();
2339 for (auto idx : Mask) {
2340 if ((idx - even_start) == 2)
2341 even_start = idx;
2342 else
2343 break;
2344 }
2345 if (even_start == (int)(mask_len * 2) - 2)
2346 return StridedLoadKind::Even;
2347 for (auto idx : Mask) {
2348 if ((idx - odd_start) == 2)
2349 odd_start = idx;
2350 else
2351 break;
2352 }
2353 if (odd_start == (int)(mask_len * 2) - 1)
2354 return StridedLoadKind::Odd;
2355
2356 return StridedLoadKind::NoPattern;
2357}
2358
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00002359bool HexagonTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &Mask,
2360 EVT VT) const {
2361 if (Subtarget.useHVXOps())
2362 return isStridedLoad(Mask) != StridedLoadKind::NoPattern;
2363 return true;
2364}
2365
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002366// Lower a vector shuffle (V1, V2, V3). V1 and V2 are the two vectors
2367// to select data from, V3 is the permutation.
2368SDValue
2369HexagonTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG)
2370 const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002371 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
2372 SDValue V1 = Op.getOperand(0);
2373 SDValue V2 = Op.getOperand(1);
2374 SDLoc dl(Op);
2375 EVT VT = Op.getValueType();
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002376 bool UseHVX = Subtarget.useHVXOps();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002377
Sanjay Patel57195842016-03-14 17:28:46 +00002378 if (V2.isUndef())
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002379 V2 = V1;
2380
2381 if (SVN->isSplat()) {
2382 int Lane = SVN->getSplatIndex();
2383 if (Lane == -1) Lane = 0;
2384
2385 // Test if V1 is a SCALAR_TO_VECTOR.
2386 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR)
Krzysztof Parzyszek89b2d7c2017-07-13 18:17:58 +00002387 return DAG.getNode(HexagonISD::VSPLAT, dl, VT, V1.getOperand(0));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002388
2389 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
2390 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
2391 // reaches it).
2392 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
2393 !isa<ConstantSDNode>(V1.getOperand(0))) {
2394 bool IsScalarToVector = true;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00002395 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i) {
Sanjay Patel75068522016-03-14 18:09:43 +00002396 if (!V1.getOperand(i).isUndef()) {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002397 IsScalarToVector = false;
2398 break;
2399 }
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00002400 }
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002401 if (IsScalarToVector)
Krzysztof Parzyszek89b2d7c2017-07-13 18:17:58 +00002402 return DAG.getNode(HexagonISD::VSPLAT, dl, VT, V1.getOperand(0));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002403 }
Krzysztof Parzyszek89b2d7c2017-07-13 18:17:58 +00002404 return DAG.getNode(HexagonISD::VSPLAT, dl, VT,
2405 DAG.getConstant(Lane, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002406 }
2407
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002408 if (UseHVX) {
2409 ArrayRef<int> Mask = SVN->getMask();
2410 size_t MaskLen = Mask.size();
Sanjay Patel1ed771f2016-09-14 16:37:15 +00002411 int ElemSizeInBits = VT.getScalarSizeInBits();
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002412 if ((Subtarget.useHVXSglOps() && (ElemSizeInBits * MaskLen) == 64 * 8) ||
2413 (Subtarget.useHVXDblOps() && (ElemSizeInBits * MaskLen) == 128 * 8)) {
2414 // Return 1 for odd and 2 of even
2415 StridedLoadKind Pattern = isStridedLoad(Mask);
2416
2417 if (Pattern == StridedLoadKind::NoPattern)
2418 return SDValue();
2419
2420 SDValue Vec0 = Op.getOperand(0);
2421 SDValue Vec1 = Op.getOperand(1);
2422 SDValue StridePattern = DAG.getConstant(Pattern, dl, MVT::i32);
2423 SDValue Ops[] = { Vec1, Vec0, StridePattern };
2424 return DAG.getNode(HexagonISD::VPACK, dl, VT, Ops);
2425 }
2426 // We used to assert in the "else" part here, but that is bad for Halide
2427 // Halide creates intermediate double registers by interleaving two
2428 // concatenated vector registers. The interleaving requires vector_shuffle
2429 // nodes and we shouldn't barf on a double register result of a
2430 // vector_shuffle because it is most likely an intermediate result.
2431 }
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002432 // FIXME: We need to support more general vector shuffles. See
2433 // below the comment from the ARM backend that deals in the general
2434 // case with the vector shuffles. For now, let expand handle these.
2435 return SDValue();
2436
2437 // If the shuffle is not directly supported and it has 4 elements, use
2438 // the PerfectShuffle-generated table to synthesize it from other shuffles.
2439}
2440
2441// If BUILD_VECTOR has same base element repeated several times,
2442// report true.
2443static bool isCommonSplatElement(BuildVectorSDNode *BVN) {
2444 unsigned NElts = BVN->getNumOperands();
2445 SDValue V0 = BVN->getOperand(0);
2446
2447 for (unsigned i = 1, e = NElts; i != e; ++i) {
2448 if (BVN->getOperand(i) != V0)
2449 return false;
2450 }
2451 return true;
2452}
2453
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002454// Lower a vector shift. Try to convert
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002455// <VT> = SHL/SRA/SRL <VT> by <VT> to Hexagon specific
2456// <VT> = SHL/SRA/SRL <VT> by <IT/i32>.
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002457SDValue
2458HexagonTargetLowering::LowerVECTOR_SHIFT(SDValue Op, SelectionDAG &DAG) const {
Eugene Zelenko58655bb2016-12-17 01:09:05 +00002459 BuildVectorSDNode *BVN = nullptr;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002460 SDValue V1 = Op.getOperand(0);
2461 SDValue V2 = Op.getOperand(1);
2462 SDValue V3;
2463 SDLoc dl(Op);
2464 EVT VT = Op.getValueType();
2465
2466 if ((BVN = dyn_cast<BuildVectorSDNode>(V1.getNode())) &&
2467 isCommonSplatElement(BVN))
2468 V3 = V2;
2469 else if ((BVN = dyn_cast<BuildVectorSDNode>(V2.getNode())) &&
2470 isCommonSplatElement(BVN))
2471 V3 = V1;
2472 else
2473 return SDValue();
2474
2475 SDValue CommonSplat = BVN->getOperand(0);
2476 SDValue Result;
2477
2478 if (VT.getSimpleVT() == MVT::v4i16) {
2479 switch (Op.getOpcode()) {
2480 case ISD::SRA:
Krzysztof Parzyszekf85dd9f2017-07-10 20:16:44 +00002481 Result = DAG.getNode(HexagonISD::VASR, dl, VT, V3, CommonSplat);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002482 break;
2483 case ISD::SHL:
Krzysztof Parzyszekf85dd9f2017-07-10 20:16:44 +00002484 Result = DAG.getNode(HexagonISD::VASL, dl, VT, V3, CommonSplat);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002485 break;
2486 case ISD::SRL:
Krzysztof Parzyszekf85dd9f2017-07-10 20:16:44 +00002487 Result = DAG.getNode(HexagonISD::VLSR, dl, VT, V3, CommonSplat);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002488 break;
2489 default:
2490 return SDValue();
2491 }
2492 } else if (VT.getSimpleVT() == MVT::v2i32) {
2493 switch (Op.getOpcode()) {
2494 case ISD::SRA:
Krzysztof Parzyszekf85dd9f2017-07-10 20:16:44 +00002495 Result = DAG.getNode(HexagonISD::VASR, dl, VT, V3, CommonSplat);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002496 break;
2497 case ISD::SHL:
Krzysztof Parzyszekf85dd9f2017-07-10 20:16:44 +00002498 Result = DAG.getNode(HexagonISD::VASL, dl, VT, V3, CommonSplat);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002499 break;
2500 case ISD::SRL:
Krzysztof Parzyszekf85dd9f2017-07-10 20:16:44 +00002501 Result = DAG.getNode(HexagonISD::VLSR, dl, VT, V3, CommonSplat);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002502 break;
2503 default:
2504 return SDValue();
2505 }
2506 } else {
2507 return SDValue();
2508 }
2509
2510 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
2511}
2512
2513SDValue
2514HexagonTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
2515 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
2516 SDLoc dl(Op);
2517 EVT VT = Op.getValueType();
2518
2519 unsigned Size = VT.getSizeInBits();
2520
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002521 // Only handle vectors of 64 bits or shorter.
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002522 if (Size > 64)
2523 return SDValue();
2524
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002525 unsigned NElts = BVN->getNumOperands();
2526
2527 // Try to generate a SPLAT instruction.
Krzysztof Parzyszek89b2d7c2017-07-13 18:17:58 +00002528 if (VT == MVT::v4i8 || VT == MVT::v4i16 || VT == MVT::v2i32) {
2529 APInt APSplatBits, APSplatUndef;
2530 unsigned SplatBitSize;
2531 bool HasAnyUndefs;
2532 if (BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
2533 HasAnyUndefs, 0, false)) {
2534 if (SplatBitSize == VT.getVectorElementType().getSizeInBits()) {
2535 unsigned ZV = APSplatBits.getZExtValue();
2536 assert(SplatBitSize <= 32 && "Can only handle up to i32");
2537 // Sign-extend the splat value from SplatBitSize to 32.
2538 int32_t SV = SplatBitSize < 32
2539 ? int32_t(ZV << (32-SplatBitSize)) >> (32-SplatBitSize)
2540 : int32_t(ZV);
2541 return DAG.getNode(HexagonISD::VSPLAT, dl, VT,
2542 DAG.getConstant(SV, dl, MVT::i32));
2543 }
2544 }
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002545 }
2546
2547 // Try to generate COMBINE to build v2i32 vectors.
2548 if (VT.getSimpleVT() == MVT::v2i32) {
2549 SDValue V0 = BVN->getOperand(0);
2550 SDValue V1 = BVN->getOperand(1);
2551
Sanjay Patel57195842016-03-14 17:28:46 +00002552 if (V0.isUndef())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002553 V0 = DAG.getConstant(0, dl, MVT::i32);
Sanjay Patel57195842016-03-14 17:28:46 +00002554 if (V1.isUndef())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002555 V1 = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002556
2557 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(V0);
2558 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(V1);
2559 // If the element isn't a constant, it is in a register:
2560 // generate a COMBINE Register Register instruction.
2561 if (!C0 || !C1)
2562 return DAG.getNode(HexagonISD::COMBINE, dl, VT, V1, V0);
2563
2564 // If one of the operands is an 8 bit integer constant, generate
2565 // a COMBINE Immediate Immediate instruction.
2566 if (isInt<8>(C0->getSExtValue()) ||
2567 isInt<8>(C1->getSExtValue()))
2568 return DAG.getNode(HexagonISD::COMBINE, dl, VT, V1, V0);
2569 }
2570
2571 // Try to generate a S2_packhl to build v2i16 vectors.
2572 if (VT.getSimpleVT() == MVT::v2i16) {
2573 for (unsigned i = 0, e = NElts; i != e; ++i) {
Sanjay Patel57195842016-03-14 17:28:46 +00002574 if (BVN->getOperand(i).isUndef())
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002575 continue;
2576 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(BVN->getOperand(i));
2577 // If the element isn't a constant, it is in a register:
2578 // generate a S2_packhl instruction.
2579 if (!Cst) {
2580 SDValue pack = DAG.getNode(HexagonISD::PACKHL, dl, MVT::v4i16,
2581 BVN->getOperand(1), BVN->getOperand(0));
2582
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00002583 return DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::v2i16,
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002584 pack);
2585 }
2586 }
2587 }
2588
2589 // In the general case, generate a CONST32 or a CONST64 for constant vectors,
2590 // and insert_vector_elt for all the other cases.
2591 uint64_t Res = 0;
2592 unsigned EltSize = Size / NElts;
2593 SDValue ConstVal;
2594 uint64_t Mask = ~uint64_t(0ULL) >> (64 - EltSize);
2595 bool HasNonConstantElements = false;
2596
2597 for (unsigned i = 0, e = NElts; i != e; ++i) {
2598 // LLVM's BUILD_VECTOR operands are in Little Endian mode, whereas Hexagon's
2599 // combine, const64, etc. are Big Endian.
2600 unsigned OpIdx = NElts - i - 1;
2601 SDValue Operand = BVN->getOperand(OpIdx);
Sanjay Patel57195842016-03-14 17:28:46 +00002602 if (Operand.isUndef())
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002603 continue;
2604
2605 int64_t Val = 0;
2606 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Operand))
2607 Val = Cst->getSExtValue();
2608 else
2609 HasNonConstantElements = true;
2610
2611 Val &= Mask;
2612 Res = (Res << EltSize) | Val;
2613 }
2614
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002615 if (Size > 64)
2616 return SDValue();
2617
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002618 if (Size == 64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002619 ConstVal = DAG.getConstant(Res, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002620 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002621 ConstVal = DAG.getConstant(Res, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002622
2623 // When there are non constant operands, add them with INSERT_VECTOR_ELT to
2624 // ConstVal, the constant part of the vector.
2625 if (HasNonConstantElements) {
2626 EVT EltVT = VT.getVectorElementType();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002627 SDValue Width = DAG.getConstant(EltVT.getSizeInBits(), dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002628 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002629 DAG.getConstant(32, dl, MVT::i64));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002630
2631 for (unsigned i = 0, e = NElts; i != e; ++i) {
2632 // LLVM's BUILD_VECTOR operands are in Little Endian mode, whereas Hexagon
2633 // is Big Endian.
2634 unsigned OpIdx = NElts - i - 1;
2635 SDValue Operand = BVN->getOperand(OpIdx);
Benjamin Kramer619c4e52015-04-10 11:24:51 +00002636 if (isa<ConstantSDNode>(Operand))
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002637 // This operand is already in ConstVal.
2638 continue;
2639
2640 if (VT.getSizeInBits() == 64 &&
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00002641 Operand.getValueSizeInBits() == 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002642 SDValue C = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002643 Operand = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, Operand);
2644 }
2645
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002646 SDValue Idx = DAG.getConstant(OpIdx, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002647 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i64, Idx, Width);
2648 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2649 const SDValue Ops[] = {ConstVal, Operand, Combined};
2650
2651 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002652 ConstVal = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002653 else
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002654 ConstVal = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002655 }
2656 }
2657
2658 return DAG.getNode(ISD::BITCAST, dl, VT, ConstVal);
2659}
2660
2661SDValue
2662HexagonTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
2663 SelectionDAG &DAG) const {
2664 SDLoc dl(Op);
Krzysztof Parzyszekc168c012015-12-03 16:47:20 +00002665 bool UseHVX = Subtarget.useHVXOps();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002666 EVT VT = Op.getValueType();
2667 unsigned NElts = Op.getNumOperands();
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002668 SDValue Vec0 = Op.getOperand(0);
2669 EVT VecVT = Vec0.getValueType();
2670 unsigned Width = VecVT.getSizeInBits();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002671
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002672 if (NElts == 2) {
2673 MVT ST = VecVT.getSimpleVT();
2674 // We are trying to concat two v2i16 to a single v4i16, or two v4i8
2675 // into a single v8i8.
2676 if (ST == MVT::v2i16 || ST == MVT::v4i8)
2677 return DAG.getNode(HexagonISD::COMBINE, dl, VT, Op.getOperand(1), Vec0);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002678
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002679 if (UseHVX) {
2680 assert((Width == 64*8 && Subtarget.useHVXSglOps()) ||
2681 (Width == 128*8 && Subtarget.useHVXDblOps()));
2682 SDValue Vec1 = Op.getOperand(1);
2683 MVT OpTy = Subtarget.useHVXSglOps() ? MVT::v16i32 : MVT::v32i32;
2684 MVT ReTy = Subtarget.useHVXSglOps() ? MVT::v32i32 : MVT::v64i32;
2685 SDValue B0 = DAG.getNode(ISD::BITCAST, dl, OpTy, Vec0);
2686 SDValue B1 = DAG.getNode(ISD::BITCAST, dl, OpTy, Vec1);
2687 SDValue VC = DAG.getNode(HexagonISD::VCOMBINE, dl, ReTy, B1, B0);
2688 return DAG.getNode(ISD::BITCAST, dl, VT, VC);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002689 }
2690 }
2691
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002692 if (VT.getSizeInBits() != 32 && VT.getSizeInBits() != 64)
2693 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002694
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002695 SDValue C0 = DAG.getConstant(0, dl, MVT::i64);
2696 SDValue C32 = DAG.getConstant(32, dl, MVT::i64);
2697 SDValue W = DAG.getConstant(Width, dl, MVT::i64);
2698 // Create the "width" part of the argument to insert_rp/insertp_rp.
2699 SDValue S = DAG.getNode(ISD::SHL, dl, MVT::i64, W, C32);
2700 SDValue V = C0;
2701
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002702 for (unsigned i = 0, e = NElts; i != e; ++i) {
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002703 unsigned N = NElts-i-1;
2704 SDValue OpN = Op.getOperand(N);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002705
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00002706 if (VT.getSizeInBits() == 64 && OpN.getValueSizeInBits() == 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002707 SDValue C = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002708 OpN = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, OpN);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002709 }
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002710 SDValue Idx = DAG.getConstant(N, dl, MVT::i64);
2711 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i64, Idx, W);
2712 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, S, Offset);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002713 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002714 V = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, {V, OpN, Or});
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002715 else if (VT.getSizeInBits() == 64)
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002716 V = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, {V, OpN, Or});
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002717 else
2718 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002719 }
2720
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002721 return DAG.getNode(ISD::BITCAST, dl, VT, V);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002722}
2723
2724SDValue
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002725HexagonTargetLowering::LowerEXTRACT_SUBVECTOR_HVX(SDValue Op,
2726 SelectionDAG &DAG) const {
2727 EVT VT = Op.getOperand(0).getValueType();
2728 SDLoc dl(Op);
2729 bool UseHVX = Subtarget.useHVXOps();
2730 bool UseHVXSgl = Subtarget.useHVXSglOps();
2731 // Just in case...
2732
2733 if (!VT.isVector() || !UseHVX)
2734 return SDValue();
2735
2736 EVT ResVT = Op.getValueType();
2737 unsigned ResSize = ResVT.getSizeInBits();
2738 unsigned VectorSizeInBits = UseHVXSgl ? (64 * 8) : (128 * 8);
2739 unsigned OpSize = VT.getSizeInBits();
2740
2741 // We deal only with cases where the result is the vector size
2742 // and the vector operand is a double register.
2743 if (!(ResVT.isByteSized() && ResSize == VectorSizeInBits) ||
2744 !(VT.isByteSized() && OpSize == 2 * VectorSizeInBits))
2745 return SDValue();
2746
2747 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Op.getOperand(1));
2748 if (!Cst)
2749 return SDValue();
2750 unsigned Val = Cst->getZExtValue();
2751
2752 // These two will get lowered to an appropriate EXTRACT_SUBREG in ISel.
2753 if (Val == 0) {
2754 SDValue Vec = Op.getOperand(0);
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00002755 return DAG.getTargetExtractSubreg(Hexagon::vsub_lo, dl, ResVT, Vec);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002756 }
2757
2758 if (ResVT.getVectorNumElements() == Val) {
2759 SDValue Vec = Op.getOperand(0);
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00002760 return DAG.getTargetExtractSubreg(Hexagon::vsub_hi, dl, ResVT, Vec);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002761 }
2762
2763 return SDValue();
2764}
2765
2766SDValue
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002767HexagonTargetLowering::LowerEXTRACT_VECTOR(SDValue Op,
2768 SelectionDAG &DAG) const {
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002769 // If we are dealing with EXTRACT_SUBVECTOR on a HVX type, we may
2770 // be able to simplify it to an EXTRACT_SUBREG.
2771 if (Op.getOpcode() == ISD::EXTRACT_SUBVECTOR && Subtarget.useHVXOps() &&
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00002772 isHvxVectorType(Op.getValueType().getSimpleVT()))
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002773 return LowerEXTRACT_SUBVECTOR_HVX(Op, DAG);
2774
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002775 EVT VT = Op.getValueType();
2776 int VTN = VT.isVector() ? VT.getVectorNumElements() : 1;
2777 SDLoc dl(Op);
2778 SDValue Idx = Op.getOperand(1);
2779 SDValue Vec = Op.getOperand(0);
2780 EVT VecVT = Vec.getValueType();
2781 EVT EltVT = VecVT.getVectorElementType();
2782 int EltSize = EltVT.getSizeInBits();
2783 SDValue Width = DAG.getConstant(Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT ?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002784 EltSize : VTN * EltSize, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002785
2786 // Constant element number.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002787 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Idx)) {
2788 uint64_t X = CI->getZExtValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002789 SDValue Offset = DAG.getConstant(X * EltSize, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002790 const SDValue Ops[] = {Vec, Width, Offset};
2791
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002792 ConstantSDNode *CW = dyn_cast<ConstantSDNode>(Width);
2793 assert(CW && "Non constant width in LowerEXTRACT_VECTOR");
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002794
2795 SDValue N;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002796 MVT SVT = VecVT.getSimpleVT();
2797 uint64_t W = CW->getZExtValue();
2798
2799 if (W == 32) {
2800 // Translate this node into EXTRACT_SUBREG.
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00002801 unsigned Subreg = (X == 0) ? Hexagon::isub_lo : 0;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002802
2803 if (X == 0)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00002804 Subreg = Hexagon::isub_lo;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002805 else if (SVT == MVT::v2i32 && X == 1)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00002806 Subreg = Hexagon::isub_hi;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002807 else if (SVT == MVT::v4i16 && X == 2)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00002808 Subreg = Hexagon::isub_hi;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002809 else if (SVT == MVT::v8i8 && X == 4)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00002810 Subreg = Hexagon::isub_hi;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002811 else
2812 llvm_unreachable("Bad offset");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002813 N = DAG.getTargetExtractSubreg(Subreg, dl, MVT::i32, Vec);
2814
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002815 } else if (SVT.getSizeInBits() == 32) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002816 N = DAG.getNode(HexagonISD::EXTRACTU, dl, MVT::i32, Ops);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002817 } else if (SVT.getSizeInBits() == 64) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002818 N = DAG.getNode(HexagonISD::EXTRACTU, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002819 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00002820 N = DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, N);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002821 } else
2822 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002823
2824 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2825 }
2826
2827 // Variable element number.
2828 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i32, Idx,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002829 DAG.getConstant(EltSize, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002830 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002831 DAG.getConstant(32, dl, MVT::i64));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002832 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2833
2834 const SDValue Ops[] = {Vec, Combined};
2835
2836 SDValue N;
2837 if (VecVT.getSizeInBits() == 32) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002838 N = DAG.getNode(HexagonISD::EXTRACTURP, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002839 } else {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002840 N = DAG.getNode(HexagonISD::EXTRACTURP, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002841 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00002842 N = DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, N);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002843 }
2844 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2845}
2846
2847SDValue
2848HexagonTargetLowering::LowerINSERT_VECTOR(SDValue Op,
2849 SelectionDAG &DAG) const {
2850 EVT VT = Op.getValueType();
2851 int VTN = VT.isVector() ? VT.getVectorNumElements() : 1;
2852 SDLoc dl(Op);
2853 SDValue Vec = Op.getOperand(0);
2854 SDValue Val = Op.getOperand(1);
2855 SDValue Idx = Op.getOperand(2);
2856 EVT VecVT = Vec.getValueType();
2857 EVT EltVT = VecVT.getVectorElementType();
2858 int EltSize = EltVT.getSizeInBits();
2859 SDValue Width = DAG.getConstant(Op.getOpcode() == ISD::INSERT_VECTOR_ELT ?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002860 EltSize : VTN * EltSize, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002861
2862 if (ConstantSDNode *C = cast<ConstantSDNode>(Idx)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002863 SDValue Offset = DAG.getConstant(C->getSExtValue() * EltSize, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002864 const SDValue Ops[] = {Vec, Val, Width, Offset};
2865
2866 SDValue N;
2867 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002868 N = DAG.getNode(HexagonISD::INSERT, dl, MVT::i32, Ops);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002869 else if (VT.getSizeInBits() == 64)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002870 N = DAG.getNode(HexagonISD::INSERT, dl, MVT::i64, Ops);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002871 else
2872 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002873
2874 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2875 }
2876
2877 // Variable element number.
2878 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i32, Idx,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002879 DAG.getConstant(EltSize, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002880 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002881 DAG.getConstant(32, dl, MVT::i64));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002882 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2883
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00002884 if (VT.getSizeInBits() == 64 && Val.getValueSizeInBits() == 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002885 SDValue C = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002886 Val = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, Val);
2887 }
2888
2889 const SDValue Ops[] = {Vec, Val, Combined};
2890
2891 SDValue N;
2892 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002893 N = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, Ops);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002894 else if (VT.getSizeInBits() == 64)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002895 N = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, Ops);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002896 else
2897 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002898
2899 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2900}
2901
Tim Northovera4415852013-08-06 09:12:35 +00002902bool
2903HexagonTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
2904 // Assuming the caller does not have either a signext or zeroext modifier, and
2905 // only one value is accepted, any reasonable truncation is allowed.
2906 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
2907 return false;
2908
2909 // FIXME: in principle up to 64-bit could be made safe, but it would be very
2910 // fragile at the moment: any support for multiple value returns would be
2911 // liable to disallow tail calls involving i64 -> iN truncation in many cases.
2912 return Ty1->getPrimitiveSizeInBits() <= 32;
2913}
2914
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002915SDValue
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002916HexagonTargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
2917 SDValue Chain = Op.getOperand(0);
2918 SDValue Offset = Op.getOperand(1);
2919 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002920 SDLoc dl(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002921 auto PtrVT = getPointerTy(DAG.getDataLayout());
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002922
2923 // Mark function as containing a call to EH_RETURN.
2924 HexagonMachineFunctionInfo *FuncInfo =
2925 DAG.getMachineFunction().getInfo<HexagonMachineFunctionInfo>();
2926 FuncInfo->setHasEHReturn();
2927
2928 unsigned OffsetReg = Hexagon::R28;
2929
Mehdi Amini44ede332015-07-09 02:09:04 +00002930 SDValue StoreAddr =
2931 DAG.getNode(ISD::ADD, dl, PtrVT, DAG.getRegister(Hexagon::R30, PtrVT),
2932 DAG.getIntPtrConstant(4, dl));
Justin Lebar9c375812016-07-15 18:27:10 +00002933 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo());
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002934 Chain = DAG.getCopyToReg(Chain, dl, OffsetReg, Offset);
2935
2936 // Not needed we already use it as explict input to EH_RETURN.
2937 // MF.getRegInfo().addLiveOut(OffsetReg);
2938
2939 return DAG.getNode(HexagonISD::EH_RETURN, dl, MVT::Other, Chain);
2940}
2941
2942SDValue
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002943HexagonTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002944 unsigned Opc = Op.getOpcode();
2945 switch (Opc) {
2946 default:
2947#ifndef NDEBUG
2948 Op.getNode()->dumpr(&DAG);
2949 if (Opc > HexagonISD::OP_BEGIN && Opc < HexagonISD::OP_END)
2950 errs() << "Check for a non-legal type in this operation\n";
2951#endif
2952 llvm_unreachable("Should not custom lower this!");
2953 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
2954 case ISD::INSERT_SUBVECTOR: return LowerINSERT_VECTOR(Op, DAG);
2955 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR(Op, DAG);
2956 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_VECTOR(Op, DAG);
2957 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR(Op, DAG);
2958 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2959 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002960 case ISD::SRA:
2961 case ISD::SHL:
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002962 case ISD::SRL: return LowerVECTOR_SHIFT(Op, DAG);
2963 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002964 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002965 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
2966 // Frame & Return address. Currently unimplemented.
2967 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
2968 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00002969 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002970 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
2971 case ISD::GlobalAddress: return LowerGLOBALADDRESS(Op, DAG);
2972 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002973 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002974 case ISD::VASTART: return LowerVASTART(Op, DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002975 // Custom lower some vector loads.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002976 case ISD::LOAD: return LowerLOAD(Op, DAG);
2977 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
2978 case ISD::SETCC: return LowerSETCC(Op, DAG);
2979 case ISD::VSELECT: return LowerVSELECT(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002980 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00002981 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002982 case ISD::INLINEASM: return LowerINLINEASM(Op, DAG);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00002983 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG);
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +00002984 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002985 }
2986}
2987
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002988/// Returns relocation base for the given PIC jumptable.
2989SDValue
2990HexagonTargetLowering::getPICJumpTableRelocBase(SDValue Table,
2991 SelectionDAG &DAG) const {
2992 int Idx = cast<JumpTableSDNode>(Table)->getIndex();
2993 EVT VT = Table.getValueType();
2994 SDValue T = DAG.getTargetJumpTable(Idx, VT, HexagonII::MO_PCREL);
2995 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Table), VT, T);
2996}
2997
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002998//===----------------------------------------------------------------------===//
2999// Inline Assembly Support
3000//===----------------------------------------------------------------------===//
3001
Krzysztof Parzyszekca3b5322016-05-18 14:34:51 +00003002TargetLowering::ConstraintType
3003HexagonTargetLowering::getConstraintType(StringRef Constraint) const {
3004 if (Constraint.size() == 1) {
3005 switch (Constraint[0]) {
3006 case 'q':
3007 case 'v':
3008 if (Subtarget.useHVXOps())
3009 return C_Register;
3010 break;
3011 }
3012 }
3013 return TargetLowering::getConstraintType(Constraint);
3014}
3015
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003016std::pair<unsigned, const TargetRegisterClass*>
Eric Christopher11e4df72015-02-26 22:38:43 +00003017HexagonTargetLowering::getRegForInlineAsmConstraint(
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003018 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003019 bool UseHVX = Subtarget.useHVXOps(), UseHVXDbl = Subtarget.useHVXDblOps();
3020
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003021 if (Constraint.size() == 1) {
3022 switch (Constraint[0]) {
3023 case 'r': // R0-R31
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003024 switch (VT.SimpleTy) {
3025 default:
3026 llvm_unreachable("getRegForInlineAsmConstraint Unhandled data type");
3027 case MVT::i1:
3028 case MVT::i8:
3029 case MVT::i16:
3030 case MVT::i32:
3031 case MVT::f32:
3032 return std::make_pair(0U, &Hexagon::IntRegsRegClass);
3033 case MVT::i64:
3034 case MVT::f64:
3035 return std::make_pair(0U, &Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003036 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003037 case 'q': // q0-q3
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00003038 switch (VT.getSizeInBits()) {
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003039 default:
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00003040 llvm_unreachable("getRegForInlineAsmConstraint Unhandled vector size");
3041 case 512:
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003042 return std::make_pair(0U, &Hexagon::VecPredRegsRegClass);
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00003043 case 1024:
3044 return std::make_pair(0U, &Hexagon::VecPredRegs128BRegClass);
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003045 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003046 case 'v': // V0-V31
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00003047 switch (VT.getSizeInBits()) {
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003048 default:
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00003049 llvm_unreachable("getRegForInlineAsmConstraint Unhandled vector size");
3050 case 512:
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003051 return std::make_pair(0U, &Hexagon::VectorRegsRegClass);
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00003052 case 1024:
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003053 if (Subtarget.hasV60TOps() && UseHVX && UseHVXDbl)
3054 return std::make_pair(0U, &Hexagon::VectorRegs128BRegClass);
3055 return std::make_pair(0U, &Hexagon::VecDblRegsRegClass);
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00003056 case 2048:
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003057 return std::make_pair(0U, &Hexagon::VecDblRegs128BRegClass);
3058 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003059
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003060 default:
Craig Toppere55c5562012-02-07 02:50:20 +00003061 llvm_unreachable("Unknown asm register class");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003062 }
3063 }
3064
Eric Christopher11e4df72015-02-26 22:38:43 +00003065 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003066}
3067
Sirish Pande69295b82012-05-10 20:20:25 +00003068/// isFPImmLegal - Returns true if the target can instruction select the
3069/// specified FP immediate natively. If false, the legalizer will
3070/// materialize the FP immediate as a load from a constant pool.
3071bool HexagonTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00003072 return Subtarget.hasV5TOps();
Sirish Pande69295b82012-05-10 20:20:25 +00003073}
3074
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003075/// isLegalAddressingMode - Return true if the addressing mode represented by
3076/// AM is legal for this target, for a load/store of the specified type.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +00003077bool HexagonTargetLowering::isLegalAddressingMode(const DataLayout &DL,
3078 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +00003079 unsigned AS) const {
Krzysztof Parzyszeked4e7822016-08-03 15:06:18 +00003080 if (Ty->isSized()) {
3081 // When LSR detects uses of the same base address to access different
3082 // types (e.g. unions), it will assume a conservative type for these
3083 // uses:
3084 // LSR Use: Kind=Address of void in addrspace(4294967295), ...
3085 // The type Ty passed here would then be "void". Skip the alignment
3086 // checks, but do not return false right away, since that confuses
3087 // LSR into crashing.
3088 unsigned A = DL.getABITypeAlignment(Ty);
3089 // The base offset must be a multiple of the alignment.
3090 if ((AM.BaseOffs % A) != 0)
3091 return false;
3092 // The shifted offset must fit in 11 bits.
3093 if (!isInt<11>(AM.BaseOffs >> Log2_32(A)))
3094 return false;
3095 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003096
3097 // No global is ever allowed as a base.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00003098 if (AM.BaseGV)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003099 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003100
3101 int Scale = AM.Scale;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00003102 if (Scale < 0)
3103 Scale = -Scale;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003104 switch (Scale) {
3105 case 0: // No scale reg, "r+i", "r", or just "i".
3106 break;
3107 default: // No scaled addressing mode.
3108 return false;
3109 }
3110 return true;
3111}
3112
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00003113/// Return true if folding a constant offset with the given GlobalAddress is
3114/// legal. It is frequently not legal in PIC relocation models.
3115bool HexagonTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA)
3116 const {
3117 return HTM.getRelocationModel() == Reloc::Static;
3118}
3119
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003120/// isLegalICmpImmediate - Return true if the specified immediate is legal
3121/// icmp immediate, that is the target has icmp instructions which can compare
3122/// a register against the immediate without having to materialize the
3123/// immediate into a register.
3124bool HexagonTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
3125 return Imm >= -512 && Imm <= 511;
3126}
3127
3128/// IsEligibleForTailCallOptimization - Check whether the call is eligible
3129/// for tail call optimization. Targets which want to do tail call
3130/// optimization should implement this function.
3131bool HexagonTargetLowering::IsEligibleForTailCallOptimization(
3132 SDValue Callee,
3133 CallingConv::ID CalleeCC,
3134 bool isVarArg,
3135 bool isCalleeStructRet,
3136 bool isCallerStructRet,
3137 const SmallVectorImpl<ISD::OutputArg> &Outs,
3138 const SmallVectorImpl<SDValue> &OutVals,
3139 const SmallVectorImpl<ISD::InputArg> &Ins,
3140 SelectionDAG& DAG) const {
3141 const Function *CallerF = DAG.getMachineFunction().getFunction();
3142 CallingConv::ID CallerCC = CallerF->getCallingConv();
3143 bool CCMatch = CallerCC == CalleeCC;
3144
3145 // ***************************************************************************
3146 // Look for obvious safe cases to perform tail call optimization that do not
3147 // require ABI changes.
3148 // ***************************************************************************
3149
3150 // If this is a tail call via a function pointer, then don't do it!
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00003151 if (!isa<GlobalAddressSDNode>(Callee) &&
3152 !isa<ExternalSymbolSDNode>(Callee)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003153 return false;
3154 }
3155
Krzysztof Parzyszek0ba97542016-08-19 15:02:18 +00003156 // Do not optimize if the calling conventions do not match and the conventions
3157 // used are not C or Fast.
3158 if (!CCMatch) {
3159 bool R = (CallerCC == CallingConv::C || CallerCC == CallingConv::Fast);
3160 bool E = (CalleeCC == CallingConv::C || CalleeCC == CallingConv::Fast);
3161 // If R & E, then ok.
3162 if (!R || !E)
3163 return false;
3164 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003165
3166 // Do not tail call optimize vararg calls.
3167 if (isVarArg)
3168 return false;
3169
3170 // Also avoid tail call optimization if either caller or callee uses struct
3171 // return semantics.
3172 if (isCalleeStructRet || isCallerStructRet)
3173 return false;
3174
3175 // In addition to the cases above, we also disable Tail Call Optimization if
3176 // the calling convention code that at least one outgoing argument needs to
3177 // go on the stack. We cannot check that here because at this point that
3178 // information is not available.
3179 return true;
3180}
Colin LeMahieu025f8602014-12-08 21:19:18 +00003181
Krzysztof Parzyszek3e409e12016-08-02 18:34:31 +00003182/// Returns the target specific optimal type for load and store operations as
3183/// a result of memset, memcpy, and memmove lowering.
3184///
3185/// If DstAlign is zero that means it's safe to destination alignment can
3186/// satisfy any constraint. Similarly if SrcAlign is zero it means there isn't
3187/// a need to check it against alignment requirement, probably because the
3188/// source does not need to be loaded. If 'IsMemset' is true, that means it's
3189/// expanding a memset. If 'ZeroMemset' is true, that means it's a memset of
3190/// zero. 'MemcpyStrSrc' indicates whether the memcpy source is constant so it
3191/// does not need to be loaded. It returns EVT::Other if the type should be
3192/// determined using generic target-independent logic.
3193EVT HexagonTargetLowering::getOptimalMemOpType(uint64_t Size,
3194 unsigned DstAlign, unsigned SrcAlign, bool IsMemset, bool ZeroMemset,
3195 bool MemcpyStrSrc, MachineFunction &MF) const {
3196
3197 auto Aligned = [](unsigned GivenA, unsigned MinA) -> bool {
3198 return (GivenA % MinA) == 0;
3199 };
3200
3201 if (Size >= 8 && Aligned(DstAlign, 8) && (IsMemset || Aligned(SrcAlign, 8)))
3202 return MVT::i64;
3203 if (Size >= 4 && Aligned(DstAlign, 4) && (IsMemset || Aligned(SrcAlign, 4)))
3204 return MVT::i32;
3205 if (Size >= 2 && Aligned(DstAlign, 2) && (IsMemset || Aligned(SrcAlign, 2)))
3206 return MVT::i16;
3207
3208 return MVT::Other;
3209}
3210
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +00003211bool HexagonTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
3212 unsigned AS, unsigned Align, bool *Fast) const {
3213 if (Fast)
3214 *Fast = false;
3215
3216 switch (VT.getSimpleVT().SimpleTy) {
3217 default:
3218 return false;
3219 case MVT::v64i8:
3220 case MVT::v128i8:
3221 case MVT::v256i8:
3222 case MVT::v32i16:
3223 case MVT::v64i16:
3224 case MVT::v128i16:
3225 case MVT::v16i32:
3226 case MVT::v32i32:
3227 case MVT::v64i32:
3228 case MVT::v8i64:
3229 case MVT::v16i64:
3230 case MVT::v32i64:
3231 return true;
3232 }
3233 return false;
3234}
3235
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003236std::pair<const TargetRegisterClass*, uint8_t>
3237HexagonTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
3238 MVT VT) const {
3239 const TargetRegisterClass *RRC = nullptr;
3240
3241 uint8_t Cost = 1;
3242 switch (VT.SimpleTy) {
3243 default:
3244 return TargetLowering::findRepresentativeClass(TRI, VT);
3245 case MVT::v64i8:
3246 case MVT::v32i16:
3247 case MVT::v16i32:
3248 case MVT::v8i64:
3249 RRC = &Hexagon::VectorRegsRegClass;
3250 break;
3251 case MVT::v128i8:
3252 case MVT::v64i16:
3253 case MVT::v32i32:
3254 case MVT::v16i64:
3255 if (Subtarget.hasV60TOps() && Subtarget.useHVXOps() &&
3256 Subtarget.useHVXDblOps())
3257 RRC = &Hexagon::VectorRegs128BRegClass;
3258 else
3259 RRC = &Hexagon::VecDblRegsRegClass;
3260 break;
3261 case MVT::v256i8:
3262 case MVT::v128i16:
3263 case MVT::v64i32:
3264 case MVT::v32i64:
3265 RRC = &Hexagon::VecDblRegs128BRegClass;
3266 break;
3267 }
3268 return std::make_pair(RRC, Cost);
3269}
3270
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003271Value *HexagonTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
3272 AtomicOrdering Ord) const {
3273 BasicBlock *BB = Builder.GetInsertBlock();
3274 Module *M = BB->getParent()->getParent();
3275 Type *Ty = cast<PointerType>(Addr->getType())->getElementType();
3276 unsigned SZ = Ty->getPrimitiveSizeInBits();
3277 assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic loads supported");
3278 Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_L2_loadw_locked
3279 : Intrinsic::hexagon_L4_loadd_locked;
3280 Value *Fn = Intrinsic::getDeclaration(M, IntID);
3281 return Builder.CreateCall(Fn, Addr, "larx");
3282}
3283
3284/// Perform a store-conditional operation to Addr. Return the status of the
3285/// store. This should be 0 if the store succeeded, non-zero otherwise.
3286Value *HexagonTargetLowering::emitStoreConditional(IRBuilder<> &Builder,
3287 Value *Val, Value *Addr, AtomicOrdering Ord) const {
3288 BasicBlock *BB = Builder.GetInsertBlock();
3289 Module *M = BB->getParent()->getParent();
3290 Type *Ty = Val->getType();
3291 unsigned SZ = Ty->getPrimitiveSizeInBits();
3292 assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic stores supported");
3293 Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_S2_storew_locked
3294 : Intrinsic::hexagon_S4_stored_locked;
3295 Value *Fn = Intrinsic::getDeclaration(M, IntID);
3296 Value *Call = Builder.CreateCall(Fn, {Addr, Val}, "stcx");
3297 Value *Cmp = Builder.CreateICmpEQ(Call, Builder.getInt32(0), "");
3298 Value *Ext = Builder.CreateZExt(Cmp, Type::getInt32Ty(M->getContext()));
3299 return Ext;
3300}
3301
Ahmed Bougacha52468672015-09-11 17:08:28 +00003302TargetLowering::AtomicExpansionKind
3303HexagonTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003304 // Do not expand loads and stores that don't exceed 64 bits.
Ahmed Bougacha52468672015-09-11 17:08:28 +00003305 return LI->getType()->getPrimitiveSizeInBits() > 64
Tim Northoverf520eff2015-12-02 18:12:57 +00003306 ? AtomicExpansionKind::LLOnly
Ahmed Bougacha52468672015-09-11 17:08:28 +00003307 : AtomicExpansionKind::None;
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003308}
3309
3310bool HexagonTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
3311 // Do not expand loads and stores that don't exceed 64 bits.
3312 return SI->getValueOperand()->getType()->getPrimitiveSizeInBits() > 64;
3313}
Krzysztof Parzyszekf228c952016-06-22 16:07:10 +00003314
3315bool HexagonTargetLowering::shouldExpandAtomicCmpXchgInIR(
3316 AtomicCmpXchgInst *AI) const {
3317 const DataLayout &DL = AI->getModule()->getDataLayout();
3318 unsigned Size = DL.getTypeStoreSize(AI->getCompareOperand()->getType());
3319 return Size >= 4 && Size <= 8;
3320}