blob: e67b2e1424dfa9bcd52c7af310cf4e45c7b50a9b [file] [log] [blame]
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001//===-- HexagonISelLowering.cpp - Hexagon DAG Lowering Implementation -----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the interfaces that Hexagon uses to lower LLVM code
11// into a selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Eugene Zelenko58655bb2016-12-17 01:09:05 +000015#include "Hexagon.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000016#include "HexagonISelLowering.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000017#include "HexagonMachineFunctionInfo.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000018#include "HexagonRegisterInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000019#include "HexagonSubtarget.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "HexagonTargetMachine.h"
21#include "HexagonTargetObjectFile.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000022#include "llvm/ADT/APInt.h"
23#include "llvm/ADT/ArrayRef.h"
24#include "llvm/ADT/SmallVector.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000025#include "llvm/CodeGen/CallingConvLower.h"
26#include "llvm/CodeGen/MachineFrameInfo.h"
27#include "llvm/CodeGen/MachineFunction.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000028#include "llvm/CodeGen/MachineMemOperand.h"
29#include "llvm/CodeGen/RuntimeLibcalls.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000031#include "llvm/CodeGen/SelectionDAG.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000032#include "llvm/CodeGen/ValueTypes.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000033#include "llvm/IR/BasicBlock.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000034#include "llvm/IR/CallingConv.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000035#include "llvm/IR/DataLayout.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000036#include "llvm/IR/DerivedTypes.h"
37#include "llvm/IR/Function.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000038#include "llvm/IR/GlobalValue.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000039#include "llvm/IR/InlineAsm.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000040#include "llvm/IR/Instructions.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000041#include "llvm/IR/Intrinsics.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000042#include "llvm/IR/Module.h"
43#include "llvm/IR/Type.h"
44#include "llvm/IR/Value.h"
45#include "llvm/MC/MCRegisterInfo.h"
46#include "llvm/Support/Casting.h"
47#include "llvm/Support/CodeGen.h"
NAKAMURA Takumi54eed762012-04-21 15:31:36 +000048#include "llvm/Support/CommandLine.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000049#include "llvm/Support/Debug.h"
50#include "llvm/Support/ErrorHandling.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000051#include "llvm/Support/MathExtras.h"
NAKAMURA Takumie30303f2012-04-21 15:31:45 +000052#include "llvm/Support/raw_ostream.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000053#include "llvm/Target/TargetCallingConv.h"
54#include "llvm/Target/TargetMachine.h"
55#include <algorithm>
56#include <cassert>
57#include <cstddef>
58#include <cstdint>
59#include <limits>
60#include <utility>
NAKAMURA Takumi54eed762012-04-21 15:31:36 +000061
Craig Topperb25fda92012-03-17 18:46:09 +000062using namespace llvm;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000063
Chandler Carruthe96dd892014-04-21 22:55:11 +000064#define DEBUG_TYPE "hexagon-lowering"
65
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +000066static cl::opt<bool> EmitJumpTables("hexagon-emit-jump-tables",
67 cl::init(true), cl::Hidden,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +000068 cl::desc("Control jump table emission on Hexagon target"));
69
70static cl::opt<bool> EnableHexSDNodeSched("enable-hexagon-sdnode-sched",
71 cl::Hidden, cl::ZeroOrMore, cl::init(false),
72 cl::desc("Enable Hexagon SDNode scheduling"));
73
74static cl::opt<bool> EnableFastMath("ffast-math",
75 cl::Hidden, cl::ZeroOrMore, cl::init(false),
76 cl::desc("Enable Fast Math processing"));
77
78static cl::opt<int> MinimumJumpTables("minimum-jump-tables",
79 cl::Hidden, cl::ZeroOrMore, cl::init(5),
80 cl::desc("Set minimum jump tables"));
81
82static cl::opt<int> MaxStoresPerMemcpyCL("max-store-memcpy",
83 cl::Hidden, cl::ZeroOrMore, cl::init(6),
84 cl::desc("Max #stores to inline memcpy"));
85
86static cl::opt<int> MaxStoresPerMemcpyOptSizeCL("max-store-memcpy-Os",
87 cl::Hidden, cl::ZeroOrMore, cl::init(4),
88 cl::desc("Max #stores to inline memcpy"));
89
90static cl::opt<int> MaxStoresPerMemmoveCL("max-store-memmove",
91 cl::Hidden, cl::ZeroOrMore, cl::init(6),
92 cl::desc("Max #stores to inline memmove"));
93
94static cl::opt<int> MaxStoresPerMemmoveOptSizeCL("max-store-memmove-Os",
95 cl::Hidden, cl::ZeroOrMore, cl::init(4),
96 cl::desc("Max #stores to inline memmove"));
97
98static cl::opt<int> MaxStoresPerMemsetCL("max-store-memset",
99 cl::Hidden, cl::ZeroOrMore, cl::init(8),
100 cl::desc("Max #stores to inline memset"));
101
102static cl::opt<int> MaxStoresPerMemsetOptSizeCL("max-store-memset-Os",
103 cl::Hidden, cl::ZeroOrMore, cl::init(4),
104 cl::desc("Max #stores to inline memset"));
105
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000106
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000107namespace {
Eugene Zelenko58655bb2016-12-17 01:09:05 +0000108
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +0000109 class HexagonCCState : public CCState {
110 unsigned NumNamedVarArgParams;
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000111
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +0000112 public:
113 HexagonCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
114 SmallVectorImpl<CCValAssign> &locs, LLVMContext &C,
115 int NumNamedVarArgParams)
116 : CCState(CC, isVarArg, MF, locs, C),
117 NumNamedVarArgParams(NumNamedVarArgParams) {}
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000118
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +0000119 unsigned getNumNamedVarArgParams() const { return NumNamedVarArgParams; }
120 };
121
122 enum StridedLoadKind {
123 Even = 0,
124 Odd,
125 NoPattern
126 };
Eugene Zelenko58655bb2016-12-17 01:09:05 +0000127
128} // end anonymous namespace
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000129
130// Implement calling convention for Hexagon.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000131
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000132static bool isHvxVectorType(MVT ty);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000133
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000134static bool
135CC_Hexagon(unsigned ValNo, MVT ValVT,
136 MVT LocVT, CCValAssign::LocInfo LocInfo,
137 ISD::ArgFlagsTy ArgFlags, CCState &State);
138
139static bool
140CC_Hexagon32(unsigned ValNo, MVT ValVT,
141 MVT LocVT, CCValAssign::LocInfo LocInfo,
142 ISD::ArgFlagsTy ArgFlags, CCState &State);
143
144static bool
145CC_Hexagon64(unsigned ValNo, MVT ValVT,
146 MVT LocVT, CCValAssign::LocInfo LocInfo,
147 ISD::ArgFlagsTy ArgFlags, CCState &State);
148
149static bool
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000150CC_HexagonVector(unsigned ValNo, MVT ValVT,
151 MVT LocVT, CCValAssign::LocInfo LocInfo,
152 ISD::ArgFlagsTy ArgFlags, CCState &State);
153
154static bool
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000155RetCC_Hexagon(unsigned ValNo, MVT ValVT,
156 MVT LocVT, CCValAssign::LocInfo LocInfo,
157 ISD::ArgFlagsTy ArgFlags, CCState &State);
158
159static bool
160RetCC_Hexagon32(unsigned ValNo, MVT ValVT,
161 MVT LocVT, CCValAssign::LocInfo LocInfo,
162 ISD::ArgFlagsTy ArgFlags, CCState &State);
163
164static bool
165RetCC_Hexagon64(unsigned ValNo, MVT ValVT,
166 MVT LocVT, CCValAssign::LocInfo LocInfo,
167 ISD::ArgFlagsTy ArgFlags, CCState &State);
168
169static bool
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000170RetCC_HexagonVector(unsigned ValNo, MVT ValVT,
171 MVT LocVT, CCValAssign::LocInfo LocInfo,
172 ISD::ArgFlagsTy ArgFlags, CCState &State);
173
174static bool
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000175CC_Hexagon_VarArg (unsigned ValNo, MVT ValVT,
176 MVT LocVT, CCValAssign::LocInfo LocInfo,
177 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000178 HexagonCCState &HState = static_cast<HexagonCCState &>(State);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000179
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000180 if (ValNo < HState.getNumNamedVarArgParams()) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000181 // Deal with named arguments.
182 return CC_Hexagon(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State);
183 }
184
185 // Deal with un-named arguments.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000186 unsigned Offset;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000187 if (ArgFlags.isByVal()) {
188 // If pass-by-value, the size allocated on stack is decided
189 // by ArgFlags.getByValSize(), not by the size of LocVT.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000190 Offset = State.AllocateStack(ArgFlags.getByValSize(),
191 ArgFlags.getByValAlign());
192 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000193 return false;
194 }
Jyotsna Vermac7dcc2f2013-03-07 20:28:34 +0000195 if (LocVT == MVT::i1 || LocVT == MVT::i8 || LocVT == MVT::i16) {
196 LocVT = MVT::i32;
197 ValVT = MVT::i32;
198 if (ArgFlags.isSExt())
199 LocInfo = CCValAssign::SExt;
200 else if (ArgFlags.isZExt())
201 LocInfo = CCValAssign::ZExt;
202 else
203 LocInfo = CCValAssign::AExt;
204 }
Sirish Pande69295b82012-05-10 20:20:25 +0000205 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000206 Offset = State.AllocateStack(4, 4);
207 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000208 return false;
209 }
Sirish Pande69295b82012-05-10 20:20:25 +0000210 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000211 Offset = State.AllocateStack(8, 8);
212 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000213 return false;
214 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000215 if (LocVT == MVT::v2i64 || LocVT == MVT::v4i32 || LocVT == MVT::v8i16 ||
216 LocVT == MVT::v16i8) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000217 Offset = State.AllocateStack(16, 16);
218 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000219 return false;
220 }
221 if (LocVT == MVT::v4i64 || LocVT == MVT::v8i32 || LocVT == MVT::v16i16 ||
222 LocVT == MVT::v32i8) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000223 Offset = State.AllocateStack(32, 32);
224 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000225 return false;
226 }
227 if (LocVT == MVT::v8i64 || LocVT == MVT::v16i32 || LocVT == MVT::v32i16 ||
228 LocVT == MVT::v64i8 || LocVT == MVT::v512i1) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000229 Offset = State.AllocateStack(64, 64);
230 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000231 return false;
232 }
233 if (LocVT == MVT::v16i64 || LocVT == MVT::v32i32 || LocVT == MVT::v64i16 ||
234 LocVT == MVT::v128i8 || LocVT == MVT::v1024i1) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000235 Offset = State.AllocateStack(128, 128);
236 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000237 return false;
238 }
239 if (LocVT == MVT::v32i64 || LocVT == MVT::v64i32 || LocVT == MVT::v128i16 ||
240 LocVT == MVT::v256i8) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000241 Offset = State.AllocateStack(256, 256);
242 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000243 return false;
244 }
245
Craig Toppere73658d2014-04-28 04:05:08 +0000246 llvm_unreachable(nullptr);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000247}
248
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000249static bool CC_Hexagon (unsigned ValNo, MVT ValVT, MVT LocVT,
250 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, CCState &State) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000251 if (ArgFlags.isByVal()) {
252 // Passed on stack.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000253 unsigned Offset = State.AllocateStack(ArgFlags.getByValSize(),
254 ArgFlags.getByValAlign());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000255 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
256 return false;
257 }
258
259 if (LocVT == MVT::i1 || LocVT == MVT::i8 || LocVT == MVT::i16) {
260 LocVT = MVT::i32;
261 ValVT = MVT::i32;
262 if (ArgFlags.isSExt())
263 LocInfo = CCValAssign::SExt;
264 else if (ArgFlags.isZExt())
265 LocInfo = CCValAssign::ZExt;
266 else
267 LocInfo = CCValAssign::AExt;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000268 } else if (LocVT == MVT::v4i8 || LocVT == MVT::v2i16) {
269 LocVT = MVT::i32;
270 LocInfo = CCValAssign::BCvt;
271 } else if (LocVT == MVT::v8i8 || LocVT == MVT::v4i16 || LocVT == MVT::v2i32) {
272 LocVT = MVT::i64;
273 LocInfo = CCValAssign::BCvt;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000274 }
275
Sirish Pande69295b82012-05-10 20:20:25 +0000276 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000277 if (!CC_Hexagon32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
278 return false;
279 }
280
Sirish Pande69295b82012-05-10 20:20:25 +0000281 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000282 if (!CC_Hexagon64(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
283 return false;
284 }
285
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000286 if (LocVT == MVT::v8i32 || LocVT == MVT::v16i16 || LocVT == MVT::v32i8) {
287 unsigned Offset = State.AllocateStack(ArgFlags.getByValSize(), 32);
288 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
289 return false;
290 }
291
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000292 if (isHvxVectorType(LocVT)) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000293 if (!CC_HexagonVector(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
294 return false;
295 }
296
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000297 return true; // CC didn't match.
298}
299
300
301static bool CC_Hexagon32(unsigned ValNo, MVT ValVT,
302 MVT LocVT, CCValAssign::LocInfo LocInfo,
303 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +0000304 static const MCPhysReg RegList[] = {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000305 Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3, Hexagon::R4,
306 Hexagon::R5
307 };
Tim Northover3b6b7ca2015-02-21 02:11:17 +0000308 if (unsigned Reg = State.AllocateReg(RegList)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000309 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
310 return false;
311 }
312
313 unsigned Offset = State.AllocateStack(4, 4);
314 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
315 return false;
316}
317
318static bool CC_Hexagon64(unsigned ValNo, MVT ValVT,
319 MVT LocVT, CCValAssign::LocInfo LocInfo,
320 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000321 if (unsigned Reg = State.AllocateReg(Hexagon::D0)) {
322 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
323 return false;
324 }
325
Craig Topper840beec2014-04-04 05:16:06 +0000326 static const MCPhysReg RegList1[] = {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000327 Hexagon::D1, Hexagon::D2
328 };
Craig Topper840beec2014-04-04 05:16:06 +0000329 static const MCPhysReg RegList2[] = {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000330 Hexagon::R1, Hexagon::R3
331 };
Tim Northover3b6b7ca2015-02-21 02:11:17 +0000332 if (unsigned Reg = State.AllocateReg(RegList1, RegList2)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000333 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
334 return false;
335 }
336
337 unsigned Offset = State.AllocateStack(8, 8, Hexagon::D2);
338 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
339 return false;
340}
341
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000342static bool CC_HexagonVector(unsigned ValNo, MVT ValVT,
343 MVT LocVT, CCValAssign::LocInfo LocInfo,
344 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000345 static const MCPhysReg VecLstS[] = {
346 Hexagon::V0, Hexagon::V1, Hexagon::V2, Hexagon::V3, Hexagon::V4,
347 Hexagon::V5, Hexagon::V6, Hexagon::V7, Hexagon::V8, Hexagon::V9,
348 Hexagon::V10, Hexagon::V11, Hexagon::V12, Hexagon::V13, Hexagon::V14,
349 Hexagon::V15
350 };
351 static const MCPhysReg VecLstD[] = {
352 Hexagon::W0, Hexagon::W1, Hexagon::W2, Hexagon::W3, Hexagon::W4,
353 Hexagon::W5, Hexagon::W6, Hexagon::W7
354 };
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000355 auto &MF = State.getMachineFunction();
356 auto &HST = MF.getSubtarget<HexagonSubtarget>();
357 bool UseHVX = HST.useHVXOps();
358 bool UseHVXDbl = HST.useHVXDblOps();
359
360 if ((UseHVX && !UseHVXDbl) &&
361 (LocVT == MVT::v8i64 || LocVT == MVT::v16i32 || LocVT == MVT::v32i16 ||
362 LocVT == MVT::v64i8 || LocVT == MVT::v512i1)) {
363 if (unsigned Reg = State.AllocateReg(VecLstS)) {
364 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
365 return false;
366 }
367 unsigned Offset = State.AllocateStack(64, 64);
368 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
369 return false;
370 }
371 if ((UseHVX && !UseHVXDbl) &&
372 (LocVT == MVT::v16i64 || LocVT == MVT::v32i32 || LocVT == MVT::v64i16 ||
373 LocVT == MVT::v128i8)) {
374 if (unsigned Reg = State.AllocateReg(VecLstD)) {
375 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
376 return false;
377 }
378 unsigned Offset = State.AllocateStack(128, 128);
379 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
380 return false;
381 }
382 // 128B Mode
383 if ((UseHVX && UseHVXDbl) &&
384 (LocVT == MVT::v32i64 || LocVT == MVT::v64i32 || LocVT == MVT::v128i16 ||
385 LocVT == MVT::v256i8)) {
386 if (unsigned Reg = State.AllocateReg(VecLstD)) {
387 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
388 return false;
389 }
390 unsigned Offset = State.AllocateStack(256, 256);
391 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
392 return false;
393 }
394 if ((UseHVX && UseHVXDbl) &&
395 (LocVT == MVT::v16i64 || LocVT == MVT::v32i32 || LocVT == MVT::v64i16 ||
396 LocVT == MVT::v128i8 || LocVT == MVT::v1024i1)) {
397 if (unsigned Reg = State.AllocateReg(VecLstS)) {
398 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
399 return false;
400 }
401 unsigned Offset = State.AllocateStack(128, 128);
402 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
403 return false;
404 }
405 return true;
406}
407
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000408static bool RetCC_Hexagon(unsigned ValNo, MVT ValVT,
409 MVT LocVT, CCValAssign::LocInfo LocInfo,
410 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000411 auto &MF = State.getMachineFunction();
412 auto &HST = MF.getSubtarget<HexagonSubtarget>();
413 bool UseHVX = HST.useHVXOps();
414 bool UseHVXDbl = HST.useHVXDblOps();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000415
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000416 if (LocVT == MVT::i1) {
417 // Return values of type MVT::i1 still need to be assigned to R0, but
418 // the value type needs to remain i1. LowerCallResult will deal with it,
419 // but it needs to recognize i1 as the value type.
420 LocVT = MVT::i32;
421 } else if (LocVT == MVT::i8 || LocVT == MVT::i16) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000422 LocVT = MVT::i32;
423 ValVT = MVT::i32;
424 if (ArgFlags.isSExt())
425 LocInfo = CCValAssign::SExt;
426 else if (ArgFlags.isZExt())
427 LocInfo = CCValAssign::ZExt;
428 else
429 LocInfo = CCValAssign::AExt;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000430 } else if (LocVT == MVT::v4i8 || LocVT == MVT::v2i16) {
431 LocVT = MVT::i32;
432 LocInfo = CCValAssign::BCvt;
433 } else if (LocVT == MVT::v8i8 || LocVT == MVT::v4i16 || LocVT == MVT::v2i32) {
434 LocVT = MVT::i64;
435 LocInfo = CCValAssign::BCvt;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000436 } else if (LocVT == MVT::v64i8 || LocVT == MVT::v32i16 ||
437 LocVT == MVT::v16i32 || LocVT == MVT::v8i64 ||
438 LocVT == MVT::v512i1) {
439 LocVT = MVT::v16i32;
440 ValVT = MVT::v16i32;
441 LocInfo = CCValAssign::Full;
442 } else if (LocVT == MVT::v128i8 || LocVT == MVT::v64i16 ||
443 LocVT == MVT::v32i32 || LocVT == MVT::v16i64 ||
444 (LocVT == MVT::v1024i1 && UseHVX && UseHVXDbl)) {
445 LocVT = MVT::v32i32;
446 ValVT = MVT::v32i32;
447 LocInfo = CCValAssign::Full;
448 } else if (LocVT == MVT::v256i8 || LocVT == MVT::v128i16 ||
449 LocVT == MVT::v64i32 || LocVT == MVT::v32i64) {
450 LocVT = MVT::v64i32;
451 ValVT = MVT::v64i32;
452 LocInfo = CCValAssign::Full;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000453 }
Sirish Pande69295b82012-05-10 20:20:25 +0000454 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000455 if (!RetCC_Hexagon32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000456 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000457 }
458
Sirish Pande69295b82012-05-10 20:20:25 +0000459 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000460 if (!RetCC_Hexagon64(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000461 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000462 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000463 if (LocVT == MVT::v16i32 || LocVT == MVT::v32i32 || LocVT == MVT::v64i32) {
464 if (!RetCC_HexagonVector(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000465 return false;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000466 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000467 return true; // CC didn't match.
468}
469
470static bool RetCC_Hexagon32(unsigned ValNo, MVT ValVT,
471 MVT LocVT, CCValAssign::LocInfo LocInfo,
472 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Sirish Pande69295b82012-05-10 20:20:25 +0000473 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Krzysztof Parzyszek14412ef2016-07-18 17:36:46 +0000474 // Note that use of registers beyond R1 is not ABI compliant. However there
475 // are (experimental) IR passes which generate internal functions that
476 // return structs using these additional registers.
477 static const uint16_t RegList[] = { Hexagon::R0, Hexagon::R1,
478 Hexagon::R2, Hexagon::R3,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000479 Hexagon::R4, Hexagon::R5 };
Krzysztof Parzyszek14412ef2016-07-18 17:36:46 +0000480 if (unsigned Reg = State.AllocateReg(RegList)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000481 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
482 return false;
483 }
484 }
485
486 unsigned Offset = State.AllocateStack(4, 4);
487 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
488 return false;
489}
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
501 unsigned Offset = State.AllocateStack(8, 8);
502 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
503 return false;
504}
505
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000506static bool RetCC_HexagonVector(unsigned ValNo, MVT ValVT,
507 MVT LocVT, CCValAssign::LocInfo LocInfo,
508 ISD::ArgFlagsTy ArgFlags, CCState &State) {
509 auto &MF = State.getMachineFunction();
510 auto &HST = MF.getSubtarget<HexagonSubtarget>();
511 bool UseHVX = HST.useHVXOps();
512 bool UseHVXDbl = HST.useHVXDblOps();
513
514 unsigned OffSiz = 64;
515 if (LocVT == MVT::v16i32) {
516 if (unsigned Reg = State.AllocateReg(Hexagon::V0)) {
517 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
518 return false;
519 }
520 } else if (LocVT == MVT::v32i32) {
521 unsigned Req = (UseHVX && UseHVXDbl) ? Hexagon::V0 : Hexagon::W0;
522 if (unsigned Reg = State.AllocateReg(Req)) {
523 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
524 return false;
525 }
526 OffSiz = 128;
527 } else if (LocVT == MVT::v64i32) {
528 if (unsigned Reg = State.AllocateReg(Hexagon::W0)) {
529 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
530 return false;
531 }
532 OffSiz = 256;
533 }
534
535 unsigned Offset = State.AllocateStack(OffSiz, OffSiz);
536 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
537 return false;
538}
539
Craig Topper18e69f42016-04-15 06:20:21 +0000540void HexagonTargetLowering::promoteLdStType(MVT VT, MVT PromotedLdStVT) {
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000541 if (VT != PromotedLdStVT) {
Craig Topper18e69f42016-04-15 06:20:21 +0000542 setOperationAction(ISD::LOAD, VT, Promote);
543 AddPromotedToType(ISD::LOAD, VT, PromotedLdStVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000544
Craig Topper18e69f42016-04-15 06:20:21 +0000545 setOperationAction(ISD::STORE, VT, Promote);
546 AddPromotedToType(ISD::STORE, VT, PromotedLdStVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000547 }
548}
549
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000550SDValue
551HexagonTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG)
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000552 const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000553 return SDValue();
554}
555
556/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
557/// by "Src" to address "Dst" of size "Size". Alignment information is
558/// specified by the specific parameter attribute. The copy will be passed as
559/// a byval function parameter. Sometimes what we are copying is the end of a
560/// larger object, the part that does not fit in registers.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000561static SDValue CreateCopyOfByValArgument(SDValue Src, SDValue Dst,
562 SDValue Chain, ISD::ArgFlagsTy Flags,
563 SelectionDAG &DAG, const SDLoc &dl) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000564 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000565 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
566 /*isVolatile=*/false, /*AlwaysInline=*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +0000567 /*isTailCall=*/false,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000568 MachinePointerInfo(), MachinePointerInfo());
569}
570
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000571static bool isHvxVectorType(MVT Ty) {
572 switch (Ty.SimpleTy) {
573 case MVT::v8i64:
574 case MVT::v16i32:
575 case MVT::v32i16:
576 case MVT::v64i8:
577 case MVT::v16i64:
578 case MVT::v32i32:
579 case MVT::v64i16:
580 case MVT::v128i8:
581 case MVT::v32i64:
582 case MVT::v64i32:
583 case MVT::v128i16:
584 case MVT::v256i8:
585 case MVT::v512i1:
586 case MVT::v1024i1:
587 return true;
588 default:
589 return false;
590 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000591}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000592
593// LowerReturn - Lower ISD::RET. If a struct is larger than 8 bytes and is
594// passed by value, the function prototype is modified to return void and
595// the value is stored in memory pointed by a pointer passed by caller.
596SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000597HexagonTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
598 bool isVarArg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000599 const SmallVectorImpl<ISD::OutputArg> &Outs,
600 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000601 const SDLoc &dl, SelectionDAG &DAG) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000602 // CCValAssign - represent the assignment of the return value to locations.
603 SmallVector<CCValAssign, 16> RVLocs;
604
605 // CCState - Info about the registers and stack slot.
Eric Christopherb5217502014-08-06 18:45:26 +0000606 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
607 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000608
609 // Analyze return values of ISD::RET
610 CCInfo.AnalyzeReturn(Outs, RetCC_Hexagon);
611
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000612 SDValue Flag;
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000613 SmallVector<SDValue, 4> RetOps(1, Chain);
614
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000615 // Copy the result values into the output registers.
616 for (unsigned i = 0; i != RVLocs.size(); ++i) {
617 CCValAssign &VA = RVLocs[i];
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000618
619 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), OutVals[i], Flag);
620
621 // Guarantee that all emitted copies are stuck together with flags.
622 Flag = Chain.getValue(1);
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000623 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000624 }
625
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000626 RetOps[0] = Chain; // Update chain.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000627
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000628 // Add the flag if we have it.
629 if (Flag.getNode())
630 RetOps.push_back(Flag);
631
Craig Topper48d114b2014-04-26 18:35:24 +0000632 return DAG.getNode(HexagonISD::RET_FLAG, dl, MVT::Other, RetOps);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000633}
634
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000635bool HexagonTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
636 // If either no tail call or told not to tail call at all, don't.
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000637 auto Attr =
638 CI->getParent()->getParent()->getFnAttribute("disable-tail-calls");
639 if (!CI->isTailCall() || Attr.getValueAsString() == "true")
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000640 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000641
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000642 return true;
643}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000644
645/// LowerCallResult - Lower the result values of an ISD::CALL into the
646/// appropriate copies out of appropriate physical registers. This assumes that
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000647/// Chain/Glue are the input chain/glue to use, and that TheCall is the call
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000648/// being lowered. Returns a SDNode with the same number of values as the
649/// ISD::CALL.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000650SDValue HexagonTargetLowering::LowerCallResult(
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000651 SDValue Chain, SDValue Glue, CallingConv::ID CallConv, bool isVarArg,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000652 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
653 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals,
654 const SmallVectorImpl<SDValue> &OutVals, SDValue Callee) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000655 // Assign locations to each value returned by this call.
656 SmallVector<CCValAssign, 16> RVLocs;
657
Eric Christopherb5217502014-08-06 18:45:26 +0000658 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
659 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000660
661 CCInfo.AnalyzeCallResult(Ins, RetCC_Hexagon);
662
663 // Copy all of the result registers out of their specified physreg.
664 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000665 SDValue RetVal;
666 if (RVLocs[i].getValVT() == MVT::i1) {
667 // Return values of type MVT::i1 require special handling. The reason
668 // is that MVT::i1 is associated with the PredRegs register class, but
669 // values of that type are still returned in R0. Generate an explicit
670 // copy into a predicate register from R0, and treat the value of the
671 // predicate register as the call result.
672 auto &MRI = DAG.getMachineFunction().getRegInfo();
673 SDValue FR0 = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000674 MVT::i32, Glue);
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000675 // FR0 = (Value, Chain, Glue)
676 unsigned PredR = MRI.createVirtualRegister(&Hexagon::PredRegsRegClass);
677 SDValue TPR = DAG.getCopyToReg(FR0.getValue(1), dl, PredR,
678 FR0.getValue(0), FR0.getValue(2));
679 // TPR = (Chain, Glue)
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000680 // Don't glue this CopyFromReg, because it copies from a virtual
681 // register. If it is glued to the call, InstrEmitter will add it
682 // as an implicit def to the call (EmitMachineNode).
683 RetVal = DAG.getCopyFromReg(TPR.getValue(0), dl, PredR, MVT::i1);
684 Glue = TPR.getValue(1);
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000685 } else {
686 RetVal = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000687 RVLocs[i].getValVT(), Glue);
688 Glue = RetVal.getValue(2);
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000689 }
690 InVals.push_back(RetVal.getValue(0));
691 Chain = RetVal.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000692 }
693
694 return Chain;
695}
696
697/// LowerCall - Functions arguments are copied from virtual regs to
698/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
699SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +0000700HexagonTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000701 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +0000702 SelectionDAG &DAG = CLI.DAG;
Craig Topperb94011f2013-07-14 04:42:23 +0000703 SDLoc &dl = CLI.DL;
704 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
705 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
706 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000707 SDValue Chain = CLI.Chain;
708 SDValue Callee = CLI.Callee;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000709 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000710 CallingConv::ID CallConv = CLI.CallConv;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000711 bool IsVarArg = CLI.IsVarArg;
712 bool DoesNotReturn = CLI.DoesNotReturn;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000713
714 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000715 MachineFunction &MF = DAG.getMachineFunction();
Mehdi Amini44ede332015-07-09 02:09:04 +0000716 auto PtrVT = getPointerTy(MF.getDataLayout());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000717
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000718 // Check for varargs.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000719 unsigned NumNamedVarArgParams = -1U;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000720 if (GlobalAddressSDNode *GAN = dyn_cast<GlobalAddressSDNode>(Callee)) {
721 const GlobalValue *GV = GAN->getGlobal();
722 Callee = DAG.getTargetGlobalAddress(GV, dl, MVT::i32);
723 if (const Function* F = dyn_cast<Function>(GV)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000724 // If a function has zero args and is a vararg function, that's
725 // disallowed so it must be an undeclared function. Do not assume
726 // varargs if the callee is undefined.
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000727 if (F->isVarArg() && F->getFunctionType()->getNumParams() != 0)
728 NumNamedVarArgParams = F->getFunctionType()->getNumParams();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000729 }
730 }
731
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000732 // Analyze operands of the call, assigning locations to each operand.
733 SmallVector<CCValAssign, 16> ArgLocs;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000734 HexagonCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
Eric Christopherb5217502014-08-06 18:45:26 +0000735 *DAG.getContext(), NumNamedVarArgParams);
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000736
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000737 if (IsVarArg)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000738 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon_VarArg);
739 else
740 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon);
741
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000742 auto Attr = MF.getFunction()->getFnAttribute("disable-tail-calls");
743 if (Attr.getValueAsString() == "true")
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000744 IsTailCall = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000745
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000746 if (IsTailCall) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000747 bool StructAttrFlag = MF.getFunction()->hasStructRetAttr();
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000748 IsTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
749 IsVarArg, IsStructRet,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000750 StructAttrFlag,
751 Outs, OutVals, Ins, DAG);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000752 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000753 CCValAssign &VA = ArgLocs[i];
754 if (VA.isMemLoc()) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000755 IsTailCall = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000756 break;
757 }
758 }
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000759 DEBUG(dbgs() << (IsTailCall ? "Eligible for Tail Call\n"
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000760 : "Argument must be passed on stack. "
761 "Not eligible for Tail Call\n"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000762 }
763 // Get a count of how many bytes are to be pushed on the stack.
764 unsigned NumBytes = CCInfo.getNextStackOffset();
765 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
766 SmallVector<SDValue, 8> MemOpChains;
767
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000768 auto &HRI = *Subtarget.getRegisterInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +0000769 SDValue StackPtr =
770 DAG.getCopyFromReg(Chain, dl, HRI.getStackRegister(), PtrVT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000771
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000772 bool NeedsArgAlign = false;
773 unsigned LargestAlignSeen = 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000774 // Walk the register/memloc assignments, inserting copies/loads.
775 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
776 CCValAssign &VA = ArgLocs[i];
777 SDValue Arg = OutVals[i];
778 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000779 // Record if we need > 8 byte alignment on an argument.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000780 bool ArgAlign = isHvxVectorType(VA.getValVT());
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000781 NeedsArgAlign |= ArgAlign;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000782
783 // Promote the value if needed.
784 switch (VA.getLocInfo()) {
785 default:
786 // Loc info must be one of Full, SExt, ZExt, or AExt.
Craig Toppere55c5562012-02-07 02:50:20 +0000787 llvm_unreachable("Unknown loc info!");
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000788 case CCValAssign::BCvt:
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000789 case CCValAssign::Full:
790 break;
791 case CCValAssign::SExt:
792 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
793 break;
794 case CCValAssign::ZExt:
795 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
796 break;
797 case CCValAssign::AExt:
798 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
799 break;
800 }
801
802 if (VA.isMemLoc()) {
803 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000804 SDValue MemAddr = DAG.getConstant(LocMemOffset, dl,
805 StackPtr.getValueType());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000806 MemAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, MemAddr);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000807 if (ArgAlign)
808 LargestAlignSeen = std::max(LargestAlignSeen,
809 VA.getLocVT().getStoreSizeInBits() >> 3);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000810 if (Flags.isByVal()) {
811 // The argument is a struct passed by value. According to LLVM, "Arg"
812 // is is pointer.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000813 MemOpChains.push_back(CreateCopyOfByValArgument(Arg, MemAddr, Chain,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000814 Flags, DAG, dl));
815 } else {
Alex Lorenze40c8a22015-08-11 23:09:45 +0000816 MachinePointerInfo LocPI = MachinePointerInfo::getStack(
817 DAG.getMachineFunction(), LocMemOffset);
Justin Lebar9c375812016-07-15 18:27:10 +0000818 SDValue S = DAG.getStore(Chain, dl, Arg, MemAddr, LocPI);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000819 MemOpChains.push_back(S);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000820 }
821 continue;
822 }
823
824 // Arguments that can be passed on register must be kept at RegsToPass
825 // vector.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000826 if (VA.isRegLoc())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000827 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000828 }
829
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000830 if (NeedsArgAlign && Subtarget.hasV60TOps()) {
831 DEBUG(dbgs() << "Function needs byte stack align due to call args\n");
Matthias Braun941a7052016-07-28 18:40:00 +0000832 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000833 // V6 vectors passed by value have 64 or 128 byte alignment depending
834 // on whether we are 64 byte vector mode or 128 byte.
835 bool UseHVXDbl = Subtarget.useHVXDblOps();
836 assert(Subtarget.useHVXOps());
837 const unsigned ObjAlign = UseHVXDbl ? 128 : 64;
838 LargestAlignSeen = std::max(LargestAlignSeen, ObjAlign);
Matthias Braun941a7052016-07-28 18:40:00 +0000839 MFI.ensureMaxAlignment(LargestAlignSeen);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000840 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000841 // Transform all store nodes into one single node because all store
842 // nodes are independent of each other.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000843 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000844 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000845
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000846 SDValue Glue;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000847 if (!IsTailCall) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000848 SDValue C = DAG.getConstant(NumBytes, dl, PtrVT, true);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000849 Chain = DAG.getCALLSEQ_START(Chain, C, dl);
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000850 Glue = Chain.getValue(1);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000851 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000852
853 // Build a sequence of copy-to-reg nodes chained together with token
854 // chain and flag operands which copy the outgoing args into registers.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000855 // The Glue is necessary since all emitted instructions must be
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000856 // stuck together.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000857 if (!IsTailCall) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000858 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
859 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000860 RegsToPass[i].second, Glue);
861 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000862 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000863 } else {
864 // For tail calls lower the arguments to the 'real' stack slot.
865 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000866 // Force all the incoming stack arguments to be loaded from the stack
867 // before any new outgoing arguments are stored to the stack, because the
868 // outgoing stack slots may alias the incoming argument stack slots, and
869 // the alias isn't otherwise explicit. This is slightly more conservative
870 // than necessary, because it means that each store effectively depends
871 // on every argument instead of just those arguments it would clobber.
872 //
Benjamin Kramerbde91762012-06-02 10:20:22 +0000873 // Do not flag preceding copytoreg stuff together with the following stuff.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000874 Glue = SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000875 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
876 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000877 RegsToPass[i].second, Glue);
878 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000879 }
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000880 Glue = SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000881 }
882
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000883 bool LongCalls = MF.getSubtarget<HexagonSubtarget>().useLongCalls();
884 unsigned Flags = LongCalls ? HexagonII::HMOTF_ConstExtended : 0;
885
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000886 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
887 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
888 // node so that legalize doesn't hack it.
Tobias Edler von Kochb51460c2015-12-16 17:29:37 +0000889 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000890 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, PtrVT, 0, Flags);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000891 } else if (ExternalSymbolSDNode *S =
892 dyn_cast<ExternalSymbolSDNode>(Callee)) {
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000893 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), PtrVT, Flags);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000894 }
895
896 // Returns a chain & a flag for retval copy to use.
897 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
898 SmallVector<SDValue, 8> Ops;
899 Ops.push_back(Chain);
900 Ops.push_back(Callee);
901
902 // Add argument registers to the end of the list so that they are
903 // known live into the call.
904 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
905 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
906 RegsToPass[i].second.getValueType()));
907 }
908
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000909 const uint32_t *Mask = HRI.getCallPreservedMask(MF, CallConv);
910 assert(Mask && "Missing call preserved mask for calling convention");
911 Ops.push_back(DAG.getRegisterMask(Mask));
912
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000913 if (Glue.getNode())
914 Ops.push_back(Glue);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000915
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000916 if (IsTailCall) {
Matthias Braun941a7052016-07-28 18:40:00 +0000917 MF.getFrameInfo().setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +0000918 return DAG.getNode(HexagonISD::TC_RETURN, dl, NodeTys, Ops);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +0000919 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000920
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +0000921 unsigned OpCode = DoesNotReturn ? HexagonISD::CALLnr : HexagonISD::CALL;
Colin LeMahieu2e3a26d2015-01-16 17:05:27 +0000922 Chain = DAG.getNode(OpCode, dl, NodeTys, Ops);
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000923 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000924
925 // Create the CALLSEQ_END node.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000926 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000927 DAG.getIntPtrConstant(0, dl, true), Glue, dl);
928 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000929
930 // Handle result values, copying them out of physregs into vregs that we
931 // return.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000932 return LowerCallResult(Chain, Glue, CallConv, IsVarArg, Ins, dl, DAG,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000933 InVals, OutVals, Callee);
934}
935
936static bool getIndexedAddressParts(SDNode *Ptr, EVT VT,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000937 SDValue &Base, SDValue &Offset,
938 bool &IsInc, SelectionDAG &DAG) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000939 if (Ptr->getOpcode() != ISD::ADD)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000940 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000941
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000942 auto &HST = static_cast<const HexagonSubtarget&>(DAG.getSubtarget());
943 bool UseHVX = HST.useHVXOps();
944 bool UseHVXDbl = HST.useHVXDblOps();
945
946 bool ValidHVXDblType =
947 (UseHVX && UseHVXDbl) && (VT == MVT::v32i32 || VT == MVT::v16i64 ||
948 VT == MVT::v64i16 || VT == MVT::v128i8);
949 bool ValidHVXType =
950 UseHVX && !UseHVXDbl && (VT == MVT::v16i32 || VT == MVT::v8i64 ||
951 VT == MVT::v32i16 || VT == MVT::v64i8);
952
953 if (ValidHVXDblType || ValidHVXType ||
954 VT == MVT::i64 || VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000955 IsInc = (Ptr->getOpcode() == ISD::ADD);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000956 Base = Ptr->getOperand(0);
957 Offset = Ptr->getOperand(1);
958 // Ensure that Offset is a constant.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000959 return isa<ConstantSDNode>(Offset);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000960 }
961
962 return false;
963}
964
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000965/// getPostIndexedAddressParts - returns true by value, base pointer and
966/// offset pointer and addressing mode by reference if this node can be
967/// combined with a load / store to form a post-indexed load / store.
968bool HexagonTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
969 SDValue &Base,
970 SDValue &Offset,
971 ISD::MemIndexedMode &AM,
972 SelectionDAG &DAG) const
973{
974 EVT VT;
975 SDValue Ptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000976
977 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
978 VT = LD->getMemoryVT();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000979 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
980 VT = ST->getMemoryVT();
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000981 if (ST->getValue().getValueType() == MVT::i64 && ST->isTruncatingStore())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000982 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000983 } else {
984 return false;
985 }
986
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000987 bool IsInc = false;
988 bool isLegal = getIndexedAddressParts(Op, VT, Base, Offset, IsInc, DAG);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000989 if (isLegal) {
990 auto &HII = *Subtarget.getInstrInfo();
991 int32_t OffsetVal = cast<ConstantSDNode>(Offset.getNode())->getSExtValue();
992 if (HII.isValidAutoIncImm(VT, OffsetVal)) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000993 AM = IsInc ? ISD::POST_INC : ISD::POST_DEC;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000994 return true;
995 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000996 }
997
998 return false;
999}
1000
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001001SDValue
1002HexagonTargetLowering::LowerINLINEASM(SDValue Op, SelectionDAG &DAG) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001003 SDNode *Node = Op.getNode();
1004 MachineFunction &MF = DAG.getMachineFunction();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001005 auto &FuncInfo = *MF.getInfo<HexagonMachineFunctionInfo>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001006 switch (Node->getOpcode()) {
1007 case ISD::INLINEASM: {
1008 unsigned NumOps = Node->getNumOperands();
1009 if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue)
1010 --NumOps; // Ignore the flag operand.
1011
1012 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001013 if (FuncInfo.hasClobberLR())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001014 break;
1015 unsigned Flags =
1016 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
1017 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
1018 ++i; // Skip the ID value.
1019
1020 switch (InlineAsm::getKind(Flags)) {
1021 default: llvm_unreachable("Bad flags!");
1022 case InlineAsm::Kind_RegDef:
1023 case InlineAsm::Kind_RegUse:
1024 case InlineAsm::Kind_Imm:
1025 case InlineAsm::Kind_Clobber:
1026 case InlineAsm::Kind_Mem: {
1027 for (; NumVals; --NumVals, ++i) {}
1028 break;
1029 }
1030 case InlineAsm::Kind_RegDefEarlyClobber: {
1031 for (; NumVals; --NumVals, ++i) {
1032 unsigned Reg =
1033 cast<RegisterSDNode>(Node->getOperand(i))->getReg();
1034
1035 // Check it to be lr
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001036 const HexagonRegisterInfo *QRI = Subtarget.getRegisterInfo();
Eric Christopherdbe1cb02014-06-27 00:13:52 +00001037 if (Reg == QRI->getRARegister()) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001038 FuncInfo.setHasClobberLR(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001039 break;
1040 }
1041 }
1042 break;
1043 }
1044 }
1045 }
1046 }
1047 } // Node->getOpcode
1048 return Op;
1049}
1050
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00001051// Need to transform ISD::PREFETCH into something that doesn't inherit
1052// all of the properties of ISD::PREFETCH, specifically SDNPMayLoad and
1053// SDNPMayStore.
1054SDValue HexagonTargetLowering::LowerPREFETCH(SDValue Op,
1055 SelectionDAG &DAG) const {
1056 SDValue Chain = Op.getOperand(0);
1057 SDValue Addr = Op.getOperand(1);
1058 // Lower it to DCFETCH($reg, #0). A "pat" will try to merge the offset in,
1059 // if the "reg" is fed by an "add".
1060 SDLoc DL(Op);
1061 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
1062 return DAG.getNode(HexagonISD::DCFETCH, DL, MVT::Other, Chain, Addr, Zero);
1063}
1064
1065SDValue HexagonTargetLowering::LowerINTRINSIC_VOID(SDValue Op,
1066 SelectionDAG &DAG) const {
1067 SDValue Chain = Op.getOperand(0);
1068 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1069 // Lower the hexagon_prefetch builtin to DCFETCH, as above.
1070 if (IntNo == Intrinsic::hexagon_prefetch) {
1071 SDValue Addr = Op.getOperand(2);
1072 SDLoc DL(Op);
1073 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
1074 return DAG.getNode(HexagonISD::DCFETCH, DL, MVT::Other, Chain, Addr, Zero);
1075 }
1076 return SDValue();
1077}
1078
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001079SDValue
1080HexagonTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
1081 SelectionDAG &DAG) const {
1082 SDValue Chain = Op.getOperand(0);
1083 SDValue Size = Op.getOperand(1);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001084 SDValue Align = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001085 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001086
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001087 ConstantSDNode *AlignConst = dyn_cast<ConstantSDNode>(Align);
1088 assert(AlignConst && "Non-constant Align in LowerDYNAMIC_STACKALLOC");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001089
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001090 unsigned A = AlignConst->getSExtValue();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001091 auto &HFI = *Subtarget.getFrameLowering();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001092 // "Zero" means natural stack alignment.
1093 if (A == 0)
1094 A = HFI.getStackAlignment();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001095
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001096 DEBUG({
Reid Kleckner40d72302016-10-20 00:22:23 +00001097 dbgs () << __func__ << " Align: " << A << " Size: ";
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001098 Size.getNode()->dump(&DAG);
1099 dbgs() << "\n";
1100 });
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001101
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001102 SDValue AC = DAG.getConstant(A, dl, MVT::i32);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001103 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00001104 SDValue AA = DAG.getNode(HexagonISD::ALLOCA, dl, VTs, Chain, Size, AC);
Nirav Davebfdb4832016-06-23 17:52:57 +00001105
1106 DAG.ReplaceAllUsesOfValueWith(Op, AA);
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00001107 return AA;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001108}
1109
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001110SDValue HexagonTargetLowering::LowerFormalArguments(
1111 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
1112 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
1113 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001114 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00001115 MachineFrameInfo &MFI = MF.getFrameInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001116 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001117 auto &FuncInfo = *MF.getInfo<HexagonMachineFunctionInfo>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001118
1119 // Assign locations to all of the incoming arguments.
1120 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001121 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1122 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001123
1124 CCInfo.AnalyzeFormalArguments(Ins, CC_Hexagon);
1125
1126 // For LLVM, in the case when returning a struct by value (>8byte),
1127 // the first argument is a pointer that points to the location on caller's
1128 // stack where the return value will be stored. For Hexagon, the location on
1129 // caller's stack is passed only when the struct size is smaller than (and
1130 // equal to) 8 bytes. If not, no address will be passed into callee and
1131 // callee return the result direclty through R0/R1.
1132
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001133 SmallVector<SDValue, 8> MemOps;
1134 bool UseHVX = Subtarget.useHVXOps(), UseHVXDbl = Subtarget.useHVXDblOps();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001135
1136 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1137 CCValAssign &VA = ArgLocs[i];
1138 ISD::ArgFlagsTy Flags = Ins[i].Flags;
1139 unsigned ObjSize;
1140 unsigned StackLocation;
1141 int FI;
1142
1143 if ( (VA.isRegLoc() && !Flags.isByVal())
1144 || (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() > 8)) {
1145 // Arguments passed in registers
1146 // 1. int, long long, ptr args that get allocated in register.
1147 // 2. Large struct that gets an register to put its address in.
1148 EVT RegVT = VA.getLocVT();
Sirish Pande69295b82012-05-10 20:20:25 +00001149 if (RegVT == MVT::i8 || RegVT == MVT::i16 ||
1150 RegVT == MVT::i32 || RegVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001151 unsigned VReg =
Craig Topperc7242e02012-04-20 07:30:17 +00001152 RegInfo.createVirtualRegister(&Hexagon::IntRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001153 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1154 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Colin LeMahieu4379d102015-01-28 22:08:16 +00001155 } else if (RegVT == MVT::i64 || RegVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001156 unsigned VReg =
Craig Topperc7242e02012-04-20 07:30:17 +00001157 RegInfo.createVirtualRegister(&Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001158 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1159 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001160
1161 // Single Vector
1162 } else if ((RegVT == MVT::v8i64 || RegVT == MVT::v16i32 ||
1163 RegVT == MVT::v32i16 || RegVT == MVT::v64i8)) {
1164 unsigned VReg =
1165 RegInfo.createVirtualRegister(&Hexagon::VectorRegsRegClass);
1166 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1167 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1168 } else if (UseHVX && UseHVXDbl &&
1169 ((RegVT == MVT::v16i64 || RegVT == MVT::v32i32 ||
1170 RegVT == MVT::v64i16 || RegVT == MVT::v128i8))) {
1171 unsigned VReg =
1172 RegInfo.createVirtualRegister(&Hexagon::VectorRegs128BRegClass);
1173 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1174 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1175
1176 // Double Vector
1177 } else if ((RegVT == MVT::v16i64 || RegVT == MVT::v32i32 ||
1178 RegVT == MVT::v64i16 || RegVT == MVT::v128i8)) {
1179 unsigned VReg =
1180 RegInfo.createVirtualRegister(&Hexagon::VecDblRegsRegClass);
1181 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1182 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1183 } else if (UseHVX && UseHVXDbl &&
1184 ((RegVT == MVT::v32i64 || RegVT == MVT::v64i32 ||
1185 RegVT == MVT::v128i16 || RegVT == MVT::v256i8))) {
1186 unsigned VReg =
1187 RegInfo.createVirtualRegister(&Hexagon::VecDblRegs128BRegClass);
1188 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1189 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1190 } else if (RegVT == MVT::v512i1 || RegVT == MVT::v1024i1) {
1191 assert(0 && "need to support VecPred regs");
1192 unsigned VReg =
1193 RegInfo.createVirtualRegister(&Hexagon::VecPredRegsRegClass);
1194 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1195 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001196 } else {
1197 assert (0);
1198 }
1199 } else if (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() <= 8) {
1200 assert (0 && "ByValSize must be bigger than 8 bytes");
1201 } else {
1202 // Sanity check.
1203 assert(VA.isMemLoc());
1204
1205 if (Flags.isByVal()) {
1206 // If it's a byval parameter, then we need to compute the
1207 // "real" size, not the size of the pointer.
1208 ObjSize = Flags.getByValSize();
1209 } else {
1210 ObjSize = VA.getLocVT().getStoreSizeInBits() >> 3;
1211 }
1212
1213 StackLocation = HEXAGON_LRFP_SIZE + VA.getLocMemOffset();
1214 // Create the frame index object for this incoming parameter...
Matthias Braun941a7052016-07-28 18:40:00 +00001215 FI = MFI.CreateFixedObject(ObjSize, StackLocation, true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001216
1217 // Create the SelectionDAG nodes cordl, responding to a load
1218 // from this parameter.
1219 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
1220
1221 if (Flags.isByVal()) {
1222 // If it's a pass-by-value aggregate, then do not dereference the stack
1223 // location. Instead, we should generate a reference to the stack
1224 // location.
1225 InVals.push_back(FIN);
1226 } else {
Justin Lebar9c375812016-07-15 18:27:10 +00001227 InVals.push_back(
1228 DAG.getLoad(VA.getLocVT(), dl, Chain, FIN, MachinePointerInfo()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001229 }
1230 }
1231 }
1232
1233 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001234 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001235
1236 if (isVarArg) {
1237 // This will point to the next argument passed via stack.
Matthias Braun941a7052016-07-28 18:40:00 +00001238 int FrameIndex = MFI.CreateFixedObject(Hexagon_PointerSize,
1239 HEXAGON_LRFP_SIZE +
1240 CCInfo.getNextStackOffset(),
1241 true);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001242 FuncInfo.setVarArgsFrameIndex(FrameIndex);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001243 }
1244
1245 return Chain;
1246}
1247
1248SDValue
1249HexagonTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
1250 // VASTART stores the address of the VarArgsFrameIndex slot into the
1251 // memory location argument.
1252 MachineFunction &MF = DAG.getMachineFunction();
1253 HexagonMachineFunctionInfo *QFI = MF.getInfo<HexagonMachineFunctionInfo>();
1254 SDValue Addr = DAG.getFrameIndex(QFI->getVarArgsFrameIndex(), MVT::i32);
1255 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Justin Lebar9c375812016-07-15 18:27:10 +00001256 return DAG.getStore(Op.getOperand(0), SDLoc(Op), Addr, Op.getOperand(1),
1257 MachinePointerInfo(SV));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001258}
1259
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001260// Creates a SPLAT instruction for a constant value VAL.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001261static SDValue createSplat(SelectionDAG &DAG, const SDLoc &dl, EVT VT,
1262 SDValue Val) {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001263 if (VT.getSimpleVT() == MVT::v4i8)
1264 return DAG.getNode(HexagonISD::VSPLATB, dl, VT, Val);
1265
1266 if (VT.getSimpleVT() == MVT::v4i16)
1267 return DAG.getNode(HexagonISD::VSPLATH, dl, VT, Val);
1268
1269 return SDValue();
1270}
1271
1272static bool isSExtFree(SDValue N) {
1273 // A sign-extend of a truncate of a sign-extend is free.
1274 if (N.getOpcode() == ISD::TRUNCATE &&
1275 N.getOperand(0).getOpcode() == ISD::AssertSext)
1276 return true;
1277 // We have sign-extended loads.
1278 if (N.getOpcode() == ISD::LOAD)
1279 return true;
1280 return false;
1281}
1282
1283SDValue HexagonTargetLowering::LowerCTPOP(SDValue Op, SelectionDAG &DAG) const {
1284 SDLoc dl(Op);
1285 SDValue InpVal = Op.getOperand(0);
1286 if (isa<ConstantSDNode>(InpVal)) {
1287 uint64_t V = cast<ConstantSDNode>(InpVal)->getZExtValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001288 return DAG.getTargetConstant(countPopulation(V), dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001289 }
1290 SDValue PopOut = DAG.getNode(HexagonISD::POPCOUNT, dl, MVT::i32, InpVal);
1291 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, PopOut);
1292}
1293
1294SDValue HexagonTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1295 SDLoc dl(Op);
1296
1297 SDValue LHS = Op.getOperand(0);
1298 SDValue RHS = Op.getOperand(1);
1299 SDValue Cmp = Op.getOperand(2);
1300 ISD::CondCode CC = cast<CondCodeSDNode>(Cmp)->get();
1301
1302 EVT VT = Op.getValueType();
1303 EVT LHSVT = LHS.getValueType();
1304 EVT RHSVT = RHS.getValueType();
1305
1306 if (LHSVT == MVT::v2i16) {
1307 assert(ISD::isSignedIntSetCC(CC) || ISD::isUnsignedIntSetCC(CC));
1308 unsigned ExtOpc = ISD::isSignedIntSetCC(CC) ? ISD::SIGN_EXTEND
1309 : ISD::ZERO_EXTEND;
1310 SDValue LX = DAG.getNode(ExtOpc, dl, MVT::v2i32, LHS);
1311 SDValue RX = DAG.getNode(ExtOpc, dl, MVT::v2i32, RHS);
1312 SDValue SC = DAG.getNode(ISD::SETCC, dl, MVT::v2i1, LX, RX, Cmp);
1313 return SC;
1314 }
1315
1316 // Treat all other vector types as legal.
1317 if (VT.isVector())
1318 return Op;
1319
1320 // Equals and not equals should use sign-extend, not zero-extend, since
1321 // we can represent small negative values in the compare instructions.
1322 // The LLVM default is to use zero-extend arbitrarily in these cases.
1323 if ((CC == ISD::SETEQ || CC == ISD::SETNE) &&
1324 (RHSVT == MVT::i8 || RHSVT == MVT::i16) &&
1325 (LHSVT == MVT::i8 || LHSVT == MVT::i16)) {
1326 ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS);
1327 if (C && C->getAPIntValue().isNegative()) {
1328 LHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, LHS);
1329 RHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, RHS);
1330 return DAG.getNode(ISD::SETCC, dl, Op.getValueType(),
1331 LHS, RHS, Op.getOperand(2));
1332 }
1333 if (isSExtFree(LHS) || isSExtFree(RHS)) {
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 }
1340 return SDValue();
1341}
1342
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001343SDValue
1344HexagonTargetLowering::LowerVSELECT(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001345 SDValue PredOp = Op.getOperand(0);
1346 SDValue Op1 = Op.getOperand(1), Op2 = Op.getOperand(2);
1347 EVT OpVT = Op1.getValueType();
1348 SDLoc DL(Op);
1349
1350 if (OpVT == MVT::v2i16) {
1351 SDValue X1 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op1);
1352 SDValue X2 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op2);
1353 SDValue SL = DAG.getNode(ISD::VSELECT, DL, MVT::v2i32, PredOp, X1, X2);
1354 SDValue TR = DAG.getNode(ISD::TRUNCATE, DL, MVT::v2i16, SL);
1355 return TR;
1356 }
1357
1358 return SDValue();
1359}
1360
1361// Handle only specific vector loads.
1362SDValue HexagonTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1363 EVT VT = Op.getValueType();
1364 SDLoc DL(Op);
1365 LoadSDNode *LoadNode = cast<LoadSDNode>(Op);
1366 SDValue Chain = LoadNode->getChain();
1367 SDValue Ptr = Op.getOperand(1);
1368 SDValue LoweredLoad;
1369 SDValue Result;
1370 SDValue Base = LoadNode->getBasePtr();
1371 ISD::LoadExtType Ext = LoadNode->getExtensionType();
1372 unsigned Alignment = LoadNode->getAlignment();
1373 SDValue LoadChain;
1374
1375 if(Ext == ISD::NON_EXTLOAD)
1376 Ext = ISD::ZEXTLOAD;
1377
1378 if (VT == MVT::v4i16) {
1379 if (Alignment == 2) {
1380 SDValue Loads[4];
1381 // Base load.
1382 Loads[0] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Base,
Justin Lebar9c375812016-07-15 18:27:10 +00001383 LoadNode->getPointerInfo(), MVT::i16, Alignment,
1384 LoadNode->getMemOperand()->getFlags());
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001385 // Base+2 load.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001386 SDValue Increment = DAG.getConstant(2, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001387 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1388 Loads[1] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
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 // SHL 16, then OR base and base+2.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001392 SDValue ShiftAmount = DAG.getConstant(16, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001393 SDValue Tmp1 = DAG.getNode(ISD::SHL, DL, MVT::i32, Loads[1], ShiftAmount);
1394 SDValue Tmp2 = DAG.getNode(ISD::OR, DL, MVT::i32, Tmp1, Loads[0]);
1395 // Base + 4.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001396 Increment = DAG.getConstant(4, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001397 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1398 Loads[2] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
Justin Lebar9c375812016-07-15 18:27:10 +00001399 LoadNode->getPointerInfo(), MVT::i16, Alignment,
1400 LoadNode->getMemOperand()->getFlags());
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001401 // Base + 6.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001402 Increment = DAG.getConstant(6, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001403 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1404 Loads[3] = 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 // SHL 16, then OR base+4 and base+6.
1408 Tmp1 = DAG.getNode(ISD::SHL, DL, MVT::i32, Loads[3], ShiftAmount);
1409 SDValue Tmp4 = DAG.getNode(ISD::OR, DL, MVT::i32, Tmp1, Loads[2]);
1410 // Combine to i64. This could be optimised out later if we can
1411 // affect reg allocation of this code.
1412 Result = DAG.getNode(HexagonISD::COMBINE, DL, MVT::i64, Tmp4, Tmp2);
1413 LoadChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
1414 Loads[0].getValue(1), Loads[1].getValue(1),
1415 Loads[2].getValue(1), Loads[3].getValue(1));
1416 } else {
1417 // Perform default type expansion.
1418 Result = DAG.getLoad(MVT::i64, DL, Chain, Ptr, LoadNode->getPointerInfo(),
Justin Lebar9c375812016-07-15 18:27:10 +00001419 LoadNode->getAlignment(),
1420 LoadNode->getMemOperand()->getFlags());
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001421 LoadChain = Result.getValue(1);
1422 }
1423 } else
1424 llvm_unreachable("Custom lowering unsupported load");
1425
1426 Result = DAG.getNode(ISD::BITCAST, DL, VT, Result);
1427 // Since we pretend to lower a load, we need the original chain
1428 // info attached to the result.
1429 SDValue Ops[] = { Result, LoadChain };
1430
1431 return DAG.getMergeValues(Ops, DL);
1432}
1433
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001434SDValue
Sirish Pande69295b82012-05-10 20:20:25 +00001435HexagonTargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
1436 EVT ValTy = Op.getValueType();
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001437 ConstantPoolSDNode *CPN = cast<ConstantPoolSDNode>(Op);
1438 unsigned Align = CPN->getAlignment();
Rafael Espindola405e25a2016-06-26 22:24:01 +00001439 bool IsPositionIndependent = isPositionIndependent();
1440 unsigned char TF = IsPositionIndependent ? HexagonII::MO_PCREL : 0;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001441
Ron Lieberman822ee882016-08-13 23:41:11 +00001442 unsigned Offset = 0;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001443 SDValue T;
1444 if (CPN->isMachineConstantPoolEntry())
Ron Lieberman822ee882016-08-13 23:41:11 +00001445 T = DAG.getTargetConstantPool(CPN->getMachineCPVal(), ValTy, Align, Offset,
1446 TF);
Sirish Pande69295b82012-05-10 20:20:25 +00001447 else
Ron Lieberman822ee882016-08-13 23:41:11 +00001448 T = DAG.getTargetConstantPool(CPN->getConstVal(), ValTy, Align, Offset,
1449 TF);
1450
1451 assert(cast<ConstantPoolSDNode>(T)->getTargetFlags() == TF &&
1452 "Inconsistent target flag encountered");
1453
Rafael Espindola405e25a2016-06-26 22:24:01 +00001454 if (IsPositionIndependent)
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001455 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), ValTy, T);
1456 return DAG.getNode(HexagonISD::CP, SDLoc(Op), ValTy, T);
1457}
1458
1459SDValue
1460HexagonTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
1461 EVT VT = Op.getValueType();
1462 int Idx = cast<JumpTableSDNode>(Op)->getIndex();
Rafael Espindola405e25a2016-06-26 22:24:01 +00001463 if (isPositionIndependent()) {
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001464 SDValue T = DAG.getTargetJumpTable(Idx, VT, HexagonII::MO_PCREL);
1465 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), VT, T);
1466 }
1467
1468 SDValue T = DAG.getTargetJumpTable(Idx, VT);
1469 return DAG.getNode(HexagonISD::JT, SDLoc(Op), VT, T);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001470}
1471
1472SDValue
1473HexagonTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001474 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001475 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00001476 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001477 MFI.setReturnAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001478
Bill Wendling908bf812014-01-06 00:43:20 +00001479 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001480 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001481
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001482 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001483 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001484 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1485 if (Depth) {
1486 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001487 SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001488 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
1489 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Justin Lebar9c375812016-07-15 18:27:10 +00001490 MachinePointerInfo());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001491 }
1492
1493 // Return LR, which contains the return address. Mark it an implicit live-in.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001494 unsigned Reg = MF.addLiveIn(HRI.getRARegister(), getRegClassFor(MVT::i32));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001495 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
1496}
1497
1498SDValue
1499HexagonTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001500 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Matthias Braun941a7052016-07-28 18:40:00 +00001501 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001502 MFI.setFrameAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001503
1504 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001505 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001506 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1507 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001508 HRI.getFrameRegister(), VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001509 while (Depth--)
1510 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00001511 MachinePointerInfo());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001512 return FrameAddr;
1513}
1514
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001515SDValue
1516HexagonTargetLowering::LowerATOMIC_FENCE(SDValue Op, SelectionDAG& DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001517 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001518 return DAG.getNode(HexagonISD::BARRIER, dl, MVT::Other, Op.getOperand(0));
1519}
1520
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001521SDValue
1522HexagonTargetLowering::LowerGLOBALADDRESS(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001523 SDLoc dl(Op);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001524 auto *GAN = cast<GlobalAddressSDNode>(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00001525 auto PtrVT = getPointerTy(DAG.getDataLayout());
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001526 auto *GV = GAN->getGlobal();
1527 int64_t Offset = GAN->getOffset();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001528
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001529 auto &HLOF = *HTM.getObjFileLowering();
1530 Reloc::Model RM = HTM.getRelocationModel();
1531
1532 if (RM == Reloc::Static) {
1533 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset);
Peter Collingbourne67335642016-10-24 19:23:39 +00001534 const GlobalObject *GO = GV->getBaseObject();
1535 if (GO && HLOF.isGlobalInSmallSection(GO, HTM))
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001536 return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, GA);
1537 return DAG.getNode(HexagonISD::CONST32, dl, PtrVT, GA);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001538 }
1539
Rafael Espindola3beef8d2016-06-27 23:15:57 +00001540 bool UsePCRel = getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001541 if (UsePCRel) {
1542 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset,
1543 HexagonII::MO_PCREL);
1544 return DAG.getNode(HexagonISD::AT_PCREL, dl, PtrVT, GA);
1545 }
1546
1547 // Use GOT index.
1548 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
1549 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, HexagonII::MO_GOT);
1550 SDValue Off = DAG.getConstant(Offset, dl, MVT::i32);
1551 return DAG.getNode(HexagonISD::AT_GOT, dl, PtrVT, GOT, GA, Off);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001552}
1553
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001554// Specifies that for loads and stores VT can be promoted to PromotedLdStVT.
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001555SDValue
1556HexagonTargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
1557 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001558 SDLoc dl(Op);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001559 EVT PtrVT = getPointerTy(DAG.getDataLayout());
1560
1561 Reloc::Model RM = HTM.getRelocationModel();
1562 if (RM == Reloc::Static) {
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001563 SDValue A = DAG.getTargetBlockAddress(BA, PtrVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001564 return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, A);
1565 }
1566
1567 SDValue A = DAG.getTargetBlockAddress(BA, PtrVT, 0, HexagonII::MO_PCREL);
1568 return DAG.getNode(HexagonISD::AT_PCREL, dl, PtrVT, A);
1569}
1570
1571SDValue
1572HexagonTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op, SelectionDAG &DAG)
1573 const {
1574 EVT PtrVT = getPointerTy(DAG.getDataLayout());
1575 SDValue GOTSym = DAG.getTargetExternalSymbol(HEXAGON_GOT_SYM_NAME, PtrVT,
1576 HexagonII::MO_PCREL);
1577 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), PtrVT, GOTSym);
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001578}
1579
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001580SDValue
1581HexagonTargetLowering::GetDynamicTLSAddr(SelectionDAG &DAG, SDValue Chain,
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001582 GlobalAddressSDNode *GA, SDValue Glue, EVT PtrVT, unsigned ReturnReg,
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001583 unsigned char OperandFlags) const {
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001584 MachineFunction &MF = DAG.getMachineFunction();
1585 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001586 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1587 SDLoc dl(GA);
1588 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
1589 GA->getValueType(0),
1590 GA->getOffset(),
1591 OperandFlags);
1592 // Create Operands for the call.The Operands should have the following:
1593 // 1. Chain SDValue
1594 // 2. Callee which in this case is the Global address value.
1595 // 3. Registers live into the call.In this case its R0, as we
1596 // have just one argument to be passed.
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001597 // 4. Glue.
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001598 // Note: The order is important.
1599
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001600 const auto &HRI = *Subtarget.getRegisterInfo();
1601 const uint32_t *Mask = HRI.getCallPreservedMask(MF, CallingConv::C);
1602 assert(Mask && "Missing call preserved mask for calling convention");
1603 SDValue Ops[] = { Chain, TGA, DAG.getRegister(Hexagon::R0, PtrVT),
1604 DAG.getRegisterMask(Mask), Glue };
1605 Chain = DAG.getNode(HexagonISD::CALL, dl, NodeTys, Ops);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001606
1607 // Inform MFI that function has calls.
Matthias Braun941a7052016-07-28 18:40:00 +00001608 MFI.setAdjustsStack(true);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001609
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001610 Glue = Chain.getValue(1);
1611 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Glue);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001612}
1613
1614//
1615// Lower using the intial executable model for TLS addresses
1616//
1617SDValue
1618HexagonTargetLowering::LowerToTLSInitialExecModel(GlobalAddressSDNode *GA,
1619 SelectionDAG &DAG) const {
1620 SDLoc dl(GA);
1621 int64_t Offset = GA->getOffset();
1622 auto PtrVT = getPointerTy(DAG.getDataLayout());
1623
1624 // Get the thread pointer.
1625 SDValue TP = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Hexagon::UGP, PtrVT);
1626
Rafael Espindola405e25a2016-06-26 22:24:01 +00001627 bool IsPositionIndependent = isPositionIndependent();
1628 unsigned char TF =
1629 IsPositionIndependent ? HexagonII::MO_IEGOT : HexagonII::MO_IE;
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001630
1631 // First generate the TLS symbol address
1632 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT,
1633 Offset, TF);
1634
1635 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1636
Rafael Espindola405e25a2016-06-26 22:24:01 +00001637 if (IsPositionIndependent) {
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001638 // Generate the GOT pointer in case of position independent code
1639 SDValue GOT = LowerGLOBAL_OFFSET_TABLE(Sym, DAG);
1640
1641 // Add the TLS Symbol address to GOT pointer.This gives
1642 // GOT relative relocation for the symbol.
1643 Sym = DAG.getNode(ISD::ADD, dl, PtrVT, GOT, Sym);
1644 }
1645
1646 // Load the offset value for TLS symbol.This offset is relative to
1647 // thread pointer.
Justin Lebar9c375812016-07-15 18:27:10 +00001648 SDValue LoadOffset =
1649 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Sym, MachinePointerInfo());
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001650
1651 // Address of the thread local variable is the add of thread
1652 // pointer and the offset of the variable.
1653 return DAG.getNode(ISD::ADD, dl, PtrVT, TP, LoadOffset);
1654}
1655
1656//
1657// Lower using the local executable model for TLS addresses
1658//
1659SDValue
1660HexagonTargetLowering::LowerToTLSLocalExecModel(GlobalAddressSDNode *GA,
1661 SelectionDAG &DAG) const {
1662 SDLoc dl(GA);
1663 int64_t Offset = GA->getOffset();
1664 auto PtrVT = getPointerTy(DAG.getDataLayout());
1665
1666 // Get the thread pointer.
1667 SDValue TP = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Hexagon::UGP, PtrVT);
1668 // Generate the TLS symbol address
1669 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT, Offset,
1670 HexagonII::MO_TPREL);
1671 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1672
1673 // Address of the thread local variable is the add of thread
1674 // pointer and the offset of the variable.
1675 return DAG.getNode(ISD::ADD, dl, PtrVT, TP, Sym);
1676}
1677
1678//
1679// Lower using the general dynamic model for TLS addresses
1680//
1681SDValue
1682HexagonTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
1683 SelectionDAG &DAG) const {
1684 SDLoc dl(GA);
1685 int64_t Offset = GA->getOffset();
1686 auto PtrVT = getPointerTy(DAG.getDataLayout());
1687
1688 // First generate the TLS symbol address
1689 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT, Offset,
1690 HexagonII::MO_GDGOT);
1691
1692 // Then, generate the GOT pointer
1693 SDValue GOT = LowerGLOBAL_OFFSET_TABLE(TGA, DAG);
1694
1695 // Add the TLS symbol and the GOT pointer
1696 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1697 SDValue Chain = DAG.getNode(ISD::ADD, dl, PtrVT, GOT, Sym);
1698
1699 // Copy over the argument to R0
1700 SDValue InFlag;
1701 Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, Hexagon::R0, Chain, InFlag);
1702 InFlag = Chain.getValue(1);
1703
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001704 return GetDynamicTLSAddr(DAG, Chain, GA, InFlag, PtrVT,
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001705 Hexagon::R0, HexagonII::MO_GDPLT);
1706}
1707
1708//
1709// Lower TLS addresses.
1710//
1711// For now for dynamic models, we only support the general dynamic model.
1712//
1713SDValue
1714HexagonTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1715 SelectionDAG &DAG) const {
1716 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1717
1718 switch (HTM.getTLSModel(GA->getGlobal())) {
1719 case TLSModel::GeneralDynamic:
1720 case TLSModel::LocalDynamic:
1721 return LowerToTLSGeneralDynamicModel(GA, DAG);
1722 case TLSModel::InitialExec:
1723 return LowerToTLSInitialExecModel(GA, DAG);
1724 case TLSModel::LocalExec:
1725 return LowerToTLSLocalExecModel(GA, DAG);
1726 }
1727 llvm_unreachable("Bogus TLS model");
1728}
1729
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001730//===----------------------------------------------------------------------===//
1731// TargetLowering Implementation
1732//===----------------------------------------------------------------------===//
1733
Eric Christopherd737b762015-02-02 22:11:36 +00001734HexagonTargetLowering::HexagonTargetLowering(const TargetMachine &TM,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001735 const HexagonSubtarget &ST)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001736 : TargetLowering(TM), HTM(static_cast<const HexagonTargetMachine&>(TM)),
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001737 Subtarget(ST) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001738 bool IsV4 = !Subtarget.hasV5TOps();
1739 auto &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00001740 bool UseHVX = Subtarget.useHVXOps();
1741 bool UseHVXSgl = Subtarget.useHVXSglOps();
1742 bool UseHVXDbl = Subtarget.useHVXDblOps();
Sirish Pande69295b82012-05-10 20:20:25 +00001743
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001744 setPrefLoopAlignment(4);
1745 setPrefFunctionAlignment(4);
1746 setMinFunctionAlignment(2);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001747 setStackPointerRegisterToSaveRestore(HRI.getStackRegister());
1748
Krzysztof Parzyszekf228c952016-06-22 16:07:10 +00001749 setMaxAtomicSizeInBitsSupported(64);
1750 setMinCmpXchgSizeInBits(32);
1751
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001752 if (EnableHexSDNodeSched)
1753 setSchedulingPreference(Sched::VLIW);
1754 else
1755 setSchedulingPreference(Sched::Source);
1756
1757 // Limits for inline expansion of memcpy/memmove
1758 MaxStoresPerMemcpy = MaxStoresPerMemcpyCL;
1759 MaxStoresPerMemcpyOptSize = MaxStoresPerMemcpyOptSizeCL;
1760 MaxStoresPerMemmove = MaxStoresPerMemmoveCL;
1761 MaxStoresPerMemmoveOptSize = MaxStoresPerMemmoveOptSizeCL;
1762 MaxStoresPerMemset = MaxStoresPerMemsetCL;
1763 MaxStoresPerMemsetOptSize = MaxStoresPerMemsetOptSizeCL;
1764
1765 //
1766 // Set up register classes.
1767 //
1768
1769 addRegisterClass(MVT::i1, &Hexagon::PredRegsRegClass);
1770 addRegisterClass(MVT::v2i1, &Hexagon::PredRegsRegClass); // bbbbaaaa
1771 addRegisterClass(MVT::v4i1, &Hexagon::PredRegsRegClass); // ddccbbaa
1772 addRegisterClass(MVT::v8i1, &Hexagon::PredRegsRegClass); // hgfedcba
1773 addRegisterClass(MVT::i32, &Hexagon::IntRegsRegClass);
1774 addRegisterClass(MVT::v4i8, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001775 addRegisterClass(MVT::v2i16, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001776 addRegisterClass(MVT::i64, &Hexagon::DoubleRegsRegClass);
1777 addRegisterClass(MVT::v8i8, &Hexagon::DoubleRegsRegClass);
1778 addRegisterClass(MVT::v4i16, &Hexagon::DoubleRegsRegClass);
1779 addRegisterClass(MVT::v2i32, &Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001780
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001781 if (Subtarget.hasV5TOps()) {
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001782 addRegisterClass(MVT::f32, &Hexagon::IntRegsRegClass);
1783 addRegisterClass(MVT::f64, &Hexagon::DoubleRegsRegClass);
1784 }
Sirish Pande69295b82012-05-10 20:20:25 +00001785
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001786 if (Subtarget.hasV60TOps()) {
1787 if (Subtarget.useHVXSglOps()) {
1788 addRegisterClass(MVT::v64i8, &Hexagon::VectorRegsRegClass);
1789 addRegisterClass(MVT::v32i16, &Hexagon::VectorRegsRegClass);
1790 addRegisterClass(MVT::v16i32, &Hexagon::VectorRegsRegClass);
1791 addRegisterClass(MVT::v8i64, &Hexagon::VectorRegsRegClass);
1792 addRegisterClass(MVT::v128i8, &Hexagon::VecDblRegsRegClass);
1793 addRegisterClass(MVT::v64i16, &Hexagon::VecDblRegsRegClass);
1794 addRegisterClass(MVT::v32i32, &Hexagon::VecDblRegsRegClass);
1795 addRegisterClass(MVT::v16i64, &Hexagon::VecDblRegsRegClass);
1796 addRegisterClass(MVT::v512i1, &Hexagon::VecPredRegsRegClass);
1797 } else if (Subtarget.useHVXDblOps()) {
1798 addRegisterClass(MVT::v128i8, &Hexagon::VectorRegs128BRegClass);
1799 addRegisterClass(MVT::v64i16, &Hexagon::VectorRegs128BRegClass);
1800 addRegisterClass(MVT::v32i32, &Hexagon::VectorRegs128BRegClass);
1801 addRegisterClass(MVT::v16i64, &Hexagon::VectorRegs128BRegClass);
1802 addRegisterClass(MVT::v256i8, &Hexagon::VecDblRegs128BRegClass);
1803 addRegisterClass(MVT::v128i16, &Hexagon::VecDblRegs128BRegClass);
1804 addRegisterClass(MVT::v64i32, &Hexagon::VecDblRegs128BRegClass);
1805 addRegisterClass(MVT::v32i64, &Hexagon::VecDblRegs128BRegClass);
1806 addRegisterClass(MVT::v1024i1, &Hexagon::VecPredRegs128BRegClass);
1807 }
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001808 }
1809
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001810 //
1811 // Handling of scalar operations.
1812 //
1813 // All operations default to "legal", except:
1814 // - indexed loads and stores (pre-/post-incremented),
1815 // - ANY_EXTEND_VECTOR_INREG, ATOMIC_CMP_SWAP_WITH_SUCCESS, CONCAT_VECTORS,
1816 // ConstantFP, DEBUGTRAP, FCEIL, FCOPYSIGN, FEXP, FEXP2, FFLOOR, FGETSIGN,
1817 // FLOG, FLOG2, FLOG10, FMAXNUM, FMINNUM, FNEARBYINT, FRINT, FROUND, TRAP,
1818 // FTRUNC, PREFETCH, SIGN_EXTEND_VECTOR_INREG, ZERO_EXTEND_VECTOR_INREG,
1819 // which default to "expand" for at least one type.
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001820
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001821 // Misc operations.
1822 setOperationAction(ISD::ConstantFP, MVT::f32, Legal); // Default: expand
1823 setOperationAction(ISD::ConstantFP, MVT::f64, Legal); // Default: expand
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001824
1825 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001826 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001827 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001828 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
1829 setOperationAction(ISD::INLINEASM, MVT::Other, Custom);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00001830 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
1831 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001832 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001833 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001834 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001835 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001836
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001837 // Custom legalize GlobalAddress nodes into CONST32.
1838 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001839 setOperationAction(ISD::GlobalAddress, MVT::i8, Custom);
1840 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001841
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001842 // Hexagon needs to optimize cases with negative constants.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001843 setOperationAction(ISD::SETCC, MVT::i8, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001844 setOperationAction(ISD::SETCC, MVT::i16, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001845
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001846 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
1847 setOperationAction(ISD::VASTART, MVT::Other, Custom);
1848 setOperationAction(ISD::VAEND, MVT::Other, Expand);
1849 setOperationAction(ISD::VAARG, MVT::Other, Expand);
1850
1851 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
1852 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
1853 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
1854
1855 if (EmitJumpTables)
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001856 setMinimumJumpTableEntries(MinimumJumpTables);
Krzysztof Parzyszeka61f7da2016-01-13 21:43:13 +00001857 else
Eugene Zelenko58655bb2016-12-17 01:09:05 +00001858 setMinimumJumpTableEntries(std::numeric_limits<int>::max());
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001859 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001860
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001861 // Hexagon has instructions for add/sub with carry. The problem with
1862 // modeling these instructions is that they produce 2 results: Rdd and Px.
1863 // To model the update of Px, we will have to use Defs[p0..p3] which will
1864 // cause any predicate live range to spill. So, we pretend we dont't have
1865 // these instructions.
1866 setOperationAction(ISD::ADDE, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001867 setOperationAction(ISD::ADDE, MVT::i16, Expand);
1868 setOperationAction(ISD::ADDE, MVT::i32, Expand);
1869 setOperationAction(ISD::ADDE, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001870 setOperationAction(ISD::SUBE, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001871 setOperationAction(ISD::SUBE, MVT::i16, Expand);
1872 setOperationAction(ISD::SUBE, MVT::i32, Expand);
1873 setOperationAction(ISD::SUBE, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001874 setOperationAction(ISD::ADDC, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001875 setOperationAction(ISD::ADDC, MVT::i16, Expand);
1876 setOperationAction(ISD::ADDC, MVT::i32, Expand);
1877 setOperationAction(ISD::ADDC, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001878 setOperationAction(ISD::SUBC, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001879 setOperationAction(ISD::SUBC, MVT::i16, Expand);
1880 setOperationAction(ISD::SUBC, MVT::i32, Expand);
1881 setOperationAction(ISD::SUBC, MVT::i64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001882
Krzysztof Parzyszek2c4487d2015-04-13 20:37:01 +00001883 // Only add and sub that detect overflow are the saturating ones.
1884 for (MVT VT : MVT::integer_valuetypes()) {
1885 setOperationAction(ISD::UADDO, VT, Expand);
1886 setOperationAction(ISD::SADDO, VT, Expand);
1887 setOperationAction(ISD::USUBO, VT, Expand);
1888 setOperationAction(ISD::SSUBO, VT, Expand);
1889 }
1890
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001891 setOperationAction(ISD::CTLZ, MVT::i8, Promote);
1892 setOperationAction(ISD::CTLZ, MVT::i16, Promote);
1893 setOperationAction(ISD::CTTZ, MVT::i8, Promote);
1894 setOperationAction(ISD::CTTZ, MVT::i16, Promote);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001895
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001896 // In V5, popcount can count # of 1s in i64 but returns i32.
1897 // On V4 it will be expanded (set later).
1898 setOperationAction(ISD::CTPOP, MVT::i8, Promote);
1899 setOperationAction(ISD::CTPOP, MVT::i16, Promote);
1900 setOperationAction(ISD::CTPOP, MVT::i32, Promote);
1901 setOperationAction(ISD::CTPOP, MVT::i64, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001902
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001903 // We custom lower i64 to i64 mul, so that it is not considered as a legal
1904 // operation. There is a pattern that will match i64 mul and transform it
1905 // to a series of instructions.
1906 setOperationAction(ISD::MUL, MVT::i64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001907
Benjamin Kramer62460692015-04-25 14:46:53 +00001908 for (unsigned IntExpOp :
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001909 { ISD::SDIV, ISD::UDIV, ISD::SREM, ISD::UREM,
1910 ISD::SDIVREM, ISD::UDIVREM, ISD::ROTL, ISD::ROTR,
1911 ISD::BSWAP, ISD::SHL_PARTS, ISD::SRA_PARTS, ISD::SRL_PARTS,
1912 ISD::SMUL_LOHI, ISD::UMUL_LOHI }) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001913 setOperationAction(IntExpOp, MVT::i32, Expand);
1914 setOperationAction(IntExpOp, MVT::i64, Expand);
1915 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001916
Benjamin Kramer62460692015-04-25 14:46:53 +00001917 for (unsigned FPExpOp :
1918 {ISD::FDIV, ISD::FREM, ISD::FSQRT, ISD::FSIN, ISD::FCOS, ISD::FSINCOS,
1919 ISD::FPOW, ISD::FCOPYSIGN}) {
1920 setOperationAction(FPExpOp, MVT::f32, Expand);
1921 setOperationAction(FPExpOp, MVT::f64, Expand);
1922 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001923
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001924 // No extending loads from i32.
1925 for (MVT VT : MVT::integer_valuetypes()) {
1926 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
1927 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
1928 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
1929 }
1930 // Turn FP truncstore into trunc + store.
1931 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Michael Kuperstein2bc3d4d2016-08-18 20:08:15 +00001932 // Turn FP extload into load/fpextend.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001933 for (MVT VT : MVT::fp_valuetypes())
1934 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001935
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001936 // Expand BR_CC and SELECT_CC for all integer and fp types.
1937 for (MVT VT : MVT::integer_valuetypes()) {
1938 setOperationAction(ISD::BR_CC, VT, Expand);
1939 setOperationAction(ISD::SELECT_CC, VT, Expand);
1940 }
1941 for (MVT VT : MVT::fp_valuetypes()) {
1942 setOperationAction(ISD::BR_CC, VT, Expand);
1943 setOperationAction(ISD::SELECT_CC, VT, Expand);
1944 }
1945 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001946
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001947 //
1948 // Handling of vector operations.
1949 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001950
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001951 // Custom lower v4i16 load only. Let v4i16 store to be
1952 // promoted for now.
1953 promoteLdStType(MVT::v4i8, MVT::i32);
1954 promoteLdStType(MVT::v2i16, MVT::i32);
1955 promoteLdStType(MVT::v8i8, MVT::i64);
1956 promoteLdStType(MVT::v2i32, MVT::i64);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001957
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001958 setOperationAction(ISD::LOAD, MVT::v4i16, Custom);
1959 setOperationAction(ISD::STORE, MVT::v4i16, Promote);
1960 AddPromotedToType(ISD::LOAD, MVT::v4i16, MVT::i64);
1961 AddPromotedToType(ISD::STORE, MVT::v4i16, MVT::i64);
1962
1963 // Set the action for vector operations to "expand", then override it with
1964 // either "custom" or "legal" for specific cases.
Craig Topper26260942015-10-18 05:15:34 +00001965 static const unsigned VectExpOps[] = {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001966 // Integer arithmetic:
1967 ISD::ADD, ISD::SUB, ISD::MUL, ISD::SDIV, ISD::UDIV,
1968 ISD::SREM, ISD::UREM, ISD::SDIVREM, ISD::UDIVREM, ISD::ADDC,
1969 ISD::SUBC, ISD::SADDO, ISD::UADDO, ISD::SSUBO, ISD::USUBO,
1970 ISD::SMUL_LOHI, ISD::UMUL_LOHI,
1971 // Logical/bit:
1972 ISD::AND, ISD::OR, ISD::XOR, ISD::ROTL, ISD::ROTR,
Craig Topper33772c52016-04-28 03:34:31 +00001973 ISD::CTPOP, ISD::CTLZ, ISD::CTTZ,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001974 // Floating point arithmetic/math functions:
1975 ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FMA, ISD::FDIV,
1976 ISD::FREM, ISD::FNEG, ISD::FABS, ISD::FSQRT, ISD::FSIN,
1977 ISD::FCOS, ISD::FPOWI, ISD::FPOW, ISD::FLOG, ISD::FLOG2,
1978 ISD::FLOG10, ISD::FEXP, ISD::FEXP2, ISD::FCEIL, ISD::FTRUNC,
1979 ISD::FRINT, ISD::FNEARBYINT, ISD::FROUND, ISD::FFLOOR,
1980 ISD::FMINNUM, ISD::FMAXNUM, ISD::FSINCOS,
1981 // Misc:
Krzysztof Parzyszek046da742016-10-27 14:30:16 +00001982 ISD::BR_CC, ISD::SELECT_CC, ISD::ConstantPool,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001983 // Vector:
1984 ISD::BUILD_VECTOR, ISD::SCALAR_TO_VECTOR,
1985 ISD::EXTRACT_VECTOR_ELT, ISD::INSERT_VECTOR_ELT,
1986 ISD::EXTRACT_SUBVECTOR, ISD::INSERT_SUBVECTOR,
1987 ISD::CONCAT_VECTORS, ISD::VECTOR_SHUFFLE
1988 };
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001989
1990 for (MVT VT : MVT::vector_valuetypes()) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001991 for (unsigned VectExpOp : VectExpOps)
1992 setOperationAction(VectExpOp, VT, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001993
Krzysztof Parzyszeka696b1b2016-09-08 17:42:14 +00001994 // Expand all extending loads and truncating stores:
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001995 for (MVT TargetVT : MVT::vector_valuetypes()) {
Krzysztof Parzyszeka696b1b2016-09-08 17:42:14 +00001996 if (TargetVT == VT)
1997 continue;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001998 setLoadExtAction(ISD::EXTLOAD, TargetVT, VT, Expand);
Krzysztof Parzyszeka696b1b2016-09-08 17:42:14 +00001999 setLoadExtAction(ISD::ZEXTLOAD, TargetVT, VT, Expand);
2000 setLoadExtAction(ISD::SEXTLOAD, TargetVT, VT, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002001 setTruncStoreAction(VT, TargetVT, Expand);
2002 }
2003
Krzysztof Parzyszek046da742016-10-27 14:30:16 +00002004 // Normalize all inputs to SELECT to be vectors of i32.
2005 if (VT.getVectorElementType() != MVT::i32) {
2006 MVT VT32 = MVT::getVectorVT(MVT::i32, VT.getSizeInBits()/32);
2007 setOperationAction(ISD::SELECT, VT, Promote);
2008 AddPromotedToType(ISD::SELECT, VT, VT32);
2009 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002010 setOperationAction(ISD::SRA, VT, Custom);
2011 setOperationAction(ISD::SHL, VT, Custom);
2012 setOperationAction(ISD::SRL, VT, Custom);
2013 }
2014
2015 // Types natively supported:
Benjamin Kramer62460692015-04-25 14:46:53 +00002016 for (MVT NativeVT : {MVT::v2i1, MVT::v4i1, MVT::v8i1, MVT::v32i1, MVT::v64i1,
2017 MVT::v4i8, MVT::v8i8, MVT::v2i16, MVT::v4i16, MVT::v1i32,
2018 MVT::v2i32, MVT::v1i64}) {
2019 setOperationAction(ISD::BUILD_VECTOR, NativeVT, Custom);
2020 setOperationAction(ISD::EXTRACT_VECTOR_ELT, NativeVT, Custom);
2021 setOperationAction(ISD::INSERT_VECTOR_ELT, NativeVT, Custom);
2022 setOperationAction(ISD::EXTRACT_SUBVECTOR, NativeVT, Custom);
2023 setOperationAction(ISD::INSERT_SUBVECTOR, NativeVT, Custom);
2024 setOperationAction(ISD::CONCAT_VECTORS, NativeVT, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002025
Benjamin Kramer62460692015-04-25 14:46:53 +00002026 setOperationAction(ISD::ADD, NativeVT, Legal);
2027 setOperationAction(ISD::SUB, NativeVT, Legal);
2028 setOperationAction(ISD::MUL, NativeVT, Legal);
2029 setOperationAction(ISD::AND, NativeVT, Legal);
2030 setOperationAction(ISD::OR, NativeVT, Legal);
2031 setOperationAction(ISD::XOR, NativeVT, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002032 }
2033
2034 setOperationAction(ISD::SETCC, MVT::v2i16, Custom);
2035 setOperationAction(ISD::VSELECT, MVT::v2i16, Custom);
2036 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
2037 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00002038
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002039 if (UseHVX) {
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002040 if (UseHVXSgl) {
2041 setOperationAction(ISD::CONCAT_VECTORS, MVT::v128i8, Custom);
2042 setOperationAction(ISD::CONCAT_VECTORS, MVT::v64i16, Custom);
2043 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i32, Custom);
2044 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i64, Custom);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002045 // We try to generate the vpack{e/o} instructions. If we fail
2046 // we fall back upon ExpandOp.
2047 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v64i8, Custom);
2048 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v32i16, Custom);
2049 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v64i8, Custom);
2050 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v32i16, Custom);
2051 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v16i32, Custom);
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002052 } else if (UseHVXDbl) {
2053 setOperationAction(ISD::CONCAT_VECTORS, MVT::v256i8, Custom);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002054 setOperationAction(ISD::CONCAT_VECTORS, MVT::v128i16, Custom);
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002055 setOperationAction(ISD::CONCAT_VECTORS, MVT::v64i32, Custom);
2056 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i64, Custom);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002057 // We try to generate the vpack{e/o} instructions. If we fail
2058 // we fall back upon ExpandOp.
2059 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v128i8, Custom);
2060 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v64i16, Custom);
2061 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v4i32, Custom);
2062 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v128i8, Custom);
2063 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v64i16, Custom);
2064 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v32i32, Custom);
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002065 } else {
2066 llvm_unreachable("Unrecognized HVX mode");
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002067 }
2068 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002069 // Subtarget-specific operation actions.
2070 //
2071 if (Subtarget.hasV5TOps()) {
2072 setOperationAction(ISD::FMA, MVT::f64, Expand);
2073 setOperationAction(ISD::FADD, MVT::f64, Expand);
2074 setOperationAction(ISD::FSUB, MVT::f64, Expand);
2075 setOperationAction(ISD::FMUL, MVT::f64, Expand);
2076
Krzysztof Parzyszekbd8ef4b2016-08-19 13:34:31 +00002077 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
2078 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
2079
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002080 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Promote);
2081 setOperationAction(ISD::FP_TO_UINT, MVT::i8, Promote);
2082 setOperationAction(ISD::FP_TO_UINT, MVT::i16, Promote);
2083 setOperationAction(ISD::FP_TO_SINT, MVT::i1, Promote);
2084 setOperationAction(ISD::FP_TO_SINT, MVT::i8, Promote);
2085 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
2086 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote);
2087 setOperationAction(ISD::UINT_TO_FP, MVT::i8, Promote);
2088 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote);
2089 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote);
2090 setOperationAction(ISD::SINT_TO_FP, MVT::i8, Promote);
2091 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002092 } else { // V4
2093 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
2094 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Expand);
2095 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
2096 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
2097 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Expand);
2098 setOperationAction(ISD::FP_TO_SINT, MVT::f32, Expand);
2099 setOperationAction(ISD::FP_EXTEND, MVT::f32, Expand);
2100 setOperationAction(ISD::FP_ROUND, MVT::f64, Expand);
2101 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
2102
2103 setOperationAction(ISD::CTPOP, MVT::i8, Expand);
2104 setOperationAction(ISD::CTPOP, MVT::i16, Expand);
2105 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
2106 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
2107
2108 // Expand these operations for both f32 and f64:
Benjamin Kramer62460692015-04-25 14:46:53 +00002109 for (unsigned FPExpOpV4 :
2110 {ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FABS, ISD::FNEG, ISD::FMA}) {
2111 setOperationAction(FPExpOpV4, MVT::f32, Expand);
2112 setOperationAction(FPExpOpV4, MVT::f64, Expand);
2113 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002114
Benjamin Kramer62460692015-04-25 14:46:53 +00002115 for (ISD::CondCode FPExpCCV4 :
2116 {ISD::SETOEQ, ISD::SETOGT, ISD::SETOLT, ISD::SETOGE, ISD::SETOLE,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002117 ISD::SETUO, ISD::SETO}) {
Benjamin Kramer62460692015-04-25 14:46:53 +00002118 setCondCodeAction(FPExpCCV4, MVT::f32, Expand);
2119 setCondCodeAction(FPExpCCV4, MVT::f64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002120 }
2121 }
2122
2123 // Handling of indexed loads/stores: default is "expand".
2124 //
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +00002125 for (MVT VT : {MVT::i8, MVT::i16, MVT::i32, MVT::i64}) {
2126 setIndexedLoadAction(ISD::POST_INC, VT, Legal);
2127 setIndexedStoreAction(ISD::POST_INC, VT, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002128 }
2129
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +00002130 if (UseHVXSgl) {
2131 for (MVT VT : {MVT::v64i8, MVT::v32i16, MVT::v16i32, MVT::v8i64,
2132 MVT::v128i8, MVT::v64i16, MVT::v32i32, MVT::v16i64}) {
2133 setIndexedLoadAction(ISD::POST_INC, VT, Legal);
2134 setIndexedStoreAction(ISD::POST_INC, VT, Legal);
2135 }
2136 } else if (UseHVXDbl) {
2137 for (MVT VT : {MVT::v128i8, MVT::v64i16, MVT::v32i32, MVT::v16i64,
2138 MVT::v256i8, MVT::v128i16, MVT::v64i32, MVT::v32i64}) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002139 setIndexedLoadAction(ISD::POST_INC, VT, Legal);
2140 setIndexedStoreAction(ISD::POST_INC, VT, Legal);
2141 }
2142 }
2143
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002144 computeRegisterProperties(&HRI);
2145
2146 //
2147 // Library calls for unsupported operations
2148 //
2149 bool FastMath = EnableFastMath;
2150
Benjamin Kramera37c8092015-04-25 14:46:46 +00002151 setLibcallName(RTLIB::SDIV_I32, "__hexagon_divsi3");
2152 setLibcallName(RTLIB::SDIV_I64, "__hexagon_divdi3");
2153 setLibcallName(RTLIB::UDIV_I32, "__hexagon_udivsi3");
2154 setLibcallName(RTLIB::UDIV_I64, "__hexagon_udivdi3");
2155 setLibcallName(RTLIB::SREM_I32, "__hexagon_modsi3");
2156 setLibcallName(RTLIB::SREM_I64, "__hexagon_moddi3");
2157 setLibcallName(RTLIB::UREM_I32, "__hexagon_umodsi3");
2158 setLibcallName(RTLIB::UREM_I64, "__hexagon_umoddi3");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002159
Benjamin Kramera37c8092015-04-25 14:46:46 +00002160 setLibcallName(RTLIB::SINTTOFP_I128_F64, "__hexagon_floattidf");
2161 setLibcallName(RTLIB::SINTTOFP_I128_F32, "__hexagon_floattisf");
2162 setLibcallName(RTLIB::FPTOUINT_F32_I128, "__hexagon_fixunssfti");
2163 setLibcallName(RTLIB::FPTOUINT_F64_I128, "__hexagon_fixunsdfti");
2164 setLibcallName(RTLIB::FPTOSINT_F32_I128, "__hexagon_fixsfti");
2165 setLibcallName(RTLIB::FPTOSINT_F64_I128, "__hexagon_fixdfti");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002166
2167 if (IsV4) {
2168 // Handle single-precision floating point operations on V4.
Benjamin Kramera37c8092015-04-25 14:46:46 +00002169 if (FastMath) {
2170 setLibcallName(RTLIB::ADD_F32, "__hexagon_fast_addsf3");
2171 setLibcallName(RTLIB::SUB_F32, "__hexagon_fast_subsf3");
2172 setLibcallName(RTLIB::MUL_F32, "__hexagon_fast_mulsf3");
2173 setLibcallName(RTLIB::OGT_F32, "__hexagon_fast_gtsf2");
2174 setLibcallName(RTLIB::OLT_F32, "__hexagon_fast_ltsf2");
2175 // Double-precision compares.
2176 setLibcallName(RTLIB::OGT_F64, "__hexagon_fast_gtdf2");
2177 setLibcallName(RTLIB::OLT_F64, "__hexagon_fast_ltdf2");
2178 } else {
2179 setLibcallName(RTLIB::ADD_F32, "__hexagon_addsf3");
2180 setLibcallName(RTLIB::SUB_F32, "__hexagon_subsf3");
2181 setLibcallName(RTLIB::MUL_F32, "__hexagon_mulsf3");
2182 setLibcallName(RTLIB::OGT_F32, "__hexagon_gtsf2");
2183 setLibcallName(RTLIB::OLT_F32, "__hexagon_ltsf2");
2184 // Double-precision compares.
2185 setLibcallName(RTLIB::OGT_F64, "__hexagon_gtdf2");
2186 setLibcallName(RTLIB::OLT_F64, "__hexagon_ltdf2");
2187 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002188 }
2189
2190 // This is the only fast library function for sqrtd.
2191 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00002192 setLibcallName(RTLIB::SQRT_F64, "__hexagon_fast2_sqrtdf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002193
Benjamin Kramera37c8092015-04-25 14:46:46 +00002194 // Prefix is: nothing for "slow-math",
2195 // "fast2_" for V4 fast-math and V5+ fast-math double-precision
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002196 // (actually, keep fast-math and fast-math2 separate for now)
Benjamin Kramera37c8092015-04-25 14:46:46 +00002197 if (FastMath) {
2198 setLibcallName(RTLIB::ADD_F64, "__hexagon_fast_adddf3");
2199 setLibcallName(RTLIB::SUB_F64, "__hexagon_fast_subdf3");
2200 setLibcallName(RTLIB::MUL_F64, "__hexagon_fast_muldf3");
2201 setLibcallName(RTLIB::DIV_F64, "__hexagon_fast_divdf3");
2202 // Calling __hexagon_fast2_divsf3 with fast-math on V5 (ok).
2203 setLibcallName(RTLIB::DIV_F32, "__hexagon_fast_divsf3");
2204 } else {
2205 setLibcallName(RTLIB::ADD_F64, "__hexagon_adddf3");
2206 setLibcallName(RTLIB::SUB_F64, "__hexagon_subdf3");
2207 setLibcallName(RTLIB::MUL_F64, "__hexagon_muldf3");
2208 setLibcallName(RTLIB::DIV_F64, "__hexagon_divdf3");
2209 setLibcallName(RTLIB::DIV_F32, "__hexagon_divsf3");
2210 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002211
2212 if (Subtarget.hasV5TOps()) {
2213 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00002214 setLibcallName(RTLIB::SQRT_F32, "__hexagon_fast2_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002215 else
Benjamin Kramera37c8092015-04-25 14:46:46 +00002216 setLibcallName(RTLIB::SQRT_F32, "__hexagon_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002217 } else {
2218 // V4
Benjamin Kramera37c8092015-04-25 14:46:46 +00002219 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__hexagon_floatsisf");
2220 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__hexagon_floatsidf");
2221 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__hexagon_floatdisf");
2222 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__hexagon_floatdidf");
2223 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__hexagon_floatunsisf");
2224 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__hexagon_floatunsidf");
2225 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__hexagon_floatundisf");
2226 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__hexagon_floatundidf");
2227 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__hexagon_fixunssfsi");
2228 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__hexagon_fixunssfdi");
2229 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__hexagon_fixunsdfsi");
2230 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__hexagon_fixunsdfdi");
2231 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__hexagon_fixsfsi");
2232 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__hexagon_fixsfdi");
2233 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__hexagon_fixdfsi");
2234 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__hexagon_fixdfdi");
2235 setLibcallName(RTLIB::FPEXT_F32_F64, "__hexagon_extendsfdf2");
2236 setLibcallName(RTLIB::FPROUND_F64_F32, "__hexagon_truncdfsf2");
2237 setLibcallName(RTLIB::OEQ_F32, "__hexagon_eqsf2");
2238 setLibcallName(RTLIB::OEQ_F64, "__hexagon_eqdf2");
2239 setLibcallName(RTLIB::OGE_F32, "__hexagon_gesf2");
2240 setLibcallName(RTLIB::OGE_F64, "__hexagon_gedf2");
2241 setLibcallName(RTLIB::OLE_F32, "__hexagon_lesf2");
2242 setLibcallName(RTLIB::OLE_F64, "__hexagon_ledf2");
2243 setLibcallName(RTLIB::UNE_F32, "__hexagon_nesf2");
2244 setLibcallName(RTLIB::UNE_F64, "__hexagon_nedf2");
2245 setLibcallName(RTLIB::UO_F32, "__hexagon_unordsf2");
2246 setLibcallName(RTLIB::UO_F64, "__hexagon_unorddf2");
2247 setLibcallName(RTLIB::O_F32, "__hexagon_unordsf2");
2248 setLibcallName(RTLIB::O_F64, "__hexagon_unorddf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002249 }
2250
2251 // These cause problems when the shift amount is non-constant.
2252 setLibcallName(RTLIB::SHL_I128, nullptr);
2253 setLibcallName(RTLIB::SRL_I128, nullptr);
2254 setLibcallName(RTLIB::SRA_I128, nullptr);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002255}
2256
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002257const char* HexagonTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00002258 switch ((HexagonISD::NodeType)Opcode) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002259 case HexagonISD::ALLOCA: return "HexagonISD::ALLOCA";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002260 case HexagonISD::AT_GOT: return "HexagonISD::AT_GOT";
2261 case HexagonISD::AT_PCREL: return "HexagonISD::AT_PCREL";
2262 case HexagonISD::BARRIER: return "HexagonISD::BARRIER";
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002263 case HexagonISD::CALL: return "HexagonISD::CALL";
2264 case HexagonISD::CALLnr: return "HexagonISD::CALLnr";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002265 case HexagonISD::CALLR: return "HexagonISD::CALLR";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002266 case HexagonISD::COMBINE: return "HexagonISD::COMBINE";
2267 case HexagonISD::CONST32_GP: return "HexagonISD::CONST32_GP";
2268 case HexagonISD::CONST32: return "HexagonISD::CONST32";
2269 case HexagonISD::CP: return "HexagonISD::CP";
2270 case HexagonISD::DCFETCH: return "HexagonISD::DCFETCH";
2271 case HexagonISD::EH_RETURN: return "HexagonISD::EH_RETURN";
2272 case HexagonISD::EXTRACTU: return "HexagonISD::EXTRACTU";
2273 case HexagonISD::EXTRACTURP: return "HexagonISD::EXTRACTURP";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002274 case HexagonISD::INSERT: return "HexagonISD::INSERT";
2275 case HexagonISD::INSERTRP: return "HexagonISD::INSERTRP";
2276 case HexagonISD::JT: return "HexagonISD::JT";
2277 case HexagonISD::PACKHL: return "HexagonISD::PACKHL";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002278 case HexagonISD::POPCOUNT: return "HexagonISD::POPCOUNT";
2279 case HexagonISD::RET_FLAG: return "HexagonISD::RET_FLAG";
2280 case HexagonISD::SHUFFEB: return "HexagonISD::SHUFFEB";
2281 case HexagonISD::SHUFFEH: return "HexagonISD::SHUFFEH";
2282 case HexagonISD::SHUFFOB: return "HexagonISD::SHUFFOB";
2283 case HexagonISD::SHUFFOH: return "HexagonISD::SHUFFOH";
2284 case HexagonISD::TC_RETURN: return "HexagonISD::TC_RETURN";
2285 case HexagonISD::VCMPBEQ: return "HexagonISD::VCMPBEQ";
2286 case HexagonISD::VCMPBGT: return "HexagonISD::VCMPBGT";
2287 case HexagonISD::VCMPBGTU: return "HexagonISD::VCMPBGTU";
2288 case HexagonISD::VCMPHEQ: return "HexagonISD::VCMPHEQ";
2289 case HexagonISD::VCMPHGT: return "HexagonISD::VCMPHGT";
2290 case HexagonISD::VCMPHGTU: return "HexagonISD::VCMPHGTU";
2291 case HexagonISD::VCMPWEQ: return "HexagonISD::VCMPWEQ";
2292 case HexagonISD::VCMPWGT: return "HexagonISD::VCMPWGT";
2293 case HexagonISD::VCMPWGTU: return "HexagonISD::VCMPWGTU";
Krzysztof Parzyszekc168c012015-12-03 16:47:20 +00002294 case HexagonISD::VCOMBINE: return "HexagonISD::VCOMBINE";
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002295 case HexagonISD::VPACK: return "HexagonISD::VPACK";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002296 case HexagonISD::VSHLH: return "HexagonISD::VSHLH";
2297 case HexagonISD::VSHLW: return "HexagonISD::VSHLW";
2298 case HexagonISD::VSPLATB: return "HexagonISD::VSPLTB";
2299 case HexagonISD::VSPLATH: return "HexagonISD::VSPLATH";
2300 case HexagonISD::VSRAH: return "HexagonISD::VSRAH";
2301 case HexagonISD::VSRAW: return "HexagonISD::VSRAW";
2302 case HexagonISD::VSRLH: return "HexagonISD::VSRLH";
2303 case HexagonISD::VSRLW: return "HexagonISD::VSRLW";
2304 case HexagonISD::VSXTBH: return "HexagonISD::VSXTBH";
2305 case HexagonISD::VSXTBW: return "HexagonISD::VSXTBW";
Matthias Braund04893f2015-05-07 21:33:59 +00002306 case HexagonISD::OP_END: break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002307 }
Matthias Braund04893f2015-05-07 21:33:59 +00002308 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002309}
2310
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002311bool HexagonTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002312 EVT MTy1 = EVT::getEVT(Ty1);
2313 EVT MTy2 = EVT::getEVT(Ty2);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002314 if (!MTy1.isSimple() || !MTy2.isSimple())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002315 return false;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002316 return (MTy1.getSimpleVT() == MVT::i64) && (MTy2.getSimpleVT() == MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002317}
2318
2319bool HexagonTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002320 if (!VT1.isSimple() || !VT2.isSimple())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002321 return false;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002322 return (VT1.getSimpleVT() == MVT::i64) && (VT2.getSimpleVT() == MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002323}
2324
Krzysztof Parzyszekbd8ef4b2016-08-19 13:34:31 +00002325bool HexagonTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
2326 return isOperationLegalOrCustom(ISD::FMA, VT);
2327}
2328
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002329// Should we expand the build vector with shuffles?
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00002330bool HexagonTargetLowering::shouldExpandBuildVectorWithShuffles(EVT VT,
2331 unsigned DefinedValues) const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002332 // Hexagon vector shuffle operates on element sizes of bytes or halfwords
2333 EVT EltVT = VT.getVectorElementType();
2334 int EltBits = EltVT.getSizeInBits();
2335 if ((EltBits != 8) && (EltBits != 16))
2336 return false;
2337
2338 return TargetLowering::shouldExpandBuildVectorWithShuffles(VT, DefinedValues);
2339}
2340
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00002341static StridedLoadKind isStridedLoad(const ArrayRef<int> &Mask) {
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002342 int even_start = -2;
2343 int odd_start = -1;
2344 size_t mask_len = Mask.size();
2345 for (auto idx : Mask) {
2346 if ((idx - even_start) == 2)
2347 even_start = idx;
2348 else
2349 break;
2350 }
2351 if (even_start == (int)(mask_len * 2) - 2)
2352 return StridedLoadKind::Even;
2353 for (auto idx : Mask) {
2354 if ((idx - odd_start) == 2)
2355 odd_start = idx;
2356 else
2357 break;
2358 }
2359 if (odd_start == (int)(mask_len * 2) - 1)
2360 return StridedLoadKind::Odd;
2361
2362 return StridedLoadKind::NoPattern;
2363}
2364
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00002365bool HexagonTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &Mask,
2366 EVT VT) const {
2367 if (Subtarget.useHVXOps())
2368 return isStridedLoad(Mask) != StridedLoadKind::NoPattern;
2369 return true;
2370}
2371
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002372// Lower a vector shuffle (V1, V2, V3). V1 and V2 are the two vectors
2373// to select data from, V3 is the permutation.
2374SDValue
2375HexagonTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG)
2376 const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002377 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
2378 SDValue V1 = Op.getOperand(0);
2379 SDValue V2 = Op.getOperand(1);
2380 SDLoc dl(Op);
2381 EVT VT = Op.getValueType();
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002382 bool UseHVX = Subtarget.useHVXOps();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002383
Sanjay Patel57195842016-03-14 17:28:46 +00002384 if (V2.isUndef())
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002385 V2 = V1;
2386
2387 if (SVN->isSplat()) {
2388 int Lane = SVN->getSplatIndex();
2389 if (Lane == -1) Lane = 0;
2390
2391 // Test if V1 is a SCALAR_TO_VECTOR.
2392 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR)
2393 return createSplat(DAG, dl, VT, V1.getOperand(0));
2394
2395 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
2396 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
2397 // reaches it).
2398 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
2399 !isa<ConstantSDNode>(V1.getOperand(0))) {
2400 bool IsScalarToVector = true;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00002401 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i) {
Sanjay Patel75068522016-03-14 18:09:43 +00002402 if (!V1.getOperand(i).isUndef()) {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002403 IsScalarToVector = false;
2404 break;
2405 }
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00002406 }
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002407 if (IsScalarToVector)
2408 return createSplat(DAG, dl, VT, V1.getOperand(0));
2409 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002410 return createSplat(DAG, dl, VT, DAG.getConstant(Lane, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002411 }
2412
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002413 if (UseHVX) {
2414 ArrayRef<int> Mask = SVN->getMask();
2415 size_t MaskLen = Mask.size();
Sanjay Patel1ed771f2016-09-14 16:37:15 +00002416 int ElemSizeInBits = VT.getScalarSizeInBits();
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002417 if ((Subtarget.useHVXSglOps() && (ElemSizeInBits * MaskLen) == 64 * 8) ||
2418 (Subtarget.useHVXDblOps() && (ElemSizeInBits * MaskLen) == 128 * 8)) {
2419 // Return 1 for odd and 2 of even
2420 StridedLoadKind Pattern = isStridedLoad(Mask);
2421
2422 if (Pattern == StridedLoadKind::NoPattern)
2423 return SDValue();
2424
2425 SDValue Vec0 = Op.getOperand(0);
2426 SDValue Vec1 = Op.getOperand(1);
2427 SDValue StridePattern = DAG.getConstant(Pattern, dl, MVT::i32);
2428 SDValue Ops[] = { Vec1, Vec0, StridePattern };
2429 return DAG.getNode(HexagonISD::VPACK, dl, VT, Ops);
2430 }
2431 // We used to assert in the "else" part here, but that is bad for Halide
2432 // Halide creates intermediate double registers by interleaving two
2433 // concatenated vector registers. The interleaving requires vector_shuffle
2434 // nodes and we shouldn't barf on a double register result of a
2435 // vector_shuffle because it is most likely an intermediate result.
2436 }
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002437 // FIXME: We need to support more general vector shuffles. See
2438 // below the comment from the ARM backend that deals in the general
2439 // case with the vector shuffles. For now, let expand handle these.
2440 return SDValue();
2441
2442 // If the shuffle is not directly supported and it has 4 elements, use
2443 // the PerfectShuffle-generated table to synthesize it from other shuffles.
2444}
2445
2446// If BUILD_VECTOR has same base element repeated several times,
2447// report true.
2448static bool isCommonSplatElement(BuildVectorSDNode *BVN) {
2449 unsigned NElts = BVN->getNumOperands();
2450 SDValue V0 = BVN->getOperand(0);
2451
2452 for (unsigned i = 1, e = NElts; i != e; ++i) {
2453 if (BVN->getOperand(i) != V0)
2454 return false;
2455 }
2456 return true;
2457}
2458
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002459// Lower a vector shift. Try to convert
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002460// <VT> = SHL/SRA/SRL <VT> by <VT> to Hexagon specific
2461// <VT> = SHL/SRA/SRL <VT> by <IT/i32>.
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002462SDValue
2463HexagonTargetLowering::LowerVECTOR_SHIFT(SDValue Op, SelectionDAG &DAG) const {
Eugene Zelenko58655bb2016-12-17 01:09:05 +00002464 BuildVectorSDNode *BVN = nullptr;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002465 SDValue V1 = Op.getOperand(0);
2466 SDValue V2 = Op.getOperand(1);
2467 SDValue V3;
2468 SDLoc dl(Op);
2469 EVT VT = Op.getValueType();
2470
2471 if ((BVN = dyn_cast<BuildVectorSDNode>(V1.getNode())) &&
2472 isCommonSplatElement(BVN))
2473 V3 = V2;
2474 else if ((BVN = dyn_cast<BuildVectorSDNode>(V2.getNode())) &&
2475 isCommonSplatElement(BVN))
2476 V3 = V1;
2477 else
2478 return SDValue();
2479
2480 SDValue CommonSplat = BVN->getOperand(0);
2481 SDValue Result;
2482
2483 if (VT.getSimpleVT() == MVT::v4i16) {
2484 switch (Op.getOpcode()) {
2485 case ISD::SRA:
2486 Result = DAG.getNode(HexagonISD::VSRAH, dl, VT, V3, CommonSplat);
2487 break;
2488 case ISD::SHL:
2489 Result = DAG.getNode(HexagonISD::VSHLH, dl, VT, V3, CommonSplat);
2490 break;
2491 case ISD::SRL:
2492 Result = DAG.getNode(HexagonISD::VSRLH, dl, VT, V3, CommonSplat);
2493 break;
2494 default:
2495 return SDValue();
2496 }
2497 } else if (VT.getSimpleVT() == MVT::v2i32) {
2498 switch (Op.getOpcode()) {
2499 case ISD::SRA:
2500 Result = DAG.getNode(HexagonISD::VSRAW, dl, VT, V3, CommonSplat);
2501 break;
2502 case ISD::SHL:
2503 Result = DAG.getNode(HexagonISD::VSHLW, dl, VT, V3, CommonSplat);
2504 break;
2505 case ISD::SRL:
2506 Result = DAG.getNode(HexagonISD::VSRLW, dl, VT, V3, CommonSplat);
2507 break;
2508 default:
2509 return SDValue();
2510 }
2511 } else {
2512 return SDValue();
2513 }
2514
2515 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
2516}
2517
2518SDValue
2519HexagonTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
2520 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
2521 SDLoc dl(Op);
2522 EVT VT = Op.getValueType();
2523
2524 unsigned Size = VT.getSizeInBits();
2525
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002526 // Only handle vectors of 64 bits or shorter.
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002527 if (Size > 64)
2528 return SDValue();
2529
2530 APInt APSplatBits, APSplatUndef;
2531 unsigned SplatBitSize;
2532 bool HasAnyUndefs;
2533 unsigned NElts = BVN->getNumOperands();
2534
2535 // Try to generate a SPLAT instruction.
2536 if ((VT.getSimpleVT() == MVT::v4i8 || VT.getSimpleVT() == MVT::v4i16) &&
2537 (BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
2538 HasAnyUndefs, 0, true) && SplatBitSize <= 16)) {
2539 unsigned SplatBits = APSplatBits.getZExtValue();
2540 int32_t SextVal = ((int32_t) (SplatBits << (32 - SplatBitSize)) >>
2541 (32 - SplatBitSize));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002542 return createSplat(DAG, dl, VT, DAG.getConstant(SextVal, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002543 }
2544
2545 // Try to generate COMBINE to build v2i32 vectors.
2546 if (VT.getSimpleVT() == MVT::v2i32) {
2547 SDValue V0 = BVN->getOperand(0);
2548 SDValue V1 = BVN->getOperand(1);
2549
Sanjay Patel57195842016-03-14 17:28:46 +00002550 if (V0.isUndef())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002551 V0 = DAG.getConstant(0, dl, MVT::i32);
Sanjay Patel57195842016-03-14 17:28:46 +00002552 if (V1.isUndef())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002553 V1 = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002554
2555 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(V0);
2556 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(V1);
2557 // If the element isn't a constant, it is in a register:
2558 // generate a COMBINE Register Register instruction.
2559 if (!C0 || !C1)
2560 return DAG.getNode(HexagonISD::COMBINE, dl, VT, V1, V0);
2561
2562 // If one of the operands is an 8 bit integer constant, generate
2563 // a COMBINE Immediate Immediate instruction.
2564 if (isInt<8>(C0->getSExtValue()) ||
2565 isInt<8>(C1->getSExtValue()))
2566 return DAG.getNode(HexagonISD::COMBINE, dl, VT, V1, V0);
2567 }
2568
2569 // Try to generate a S2_packhl to build v2i16 vectors.
2570 if (VT.getSimpleVT() == MVT::v2i16) {
2571 for (unsigned i = 0, e = NElts; i != e; ++i) {
Sanjay Patel57195842016-03-14 17:28:46 +00002572 if (BVN->getOperand(i).isUndef())
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002573 continue;
2574 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(BVN->getOperand(i));
2575 // If the element isn't a constant, it is in a register:
2576 // generate a S2_packhl instruction.
2577 if (!Cst) {
2578 SDValue pack = DAG.getNode(HexagonISD::PACKHL, dl, MVT::v4i16,
2579 BVN->getOperand(1), BVN->getOperand(0));
2580
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00002581 return DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::v2i16,
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002582 pack);
2583 }
2584 }
2585 }
2586
2587 // In the general case, generate a CONST32 or a CONST64 for constant vectors,
2588 // and insert_vector_elt for all the other cases.
2589 uint64_t Res = 0;
2590 unsigned EltSize = Size / NElts;
2591 SDValue ConstVal;
2592 uint64_t Mask = ~uint64_t(0ULL) >> (64 - EltSize);
2593 bool HasNonConstantElements = false;
2594
2595 for (unsigned i = 0, e = NElts; i != e; ++i) {
2596 // LLVM's BUILD_VECTOR operands are in Little Endian mode, whereas Hexagon's
2597 // combine, const64, etc. are Big Endian.
2598 unsigned OpIdx = NElts - i - 1;
2599 SDValue Operand = BVN->getOperand(OpIdx);
Sanjay Patel57195842016-03-14 17:28:46 +00002600 if (Operand.isUndef())
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002601 continue;
2602
2603 int64_t Val = 0;
2604 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Operand))
2605 Val = Cst->getSExtValue();
2606 else
2607 HasNonConstantElements = true;
2608
2609 Val &= Mask;
2610 Res = (Res << EltSize) | Val;
2611 }
2612
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002613 if (Size > 64)
2614 return SDValue();
2615
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002616 if (Size == 64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002617 ConstVal = DAG.getConstant(Res, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002618 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002619 ConstVal = DAG.getConstant(Res, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002620
2621 // When there are non constant operands, add them with INSERT_VECTOR_ELT to
2622 // ConstVal, the constant part of the vector.
2623 if (HasNonConstantElements) {
2624 EVT EltVT = VT.getVectorElementType();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002625 SDValue Width = DAG.getConstant(EltVT.getSizeInBits(), dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002626 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002627 DAG.getConstant(32, dl, MVT::i64));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002628
2629 for (unsigned i = 0, e = NElts; i != e; ++i) {
2630 // LLVM's BUILD_VECTOR operands are in Little Endian mode, whereas Hexagon
2631 // is Big Endian.
2632 unsigned OpIdx = NElts - i - 1;
2633 SDValue Operand = BVN->getOperand(OpIdx);
Benjamin Kramer619c4e52015-04-10 11:24:51 +00002634 if (isa<ConstantSDNode>(Operand))
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002635 // This operand is already in ConstVal.
2636 continue;
2637
2638 if (VT.getSizeInBits() == 64 &&
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00002639 Operand.getValueSizeInBits() == 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002640 SDValue C = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002641 Operand = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, Operand);
2642 }
2643
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002644 SDValue Idx = DAG.getConstant(OpIdx, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002645 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i64, Idx, Width);
2646 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2647 const SDValue Ops[] = {ConstVal, Operand, Combined};
2648
2649 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002650 ConstVal = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002651 else
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002652 ConstVal = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002653 }
2654 }
2655
2656 return DAG.getNode(ISD::BITCAST, dl, VT, ConstVal);
2657}
2658
2659SDValue
2660HexagonTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
2661 SelectionDAG &DAG) const {
2662 SDLoc dl(Op);
Krzysztof Parzyszekc168c012015-12-03 16:47:20 +00002663 bool UseHVX = Subtarget.useHVXOps();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002664 EVT VT = Op.getValueType();
2665 unsigned NElts = Op.getNumOperands();
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002666 SDValue Vec0 = Op.getOperand(0);
2667 EVT VecVT = Vec0.getValueType();
2668 unsigned Width = VecVT.getSizeInBits();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002669
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002670 if (NElts == 2) {
2671 MVT ST = VecVT.getSimpleVT();
2672 // We are trying to concat two v2i16 to a single v4i16, or two v4i8
2673 // into a single v8i8.
2674 if (ST == MVT::v2i16 || ST == MVT::v4i8)
2675 return DAG.getNode(HexagonISD::COMBINE, dl, VT, Op.getOperand(1), Vec0);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002676
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002677 if (UseHVX) {
2678 assert((Width == 64*8 && Subtarget.useHVXSglOps()) ||
2679 (Width == 128*8 && Subtarget.useHVXDblOps()));
2680 SDValue Vec1 = Op.getOperand(1);
2681 MVT OpTy = Subtarget.useHVXSglOps() ? MVT::v16i32 : MVT::v32i32;
2682 MVT ReTy = Subtarget.useHVXSglOps() ? MVT::v32i32 : MVT::v64i32;
2683 SDValue B0 = DAG.getNode(ISD::BITCAST, dl, OpTy, Vec0);
2684 SDValue B1 = DAG.getNode(ISD::BITCAST, dl, OpTy, Vec1);
2685 SDValue VC = DAG.getNode(HexagonISD::VCOMBINE, dl, ReTy, B1, B0);
2686 return DAG.getNode(ISD::BITCAST, dl, VT, VC);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002687 }
2688 }
2689
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002690 if (VT.getSizeInBits() != 32 && VT.getSizeInBits() != 64)
2691 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002692
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002693 SDValue C0 = DAG.getConstant(0, dl, MVT::i64);
2694 SDValue C32 = DAG.getConstant(32, dl, MVT::i64);
2695 SDValue W = DAG.getConstant(Width, dl, MVT::i64);
2696 // Create the "width" part of the argument to insert_rp/insertp_rp.
2697 SDValue S = DAG.getNode(ISD::SHL, dl, MVT::i64, W, C32);
2698 SDValue V = C0;
2699
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002700 for (unsigned i = 0, e = NElts; i != e; ++i) {
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002701 unsigned N = NElts-i-1;
2702 SDValue OpN = Op.getOperand(N);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002703
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00002704 if (VT.getSizeInBits() == 64 && OpN.getValueSizeInBits() == 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002705 SDValue C = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002706 OpN = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, OpN);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002707 }
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002708 SDValue Idx = DAG.getConstant(N, dl, MVT::i64);
2709 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i64, Idx, W);
2710 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, S, Offset);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002711 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002712 V = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, {V, OpN, Or});
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002713 else if (VT.getSizeInBits() == 64)
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002714 V = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, {V, OpN, Or});
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002715 else
2716 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002717 }
2718
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002719 return DAG.getNode(ISD::BITCAST, dl, VT, V);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002720}
2721
2722SDValue
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002723HexagonTargetLowering::LowerEXTRACT_SUBVECTOR_HVX(SDValue Op,
2724 SelectionDAG &DAG) const {
2725 EVT VT = Op.getOperand(0).getValueType();
2726 SDLoc dl(Op);
2727 bool UseHVX = Subtarget.useHVXOps();
2728 bool UseHVXSgl = Subtarget.useHVXSglOps();
2729 // Just in case...
2730
2731 if (!VT.isVector() || !UseHVX)
2732 return SDValue();
2733
2734 EVT ResVT = Op.getValueType();
2735 unsigned ResSize = ResVT.getSizeInBits();
2736 unsigned VectorSizeInBits = UseHVXSgl ? (64 * 8) : (128 * 8);
2737 unsigned OpSize = VT.getSizeInBits();
2738
2739 // We deal only with cases where the result is the vector size
2740 // and the vector operand is a double register.
2741 if (!(ResVT.isByteSized() && ResSize == VectorSizeInBits) ||
2742 !(VT.isByteSized() && OpSize == 2 * VectorSizeInBits))
2743 return SDValue();
2744
2745 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Op.getOperand(1));
2746 if (!Cst)
2747 return SDValue();
2748 unsigned Val = Cst->getZExtValue();
2749
2750 // These two will get lowered to an appropriate EXTRACT_SUBREG in ISel.
2751 if (Val == 0) {
2752 SDValue Vec = Op.getOperand(0);
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00002753 return DAG.getTargetExtractSubreg(Hexagon::vsub_lo, dl, ResVT, Vec);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002754 }
2755
2756 if (ResVT.getVectorNumElements() == Val) {
2757 SDValue Vec = Op.getOperand(0);
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00002758 return DAG.getTargetExtractSubreg(Hexagon::vsub_hi, dl, ResVT, Vec);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002759 }
2760
2761 return SDValue();
2762}
2763
2764SDValue
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002765HexagonTargetLowering::LowerEXTRACT_VECTOR(SDValue Op,
2766 SelectionDAG &DAG) const {
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002767 // If we are dealing with EXTRACT_SUBVECTOR on a HVX type, we may
2768 // be able to simplify it to an EXTRACT_SUBREG.
2769 if (Op.getOpcode() == ISD::EXTRACT_SUBVECTOR && Subtarget.useHVXOps() &&
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00002770 isHvxVectorType(Op.getValueType().getSimpleVT()))
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002771 return LowerEXTRACT_SUBVECTOR_HVX(Op, DAG);
2772
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002773 EVT VT = Op.getValueType();
2774 int VTN = VT.isVector() ? VT.getVectorNumElements() : 1;
2775 SDLoc dl(Op);
2776 SDValue Idx = Op.getOperand(1);
2777 SDValue Vec = Op.getOperand(0);
2778 EVT VecVT = Vec.getValueType();
2779 EVT EltVT = VecVT.getVectorElementType();
2780 int EltSize = EltVT.getSizeInBits();
2781 SDValue Width = DAG.getConstant(Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT ?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002782 EltSize : VTN * EltSize, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002783
2784 // Constant element number.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002785 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Idx)) {
2786 uint64_t X = CI->getZExtValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002787 SDValue Offset = DAG.getConstant(X * EltSize, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002788 const SDValue Ops[] = {Vec, Width, Offset};
2789
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002790 ConstantSDNode *CW = dyn_cast<ConstantSDNode>(Width);
2791 assert(CW && "Non constant width in LowerEXTRACT_VECTOR");
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002792
2793 SDValue N;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002794 MVT SVT = VecVT.getSimpleVT();
2795 uint64_t W = CW->getZExtValue();
2796
2797 if (W == 32) {
2798 // Translate this node into EXTRACT_SUBREG.
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00002799 unsigned Subreg = (X == 0) ? Hexagon::isub_lo : 0;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002800
2801 if (X == 0)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00002802 Subreg = Hexagon::isub_lo;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002803 else if (SVT == MVT::v2i32 && X == 1)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00002804 Subreg = Hexagon::isub_hi;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002805 else if (SVT == MVT::v4i16 && X == 2)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00002806 Subreg = Hexagon::isub_hi;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002807 else if (SVT == MVT::v8i8 && X == 4)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00002808 Subreg = Hexagon::isub_hi;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002809 else
2810 llvm_unreachable("Bad offset");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002811 N = DAG.getTargetExtractSubreg(Subreg, dl, MVT::i32, Vec);
2812
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002813 } else if (SVT.getSizeInBits() == 32) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002814 N = DAG.getNode(HexagonISD::EXTRACTU, dl, MVT::i32, Ops);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002815 } else if (SVT.getSizeInBits() == 64) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002816 N = DAG.getNode(HexagonISD::EXTRACTU, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002817 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00002818 N = DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, N);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002819 } else
2820 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002821
2822 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2823 }
2824
2825 // Variable element number.
2826 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i32, Idx,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002827 DAG.getConstant(EltSize, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002828 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002829 DAG.getConstant(32, dl, MVT::i64));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002830 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2831
2832 const SDValue Ops[] = {Vec, Combined};
2833
2834 SDValue N;
2835 if (VecVT.getSizeInBits() == 32) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002836 N = DAG.getNode(HexagonISD::EXTRACTURP, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002837 } else {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002838 N = DAG.getNode(HexagonISD::EXTRACTURP, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002839 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00002840 N = DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, N);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002841 }
2842 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2843}
2844
2845SDValue
2846HexagonTargetLowering::LowerINSERT_VECTOR(SDValue Op,
2847 SelectionDAG &DAG) const {
2848 EVT VT = Op.getValueType();
2849 int VTN = VT.isVector() ? VT.getVectorNumElements() : 1;
2850 SDLoc dl(Op);
2851 SDValue Vec = Op.getOperand(0);
2852 SDValue Val = Op.getOperand(1);
2853 SDValue Idx = Op.getOperand(2);
2854 EVT VecVT = Vec.getValueType();
2855 EVT EltVT = VecVT.getVectorElementType();
2856 int EltSize = EltVT.getSizeInBits();
2857 SDValue Width = DAG.getConstant(Op.getOpcode() == ISD::INSERT_VECTOR_ELT ?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002858 EltSize : VTN * EltSize, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002859
2860 if (ConstantSDNode *C = cast<ConstantSDNode>(Idx)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002861 SDValue Offset = DAG.getConstant(C->getSExtValue() * EltSize, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002862 const SDValue Ops[] = {Vec, Val, Width, Offset};
2863
2864 SDValue N;
2865 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002866 N = DAG.getNode(HexagonISD::INSERT, dl, MVT::i32, Ops);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002867 else if (VT.getSizeInBits() == 64)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002868 N = DAG.getNode(HexagonISD::INSERT, dl, MVT::i64, Ops);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002869 else
2870 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002871
2872 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2873 }
2874
2875 // Variable element number.
2876 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i32, Idx,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002877 DAG.getConstant(EltSize, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002878 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002879 DAG.getConstant(32, dl, MVT::i64));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002880 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2881
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00002882 if (VT.getSizeInBits() == 64 && Val.getValueSizeInBits() == 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002883 SDValue C = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002884 Val = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, Val);
2885 }
2886
2887 const SDValue Ops[] = {Vec, Val, Combined};
2888
2889 SDValue N;
2890 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002891 N = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, Ops);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002892 else if (VT.getSizeInBits() == 64)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002893 N = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, Ops);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002894 else
2895 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002896
2897 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2898}
2899
Tim Northovera4415852013-08-06 09:12:35 +00002900bool
2901HexagonTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
2902 // Assuming the caller does not have either a signext or zeroext modifier, and
2903 // only one value is accepted, any reasonable truncation is allowed.
2904 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
2905 return false;
2906
2907 // FIXME: in principle up to 64-bit could be made safe, but it would be very
2908 // fragile at the moment: any support for multiple value returns would be
2909 // liable to disallow tail calls involving i64 -> iN truncation in many cases.
2910 return Ty1->getPrimitiveSizeInBits() <= 32;
2911}
2912
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002913SDValue
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002914HexagonTargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
2915 SDValue Chain = Op.getOperand(0);
2916 SDValue Offset = Op.getOperand(1);
2917 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002918 SDLoc dl(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002919 auto PtrVT = getPointerTy(DAG.getDataLayout());
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002920
2921 // Mark function as containing a call to EH_RETURN.
2922 HexagonMachineFunctionInfo *FuncInfo =
2923 DAG.getMachineFunction().getInfo<HexagonMachineFunctionInfo>();
2924 FuncInfo->setHasEHReturn();
2925
2926 unsigned OffsetReg = Hexagon::R28;
2927
Mehdi Amini44ede332015-07-09 02:09:04 +00002928 SDValue StoreAddr =
2929 DAG.getNode(ISD::ADD, dl, PtrVT, DAG.getRegister(Hexagon::R30, PtrVT),
2930 DAG.getIntPtrConstant(4, dl));
Justin Lebar9c375812016-07-15 18:27:10 +00002931 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo());
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002932 Chain = DAG.getCopyToReg(Chain, dl, OffsetReg, Offset);
2933
2934 // Not needed we already use it as explict input to EH_RETURN.
2935 // MF.getRegInfo().addLiveOut(OffsetReg);
2936
2937 return DAG.getNode(HexagonISD::EH_RETURN, dl, MVT::Other, Chain);
2938}
2939
2940SDValue
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002941HexagonTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002942 unsigned Opc = Op.getOpcode();
2943 switch (Opc) {
2944 default:
2945#ifndef NDEBUG
2946 Op.getNode()->dumpr(&DAG);
2947 if (Opc > HexagonISD::OP_BEGIN && Opc < HexagonISD::OP_END)
2948 errs() << "Check for a non-legal type in this operation\n";
2949#endif
2950 llvm_unreachable("Should not custom lower this!");
2951 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
2952 case ISD::INSERT_SUBVECTOR: return LowerINSERT_VECTOR(Op, DAG);
2953 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR(Op, DAG);
2954 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_VECTOR(Op, DAG);
2955 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR(Op, DAG);
2956 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2957 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002958 case ISD::SRA:
2959 case ISD::SHL:
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002960 case ISD::SRL: return LowerVECTOR_SHIFT(Op, DAG);
2961 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002962 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002963 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
2964 // Frame & Return address. Currently unimplemented.
2965 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
2966 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00002967 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002968 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
2969 case ISD::GlobalAddress: return LowerGLOBALADDRESS(Op, DAG);
2970 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002971 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002972 case ISD::VASTART: return LowerVASTART(Op, DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002973 // Custom lower some vector loads.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002974 case ISD::LOAD: return LowerLOAD(Op, DAG);
2975 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
2976 case ISD::SETCC: return LowerSETCC(Op, DAG);
2977 case ISD::VSELECT: return LowerVSELECT(Op, DAG);
2978 case ISD::CTPOP: return LowerCTPOP(Op, DAG);
2979 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00002980 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002981 case ISD::INLINEASM: return LowerINLINEASM(Op, DAG);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00002982 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002983 }
2984}
2985
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002986/// Returns relocation base for the given PIC jumptable.
2987SDValue
2988HexagonTargetLowering::getPICJumpTableRelocBase(SDValue Table,
2989 SelectionDAG &DAG) const {
2990 int Idx = cast<JumpTableSDNode>(Table)->getIndex();
2991 EVT VT = Table.getValueType();
2992 SDValue T = DAG.getTargetJumpTable(Idx, VT, HexagonII::MO_PCREL);
2993 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Table), VT, T);
2994}
2995
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002996//===----------------------------------------------------------------------===//
2997// Inline Assembly Support
2998//===----------------------------------------------------------------------===//
2999
Krzysztof Parzyszekca3b5322016-05-18 14:34:51 +00003000TargetLowering::ConstraintType
3001HexagonTargetLowering::getConstraintType(StringRef Constraint) const {
3002 if (Constraint.size() == 1) {
3003 switch (Constraint[0]) {
3004 case 'q':
3005 case 'v':
3006 if (Subtarget.useHVXOps())
3007 return C_Register;
3008 break;
3009 }
3010 }
3011 return TargetLowering::getConstraintType(Constraint);
3012}
3013
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003014std::pair<unsigned, const TargetRegisterClass*>
Eric Christopher11e4df72015-02-26 22:38:43 +00003015HexagonTargetLowering::getRegForInlineAsmConstraint(
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003016 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003017 bool UseHVX = Subtarget.useHVXOps(), UseHVXDbl = Subtarget.useHVXDblOps();
3018
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003019 if (Constraint.size() == 1) {
3020 switch (Constraint[0]) {
3021 case 'r': // R0-R31
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003022 switch (VT.SimpleTy) {
3023 default:
3024 llvm_unreachable("getRegForInlineAsmConstraint Unhandled data type");
3025 case MVT::i1:
3026 case MVT::i8:
3027 case MVT::i16:
3028 case MVT::i32:
3029 case MVT::f32:
3030 return std::make_pair(0U, &Hexagon::IntRegsRegClass);
3031 case MVT::i64:
3032 case MVT::f64:
3033 return std::make_pair(0U, &Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003034 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003035 case 'q': // q0-q3
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003036 switch (VT.SimpleTy) {
3037 default:
3038 llvm_unreachable("getRegForInlineAsmConstraint Unhandled data type");
3039 case MVT::v1024i1:
3040 case MVT::v512i1:
3041 case MVT::v32i16:
3042 case MVT::v16i32:
3043 case MVT::v64i8:
3044 case MVT::v8i64:
3045 return std::make_pair(0U, &Hexagon::VecPredRegsRegClass);
3046 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003047 case 'v': // V0-V31
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003048 switch (VT.SimpleTy) {
3049 default:
3050 llvm_unreachable("getRegForInlineAsmConstraint Unhandled data type");
3051 case MVT::v16i32:
3052 case MVT::v32i16:
3053 case MVT::v64i8:
3054 case MVT::v8i64:
3055 return std::make_pair(0U, &Hexagon::VectorRegsRegClass);
3056 case MVT::v32i32:
3057 case MVT::v64i16:
3058 case MVT::v16i64:
3059 case MVT::v128i8:
3060 if (Subtarget.hasV60TOps() && UseHVX && UseHVXDbl)
3061 return std::make_pair(0U, &Hexagon::VectorRegs128BRegClass);
3062 return std::make_pair(0U, &Hexagon::VecDblRegsRegClass);
3063 case MVT::v256i8:
3064 case MVT::v128i16:
3065 case MVT::v64i32:
3066 case MVT::v32i64:
3067 return std::make_pair(0U, &Hexagon::VecDblRegs128BRegClass);
3068 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003069
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003070 default:
Craig Toppere55c5562012-02-07 02:50:20 +00003071 llvm_unreachable("Unknown asm register class");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003072 }
3073 }
3074
Eric Christopher11e4df72015-02-26 22:38:43 +00003075 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003076}
3077
Sirish Pande69295b82012-05-10 20:20:25 +00003078/// isFPImmLegal - Returns true if the target can instruction select the
3079/// specified FP immediate natively. If false, the legalizer will
3080/// materialize the FP immediate as a load from a constant pool.
3081bool HexagonTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00003082 return Subtarget.hasV5TOps();
Sirish Pande69295b82012-05-10 20:20:25 +00003083}
3084
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003085/// isLegalAddressingMode - Return true if the addressing mode represented by
3086/// AM is legal for this target, for a load/store of the specified type.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +00003087bool HexagonTargetLowering::isLegalAddressingMode(const DataLayout &DL,
3088 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +00003089 unsigned AS) const {
Krzysztof Parzyszeked4e7822016-08-03 15:06:18 +00003090 if (Ty->isSized()) {
3091 // When LSR detects uses of the same base address to access different
3092 // types (e.g. unions), it will assume a conservative type for these
3093 // uses:
3094 // LSR Use: Kind=Address of void in addrspace(4294967295), ...
3095 // The type Ty passed here would then be "void". Skip the alignment
3096 // checks, but do not return false right away, since that confuses
3097 // LSR into crashing.
3098 unsigned A = DL.getABITypeAlignment(Ty);
3099 // The base offset must be a multiple of the alignment.
3100 if ((AM.BaseOffs % A) != 0)
3101 return false;
3102 // The shifted offset must fit in 11 bits.
3103 if (!isInt<11>(AM.BaseOffs >> Log2_32(A)))
3104 return false;
3105 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003106
3107 // No global is ever allowed as a base.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00003108 if (AM.BaseGV)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003109 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003110
3111 int Scale = AM.Scale;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00003112 if (Scale < 0)
3113 Scale = -Scale;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003114 switch (Scale) {
3115 case 0: // No scale reg, "r+i", "r", or just "i".
3116 break;
3117 default: // No scaled addressing mode.
3118 return false;
3119 }
3120 return true;
3121}
3122
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00003123/// Return true if folding a constant offset with the given GlobalAddress is
3124/// legal. It is frequently not legal in PIC relocation models.
3125bool HexagonTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA)
3126 const {
3127 return HTM.getRelocationModel() == Reloc::Static;
3128}
3129
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003130/// isLegalICmpImmediate - Return true if the specified immediate is legal
3131/// icmp immediate, that is the target has icmp instructions which can compare
3132/// a register against the immediate without having to materialize the
3133/// immediate into a register.
3134bool HexagonTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
3135 return Imm >= -512 && Imm <= 511;
3136}
3137
3138/// IsEligibleForTailCallOptimization - Check whether the call is eligible
3139/// for tail call optimization. Targets which want to do tail call
3140/// optimization should implement this function.
3141bool HexagonTargetLowering::IsEligibleForTailCallOptimization(
3142 SDValue Callee,
3143 CallingConv::ID CalleeCC,
3144 bool isVarArg,
3145 bool isCalleeStructRet,
3146 bool isCallerStructRet,
3147 const SmallVectorImpl<ISD::OutputArg> &Outs,
3148 const SmallVectorImpl<SDValue> &OutVals,
3149 const SmallVectorImpl<ISD::InputArg> &Ins,
3150 SelectionDAG& DAG) const {
3151 const Function *CallerF = DAG.getMachineFunction().getFunction();
3152 CallingConv::ID CallerCC = CallerF->getCallingConv();
3153 bool CCMatch = CallerCC == CalleeCC;
3154
3155 // ***************************************************************************
3156 // Look for obvious safe cases to perform tail call optimization that do not
3157 // require ABI changes.
3158 // ***************************************************************************
3159
3160 // If this is a tail call via a function pointer, then don't do it!
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00003161 if (!isa<GlobalAddressSDNode>(Callee) &&
3162 !isa<ExternalSymbolSDNode>(Callee)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003163 return false;
3164 }
3165
Krzysztof Parzyszek0ba97542016-08-19 15:02:18 +00003166 // Do not optimize if the calling conventions do not match and the conventions
3167 // used are not C or Fast.
3168 if (!CCMatch) {
3169 bool R = (CallerCC == CallingConv::C || CallerCC == CallingConv::Fast);
3170 bool E = (CalleeCC == CallingConv::C || CalleeCC == CallingConv::Fast);
3171 // If R & E, then ok.
3172 if (!R || !E)
3173 return false;
3174 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003175
3176 // Do not tail call optimize vararg calls.
3177 if (isVarArg)
3178 return false;
3179
3180 // Also avoid tail call optimization if either caller or callee uses struct
3181 // return semantics.
3182 if (isCalleeStructRet || isCallerStructRet)
3183 return false;
3184
3185 // In addition to the cases above, we also disable Tail Call Optimization if
3186 // the calling convention code that at least one outgoing argument needs to
3187 // go on the stack. We cannot check that here because at this point that
3188 // information is not available.
3189 return true;
3190}
Colin LeMahieu025f8602014-12-08 21:19:18 +00003191
Krzysztof Parzyszek3e409e12016-08-02 18:34:31 +00003192/// Returns the target specific optimal type for load and store operations as
3193/// a result of memset, memcpy, and memmove lowering.
3194///
3195/// If DstAlign is zero that means it's safe to destination alignment can
3196/// satisfy any constraint. Similarly if SrcAlign is zero it means there isn't
3197/// a need to check it against alignment requirement, probably because the
3198/// source does not need to be loaded. If 'IsMemset' is true, that means it's
3199/// expanding a memset. If 'ZeroMemset' is true, that means it's a memset of
3200/// zero. 'MemcpyStrSrc' indicates whether the memcpy source is constant so it
3201/// does not need to be loaded. It returns EVT::Other if the type should be
3202/// determined using generic target-independent logic.
3203EVT HexagonTargetLowering::getOptimalMemOpType(uint64_t Size,
3204 unsigned DstAlign, unsigned SrcAlign, bool IsMemset, bool ZeroMemset,
3205 bool MemcpyStrSrc, MachineFunction &MF) const {
3206
3207 auto Aligned = [](unsigned GivenA, unsigned MinA) -> bool {
3208 return (GivenA % MinA) == 0;
3209 };
3210
3211 if (Size >= 8 && Aligned(DstAlign, 8) && (IsMemset || Aligned(SrcAlign, 8)))
3212 return MVT::i64;
3213 if (Size >= 4 && Aligned(DstAlign, 4) && (IsMemset || Aligned(SrcAlign, 4)))
3214 return MVT::i32;
3215 if (Size >= 2 && Aligned(DstAlign, 2) && (IsMemset || Aligned(SrcAlign, 2)))
3216 return MVT::i16;
3217
3218 return MVT::Other;
3219}
3220
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +00003221bool HexagonTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
3222 unsigned AS, unsigned Align, bool *Fast) const {
3223 if (Fast)
3224 *Fast = false;
3225
3226 switch (VT.getSimpleVT().SimpleTy) {
3227 default:
3228 return false;
3229 case MVT::v64i8:
3230 case MVT::v128i8:
3231 case MVT::v256i8:
3232 case MVT::v32i16:
3233 case MVT::v64i16:
3234 case MVT::v128i16:
3235 case MVT::v16i32:
3236 case MVT::v32i32:
3237 case MVT::v64i32:
3238 case MVT::v8i64:
3239 case MVT::v16i64:
3240 case MVT::v32i64:
3241 return true;
3242 }
3243 return false;
3244}
3245
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003246std::pair<const TargetRegisterClass*, uint8_t>
3247HexagonTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
3248 MVT VT) const {
3249 const TargetRegisterClass *RRC = nullptr;
3250
3251 uint8_t Cost = 1;
3252 switch (VT.SimpleTy) {
3253 default:
3254 return TargetLowering::findRepresentativeClass(TRI, VT);
3255 case MVT::v64i8:
3256 case MVT::v32i16:
3257 case MVT::v16i32:
3258 case MVT::v8i64:
3259 RRC = &Hexagon::VectorRegsRegClass;
3260 break;
3261 case MVT::v128i8:
3262 case MVT::v64i16:
3263 case MVT::v32i32:
3264 case MVT::v16i64:
3265 if (Subtarget.hasV60TOps() && Subtarget.useHVXOps() &&
3266 Subtarget.useHVXDblOps())
3267 RRC = &Hexagon::VectorRegs128BRegClass;
3268 else
3269 RRC = &Hexagon::VecDblRegsRegClass;
3270 break;
3271 case MVT::v256i8:
3272 case MVT::v128i16:
3273 case MVT::v64i32:
3274 case MVT::v32i64:
3275 RRC = &Hexagon::VecDblRegs128BRegClass;
3276 break;
3277 }
3278 return std::make_pair(RRC, Cost);
3279}
3280
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003281Value *HexagonTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
3282 AtomicOrdering Ord) const {
3283 BasicBlock *BB = Builder.GetInsertBlock();
3284 Module *M = BB->getParent()->getParent();
3285 Type *Ty = cast<PointerType>(Addr->getType())->getElementType();
3286 unsigned SZ = Ty->getPrimitiveSizeInBits();
3287 assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic loads supported");
3288 Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_L2_loadw_locked
3289 : Intrinsic::hexagon_L4_loadd_locked;
3290 Value *Fn = Intrinsic::getDeclaration(M, IntID);
3291 return Builder.CreateCall(Fn, Addr, "larx");
3292}
3293
3294/// Perform a store-conditional operation to Addr. Return the status of the
3295/// store. This should be 0 if the store succeeded, non-zero otherwise.
3296Value *HexagonTargetLowering::emitStoreConditional(IRBuilder<> &Builder,
3297 Value *Val, Value *Addr, AtomicOrdering Ord) const {
3298 BasicBlock *BB = Builder.GetInsertBlock();
3299 Module *M = BB->getParent()->getParent();
3300 Type *Ty = Val->getType();
3301 unsigned SZ = Ty->getPrimitiveSizeInBits();
3302 assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic stores supported");
3303 Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_S2_storew_locked
3304 : Intrinsic::hexagon_S4_stored_locked;
3305 Value *Fn = Intrinsic::getDeclaration(M, IntID);
3306 Value *Call = Builder.CreateCall(Fn, {Addr, Val}, "stcx");
3307 Value *Cmp = Builder.CreateICmpEQ(Call, Builder.getInt32(0), "");
3308 Value *Ext = Builder.CreateZExt(Cmp, Type::getInt32Ty(M->getContext()));
3309 return Ext;
3310}
3311
Ahmed Bougacha52468672015-09-11 17:08:28 +00003312TargetLowering::AtomicExpansionKind
3313HexagonTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003314 // Do not expand loads and stores that don't exceed 64 bits.
Ahmed Bougacha52468672015-09-11 17:08:28 +00003315 return LI->getType()->getPrimitiveSizeInBits() > 64
Tim Northoverf520eff2015-12-02 18:12:57 +00003316 ? AtomicExpansionKind::LLOnly
Ahmed Bougacha52468672015-09-11 17:08:28 +00003317 : AtomicExpansionKind::None;
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003318}
3319
3320bool HexagonTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
3321 // Do not expand loads and stores that don't exceed 64 bits.
3322 return SI->getValueOperand()->getType()->getPrimitiveSizeInBits() > 64;
3323}
Krzysztof Parzyszekf228c952016-06-22 16:07:10 +00003324
3325bool HexagonTargetLowering::shouldExpandAtomicCmpXchgInIR(
3326 AtomicCmpXchgInst *AI) const {
3327 const DataLayout &DL = AI->getModule()->getDataLayout();
3328 unsigned Size = DL.getTypeStoreSize(AI->getCompareOperand()->getType());
3329 return Size >= 4 && Size <= 8;
3330}