blob: bd36978c4364f0f3f15e214ec86e86a28d0f0c80 [file] [log] [blame]
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001//===-- HexagonISelLowering.cpp - Hexagon DAG Lowering Implementation -----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the interfaces that Hexagon uses to lower LLVM code
11// into a selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "HexagonISelLowering.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000016#include "HexagonMachineFunctionInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000017#include "HexagonSubtarget.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "HexagonTargetMachine.h"
19#include "HexagonTargetObjectFile.h"
Rafael Espindola8474fdf2016-06-22 19:09:14 +000020#include "llvm/CodeGen/Analysis.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000021#include "llvm/CodeGen/CallingConvLower.h"
22#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineInstrBuilder.h"
Craig Topperb25fda92012-03-17 18:46:09 +000025#include "llvm/CodeGen/MachineJumpTableInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000026#include "llvm/CodeGen/MachineRegisterInfo.h"
27#include "llvm/CodeGen/SelectionDAGISel.h"
28#include "llvm/CodeGen/ValueTypes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000029#include "llvm/IR/CallingConv.h"
30#include "llvm/IR/DerivedTypes.h"
31#include "llvm/IR/Function.h"
32#include "llvm/IR/GlobalAlias.h"
33#include "llvm/IR/GlobalVariable.h"
34#include "llvm/IR/InlineAsm.h"
35#include "llvm/IR/Intrinsics.h"
NAKAMURA Takumi54eed762012-04-21 15:31:36 +000036#include "llvm/Support/CommandLine.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000037#include "llvm/Support/Debug.h"
38#include "llvm/Support/ErrorHandling.h"
NAKAMURA Takumie30303f2012-04-21 15:31:45 +000039#include "llvm/Support/raw_ostream.h"
NAKAMURA Takumi54eed762012-04-21 15:31:36 +000040
Craig Topperb25fda92012-03-17 18:46:09 +000041using namespace llvm;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000042
Chandler Carruthe96dd892014-04-21 22:55:11 +000043#define DEBUG_TYPE "hexagon-lowering"
44
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +000045static cl::opt<bool> EmitJumpTables("hexagon-emit-jump-tables",
46 cl::init(true), cl::Hidden,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +000047 cl::desc("Control jump table emission on Hexagon target"));
48
49static cl::opt<bool> EnableHexSDNodeSched("enable-hexagon-sdnode-sched",
50 cl::Hidden, cl::ZeroOrMore, cl::init(false),
51 cl::desc("Enable Hexagon SDNode scheduling"));
52
53static cl::opt<bool> EnableFastMath("ffast-math",
54 cl::Hidden, cl::ZeroOrMore, cl::init(false),
55 cl::desc("Enable Fast Math processing"));
56
57static cl::opt<int> MinimumJumpTables("minimum-jump-tables",
58 cl::Hidden, cl::ZeroOrMore, cl::init(5),
59 cl::desc("Set minimum jump tables"));
60
61static cl::opt<int> MaxStoresPerMemcpyCL("max-store-memcpy",
62 cl::Hidden, cl::ZeroOrMore, cl::init(6),
63 cl::desc("Max #stores to inline memcpy"));
64
65static cl::opt<int> MaxStoresPerMemcpyOptSizeCL("max-store-memcpy-Os",
66 cl::Hidden, cl::ZeroOrMore, cl::init(4),
67 cl::desc("Max #stores to inline memcpy"));
68
69static cl::opt<int> MaxStoresPerMemmoveCL("max-store-memmove",
70 cl::Hidden, cl::ZeroOrMore, cl::init(6),
71 cl::desc("Max #stores to inline memmove"));
72
73static cl::opt<int> MaxStoresPerMemmoveOptSizeCL("max-store-memmove-Os",
74 cl::Hidden, cl::ZeroOrMore, cl::init(4),
75 cl::desc("Max #stores to inline memmove"));
76
77static cl::opt<int> MaxStoresPerMemsetCL("max-store-memset",
78 cl::Hidden, cl::ZeroOrMore, cl::init(8),
79 cl::desc("Max #stores to inline memset"));
80
81static cl::opt<int> MaxStoresPerMemsetOptSizeCL("max-store-memset-Os",
82 cl::Hidden, cl::ZeroOrMore, cl::init(4),
83 cl::desc("Max #stores to inline memset"));
84
Tony Linthicum1213a7a2011-12-12 21:14:40 +000085
Benjamin Kramer602bb4a2013-10-27 11:16:09 +000086namespace {
87class HexagonCCState : public CCState {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +000088 unsigned NumNamedVarArgParams;
Benjamin Kramer602bb4a2013-10-27 11:16:09 +000089
90public:
91 HexagonCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
Eric Christopherb5217502014-08-06 18:45:26 +000092 SmallVectorImpl<CCValAssign> &locs, LLVMContext &C,
93 int NumNamedVarArgParams)
94 : CCState(CC, isVarArg, MF, locs, C),
Benjamin Kramer602bb4a2013-10-27 11:16:09 +000095 NumNamedVarArgParams(NumNamedVarArgParams) {}
96
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +000097 unsigned getNumNamedVarArgParams() const { return NumNamedVarArgParams; }
Benjamin Kramer602bb4a2013-10-27 11:16:09 +000098};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000099}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000100
101// Implement calling convention for Hexagon.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000102
103static bool IsHvxVectorType(MVT ty);
104
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000105static bool
106CC_Hexagon(unsigned ValNo, MVT ValVT,
107 MVT LocVT, CCValAssign::LocInfo LocInfo,
108 ISD::ArgFlagsTy ArgFlags, CCState &State);
109
110static bool
111CC_Hexagon32(unsigned ValNo, MVT ValVT,
112 MVT LocVT, CCValAssign::LocInfo LocInfo,
113 ISD::ArgFlagsTy ArgFlags, CCState &State);
114
115static bool
116CC_Hexagon64(unsigned ValNo, MVT ValVT,
117 MVT LocVT, CCValAssign::LocInfo LocInfo,
118 ISD::ArgFlagsTy ArgFlags, CCState &State);
119
120static bool
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000121CC_HexagonVector(unsigned ValNo, MVT ValVT,
122 MVT LocVT, CCValAssign::LocInfo LocInfo,
123 ISD::ArgFlagsTy ArgFlags, CCState &State);
124
125static bool
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000126RetCC_Hexagon(unsigned ValNo, MVT ValVT,
127 MVT LocVT, CCValAssign::LocInfo LocInfo,
128 ISD::ArgFlagsTy ArgFlags, CCState &State);
129
130static bool
131RetCC_Hexagon32(unsigned ValNo, MVT ValVT,
132 MVT LocVT, CCValAssign::LocInfo LocInfo,
133 ISD::ArgFlagsTy ArgFlags, CCState &State);
134
135static bool
136RetCC_Hexagon64(unsigned ValNo, MVT ValVT,
137 MVT LocVT, CCValAssign::LocInfo LocInfo,
138 ISD::ArgFlagsTy ArgFlags, CCState &State);
139
140static bool
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000141RetCC_HexagonVector(unsigned ValNo, MVT ValVT,
142 MVT LocVT, CCValAssign::LocInfo LocInfo,
143 ISD::ArgFlagsTy ArgFlags, CCState &State);
144
145static bool
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000146CC_Hexagon_VarArg (unsigned ValNo, MVT ValVT,
147 MVT LocVT, CCValAssign::LocInfo LocInfo,
148 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000149 HexagonCCState &HState = static_cast<HexagonCCState &>(State);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000150
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000151 if (ValNo < HState.getNumNamedVarArgParams()) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000152 // Deal with named arguments.
153 return CC_Hexagon(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State);
154 }
155
156 // Deal with un-named arguments.
157 unsigned ofst;
158 if (ArgFlags.isByVal()) {
159 // If pass-by-value, the size allocated on stack is decided
160 // by ArgFlags.getByValSize(), not by the size of LocVT.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000161 ofst = State.AllocateStack(ArgFlags.getByValSize(),
162 ArgFlags.getByValAlign());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000163 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
164 return false;
165 }
Jyotsna Vermac7dcc2f2013-03-07 20:28:34 +0000166 if (LocVT == MVT::i1 || LocVT == MVT::i8 || LocVT == MVT::i16) {
167 LocVT = MVT::i32;
168 ValVT = MVT::i32;
169 if (ArgFlags.isSExt())
170 LocInfo = CCValAssign::SExt;
171 else if (ArgFlags.isZExt())
172 LocInfo = CCValAssign::ZExt;
173 else
174 LocInfo = CCValAssign::AExt;
175 }
Sirish Pande69295b82012-05-10 20:20:25 +0000176 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000177 ofst = State.AllocateStack(4, 4);
178 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
179 return false;
180 }
Sirish Pande69295b82012-05-10 20:20:25 +0000181 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000182 ofst = State.AllocateStack(8, 8);
183 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
184 return false;
185 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000186 if (LocVT == MVT::v2i64 || LocVT == MVT::v4i32 || LocVT == MVT::v8i16 ||
187 LocVT == MVT::v16i8) {
188 ofst = State.AllocateStack(16, 16);
189 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
190 return false;
191 }
192 if (LocVT == MVT::v4i64 || LocVT == MVT::v8i32 || LocVT == MVT::v16i16 ||
193 LocVT == MVT::v32i8) {
194 ofst = State.AllocateStack(32, 32);
195 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
196 return false;
197 }
198 if (LocVT == MVT::v8i64 || LocVT == MVT::v16i32 || LocVT == MVT::v32i16 ||
199 LocVT == MVT::v64i8 || LocVT == MVT::v512i1) {
200 ofst = State.AllocateStack(64, 64);
201 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
202 return false;
203 }
204 if (LocVT == MVT::v16i64 || LocVT == MVT::v32i32 || LocVT == MVT::v64i16 ||
205 LocVT == MVT::v128i8 || LocVT == MVT::v1024i1) {
206 ofst = State.AllocateStack(128, 128);
207 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
208 return false;
209 }
210 if (LocVT == MVT::v32i64 || LocVT == MVT::v64i32 || LocVT == MVT::v128i16 ||
211 LocVT == MVT::v256i8) {
212 ofst = State.AllocateStack(256, 256);
213 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
214 return false;
215 }
216
Craig Toppere73658d2014-04-28 04:05:08 +0000217 llvm_unreachable(nullptr);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000218}
219
220
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000221static bool CC_Hexagon (unsigned ValNo, MVT ValVT, MVT LocVT,
222 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, CCState &State) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000223 if (ArgFlags.isByVal()) {
224 // Passed on stack.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000225 unsigned Offset = State.AllocateStack(ArgFlags.getByValSize(),
226 ArgFlags.getByValAlign());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000227 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
228 return false;
229 }
230
231 if (LocVT == MVT::i1 || LocVT == MVT::i8 || LocVT == MVT::i16) {
232 LocVT = MVT::i32;
233 ValVT = MVT::i32;
234 if (ArgFlags.isSExt())
235 LocInfo = CCValAssign::SExt;
236 else if (ArgFlags.isZExt())
237 LocInfo = CCValAssign::ZExt;
238 else
239 LocInfo = CCValAssign::AExt;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000240 } else if (LocVT == MVT::v4i8 || LocVT == MVT::v2i16) {
241 LocVT = MVT::i32;
242 LocInfo = CCValAssign::BCvt;
243 } else if (LocVT == MVT::v8i8 || LocVT == MVT::v4i16 || LocVT == MVT::v2i32) {
244 LocVT = MVT::i64;
245 LocInfo = CCValAssign::BCvt;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000246 }
247
Sirish Pande69295b82012-05-10 20:20:25 +0000248 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000249 if (!CC_Hexagon32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
250 return false;
251 }
252
Sirish Pande69295b82012-05-10 20:20:25 +0000253 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000254 if (!CC_Hexagon64(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
255 return false;
256 }
257
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000258 if (LocVT == MVT::v8i32 || LocVT == MVT::v16i16 || LocVT == MVT::v32i8) {
259 unsigned Offset = State.AllocateStack(ArgFlags.getByValSize(), 32);
260 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
261 return false;
262 }
263
264 if (IsHvxVectorType(LocVT)) {
265 if (!CC_HexagonVector(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
266 return false;
267 }
268
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000269 return true; // CC didn't match.
270}
271
272
273static bool CC_Hexagon32(unsigned ValNo, MVT ValVT,
274 MVT LocVT, CCValAssign::LocInfo LocInfo,
275 ISD::ArgFlagsTy ArgFlags, CCState &State) {
276
Craig Topper840beec2014-04-04 05:16:06 +0000277 static const MCPhysReg RegList[] = {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000278 Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3, Hexagon::R4,
279 Hexagon::R5
280 };
Tim Northover3b6b7ca2015-02-21 02:11:17 +0000281 if (unsigned Reg = State.AllocateReg(RegList)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000282 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
283 return false;
284 }
285
286 unsigned Offset = State.AllocateStack(4, 4);
287 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
288 return false;
289}
290
291static bool CC_Hexagon64(unsigned ValNo, MVT ValVT,
292 MVT LocVT, CCValAssign::LocInfo LocInfo,
293 ISD::ArgFlagsTy ArgFlags, CCState &State) {
294
295 if (unsigned Reg = State.AllocateReg(Hexagon::D0)) {
296 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
297 return false;
298 }
299
Craig Topper840beec2014-04-04 05:16:06 +0000300 static const MCPhysReg RegList1[] = {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000301 Hexagon::D1, Hexagon::D2
302 };
Craig Topper840beec2014-04-04 05:16:06 +0000303 static const MCPhysReg RegList2[] = {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000304 Hexagon::R1, Hexagon::R3
305 };
Tim Northover3b6b7ca2015-02-21 02:11:17 +0000306 if (unsigned Reg = State.AllocateReg(RegList1, RegList2)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000307 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
308 return false;
309 }
310
311 unsigned Offset = State.AllocateStack(8, 8, Hexagon::D2);
312 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
313 return false;
314}
315
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000316static bool CC_HexagonVector(unsigned ValNo, MVT ValVT,
317 MVT LocVT, CCValAssign::LocInfo LocInfo,
318 ISD::ArgFlagsTy ArgFlags, CCState &State) {
319
Craig Toppere5e035a32015-12-05 07:13:35 +0000320 static const MCPhysReg VecLstS[] = { Hexagon::V0, Hexagon::V1,
321 Hexagon::V2, Hexagon::V3,
322 Hexagon::V4, Hexagon::V5,
323 Hexagon::V6, Hexagon::V7,
324 Hexagon::V8, Hexagon::V9,
325 Hexagon::V10, Hexagon::V11,
326 Hexagon::V12, Hexagon::V13,
327 Hexagon::V14, Hexagon::V15};
328 static const MCPhysReg VecLstD[] = { Hexagon::W0, Hexagon::W1,
329 Hexagon::W2, Hexagon::W3,
330 Hexagon::W4, Hexagon::W5,
331 Hexagon::W6, Hexagon::W7};
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000332 auto &MF = State.getMachineFunction();
333 auto &HST = MF.getSubtarget<HexagonSubtarget>();
334 bool UseHVX = HST.useHVXOps();
335 bool UseHVXDbl = HST.useHVXDblOps();
336
337 if ((UseHVX && !UseHVXDbl) &&
338 (LocVT == MVT::v8i64 || LocVT == MVT::v16i32 || LocVT == MVT::v32i16 ||
339 LocVT == MVT::v64i8 || LocVT == MVT::v512i1)) {
340 if (unsigned Reg = State.AllocateReg(VecLstS)) {
341 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
342 return false;
343 }
344 unsigned Offset = State.AllocateStack(64, 64);
345 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
346 return false;
347 }
348 if ((UseHVX && !UseHVXDbl) &&
349 (LocVT == MVT::v16i64 || LocVT == MVT::v32i32 || LocVT == MVT::v64i16 ||
350 LocVT == MVT::v128i8)) {
351 if (unsigned Reg = State.AllocateReg(VecLstD)) {
352 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
353 return false;
354 }
355 unsigned Offset = State.AllocateStack(128, 128);
356 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
357 return false;
358 }
359 // 128B Mode
360 if ((UseHVX && UseHVXDbl) &&
361 (LocVT == MVT::v32i64 || LocVT == MVT::v64i32 || LocVT == MVT::v128i16 ||
362 LocVT == MVT::v256i8)) {
363 if (unsigned Reg = State.AllocateReg(VecLstD)) {
364 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
365 return false;
366 }
367 unsigned Offset = State.AllocateStack(256, 256);
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 || LocVT == MVT::v1024i1)) {
374 if (unsigned Reg = State.AllocateReg(VecLstS)) {
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 return true;
383}
384
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000385static bool RetCC_Hexagon(unsigned ValNo, MVT ValVT,
386 MVT LocVT, CCValAssign::LocInfo LocInfo,
387 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000388 auto &MF = State.getMachineFunction();
389 auto &HST = MF.getSubtarget<HexagonSubtarget>();
390 bool UseHVX = HST.useHVXOps();
391 bool UseHVXDbl = HST.useHVXDblOps();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000392
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000393 if (LocVT == MVT::i1) {
394 // Return values of type MVT::i1 still need to be assigned to R0, but
395 // the value type needs to remain i1. LowerCallResult will deal with it,
396 // but it needs to recognize i1 as the value type.
397 LocVT = MVT::i32;
398 } else if (LocVT == MVT::i8 || LocVT == MVT::i16) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000399 LocVT = MVT::i32;
400 ValVT = MVT::i32;
401 if (ArgFlags.isSExt())
402 LocInfo = CCValAssign::SExt;
403 else if (ArgFlags.isZExt())
404 LocInfo = CCValAssign::ZExt;
405 else
406 LocInfo = CCValAssign::AExt;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000407 } else if (LocVT == MVT::v4i8 || LocVT == MVT::v2i16) {
408 LocVT = MVT::i32;
409 LocInfo = CCValAssign::BCvt;
410 } else if (LocVT == MVT::v8i8 || LocVT == MVT::v4i16 || LocVT == MVT::v2i32) {
411 LocVT = MVT::i64;
412 LocInfo = CCValAssign::BCvt;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000413 } else if (LocVT == MVT::v64i8 || LocVT == MVT::v32i16 ||
414 LocVT == MVT::v16i32 || LocVT == MVT::v8i64 ||
415 LocVT == MVT::v512i1) {
416 LocVT = MVT::v16i32;
417 ValVT = MVT::v16i32;
418 LocInfo = CCValAssign::Full;
419 } else if (LocVT == MVT::v128i8 || LocVT == MVT::v64i16 ||
420 LocVT == MVT::v32i32 || LocVT == MVT::v16i64 ||
421 (LocVT == MVT::v1024i1 && UseHVX && UseHVXDbl)) {
422 LocVT = MVT::v32i32;
423 ValVT = MVT::v32i32;
424 LocInfo = CCValAssign::Full;
425 } else if (LocVT == MVT::v256i8 || LocVT == MVT::v128i16 ||
426 LocVT == MVT::v64i32 || LocVT == MVT::v32i64) {
427 LocVT = MVT::v64i32;
428 ValVT = MVT::v64i32;
429 LocInfo = CCValAssign::Full;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000430 }
Sirish Pande69295b82012-05-10 20:20:25 +0000431 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000432 if (!RetCC_Hexagon32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
433 return false;
434 }
435
Sirish Pande69295b82012-05-10 20:20:25 +0000436 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000437 if (!RetCC_Hexagon64(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
438 return false;
439 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000440 if (LocVT == MVT::v16i32 || LocVT == MVT::v32i32 || LocVT == MVT::v64i32) {
441 if (!RetCC_HexagonVector(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
442 return false;
443 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000444 return true; // CC didn't match.
445}
446
447static bool RetCC_Hexagon32(unsigned ValNo, MVT ValVT,
448 MVT LocVT, CCValAssign::LocInfo LocInfo,
449 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Sirish Pande69295b82012-05-10 20:20:25 +0000450 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000451 if (unsigned Reg = State.AllocateReg(Hexagon::R0)) {
452 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
453 return false;
454 }
455 }
456
457 unsigned Offset = State.AllocateStack(4, 4);
458 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
459 return false;
460}
461
462static bool RetCC_Hexagon64(unsigned ValNo, MVT ValVT,
463 MVT LocVT, CCValAssign::LocInfo LocInfo,
464 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Sirish Pande69295b82012-05-10 20:20:25 +0000465 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000466 if (unsigned Reg = State.AllocateReg(Hexagon::D0)) {
467 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
468 return false;
469 }
470 }
471
472 unsigned Offset = State.AllocateStack(8, 8);
473 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
474 return false;
475}
476
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000477static bool RetCC_HexagonVector(unsigned ValNo, MVT ValVT,
478 MVT LocVT, CCValAssign::LocInfo LocInfo,
479 ISD::ArgFlagsTy ArgFlags, CCState &State) {
480 auto &MF = State.getMachineFunction();
481 auto &HST = MF.getSubtarget<HexagonSubtarget>();
482 bool UseHVX = HST.useHVXOps();
483 bool UseHVXDbl = HST.useHVXDblOps();
484
485 unsigned OffSiz = 64;
486 if (LocVT == MVT::v16i32) {
487 if (unsigned Reg = State.AllocateReg(Hexagon::V0)) {
488 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
489 return false;
490 }
491 } else if (LocVT == MVT::v32i32) {
492 unsigned Req = (UseHVX && UseHVXDbl) ? Hexagon::V0 : Hexagon::W0;
493 if (unsigned Reg = State.AllocateReg(Req)) {
494 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
495 return false;
496 }
497 OffSiz = 128;
498 } else if (LocVT == MVT::v64i32) {
499 if (unsigned Reg = State.AllocateReg(Hexagon::W0)) {
500 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
501 return false;
502 }
503 OffSiz = 256;
504 }
505
506 unsigned Offset = State.AllocateStack(OffSiz, OffSiz);
507 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
508 return false;
509}
510
Craig Topper18e69f42016-04-15 06:20:21 +0000511void HexagonTargetLowering::promoteLdStType(MVT VT, MVT PromotedLdStVT) {
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000512 if (VT != PromotedLdStVT) {
Craig Topper18e69f42016-04-15 06:20:21 +0000513 setOperationAction(ISD::LOAD, VT, Promote);
514 AddPromotedToType(ISD::LOAD, VT, PromotedLdStVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000515
Craig Topper18e69f42016-04-15 06:20:21 +0000516 setOperationAction(ISD::STORE, VT, Promote);
517 AddPromotedToType(ISD::STORE, VT, PromotedLdStVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000518 }
519}
520
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000521SDValue
522HexagonTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG)
523const {
524 return SDValue();
525}
526
527/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
528/// by "Src" to address "Dst" of size "Size". Alignment information is
529/// specified by the specific parameter attribute. The copy will be passed as
530/// a byval function parameter. Sometimes what we are copying is the end of a
531/// larger object, the part that does not fit in registers.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000532static SDValue CreateCopyOfByValArgument(SDValue Src, SDValue Dst,
533 SDValue Chain, ISD::ArgFlagsTy Flags,
534 SelectionDAG &DAG, const SDLoc &dl) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000535
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000536 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000537 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
538 /*isVolatile=*/false, /*AlwaysInline=*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +0000539 /*isTailCall=*/false,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000540 MachinePointerInfo(), MachinePointerInfo());
541}
542
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000543static bool IsHvxVectorType(MVT ty) {
544 return (ty == MVT::v8i64 || ty == MVT::v16i32 || ty == MVT::v32i16 ||
545 ty == MVT::v64i8 ||
546 ty == MVT::v16i64 || ty == MVT::v32i32 || ty == MVT::v64i16 ||
547 ty == MVT::v128i8 ||
548 ty == MVT::v32i64 || ty == MVT::v64i32 || ty == MVT::v128i16 ||
549 ty == MVT::v256i8 ||
550 ty == MVT::v512i1 || ty == MVT::v1024i1);
551}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000552
553// LowerReturn - Lower ISD::RET. If a struct is larger than 8 bytes and is
554// passed by value, the function prototype is modified to return void and
555// the value is stored in memory pointed by a pointer passed by caller.
556SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000557HexagonTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
558 bool isVarArg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000559 const SmallVectorImpl<ISD::OutputArg> &Outs,
560 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000561 const SDLoc &dl, SelectionDAG &DAG) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000562
563 // CCValAssign - represent the assignment of the return value to locations.
564 SmallVector<CCValAssign, 16> RVLocs;
565
566 // CCState - Info about the registers and stack slot.
Eric Christopherb5217502014-08-06 18:45:26 +0000567 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
568 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000569
570 // Analyze return values of ISD::RET
571 CCInfo.AnalyzeReturn(Outs, RetCC_Hexagon);
572
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000573 SDValue Flag;
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000574 SmallVector<SDValue, 4> RetOps(1, Chain);
575
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000576 // Copy the result values into the output registers.
577 for (unsigned i = 0; i != RVLocs.size(); ++i) {
578 CCValAssign &VA = RVLocs[i];
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000579
580 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), OutVals[i], Flag);
581
582 // Guarantee that all emitted copies are stuck together with flags.
583 Flag = Chain.getValue(1);
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000584 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000585 }
586
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000587 RetOps[0] = Chain; // Update chain.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000588
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000589 // Add the flag if we have it.
590 if (Flag.getNode())
591 RetOps.push_back(Flag);
592
Craig Topper48d114b2014-04-26 18:35:24 +0000593 return DAG.getNode(HexagonISD::RET_FLAG, dl, MVT::Other, RetOps);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000594}
595
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000596bool HexagonTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
597 // If either no tail call or told not to tail call at all, don't.
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000598 auto Attr =
599 CI->getParent()->getParent()->getFnAttribute("disable-tail-calls");
600 if (!CI->isTailCall() || Attr.getValueAsString() == "true")
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000601 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000602
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000603 return true;
604}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000605
606/// LowerCallResult - Lower the result values of an ISD::CALL into the
607/// appropriate copies out of appropriate physical registers. This assumes that
608/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
609/// being lowered. Returns a SDNode with the same number of values as the
610/// ISD::CALL.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000611SDValue HexagonTargetLowering::LowerCallResult(
612 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool isVarArg,
613 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
614 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals,
615 const SmallVectorImpl<SDValue> &OutVals, SDValue Callee) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000616 // Assign locations to each value returned by this call.
617 SmallVector<CCValAssign, 16> RVLocs;
618
Eric Christopherb5217502014-08-06 18:45:26 +0000619 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
620 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000621
622 CCInfo.AnalyzeCallResult(Ins, RetCC_Hexagon);
623
624 // Copy all of the result registers out of their specified physreg.
625 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000626 SDValue RetVal;
627 if (RVLocs[i].getValVT() == MVT::i1) {
628 // Return values of type MVT::i1 require special handling. The reason
629 // is that MVT::i1 is associated with the PredRegs register class, but
630 // values of that type are still returned in R0. Generate an explicit
631 // copy into a predicate register from R0, and treat the value of the
632 // predicate register as the call result.
633 auto &MRI = DAG.getMachineFunction().getRegInfo();
634 SDValue FR0 = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
635 MVT::i32, InFlag);
636 // FR0 = (Value, Chain, Glue)
637 unsigned PredR = MRI.createVirtualRegister(&Hexagon::PredRegsRegClass);
638 SDValue TPR = DAG.getCopyToReg(FR0.getValue(1), dl, PredR,
639 FR0.getValue(0), FR0.getValue(2));
640 // TPR = (Chain, Glue)
641 RetVal = DAG.getCopyFromReg(TPR.getValue(0), dl, PredR, MVT::i1,
642 TPR.getValue(1));
643 } else {
644 RetVal = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
645 RVLocs[i].getValVT(), InFlag);
646 }
647 InVals.push_back(RetVal.getValue(0));
648 Chain = RetVal.getValue(1);
649 InFlag = RetVal.getValue(2);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000650 }
651
652 return Chain;
653}
654
655/// LowerCall - Functions arguments are copied from virtual regs to
656/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
657SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +0000658HexagonTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000659 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +0000660 SelectionDAG &DAG = CLI.DAG;
Craig Topperb94011f2013-07-14 04:42:23 +0000661 SDLoc &dl = CLI.DL;
662 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
663 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
664 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000665 SDValue Chain = CLI.Chain;
666 SDValue Callee = CLI.Callee;
667 bool &isTailCall = CLI.IsTailCall;
668 CallingConv::ID CallConv = CLI.CallConv;
669 bool isVarArg = CLI.IsVarArg;
Colin LeMahieu2e3a26d2015-01-16 17:05:27 +0000670 bool doesNotReturn = CLI.DoesNotReturn;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000671
672 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000673 MachineFunction &MF = DAG.getMachineFunction();
Mehdi Amini44ede332015-07-09 02:09:04 +0000674 auto PtrVT = getPointerTy(MF.getDataLayout());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000675
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000676 // Check for varargs.
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000677 int NumNamedVarArgParams = -1;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000678 if (GlobalAddressSDNode *GAN = dyn_cast<GlobalAddressSDNode>(Callee)) {
679 const GlobalValue *GV = GAN->getGlobal();
680 Callee = DAG.getTargetGlobalAddress(GV, dl, MVT::i32);
681 if (const Function* F = dyn_cast<Function>(GV)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000682 // If a function has zero args and is a vararg function, that's
683 // disallowed so it must be an undeclared function. Do not assume
684 // varargs if the callee is undefined.
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000685 if (F->isVarArg() && F->getFunctionType()->getNumParams() != 0)
686 NumNamedVarArgParams = F->getFunctionType()->getNumParams();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000687 }
688 }
689
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000690 // Analyze operands of the call, assigning locations to each operand.
691 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000692 HexagonCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
693 *DAG.getContext(), NumNamedVarArgParams);
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000694
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000695 if (isVarArg)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000696 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon_VarArg);
697 else
698 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon);
699
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000700 auto Attr = MF.getFunction()->getFnAttribute("disable-tail-calls");
701 if (Attr.getValueAsString() == "true")
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000702 isTailCall = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000703
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000704 if (isTailCall) {
705 bool StructAttrFlag = MF.getFunction()->hasStructRetAttr();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000706 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
707 isVarArg, IsStructRet,
708 StructAttrFlag,
709 Outs, OutVals, Ins, DAG);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000710 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000711 CCValAssign &VA = ArgLocs[i];
712 if (VA.isMemLoc()) {
713 isTailCall = false;
714 break;
715 }
716 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000717 DEBUG(dbgs() << (isTailCall ? "Eligible for Tail Call\n"
718 : "Argument must be passed on stack. "
719 "Not eligible for Tail Call\n"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000720 }
721 // Get a count of how many bytes are to be pushed on the stack.
722 unsigned NumBytes = CCInfo.getNextStackOffset();
723 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
724 SmallVector<SDValue, 8> MemOpChains;
725
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000726 auto &HRI = *Subtarget.getRegisterInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +0000727 SDValue StackPtr =
728 DAG.getCopyFromReg(Chain, dl, HRI.getStackRegister(), PtrVT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000729
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000730 bool NeedsArgAlign = false;
731 unsigned LargestAlignSeen = 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000732 // Walk the register/memloc assignments, inserting copies/loads.
733 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
734 CCValAssign &VA = ArgLocs[i];
735 SDValue Arg = OutVals[i];
736 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000737 // Record if we need > 8 byte alignment on an argument.
738 bool ArgAlign = IsHvxVectorType(VA.getValVT());
739 NeedsArgAlign |= ArgAlign;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000740
741 // Promote the value if needed.
742 switch (VA.getLocInfo()) {
743 default:
744 // Loc info must be one of Full, SExt, ZExt, or AExt.
Craig Toppere55c5562012-02-07 02:50:20 +0000745 llvm_unreachable("Unknown loc info!");
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000746 case CCValAssign::BCvt:
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000747 case CCValAssign::Full:
748 break;
749 case CCValAssign::SExt:
750 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
751 break;
752 case CCValAssign::ZExt:
753 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
754 break;
755 case CCValAssign::AExt:
756 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
757 break;
758 }
759
760 if (VA.isMemLoc()) {
761 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000762 SDValue MemAddr = DAG.getConstant(LocMemOffset, dl,
763 StackPtr.getValueType());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000764 MemAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, MemAddr);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000765 if (ArgAlign)
766 LargestAlignSeen = std::max(LargestAlignSeen,
767 VA.getLocVT().getStoreSizeInBits() >> 3);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000768 if (Flags.isByVal()) {
769 // The argument is a struct passed by value. According to LLVM, "Arg"
770 // is is pointer.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000771 MemOpChains.push_back(CreateCopyOfByValArgument(Arg, MemAddr, Chain,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000772 Flags, DAG, dl));
773 } else {
Alex Lorenze40c8a22015-08-11 23:09:45 +0000774 MachinePointerInfo LocPI = MachinePointerInfo::getStack(
775 DAG.getMachineFunction(), LocMemOffset);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000776 SDValue S = DAG.getStore(Chain, dl, Arg, MemAddr, LocPI, false,
777 false, 0);
778 MemOpChains.push_back(S);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000779 }
780 continue;
781 }
782
783 // Arguments that can be passed on register must be kept at RegsToPass
784 // vector.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000785 if (VA.isRegLoc())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000786 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000787 }
788
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000789 if (NeedsArgAlign && Subtarget.hasV60TOps()) {
790 DEBUG(dbgs() << "Function needs byte stack align due to call args\n");
791 MachineFrameInfo* MFI = DAG.getMachineFunction().getFrameInfo();
792 // V6 vectors passed by value have 64 or 128 byte alignment depending
793 // on whether we are 64 byte vector mode or 128 byte.
794 bool UseHVXDbl = Subtarget.useHVXDblOps();
795 assert(Subtarget.useHVXOps());
796 const unsigned ObjAlign = UseHVXDbl ? 128 : 64;
797 LargestAlignSeen = std::max(LargestAlignSeen, ObjAlign);
798 MFI->ensureMaxAlignment(LargestAlignSeen);
799 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000800 // Transform all store nodes into one single node because all store
801 // nodes are independent of each other.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000802 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000803 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000804
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000805 if (!isTailCall) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000806 SDValue C = DAG.getConstant(NumBytes, dl, PtrVT, true);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000807 Chain = DAG.getCALLSEQ_START(Chain, C, dl);
808 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000809
810 // Build a sequence of copy-to-reg nodes chained together with token
811 // chain and flag operands which copy the outgoing args into registers.
Benjamin Kramerbde91762012-06-02 10:20:22 +0000812 // The InFlag in necessary since all emitted instructions must be
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000813 // stuck together.
814 SDValue InFlag;
815 if (!isTailCall) {
816 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
817 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
818 RegsToPass[i].second, InFlag);
819 InFlag = Chain.getValue(1);
820 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000821 } else {
822 // For tail calls lower the arguments to the 'real' stack slot.
823 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000824 // Force all the incoming stack arguments to be loaded from the stack
825 // before any new outgoing arguments are stored to the stack, because the
826 // outgoing stack slots may alias the incoming argument stack slots, and
827 // the alias isn't otherwise explicit. This is slightly more conservative
828 // than necessary, because it means that each store effectively depends
829 // on every argument instead of just those arguments it would clobber.
830 //
Benjamin Kramerbde91762012-06-02 10:20:22 +0000831 // Do not flag preceding copytoreg stuff together with the following stuff.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000832 InFlag = SDValue();
833 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
834 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
835 RegsToPass[i].second, InFlag);
836 InFlag = Chain.getValue(1);
837 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000838 InFlag = SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000839 }
840
841 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
842 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
843 // node so that legalize doesn't hack it.
Tobias Edler von Kochb51460c2015-12-16 17:29:37 +0000844 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000845 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, PtrVT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000846 } else if (ExternalSymbolSDNode *S =
847 dyn_cast<ExternalSymbolSDNode>(Callee)) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000848 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), PtrVT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000849 }
850
851 // Returns a chain & a flag for retval copy to use.
852 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
853 SmallVector<SDValue, 8> Ops;
854 Ops.push_back(Chain);
855 Ops.push_back(Callee);
856
857 // Add argument registers to the end of the list so that they are
858 // known live into the call.
859 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
860 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
861 RegsToPass[i].second.getValueType()));
862 }
863
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000864 if (InFlag.getNode())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000865 Ops.push_back(InFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000866
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +0000867 if (isTailCall) {
868 MF.getFrameInfo()->setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +0000869 return DAG.getNode(HexagonISD::TC_RETURN, dl, NodeTys, Ops);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +0000870 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000871
Colin LeMahieu2e3a26d2015-01-16 17:05:27 +0000872 int OpCode = doesNotReturn ? HexagonISD::CALLv3nr : HexagonISD::CALLv3;
873 Chain = DAG.getNode(OpCode, dl, NodeTys, Ops);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000874 InFlag = Chain.getValue(1);
875
876 // Create the CALLSEQ_END node.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000877 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
878 DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000879 InFlag = Chain.getValue(1);
880
881 // Handle result values, copying them out of physregs into vregs that we
882 // return.
883 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
884 InVals, OutVals, Callee);
885}
886
887static bool getIndexedAddressParts(SDNode *Ptr, EVT VT,
888 bool isSEXTLoad, SDValue &Base,
889 SDValue &Offset, bool &isInc,
890 SelectionDAG &DAG) {
891 if (Ptr->getOpcode() != ISD::ADD)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000892 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000893
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000894 auto &HST = static_cast<const HexagonSubtarget&>(DAG.getSubtarget());
895 bool UseHVX = HST.useHVXOps();
896 bool UseHVXDbl = HST.useHVXDblOps();
897
898 bool ValidHVXDblType =
899 (UseHVX && UseHVXDbl) && (VT == MVT::v32i32 || VT == MVT::v16i64 ||
900 VT == MVT::v64i16 || VT == MVT::v128i8);
901 bool ValidHVXType =
902 UseHVX && !UseHVXDbl && (VT == MVT::v16i32 || VT == MVT::v8i64 ||
903 VT == MVT::v32i16 || VT == MVT::v64i8);
904
905 if (ValidHVXDblType || ValidHVXType ||
906 VT == MVT::i64 || VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000907 isInc = (Ptr->getOpcode() == ISD::ADD);
908 Base = Ptr->getOperand(0);
909 Offset = Ptr->getOperand(1);
910 // Ensure that Offset is a constant.
911 return (isa<ConstantSDNode>(Offset));
912 }
913
914 return false;
915}
916
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000917/// getPostIndexedAddressParts - returns true by value, base pointer and
918/// offset pointer and addressing mode by reference if this node can be
919/// combined with a load / store to form a post-indexed load / store.
920bool HexagonTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
921 SDValue &Base,
922 SDValue &Offset,
923 ISD::MemIndexedMode &AM,
924 SelectionDAG &DAG) const
925{
926 EVT VT;
927 SDValue Ptr;
928 bool isSEXTLoad = false;
929
930 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
931 VT = LD->getMemoryVT();
932 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
933 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
934 VT = ST->getMemoryVT();
935 if (ST->getValue().getValueType() == MVT::i64 && ST->isTruncatingStore()) {
936 return false;
937 }
938 } else {
939 return false;
940 }
941
Chad Rosier64dc8aa2012-01-06 20:11:59 +0000942 bool isInc = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000943 bool isLegal = getIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
944 isInc, DAG);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000945 if (isLegal) {
946 auto &HII = *Subtarget.getInstrInfo();
947 int32_t OffsetVal = cast<ConstantSDNode>(Offset.getNode())->getSExtValue();
948 if (HII.isValidAutoIncImm(VT, OffsetVal)) {
949 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
950 return true;
951 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000952 }
953
954 return false;
955}
956
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000957SDValue
958HexagonTargetLowering::LowerINLINEASM(SDValue Op, SelectionDAG &DAG) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000959 SDNode *Node = Op.getNode();
960 MachineFunction &MF = DAG.getMachineFunction();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000961 auto &FuncInfo = *MF.getInfo<HexagonMachineFunctionInfo>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000962 switch (Node->getOpcode()) {
963 case ISD::INLINEASM: {
964 unsigned NumOps = Node->getNumOperands();
965 if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue)
966 --NumOps; // Ignore the flag operand.
967
968 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000969 if (FuncInfo.hasClobberLR())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000970 break;
971 unsigned Flags =
972 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
973 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
974 ++i; // Skip the ID value.
975
976 switch (InlineAsm::getKind(Flags)) {
977 default: llvm_unreachable("Bad flags!");
978 case InlineAsm::Kind_RegDef:
979 case InlineAsm::Kind_RegUse:
980 case InlineAsm::Kind_Imm:
981 case InlineAsm::Kind_Clobber:
982 case InlineAsm::Kind_Mem: {
983 for (; NumVals; --NumVals, ++i) {}
984 break;
985 }
986 case InlineAsm::Kind_RegDefEarlyClobber: {
987 for (; NumVals; --NumVals, ++i) {
988 unsigned Reg =
989 cast<RegisterSDNode>(Node->getOperand(i))->getReg();
990
991 // Check it to be lr
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000992 const HexagonRegisterInfo *QRI = Subtarget.getRegisterInfo();
Eric Christopherdbe1cb02014-06-27 00:13:52 +0000993 if (Reg == QRI->getRARegister()) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000994 FuncInfo.setHasClobberLR(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000995 break;
996 }
997 }
998 break;
999 }
1000 }
1001 }
1002 }
1003 } // Node->getOpcode
1004 return Op;
1005}
1006
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00001007// Need to transform ISD::PREFETCH into something that doesn't inherit
1008// all of the properties of ISD::PREFETCH, specifically SDNPMayLoad and
1009// SDNPMayStore.
1010SDValue HexagonTargetLowering::LowerPREFETCH(SDValue Op,
1011 SelectionDAG &DAG) const {
1012 SDValue Chain = Op.getOperand(0);
1013 SDValue Addr = Op.getOperand(1);
1014 // Lower it to DCFETCH($reg, #0). A "pat" will try to merge the offset in,
1015 // if the "reg" is fed by an "add".
1016 SDLoc DL(Op);
1017 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
1018 return DAG.getNode(HexagonISD::DCFETCH, DL, MVT::Other, Chain, Addr, Zero);
1019}
1020
1021SDValue HexagonTargetLowering::LowerINTRINSIC_VOID(SDValue Op,
1022 SelectionDAG &DAG) const {
1023 SDValue Chain = Op.getOperand(0);
1024 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1025 // Lower the hexagon_prefetch builtin to DCFETCH, as above.
1026 if (IntNo == Intrinsic::hexagon_prefetch) {
1027 SDValue Addr = Op.getOperand(2);
1028 SDLoc DL(Op);
1029 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
1030 return DAG.getNode(HexagonISD::DCFETCH, DL, MVT::Other, Chain, Addr, Zero);
1031 }
1032 return SDValue();
1033}
1034
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001035SDValue
1036HexagonTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
1037 SelectionDAG &DAG) const {
1038 SDValue Chain = Op.getOperand(0);
1039 SDValue Size = Op.getOperand(1);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001040 SDValue Align = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001041 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001042
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001043 ConstantSDNode *AlignConst = dyn_cast<ConstantSDNode>(Align);
1044 assert(AlignConst && "Non-constant Align in LowerDYNAMIC_STACKALLOC");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001045
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001046 unsigned A = AlignConst->getSExtValue();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001047 auto &HFI = *Subtarget.getFrameLowering();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001048 // "Zero" means natural stack alignment.
1049 if (A == 0)
1050 A = HFI.getStackAlignment();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001051
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001052 DEBUG({
Krzysztof Parzyszek9ee04e42015-04-22 17:19:44 +00001053 dbgs () << LLVM_FUNCTION_NAME << " Align: " << A << " Size: ";
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001054 Size.getNode()->dump(&DAG);
1055 dbgs() << "\n";
1056 });
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001057
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001058 SDValue AC = DAG.getConstant(A, dl, MVT::i32);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001059 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00001060 SDValue AA = DAG.getNode(HexagonISD::ALLOCA, dl, VTs, Chain, Size, AC);
Nirav Davebfdb4832016-06-23 17:52:57 +00001061
1062 DAG.ReplaceAllUsesOfValueWith(Op, AA);
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00001063 return AA;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001064}
1065
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001066SDValue HexagonTargetLowering::LowerFormalArguments(
1067 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
1068 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
1069 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001070
1071 MachineFunction &MF = DAG.getMachineFunction();
1072 MachineFrameInfo *MFI = MF.getFrameInfo();
1073 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001074 auto &FuncInfo = *MF.getInfo<HexagonMachineFunctionInfo>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001075
1076 // Assign locations to all of the incoming arguments.
1077 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001078 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1079 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001080
1081 CCInfo.AnalyzeFormalArguments(Ins, CC_Hexagon);
1082
1083 // For LLVM, in the case when returning a struct by value (>8byte),
1084 // the first argument is a pointer that points to the location on caller's
1085 // stack where the return value will be stored. For Hexagon, the location on
1086 // caller's stack is passed only when the struct size is smaller than (and
1087 // equal to) 8 bytes. If not, no address will be passed into callee and
1088 // callee return the result direclty through R0/R1.
1089
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001090 SmallVector<SDValue, 8> MemOps;
1091 bool UseHVX = Subtarget.useHVXOps(), UseHVXDbl = Subtarget.useHVXDblOps();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001092
1093 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1094 CCValAssign &VA = ArgLocs[i];
1095 ISD::ArgFlagsTy Flags = Ins[i].Flags;
1096 unsigned ObjSize;
1097 unsigned StackLocation;
1098 int FI;
1099
1100 if ( (VA.isRegLoc() && !Flags.isByVal())
1101 || (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() > 8)) {
1102 // Arguments passed in registers
1103 // 1. int, long long, ptr args that get allocated in register.
1104 // 2. Large struct that gets an register to put its address in.
1105 EVT RegVT = VA.getLocVT();
Sirish Pande69295b82012-05-10 20:20:25 +00001106 if (RegVT == MVT::i8 || RegVT == MVT::i16 ||
1107 RegVT == MVT::i32 || RegVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001108 unsigned VReg =
Craig Topperc7242e02012-04-20 07:30:17 +00001109 RegInfo.createVirtualRegister(&Hexagon::IntRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001110 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1111 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Colin LeMahieu4379d102015-01-28 22:08:16 +00001112 } else if (RegVT == MVT::i64 || RegVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001113 unsigned VReg =
Craig Topperc7242e02012-04-20 07:30:17 +00001114 RegInfo.createVirtualRegister(&Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001115 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1116 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001117
1118 // Single Vector
1119 } else if ((RegVT == MVT::v8i64 || RegVT == MVT::v16i32 ||
1120 RegVT == MVT::v32i16 || RegVT == MVT::v64i8)) {
1121 unsigned VReg =
1122 RegInfo.createVirtualRegister(&Hexagon::VectorRegsRegClass);
1123 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1124 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1125 } else if (UseHVX && UseHVXDbl &&
1126 ((RegVT == MVT::v16i64 || RegVT == MVT::v32i32 ||
1127 RegVT == MVT::v64i16 || RegVT == MVT::v128i8))) {
1128 unsigned VReg =
1129 RegInfo.createVirtualRegister(&Hexagon::VectorRegs128BRegClass);
1130 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1131 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1132
1133 // Double Vector
1134 } else if ((RegVT == MVT::v16i64 || RegVT == MVT::v32i32 ||
1135 RegVT == MVT::v64i16 || RegVT == MVT::v128i8)) {
1136 unsigned VReg =
1137 RegInfo.createVirtualRegister(&Hexagon::VecDblRegsRegClass);
1138 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1139 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1140 } else if (UseHVX && UseHVXDbl &&
1141 ((RegVT == MVT::v32i64 || RegVT == MVT::v64i32 ||
1142 RegVT == MVT::v128i16 || RegVT == MVT::v256i8))) {
1143 unsigned VReg =
1144 RegInfo.createVirtualRegister(&Hexagon::VecDblRegs128BRegClass);
1145 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1146 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1147 } else if (RegVT == MVT::v512i1 || RegVT == MVT::v1024i1) {
1148 assert(0 && "need to support VecPred regs");
1149 unsigned VReg =
1150 RegInfo.createVirtualRegister(&Hexagon::VecPredRegsRegClass);
1151 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1152 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001153 } else {
1154 assert (0);
1155 }
1156 } else if (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() <= 8) {
1157 assert (0 && "ByValSize must be bigger than 8 bytes");
1158 } else {
1159 // Sanity check.
1160 assert(VA.isMemLoc());
1161
1162 if (Flags.isByVal()) {
1163 // If it's a byval parameter, then we need to compute the
1164 // "real" size, not the size of the pointer.
1165 ObjSize = Flags.getByValSize();
1166 } else {
1167 ObjSize = VA.getLocVT().getStoreSizeInBits() >> 3;
1168 }
1169
1170 StackLocation = HEXAGON_LRFP_SIZE + VA.getLocMemOffset();
1171 // Create the frame index object for this incoming parameter...
1172 FI = MFI->CreateFixedObject(ObjSize, StackLocation, true);
1173
1174 // Create the SelectionDAG nodes cordl, responding to a load
1175 // from this parameter.
1176 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
1177
1178 if (Flags.isByVal()) {
1179 // If it's a pass-by-value aggregate, then do not dereference the stack
1180 // location. Instead, we should generate a reference to the stack
1181 // location.
1182 InVals.push_back(FIN);
1183 } else {
1184 InVals.push_back(DAG.getLoad(VA.getLocVT(), dl, Chain, FIN,
1185 MachinePointerInfo(), false, false,
1186 false, 0));
1187 }
1188 }
1189 }
1190
1191 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001192 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001193
1194 if (isVarArg) {
1195 // This will point to the next argument passed via stack.
1196 int FrameIndex = MFI->CreateFixedObject(Hexagon_PointerSize,
1197 HEXAGON_LRFP_SIZE +
1198 CCInfo.getNextStackOffset(),
1199 true);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001200 FuncInfo.setVarArgsFrameIndex(FrameIndex);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001201 }
1202
1203 return Chain;
1204}
1205
1206SDValue
1207HexagonTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
1208 // VASTART stores the address of the VarArgsFrameIndex slot into the
1209 // memory location argument.
1210 MachineFunction &MF = DAG.getMachineFunction();
1211 HexagonMachineFunctionInfo *QFI = MF.getInfo<HexagonMachineFunctionInfo>();
1212 SDValue Addr = DAG.getFrameIndex(QFI->getVarArgsFrameIndex(), MVT::i32);
1213 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001214 return DAG.getStore(Op.getOperand(0), SDLoc(Op), Addr,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001215 Op.getOperand(1), MachinePointerInfo(SV), false,
1216 false, 0);
1217}
1218
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001219// Creates a SPLAT instruction for a constant value VAL.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001220static SDValue createSplat(SelectionDAG &DAG, const SDLoc &dl, EVT VT,
1221 SDValue Val) {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001222 if (VT.getSimpleVT() == MVT::v4i8)
1223 return DAG.getNode(HexagonISD::VSPLATB, dl, VT, Val);
1224
1225 if (VT.getSimpleVT() == MVT::v4i16)
1226 return DAG.getNode(HexagonISD::VSPLATH, dl, VT, Val);
1227
1228 return SDValue();
1229}
1230
1231static bool isSExtFree(SDValue N) {
1232 // A sign-extend of a truncate of a sign-extend is free.
1233 if (N.getOpcode() == ISD::TRUNCATE &&
1234 N.getOperand(0).getOpcode() == ISD::AssertSext)
1235 return true;
1236 // We have sign-extended loads.
1237 if (N.getOpcode() == ISD::LOAD)
1238 return true;
1239 return false;
1240}
1241
1242SDValue HexagonTargetLowering::LowerCTPOP(SDValue Op, SelectionDAG &DAG) const {
1243 SDLoc dl(Op);
1244 SDValue InpVal = Op.getOperand(0);
1245 if (isa<ConstantSDNode>(InpVal)) {
1246 uint64_t V = cast<ConstantSDNode>(InpVal)->getZExtValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001247 return DAG.getTargetConstant(countPopulation(V), dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001248 }
1249 SDValue PopOut = DAG.getNode(HexagonISD::POPCOUNT, dl, MVT::i32, InpVal);
1250 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, PopOut);
1251}
1252
1253SDValue HexagonTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1254 SDLoc dl(Op);
1255
1256 SDValue LHS = Op.getOperand(0);
1257 SDValue RHS = Op.getOperand(1);
1258 SDValue Cmp = Op.getOperand(2);
1259 ISD::CondCode CC = cast<CondCodeSDNode>(Cmp)->get();
1260
1261 EVT VT = Op.getValueType();
1262 EVT LHSVT = LHS.getValueType();
1263 EVT RHSVT = RHS.getValueType();
1264
1265 if (LHSVT == MVT::v2i16) {
1266 assert(ISD::isSignedIntSetCC(CC) || ISD::isUnsignedIntSetCC(CC));
1267 unsigned ExtOpc = ISD::isSignedIntSetCC(CC) ? ISD::SIGN_EXTEND
1268 : ISD::ZERO_EXTEND;
1269 SDValue LX = DAG.getNode(ExtOpc, dl, MVT::v2i32, LHS);
1270 SDValue RX = DAG.getNode(ExtOpc, dl, MVT::v2i32, RHS);
1271 SDValue SC = DAG.getNode(ISD::SETCC, dl, MVT::v2i1, LX, RX, Cmp);
1272 return SC;
1273 }
1274
1275 // Treat all other vector types as legal.
1276 if (VT.isVector())
1277 return Op;
1278
1279 // Equals and not equals should use sign-extend, not zero-extend, since
1280 // we can represent small negative values in the compare instructions.
1281 // The LLVM default is to use zero-extend arbitrarily in these cases.
1282 if ((CC == ISD::SETEQ || CC == ISD::SETNE) &&
1283 (RHSVT == MVT::i8 || RHSVT == MVT::i16) &&
1284 (LHSVT == MVT::i8 || LHSVT == MVT::i16)) {
1285 ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS);
1286 if (C && C->getAPIntValue().isNegative()) {
1287 LHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, LHS);
1288 RHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, RHS);
1289 return DAG.getNode(ISD::SETCC, dl, Op.getValueType(),
1290 LHS, RHS, Op.getOperand(2));
1291 }
1292 if (isSExtFree(LHS) || isSExtFree(RHS)) {
1293 LHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, LHS);
1294 RHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, RHS);
1295 return DAG.getNode(ISD::SETCC, dl, Op.getValueType(),
1296 LHS, RHS, Op.getOperand(2));
1297 }
1298 }
1299 return SDValue();
1300}
1301
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001302SDValue
1303HexagonTargetLowering::LowerVSELECT(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001304 SDValue PredOp = Op.getOperand(0);
1305 SDValue Op1 = Op.getOperand(1), Op2 = Op.getOperand(2);
1306 EVT OpVT = Op1.getValueType();
1307 SDLoc DL(Op);
1308
1309 if (OpVT == MVT::v2i16) {
1310 SDValue X1 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op1);
1311 SDValue X2 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op2);
1312 SDValue SL = DAG.getNode(ISD::VSELECT, DL, MVT::v2i32, PredOp, X1, X2);
1313 SDValue TR = DAG.getNode(ISD::TRUNCATE, DL, MVT::v2i16, SL);
1314 return TR;
1315 }
1316
1317 return SDValue();
1318}
1319
1320// Handle only specific vector loads.
1321SDValue HexagonTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1322 EVT VT = Op.getValueType();
1323 SDLoc DL(Op);
1324 LoadSDNode *LoadNode = cast<LoadSDNode>(Op);
1325 SDValue Chain = LoadNode->getChain();
1326 SDValue Ptr = Op.getOperand(1);
1327 SDValue LoweredLoad;
1328 SDValue Result;
1329 SDValue Base = LoadNode->getBasePtr();
1330 ISD::LoadExtType Ext = LoadNode->getExtensionType();
1331 unsigned Alignment = LoadNode->getAlignment();
1332 SDValue LoadChain;
1333
1334 if(Ext == ISD::NON_EXTLOAD)
1335 Ext = ISD::ZEXTLOAD;
1336
1337 if (VT == MVT::v4i16) {
1338 if (Alignment == 2) {
1339 SDValue Loads[4];
1340 // Base load.
1341 Loads[0] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Base,
1342 LoadNode->getPointerInfo(), MVT::i16,
1343 LoadNode->isVolatile(),
1344 LoadNode->isNonTemporal(),
1345 LoadNode->isInvariant(),
1346 Alignment);
1347 // Base+2 load.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001348 SDValue Increment = DAG.getConstant(2, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001349 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1350 Loads[1] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
1351 LoadNode->getPointerInfo(), MVT::i16,
1352 LoadNode->isVolatile(),
1353 LoadNode->isNonTemporal(),
1354 LoadNode->isInvariant(),
1355 Alignment);
1356 // SHL 16, then OR base and base+2.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001357 SDValue ShiftAmount = DAG.getConstant(16, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001358 SDValue Tmp1 = DAG.getNode(ISD::SHL, DL, MVT::i32, Loads[1], ShiftAmount);
1359 SDValue Tmp2 = DAG.getNode(ISD::OR, DL, MVT::i32, Tmp1, Loads[0]);
1360 // Base + 4.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001361 Increment = DAG.getConstant(4, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001362 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1363 Loads[2] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
1364 LoadNode->getPointerInfo(), MVT::i16,
1365 LoadNode->isVolatile(),
1366 LoadNode->isNonTemporal(),
1367 LoadNode->isInvariant(),
1368 Alignment);
1369 // Base + 6.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001370 Increment = DAG.getConstant(6, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001371 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1372 Loads[3] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
1373 LoadNode->getPointerInfo(), MVT::i16,
1374 LoadNode->isVolatile(),
1375 LoadNode->isNonTemporal(),
1376 LoadNode->isInvariant(),
1377 Alignment);
1378 // SHL 16, then OR base+4 and base+6.
1379 Tmp1 = DAG.getNode(ISD::SHL, DL, MVT::i32, Loads[3], ShiftAmount);
1380 SDValue Tmp4 = DAG.getNode(ISD::OR, DL, MVT::i32, Tmp1, Loads[2]);
1381 // Combine to i64. This could be optimised out later if we can
1382 // affect reg allocation of this code.
1383 Result = DAG.getNode(HexagonISD::COMBINE, DL, MVT::i64, Tmp4, Tmp2);
1384 LoadChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
1385 Loads[0].getValue(1), Loads[1].getValue(1),
1386 Loads[2].getValue(1), Loads[3].getValue(1));
1387 } else {
1388 // Perform default type expansion.
1389 Result = DAG.getLoad(MVT::i64, DL, Chain, Ptr, LoadNode->getPointerInfo(),
1390 LoadNode->isVolatile(), LoadNode->isNonTemporal(),
1391 LoadNode->isInvariant(), LoadNode->getAlignment());
1392 LoadChain = Result.getValue(1);
1393 }
1394 } else
1395 llvm_unreachable("Custom lowering unsupported load");
1396
1397 Result = DAG.getNode(ISD::BITCAST, DL, VT, Result);
1398 // Since we pretend to lower a load, we need the original chain
1399 // info attached to the result.
1400 SDValue Ops[] = { Result, LoadChain };
1401
1402 return DAG.getMergeValues(Ops, DL);
1403}
1404
1405
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001406SDValue
Sirish Pande69295b82012-05-10 20:20:25 +00001407HexagonTargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
1408 EVT ValTy = Op.getValueType();
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001409 ConstantPoolSDNode *CPN = cast<ConstantPoolSDNode>(Op);
1410 unsigned Align = CPN->getAlignment();
1411 Reloc::Model RM = HTM.getRelocationModel();
1412 unsigned char TF = (RM == Reloc::PIC_) ? HexagonII::MO_PCREL : 0;
1413
1414 SDValue T;
1415 if (CPN->isMachineConstantPoolEntry())
1416 T = DAG.getTargetConstantPool(CPN->getMachineCPVal(), ValTy, Align, TF);
Sirish Pande69295b82012-05-10 20:20:25 +00001417 else
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001418 T = DAG.getTargetConstantPool(CPN->getConstVal(), ValTy, Align, TF);
1419 if (RM == Reloc::PIC_)
1420 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), ValTy, T);
1421 return DAG.getNode(HexagonISD::CP, SDLoc(Op), ValTy, T);
1422}
1423
1424SDValue
1425HexagonTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
1426 EVT VT = Op.getValueType();
1427 int Idx = cast<JumpTableSDNode>(Op)->getIndex();
1428 Reloc::Model RM = HTM.getRelocationModel();
1429 if (RM == Reloc::PIC_) {
1430 SDValue T = DAG.getTargetJumpTable(Idx, VT, HexagonII::MO_PCREL);
1431 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), VT, T);
1432 }
1433
1434 SDValue T = DAG.getTargetJumpTable(Idx, VT);
1435 return DAG.getNode(HexagonISD::JT, SDLoc(Op), VT, T);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001436}
1437
1438SDValue
1439HexagonTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001440 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001441 MachineFunction &MF = DAG.getMachineFunction();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001442 MachineFrameInfo &MFI = *MF.getFrameInfo();
1443 MFI.setReturnAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001444
Bill Wendling908bf812014-01-06 00:43:20 +00001445 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001446 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001447
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001448 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001449 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001450 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1451 if (Depth) {
1452 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001453 SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001454 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
1455 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
1456 MachinePointerInfo(), false, false, false, 0);
1457 }
1458
1459 // Return LR, which contains the return address. Mark it an implicit live-in.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001460 unsigned Reg = MF.addLiveIn(HRI.getRARegister(), getRegClassFor(MVT::i32));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001461 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
1462}
1463
1464SDValue
1465HexagonTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001466 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
1467 MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
1468 MFI.setFrameAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001469
1470 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001471 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001472 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1473 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001474 HRI.getFrameRegister(), VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001475 while (Depth--)
1476 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
1477 MachinePointerInfo(),
1478 false, false, false, 0);
1479 return FrameAddr;
1480}
1481
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001482SDValue
1483HexagonTargetLowering::LowerATOMIC_FENCE(SDValue Op, SelectionDAG& DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001484 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001485 return DAG.getNode(HexagonISD::BARRIER, dl, MVT::Other, Op.getOperand(0));
1486}
1487
1488
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001489SDValue
1490HexagonTargetLowering::LowerGLOBALADDRESS(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001491 SDLoc dl(Op);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001492 auto *GAN = cast<GlobalAddressSDNode>(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00001493 auto PtrVT = getPointerTy(DAG.getDataLayout());
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001494 auto *GV = GAN->getGlobal();
1495 int64_t Offset = GAN->getOffset();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001496
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001497 auto &HLOF = *HTM.getObjFileLowering();
1498 Reloc::Model RM = HTM.getRelocationModel();
1499
1500 if (RM == Reloc::Static) {
1501 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset);
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +00001502 if (HLOF.isGlobalInSmallSection(GV, HTM))
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001503 return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, GA);
1504 return DAG.getNode(HexagonISD::CONST32, dl, PtrVT, GA);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001505 }
1506
Rafael Espindola8474fdf2016-06-22 19:09:14 +00001507 const Triple &TargetTriple = HTM.getTargetTriple();
1508 bool UsePCRel = shouldAssumeDSOLocal(RM, TargetTriple, *GV->getParent(), GV);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001509 if (UsePCRel) {
1510 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset,
1511 HexagonII::MO_PCREL);
1512 return DAG.getNode(HexagonISD::AT_PCREL, dl, PtrVT, GA);
1513 }
1514
1515 // Use GOT index.
1516 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
1517 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, HexagonII::MO_GOT);
1518 SDValue Off = DAG.getConstant(Offset, dl, MVT::i32);
1519 return DAG.getNode(HexagonISD::AT_GOT, dl, PtrVT, GOT, GA, Off);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001520}
1521
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001522// Specifies that for loads and stores VT can be promoted to PromotedLdStVT.
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001523SDValue
1524HexagonTargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
1525 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001526 SDLoc dl(Op);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001527 EVT PtrVT = getPointerTy(DAG.getDataLayout());
1528
1529 Reloc::Model RM = HTM.getRelocationModel();
1530 if (RM == Reloc::Static) {
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001531 SDValue A = DAG.getTargetBlockAddress(BA, PtrVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001532 return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, A);
1533 }
1534
1535 SDValue A = DAG.getTargetBlockAddress(BA, PtrVT, 0, HexagonII::MO_PCREL);
1536 return DAG.getNode(HexagonISD::AT_PCREL, dl, PtrVT, A);
1537}
1538
1539SDValue
1540HexagonTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op, SelectionDAG &DAG)
1541 const {
1542 EVT PtrVT = getPointerTy(DAG.getDataLayout());
1543 SDValue GOTSym = DAG.getTargetExternalSymbol(HEXAGON_GOT_SYM_NAME, PtrVT,
1544 HexagonII::MO_PCREL);
1545 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), PtrVT, GOTSym);
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001546}
1547
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001548SDValue
1549HexagonTargetLowering::GetDynamicTLSAddr(SelectionDAG &DAG, SDValue Chain,
1550 GlobalAddressSDNode *GA, SDValue *InFlag, EVT PtrVT, unsigned ReturnReg,
1551 unsigned char OperandFlags) const {
1552 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1553 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1554 SDLoc dl(GA);
1555 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
1556 GA->getValueType(0),
1557 GA->getOffset(),
1558 OperandFlags);
1559 // Create Operands for the call.The Operands should have the following:
1560 // 1. Chain SDValue
1561 // 2. Callee which in this case is the Global address value.
1562 // 3. Registers live into the call.In this case its R0, as we
1563 // have just one argument to be passed.
1564 // 4. InFlag if there is any.
1565 // Note: The order is important.
1566
1567 if (InFlag) {
1568 SDValue Ops[] = { Chain, TGA,
1569 DAG.getRegister(Hexagon::R0, PtrVT), *InFlag };
1570 Chain = DAG.getNode(HexagonISD::CALLv3, dl, NodeTys, Ops);
1571 } else {
1572 SDValue Ops[] = { Chain, TGA, DAG.getRegister(Hexagon::R0, PtrVT)};
1573 Chain = DAG.getNode(HexagonISD::CALLv3, dl, NodeTys, Ops);
1574 }
1575
1576 // Inform MFI that function has calls.
1577 MFI->setAdjustsStack(true);
1578
1579 SDValue Flag = Chain.getValue(1);
1580 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
1581}
1582
1583//
1584// Lower using the intial executable model for TLS addresses
1585//
1586SDValue
1587HexagonTargetLowering::LowerToTLSInitialExecModel(GlobalAddressSDNode *GA,
1588 SelectionDAG &DAG) const {
1589 SDLoc dl(GA);
1590 int64_t Offset = GA->getOffset();
1591 auto PtrVT = getPointerTy(DAG.getDataLayout());
1592
1593 // Get the thread pointer.
1594 SDValue TP = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Hexagon::UGP, PtrVT);
1595
1596 Reloc::Model RM = HTM.getRelocationModel();
1597 unsigned char TF = (RM == Reloc::PIC_) ? HexagonII::MO_IEGOT
1598 : HexagonII::MO_IE;
1599
1600 // First generate the TLS symbol address
1601 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT,
1602 Offset, TF);
1603
1604 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1605
1606 if (RM == Reloc::PIC_) {
1607 // Generate the GOT pointer in case of position independent code
1608 SDValue GOT = LowerGLOBAL_OFFSET_TABLE(Sym, DAG);
1609
1610 // Add the TLS Symbol address to GOT pointer.This gives
1611 // GOT relative relocation for the symbol.
1612 Sym = DAG.getNode(ISD::ADD, dl, PtrVT, GOT, Sym);
1613 }
1614
1615 // Load the offset value for TLS symbol.This offset is relative to
1616 // thread pointer.
1617 SDValue LoadOffset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Sym,
1618 MachinePointerInfo(),
1619 false, false, false, 0);
1620
1621 // Address of the thread local variable is the add of thread
1622 // pointer and the offset of the variable.
1623 return DAG.getNode(ISD::ADD, dl, PtrVT, TP, LoadOffset);
1624}
1625
1626//
1627// Lower using the local executable model for TLS addresses
1628//
1629SDValue
1630HexagonTargetLowering::LowerToTLSLocalExecModel(GlobalAddressSDNode *GA,
1631 SelectionDAG &DAG) const {
1632 SDLoc dl(GA);
1633 int64_t Offset = GA->getOffset();
1634 auto PtrVT = getPointerTy(DAG.getDataLayout());
1635
1636 // Get the thread pointer.
1637 SDValue TP = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Hexagon::UGP, PtrVT);
1638 // Generate the TLS symbol address
1639 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT, Offset,
1640 HexagonII::MO_TPREL);
1641 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1642
1643 // Address of the thread local variable is the add of thread
1644 // pointer and the offset of the variable.
1645 return DAG.getNode(ISD::ADD, dl, PtrVT, TP, Sym);
1646}
1647
1648//
1649// Lower using the general dynamic model for TLS addresses
1650//
1651SDValue
1652HexagonTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
1653 SelectionDAG &DAG) const {
1654 SDLoc dl(GA);
1655 int64_t Offset = GA->getOffset();
1656 auto PtrVT = getPointerTy(DAG.getDataLayout());
1657
1658 // First generate the TLS symbol address
1659 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT, Offset,
1660 HexagonII::MO_GDGOT);
1661
1662 // Then, generate the GOT pointer
1663 SDValue GOT = LowerGLOBAL_OFFSET_TABLE(TGA, DAG);
1664
1665 // Add the TLS symbol and the GOT pointer
1666 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1667 SDValue Chain = DAG.getNode(ISD::ADD, dl, PtrVT, GOT, Sym);
1668
1669 // Copy over the argument to R0
1670 SDValue InFlag;
1671 Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, Hexagon::R0, Chain, InFlag);
1672 InFlag = Chain.getValue(1);
1673
1674 return GetDynamicTLSAddr(DAG, Chain, GA, &InFlag, PtrVT,
1675 Hexagon::R0, HexagonII::MO_GDPLT);
1676}
1677
1678//
1679// Lower TLS addresses.
1680//
1681// For now for dynamic models, we only support the general dynamic model.
1682//
1683SDValue
1684HexagonTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1685 SelectionDAG &DAG) const {
1686 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1687
1688 switch (HTM.getTLSModel(GA->getGlobal())) {
1689 case TLSModel::GeneralDynamic:
1690 case TLSModel::LocalDynamic:
1691 return LowerToTLSGeneralDynamicModel(GA, DAG);
1692 case TLSModel::InitialExec:
1693 return LowerToTLSInitialExecModel(GA, DAG);
1694 case TLSModel::LocalExec:
1695 return LowerToTLSLocalExecModel(GA, DAG);
1696 }
1697 llvm_unreachable("Bogus TLS model");
1698}
1699
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001700//===----------------------------------------------------------------------===//
1701// TargetLowering Implementation
1702//===----------------------------------------------------------------------===//
1703
Eric Christopherd737b762015-02-02 22:11:36 +00001704HexagonTargetLowering::HexagonTargetLowering(const TargetMachine &TM,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001705 const HexagonSubtarget &ST)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001706 : TargetLowering(TM), HTM(static_cast<const HexagonTargetMachine&>(TM)),
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001707 Subtarget(ST) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001708 bool IsV4 = !Subtarget.hasV5TOps();
1709 auto &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00001710 bool UseHVX = Subtarget.useHVXOps();
1711 bool UseHVXSgl = Subtarget.useHVXSglOps();
1712 bool UseHVXDbl = Subtarget.useHVXDblOps();
Sirish Pande69295b82012-05-10 20:20:25 +00001713
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001714 setPrefLoopAlignment(4);
1715 setPrefFunctionAlignment(4);
1716 setMinFunctionAlignment(2);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001717 setStackPointerRegisterToSaveRestore(HRI.getStackRegister());
1718
Krzysztof Parzyszekf228c952016-06-22 16:07:10 +00001719 setMaxAtomicSizeInBitsSupported(64);
1720 setMinCmpXchgSizeInBits(32);
1721
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001722 if (EnableHexSDNodeSched)
1723 setSchedulingPreference(Sched::VLIW);
1724 else
1725 setSchedulingPreference(Sched::Source);
1726
1727 // Limits for inline expansion of memcpy/memmove
1728 MaxStoresPerMemcpy = MaxStoresPerMemcpyCL;
1729 MaxStoresPerMemcpyOptSize = MaxStoresPerMemcpyOptSizeCL;
1730 MaxStoresPerMemmove = MaxStoresPerMemmoveCL;
1731 MaxStoresPerMemmoveOptSize = MaxStoresPerMemmoveOptSizeCL;
1732 MaxStoresPerMemset = MaxStoresPerMemsetCL;
1733 MaxStoresPerMemsetOptSize = MaxStoresPerMemsetOptSizeCL;
1734
1735 //
1736 // Set up register classes.
1737 //
1738
1739 addRegisterClass(MVT::i1, &Hexagon::PredRegsRegClass);
1740 addRegisterClass(MVT::v2i1, &Hexagon::PredRegsRegClass); // bbbbaaaa
1741 addRegisterClass(MVT::v4i1, &Hexagon::PredRegsRegClass); // ddccbbaa
1742 addRegisterClass(MVT::v8i1, &Hexagon::PredRegsRegClass); // hgfedcba
1743 addRegisterClass(MVT::i32, &Hexagon::IntRegsRegClass);
1744 addRegisterClass(MVT::v4i8, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001745 addRegisterClass(MVT::v2i16, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001746 addRegisterClass(MVT::i64, &Hexagon::DoubleRegsRegClass);
1747 addRegisterClass(MVT::v8i8, &Hexagon::DoubleRegsRegClass);
1748 addRegisterClass(MVT::v4i16, &Hexagon::DoubleRegsRegClass);
1749 addRegisterClass(MVT::v2i32, &Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001750
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001751 if (Subtarget.hasV5TOps()) {
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001752 addRegisterClass(MVT::f32, &Hexagon::IntRegsRegClass);
1753 addRegisterClass(MVT::f64, &Hexagon::DoubleRegsRegClass);
1754 }
Sirish Pande69295b82012-05-10 20:20:25 +00001755
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001756 if (Subtarget.hasV60TOps()) {
1757 if (Subtarget.useHVXSglOps()) {
1758 addRegisterClass(MVT::v64i8, &Hexagon::VectorRegsRegClass);
1759 addRegisterClass(MVT::v32i16, &Hexagon::VectorRegsRegClass);
1760 addRegisterClass(MVT::v16i32, &Hexagon::VectorRegsRegClass);
1761 addRegisterClass(MVT::v8i64, &Hexagon::VectorRegsRegClass);
1762 addRegisterClass(MVT::v128i8, &Hexagon::VecDblRegsRegClass);
1763 addRegisterClass(MVT::v64i16, &Hexagon::VecDblRegsRegClass);
1764 addRegisterClass(MVT::v32i32, &Hexagon::VecDblRegsRegClass);
1765 addRegisterClass(MVT::v16i64, &Hexagon::VecDblRegsRegClass);
1766 addRegisterClass(MVT::v512i1, &Hexagon::VecPredRegsRegClass);
1767 } else if (Subtarget.useHVXDblOps()) {
1768 addRegisterClass(MVT::v128i8, &Hexagon::VectorRegs128BRegClass);
1769 addRegisterClass(MVT::v64i16, &Hexagon::VectorRegs128BRegClass);
1770 addRegisterClass(MVT::v32i32, &Hexagon::VectorRegs128BRegClass);
1771 addRegisterClass(MVT::v16i64, &Hexagon::VectorRegs128BRegClass);
1772 addRegisterClass(MVT::v256i8, &Hexagon::VecDblRegs128BRegClass);
1773 addRegisterClass(MVT::v128i16, &Hexagon::VecDblRegs128BRegClass);
1774 addRegisterClass(MVT::v64i32, &Hexagon::VecDblRegs128BRegClass);
1775 addRegisterClass(MVT::v32i64, &Hexagon::VecDblRegs128BRegClass);
1776 addRegisterClass(MVT::v1024i1, &Hexagon::VecPredRegs128BRegClass);
1777 }
1778
1779 }
1780
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001781 //
1782 // Handling of scalar operations.
1783 //
1784 // All operations default to "legal", except:
1785 // - indexed loads and stores (pre-/post-incremented),
1786 // - ANY_EXTEND_VECTOR_INREG, ATOMIC_CMP_SWAP_WITH_SUCCESS, CONCAT_VECTORS,
1787 // ConstantFP, DEBUGTRAP, FCEIL, FCOPYSIGN, FEXP, FEXP2, FFLOOR, FGETSIGN,
1788 // FLOG, FLOG2, FLOG10, FMAXNUM, FMINNUM, FNEARBYINT, FRINT, FROUND, TRAP,
1789 // FTRUNC, PREFETCH, SIGN_EXTEND_VECTOR_INREG, ZERO_EXTEND_VECTOR_INREG,
1790 // which default to "expand" for at least one type.
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001791
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001792 // Misc operations.
1793 setOperationAction(ISD::ConstantFP, MVT::f32, Legal); // Default: expand
1794 setOperationAction(ISD::ConstantFP, MVT::f64, Legal); // Default: expand
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001795
1796 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001797 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001798 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001799 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
1800 setOperationAction(ISD::INLINEASM, MVT::Other, Custom);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00001801 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
1802 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001803 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001804 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001805 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001806 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001807
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001808 // Custom legalize GlobalAddress nodes into CONST32.
1809 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001810 setOperationAction(ISD::GlobalAddress, MVT::i8, Custom);
1811 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001812
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001813 // Hexagon needs to optimize cases with negative constants.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001814 setOperationAction(ISD::SETCC, MVT::i8, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001815 setOperationAction(ISD::SETCC, MVT::i16, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001816
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001817 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
1818 setOperationAction(ISD::VASTART, MVT::Other, Custom);
1819 setOperationAction(ISD::VAEND, MVT::Other, Expand);
1820 setOperationAction(ISD::VAARG, MVT::Other, Expand);
1821
1822 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
1823 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
1824 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
1825
1826 if (EmitJumpTables)
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001827 setMinimumJumpTableEntries(MinimumJumpTables);
Krzysztof Parzyszeka61f7da2016-01-13 21:43:13 +00001828 else
1829 setMinimumJumpTableEntries(INT_MAX);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001830 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001831
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001832 // Hexagon has instructions for add/sub with carry. The problem with
1833 // modeling these instructions is that they produce 2 results: Rdd and Px.
1834 // To model the update of Px, we will have to use Defs[p0..p3] which will
1835 // cause any predicate live range to spill. So, we pretend we dont't have
1836 // these instructions.
1837 setOperationAction(ISD::ADDE, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001838 setOperationAction(ISD::ADDE, MVT::i16, Expand);
1839 setOperationAction(ISD::ADDE, MVT::i32, Expand);
1840 setOperationAction(ISD::ADDE, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001841 setOperationAction(ISD::SUBE, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001842 setOperationAction(ISD::SUBE, MVT::i16, Expand);
1843 setOperationAction(ISD::SUBE, MVT::i32, Expand);
1844 setOperationAction(ISD::SUBE, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001845 setOperationAction(ISD::ADDC, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001846 setOperationAction(ISD::ADDC, MVT::i16, Expand);
1847 setOperationAction(ISD::ADDC, MVT::i32, Expand);
1848 setOperationAction(ISD::ADDC, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001849 setOperationAction(ISD::SUBC, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001850 setOperationAction(ISD::SUBC, MVT::i16, Expand);
1851 setOperationAction(ISD::SUBC, MVT::i32, Expand);
1852 setOperationAction(ISD::SUBC, MVT::i64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001853
Krzysztof Parzyszek2c4487d2015-04-13 20:37:01 +00001854 // Only add and sub that detect overflow are the saturating ones.
1855 for (MVT VT : MVT::integer_valuetypes()) {
1856 setOperationAction(ISD::UADDO, VT, Expand);
1857 setOperationAction(ISD::SADDO, VT, Expand);
1858 setOperationAction(ISD::USUBO, VT, Expand);
1859 setOperationAction(ISD::SSUBO, VT, Expand);
1860 }
1861
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001862 setOperationAction(ISD::CTLZ, MVT::i8, Promote);
1863 setOperationAction(ISD::CTLZ, MVT::i16, Promote);
1864 setOperationAction(ISD::CTTZ, MVT::i8, Promote);
1865 setOperationAction(ISD::CTTZ, MVT::i16, Promote);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001866
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001867 // In V5, popcount can count # of 1s in i64 but returns i32.
1868 // On V4 it will be expanded (set later).
1869 setOperationAction(ISD::CTPOP, MVT::i8, Promote);
1870 setOperationAction(ISD::CTPOP, MVT::i16, Promote);
1871 setOperationAction(ISD::CTPOP, MVT::i32, Promote);
1872 setOperationAction(ISD::CTPOP, MVT::i64, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001873
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001874 // We custom lower i64 to i64 mul, so that it is not considered as a legal
1875 // operation. There is a pattern that will match i64 mul and transform it
1876 // to a series of instructions.
1877 setOperationAction(ISD::MUL, MVT::i64, Expand);
Colin LeMahieude68b662015-02-05 21:13:25 +00001878 setOperationAction(ISD::MULHS, MVT::i64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001879
Benjamin Kramer62460692015-04-25 14:46:53 +00001880 for (unsigned IntExpOp :
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001881 { ISD::SDIV, ISD::UDIV, ISD::SREM, ISD::UREM,
1882 ISD::SDIVREM, ISD::UDIVREM, ISD::ROTL, ISD::ROTR,
1883 ISD::BSWAP, ISD::SHL_PARTS, ISD::SRA_PARTS, ISD::SRL_PARTS,
1884 ISD::SMUL_LOHI, ISD::UMUL_LOHI }) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001885 setOperationAction(IntExpOp, MVT::i32, Expand);
1886 setOperationAction(IntExpOp, MVT::i64, Expand);
1887 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001888
Benjamin Kramer62460692015-04-25 14:46:53 +00001889 for (unsigned FPExpOp :
1890 {ISD::FDIV, ISD::FREM, ISD::FSQRT, ISD::FSIN, ISD::FCOS, ISD::FSINCOS,
1891 ISD::FPOW, ISD::FCOPYSIGN}) {
1892 setOperationAction(FPExpOp, MVT::f32, Expand);
1893 setOperationAction(FPExpOp, MVT::f64, Expand);
1894 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001895
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001896 // No extending loads from i32.
1897 for (MVT VT : MVT::integer_valuetypes()) {
1898 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
1899 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
1900 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
1901 }
1902 // Turn FP truncstore into trunc + store.
1903 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
1904 // Turn FP extload into load/fextend.
1905 for (MVT VT : MVT::fp_valuetypes())
1906 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001907
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001908 // Expand BR_CC and SELECT_CC for all integer and fp types.
1909 for (MVT VT : MVT::integer_valuetypes()) {
1910 setOperationAction(ISD::BR_CC, VT, Expand);
1911 setOperationAction(ISD::SELECT_CC, VT, Expand);
1912 }
1913 for (MVT VT : MVT::fp_valuetypes()) {
1914 setOperationAction(ISD::BR_CC, VT, Expand);
1915 setOperationAction(ISD::SELECT_CC, VT, Expand);
1916 }
1917 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001918
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001919 //
1920 // Handling of vector operations.
1921 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001922
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001923 // Custom lower v4i16 load only. Let v4i16 store to be
1924 // promoted for now.
1925 promoteLdStType(MVT::v4i8, MVT::i32);
1926 promoteLdStType(MVT::v2i16, MVT::i32);
1927 promoteLdStType(MVT::v8i8, MVT::i64);
1928 promoteLdStType(MVT::v2i32, MVT::i64);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001929
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001930 setOperationAction(ISD::LOAD, MVT::v4i16, Custom);
1931 setOperationAction(ISD::STORE, MVT::v4i16, Promote);
1932 AddPromotedToType(ISD::LOAD, MVT::v4i16, MVT::i64);
1933 AddPromotedToType(ISD::STORE, MVT::v4i16, MVT::i64);
1934
1935 // Set the action for vector operations to "expand", then override it with
1936 // either "custom" or "legal" for specific cases.
Craig Topper26260942015-10-18 05:15:34 +00001937 static const unsigned VectExpOps[] = {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001938 // Integer arithmetic:
1939 ISD::ADD, ISD::SUB, ISD::MUL, ISD::SDIV, ISD::UDIV,
1940 ISD::SREM, ISD::UREM, ISD::SDIVREM, ISD::UDIVREM, ISD::ADDC,
1941 ISD::SUBC, ISD::SADDO, ISD::UADDO, ISD::SSUBO, ISD::USUBO,
1942 ISD::SMUL_LOHI, ISD::UMUL_LOHI,
1943 // Logical/bit:
1944 ISD::AND, ISD::OR, ISD::XOR, ISD::ROTL, ISD::ROTR,
Craig Topper33772c52016-04-28 03:34:31 +00001945 ISD::CTPOP, ISD::CTLZ, ISD::CTTZ,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001946 // Floating point arithmetic/math functions:
1947 ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FMA, ISD::FDIV,
1948 ISD::FREM, ISD::FNEG, ISD::FABS, ISD::FSQRT, ISD::FSIN,
1949 ISD::FCOS, ISD::FPOWI, ISD::FPOW, ISD::FLOG, ISD::FLOG2,
1950 ISD::FLOG10, ISD::FEXP, ISD::FEXP2, ISD::FCEIL, ISD::FTRUNC,
1951 ISD::FRINT, ISD::FNEARBYINT, ISD::FROUND, ISD::FFLOOR,
1952 ISD::FMINNUM, ISD::FMAXNUM, ISD::FSINCOS,
1953 // Misc:
1954 ISD::SELECT, ISD::ConstantPool,
1955 // Vector:
1956 ISD::BUILD_VECTOR, ISD::SCALAR_TO_VECTOR,
1957 ISD::EXTRACT_VECTOR_ELT, ISD::INSERT_VECTOR_ELT,
1958 ISD::EXTRACT_SUBVECTOR, ISD::INSERT_SUBVECTOR,
1959 ISD::CONCAT_VECTORS, ISD::VECTOR_SHUFFLE
1960 };
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001961
1962 for (MVT VT : MVT::vector_valuetypes()) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001963 for (unsigned VectExpOp : VectExpOps)
1964 setOperationAction(VectExpOp, VT, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001965
1966 // Expand all extended loads and truncating stores:
1967 for (MVT TargetVT : MVT::vector_valuetypes()) {
1968 setLoadExtAction(ISD::EXTLOAD, TargetVT, VT, Expand);
1969 setTruncStoreAction(VT, TargetVT, Expand);
1970 }
1971
1972 setOperationAction(ISD::SRA, VT, Custom);
1973 setOperationAction(ISD::SHL, VT, Custom);
1974 setOperationAction(ISD::SRL, VT, Custom);
1975 }
1976
1977 // Types natively supported:
Benjamin Kramer62460692015-04-25 14:46:53 +00001978 for (MVT NativeVT : {MVT::v2i1, MVT::v4i1, MVT::v8i1, MVT::v32i1, MVT::v64i1,
1979 MVT::v4i8, MVT::v8i8, MVT::v2i16, MVT::v4i16, MVT::v1i32,
1980 MVT::v2i32, MVT::v1i64}) {
1981 setOperationAction(ISD::BUILD_VECTOR, NativeVT, Custom);
1982 setOperationAction(ISD::EXTRACT_VECTOR_ELT, NativeVT, Custom);
1983 setOperationAction(ISD::INSERT_VECTOR_ELT, NativeVT, Custom);
1984 setOperationAction(ISD::EXTRACT_SUBVECTOR, NativeVT, Custom);
1985 setOperationAction(ISD::INSERT_SUBVECTOR, NativeVT, Custom);
1986 setOperationAction(ISD::CONCAT_VECTORS, NativeVT, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001987
Benjamin Kramer62460692015-04-25 14:46:53 +00001988 setOperationAction(ISD::ADD, NativeVT, Legal);
1989 setOperationAction(ISD::SUB, NativeVT, Legal);
1990 setOperationAction(ISD::MUL, NativeVT, Legal);
1991 setOperationAction(ISD::AND, NativeVT, Legal);
1992 setOperationAction(ISD::OR, NativeVT, Legal);
1993 setOperationAction(ISD::XOR, NativeVT, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001994 }
1995
1996 setOperationAction(ISD::SETCC, MVT::v2i16, Custom);
1997 setOperationAction(ISD::VSELECT, MVT::v2i16, Custom);
1998 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
1999 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002000 if (UseHVX) {
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002001 if (UseHVXSgl) {
2002 setOperationAction(ISD::CONCAT_VECTORS, MVT::v128i8, Custom);
2003 setOperationAction(ISD::CONCAT_VECTORS, MVT::v64i16, Custom);
2004 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i32, Custom);
2005 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i64, Custom);
2006 } else if (UseHVXDbl) {
2007 setOperationAction(ISD::CONCAT_VECTORS, MVT::v256i8, Custom);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002008 setOperationAction(ISD::CONCAT_VECTORS, MVT::v128i16, Custom);
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002009 setOperationAction(ISD::CONCAT_VECTORS, MVT::v64i32, Custom);
2010 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i64, Custom);
2011 } else {
2012 llvm_unreachable("Unrecognized HVX mode");
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002013 }
2014 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002015 // Subtarget-specific operation actions.
2016 //
2017 if (Subtarget.hasV5TOps()) {
2018 setOperationAction(ISD::FMA, MVT::f64, Expand);
2019 setOperationAction(ISD::FADD, MVT::f64, Expand);
2020 setOperationAction(ISD::FSUB, MVT::f64, Expand);
2021 setOperationAction(ISD::FMUL, MVT::f64, Expand);
2022
2023 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Promote);
2024 setOperationAction(ISD::FP_TO_UINT, MVT::i8, Promote);
2025 setOperationAction(ISD::FP_TO_UINT, MVT::i16, Promote);
2026 setOperationAction(ISD::FP_TO_SINT, MVT::i1, Promote);
2027 setOperationAction(ISD::FP_TO_SINT, MVT::i8, Promote);
2028 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
2029 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote);
2030 setOperationAction(ISD::UINT_TO_FP, MVT::i8, Promote);
2031 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote);
2032 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote);
2033 setOperationAction(ISD::SINT_TO_FP, MVT::i8, Promote);
2034 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
2035
2036 } else { // V4
2037 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
2038 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Expand);
2039 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
2040 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
2041 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Expand);
2042 setOperationAction(ISD::FP_TO_SINT, MVT::f32, Expand);
2043 setOperationAction(ISD::FP_EXTEND, MVT::f32, Expand);
2044 setOperationAction(ISD::FP_ROUND, MVT::f64, Expand);
2045 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
2046
2047 setOperationAction(ISD::CTPOP, MVT::i8, Expand);
2048 setOperationAction(ISD::CTPOP, MVT::i16, Expand);
2049 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
2050 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
2051
2052 // Expand these operations for both f32 and f64:
Benjamin Kramer62460692015-04-25 14:46:53 +00002053 for (unsigned FPExpOpV4 :
2054 {ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FABS, ISD::FNEG, ISD::FMA}) {
2055 setOperationAction(FPExpOpV4, MVT::f32, Expand);
2056 setOperationAction(FPExpOpV4, MVT::f64, Expand);
2057 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002058
Benjamin Kramer62460692015-04-25 14:46:53 +00002059 for (ISD::CondCode FPExpCCV4 :
2060 {ISD::SETOEQ, ISD::SETOGT, ISD::SETOLT, ISD::SETOGE, ISD::SETOLE,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002061 ISD::SETUO, ISD::SETO}) {
Benjamin Kramer62460692015-04-25 14:46:53 +00002062 setCondCodeAction(FPExpCCV4, MVT::f32, Expand);
2063 setCondCodeAction(FPExpCCV4, MVT::f64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002064 }
2065 }
2066
2067 // Handling of indexed loads/stores: default is "expand".
2068 //
Benjamin Kramer62460692015-04-25 14:46:53 +00002069 for (MVT LSXTy : {MVT::i8, MVT::i16, MVT::i32, MVT::i64}) {
2070 setIndexedLoadAction(ISD::POST_INC, LSXTy, Legal);
2071 setIndexedStoreAction(ISD::POST_INC, LSXTy, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002072 }
2073
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002074 if (UseHVXDbl) {
2075 for (MVT VT : {MVT::v128i8, MVT::v64i16, MVT::v32i32, MVT::v16i64}) {
2076 setIndexedLoadAction(ISD::POST_INC, VT, Legal);
2077 setIndexedStoreAction(ISD::POST_INC, VT, Legal);
2078 }
2079 }
2080
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002081 computeRegisterProperties(&HRI);
2082
2083 //
2084 // Library calls for unsupported operations
2085 //
2086 bool FastMath = EnableFastMath;
2087
Benjamin Kramera37c8092015-04-25 14:46:46 +00002088 setLibcallName(RTLIB::SDIV_I32, "__hexagon_divsi3");
2089 setLibcallName(RTLIB::SDIV_I64, "__hexagon_divdi3");
2090 setLibcallName(RTLIB::UDIV_I32, "__hexagon_udivsi3");
2091 setLibcallName(RTLIB::UDIV_I64, "__hexagon_udivdi3");
2092 setLibcallName(RTLIB::SREM_I32, "__hexagon_modsi3");
2093 setLibcallName(RTLIB::SREM_I64, "__hexagon_moddi3");
2094 setLibcallName(RTLIB::UREM_I32, "__hexagon_umodsi3");
2095 setLibcallName(RTLIB::UREM_I64, "__hexagon_umoddi3");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002096
Benjamin Kramera37c8092015-04-25 14:46:46 +00002097 setLibcallName(RTLIB::SINTTOFP_I128_F64, "__hexagon_floattidf");
2098 setLibcallName(RTLIB::SINTTOFP_I128_F32, "__hexagon_floattisf");
2099 setLibcallName(RTLIB::FPTOUINT_F32_I128, "__hexagon_fixunssfti");
2100 setLibcallName(RTLIB::FPTOUINT_F64_I128, "__hexagon_fixunsdfti");
2101 setLibcallName(RTLIB::FPTOSINT_F32_I128, "__hexagon_fixsfti");
2102 setLibcallName(RTLIB::FPTOSINT_F64_I128, "__hexagon_fixdfti");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002103
2104 if (IsV4) {
2105 // Handle single-precision floating point operations on V4.
Benjamin Kramera37c8092015-04-25 14:46:46 +00002106 if (FastMath) {
2107 setLibcallName(RTLIB::ADD_F32, "__hexagon_fast_addsf3");
2108 setLibcallName(RTLIB::SUB_F32, "__hexagon_fast_subsf3");
2109 setLibcallName(RTLIB::MUL_F32, "__hexagon_fast_mulsf3");
2110 setLibcallName(RTLIB::OGT_F32, "__hexagon_fast_gtsf2");
2111 setLibcallName(RTLIB::OLT_F32, "__hexagon_fast_ltsf2");
2112 // Double-precision compares.
2113 setLibcallName(RTLIB::OGT_F64, "__hexagon_fast_gtdf2");
2114 setLibcallName(RTLIB::OLT_F64, "__hexagon_fast_ltdf2");
2115 } else {
2116 setLibcallName(RTLIB::ADD_F32, "__hexagon_addsf3");
2117 setLibcallName(RTLIB::SUB_F32, "__hexagon_subsf3");
2118 setLibcallName(RTLIB::MUL_F32, "__hexagon_mulsf3");
2119 setLibcallName(RTLIB::OGT_F32, "__hexagon_gtsf2");
2120 setLibcallName(RTLIB::OLT_F32, "__hexagon_ltsf2");
2121 // Double-precision compares.
2122 setLibcallName(RTLIB::OGT_F64, "__hexagon_gtdf2");
2123 setLibcallName(RTLIB::OLT_F64, "__hexagon_ltdf2");
2124 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002125 }
2126
2127 // This is the only fast library function for sqrtd.
2128 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00002129 setLibcallName(RTLIB::SQRT_F64, "__hexagon_fast2_sqrtdf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002130
Benjamin Kramera37c8092015-04-25 14:46:46 +00002131 // Prefix is: nothing for "slow-math",
2132 // "fast2_" for V4 fast-math and V5+ fast-math double-precision
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002133 // (actually, keep fast-math and fast-math2 separate for now)
Benjamin Kramera37c8092015-04-25 14:46:46 +00002134 if (FastMath) {
2135 setLibcallName(RTLIB::ADD_F64, "__hexagon_fast_adddf3");
2136 setLibcallName(RTLIB::SUB_F64, "__hexagon_fast_subdf3");
2137 setLibcallName(RTLIB::MUL_F64, "__hexagon_fast_muldf3");
2138 setLibcallName(RTLIB::DIV_F64, "__hexagon_fast_divdf3");
2139 // Calling __hexagon_fast2_divsf3 with fast-math on V5 (ok).
2140 setLibcallName(RTLIB::DIV_F32, "__hexagon_fast_divsf3");
2141 } else {
2142 setLibcallName(RTLIB::ADD_F64, "__hexagon_adddf3");
2143 setLibcallName(RTLIB::SUB_F64, "__hexagon_subdf3");
2144 setLibcallName(RTLIB::MUL_F64, "__hexagon_muldf3");
2145 setLibcallName(RTLIB::DIV_F64, "__hexagon_divdf3");
2146 setLibcallName(RTLIB::DIV_F32, "__hexagon_divsf3");
2147 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002148
2149 if (Subtarget.hasV5TOps()) {
2150 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00002151 setLibcallName(RTLIB::SQRT_F32, "__hexagon_fast2_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002152 else
Benjamin Kramera37c8092015-04-25 14:46:46 +00002153 setLibcallName(RTLIB::SQRT_F32, "__hexagon_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002154 } else {
2155 // V4
Benjamin Kramera37c8092015-04-25 14:46:46 +00002156 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__hexagon_floatsisf");
2157 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__hexagon_floatsidf");
2158 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__hexagon_floatdisf");
2159 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__hexagon_floatdidf");
2160 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__hexagon_floatunsisf");
2161 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__hexagon_floatunsidf");
2162 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__hexagon_floatundisf");
2163 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__hexagon_floatundidf");
2164 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__hexagon_fixunssfsi");
2165 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__hexagon_fixunssfdi");
2166 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__hexagon_fixunsdfsi");
2167 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__hexagon_fixunsdfdi");
2168 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__hexagon_fixsfsi");
2169 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__hexagon_fixsfdi");
2170 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__hexagon_fixdfsi");
2171 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__hexagon_fixdfdi");
2172 setLibcallName(RTLIB::FPEXT_F32_F64, "__hexagon_extendsfdf2");
2173 setLibcallName(RTLIB::FPROUND_F64_F32, "__hexagon_truncdfsf2");
2174 setLibcallName(RTLIB::OEQ_F32, "__hexagon_eqsf2");
2175 setLibcallName(RTLIB::OEQ_F64, "__hexagon_eqdf2");
2176 setLibcallName(RTLIB::OGE_F32, "__hexagon_gesf2");
2177 setLibcallName(RTLIB::OGE_F64, "__hexagon_gedf2");
2178 setLibcallName(RTLIB::OLE_F32, "__hexagon_lesf2");
2179 setLibcallName(RTLIB::OLE_F64, "__hexagon_ledf2");
2180 setLibcallName(RTLIB::UNE_F32, "__hexagon_nesf2");
2181 setLibcallName(RTLIB::UNE_F64, "__hexagon_nedf2");
2182 setLibcallName(RTLIB::UO_F32, "__hexagon_unordsf2");
2183 setLibcallName(RTLIB::UO_F64, "__hexagon_unorddf2");
2184 setLibcallName(RTLIB::O_F32, "__hexagon_unordsf2");
2185 setLibcallName(RTLIB::O_F64, "__hexagon_unorddf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002186 }
2187
2188 // These cause problems when the shift amount is non-constant.
2189 setLibcallName(RTLIB::SHL_I128, nullptr);
2190 setLibcallName(RTLIB::SRL_I128, nullptr);
2191 setLibcallName(RTLIB::SRA_I128, nullptr);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002192}
2193
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002194
2195const char* HexagonTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00002196 switch ((HexagonISD::NodeType)Opcode) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002197 case HexagonISD::ALLOCA: return "HexagonISD::ALLOCA";
2198 case HexagonISD::ARGEXTEND: return "HexagonISD::ARGEXTEND";
2199 case HexagonISD::AT_GOT: return "HexagonISD::AT_GOT";
2200 case HexagonISD::AT_PCREL: return "HexagonISD::AT_PCREL";
2201 case HexagonISD::BARRIER: return "HexagonISD::BARRIER";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002202 case HexagonISD::CALLR: return "HexagonISD::CALLR";
2203 case HexagonISD::CALLv3nr: return "HexagonISD::CALLv3nr";
2204 case HexagonISD::CALLv3: return "HexagonISD::CALLv3";
2205 case HexagonISD::COMBINE: return "HexagonISD::COMBINE";
2206 case HexagonISD::CONST32_GP: return "HexagonISD::CONST32_GP";
2207 case HexagonISD::CONST32: return "HexagonISD::CONST32";
2208 case HexagonISD::CP: return "HexagonISD::CP";
2209 case HexagonISD::DCFETCH: return "HexagonISD::DCFETCH";
2210 case HexagonISD::EH_RETURN: return "HexagonISD::EH_RETURN";
2211 case HexagonISD::EXTRACTU: return "HexagonISD::EXTRACTU";
2212 case HexagonISD::EXTRACTURP: return "HexagonISD::EXTRACTURP";
2213 case HexagonISD::FCONST32: return "HexagonISD::FCONST32";
2214 case HexagonISD::INSERT: return "HexagonISD::INSERT";
2215 case HexagonISD::INSERTRP: return "HexagonISD::INSERTRP";
2216 case HexagonISD::JT: return "HexagonISD::JT";
2217 case HexagonISD::PACKHL: return "HexagonISD::PACKHL";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002218 case HexagonISD::POPCOUNT: return "HexagonISD::POPCOUNT";
2219 case HexagonISD::RET_FLAG: return "HexagonISD::RET_FLAG";
2220 case HexagonISD::SHUFFEB: return "HexagonISD::SHUFFEB";
2221 case HexagonISD::SHUFFEH: return "HexagonISD::SHUFFEH";
2222 case HexagonISD::SHUFFOB: return "HexagonISD::SHUFFOB";
2223 case HexagonISD::SHUFFOH: return "HexagonISD::SHUFFOH";
2224 case HexagonISD::TC_RETURN: return "HexagonISD::TC_RETURN";
2225 case HexagonISD::VCMPBEQ: return "HexagonISD::VCMPBEQ";
2226 case HexagonISD::VCMPBGT: return "HexagonISD::VCMPBGT";
2227 case HexagonISD::VCMPBGTU: return "HexagonISD::VCMPBGTU";
2228 case HexagonISD::VCMPHEQ: return "HexagonISD::VCMPHEQ";
2229 case HexagonISD::VCMPHGT: return "HexagonISD::VCMPHGT";
2230 case HexagonISD::VCMPHGTU: return "HexagonISD::VCMPHGTU";
2231 case HexagonISD::VCMPWEQ: return "HexagonISD::VCMPWEQ";
2232 case HexagonISD::VCMPWGT: return "HexagonISD::VCMPWGT";
2233 case HexagonISD::VCMPWGTU: return "HexagonISD::VCMPWGTU";
Krzysztof Parzyszekc168c012015-12-03 16:47:20 +00002234 case HexagonISD::VCOMBINE: return "HexagonISD::VCOMBINE";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002235 case HexagonISD::VSHLH: return "HexagonISD::VSHLH";
2236 case HexagonISD::VSHLW: return "HexagonISD::VSHLW";
2237 case HexagonISD::VSPLATB: return "HexagonISD::VSPLTB";
2238 case HexagonISD::VSPLATH: return "HexagonISD::VSPLATH";
2239 case HexagonISD::VSRAH: return "HexagonISD::VSRAH";
2240 case HexagonISD::VSRAW: return "HexagonISD::VSRAW";
2241 case HexagonISD::VSRLH: return "HexagonISD::VSRLH";
2242 case HexagonISD::VSRLW: return "HexagonISD::VSRLW";
2243 case HexagonISD::VSXTBH: return "HexagonISD::VSXTBH";
2244 case HexagonISD::VSXTBW: return "HexagonISD::VSXTBW";
Matthias Braund04893f2015-05-07 21:33:59 +00002245 case HexagonISD::OP_END: break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002246 }
Matthias Braund04893f2015-05-07 21:33:59 +00002247 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002248}
2249
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002250bool HexagonTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002251 EVT MTy1 = EVT::getEVT(Ty1);
2252 EVT MTy2 = EVT::getEVT(Ty2);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002253 if (!MTy1.isSimple() || !MTy2.isSimple())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002254 return false;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002255 return (MTy1.getSimpleVT() == MVT::i64) && (MTy2.getSimpleVT() == MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002256}
2257
2258bool HexagonTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002259 if (!VT1.isSimple() || !VT2.isSimple())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002260 return false;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002261 return (VT1.getSimpleVT() == MVT::i64) && (VT2.getSimpleVT() == MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002262}
2263
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002264// shouldExpandBuildVectorWithShuffles
2265// Should we expand the build vector with shuffles?
2266bool
2267HexagonTargetLowering::shouldExpandBuildVectorWithShuffles(EVT VT,
2268 unsigned DefinedValues) const {
2269
2270 // Hexagon vector shuffle operates on element sizes of bytes or halfwords
2271 EVT EltVT = VT.getVectorElementType();
2272 int EltBits = EltVT.getSizeInBits();
2273 if ((EltBits != 8) && (EltBits != 16))
2274 return false;
2275
2276 return TargetLowering::shouldExpandBuildVectorWithShuffles(VT, DefinedValues);
2277}
2278
2279// LowerVECTOR_SHUFFLE - Lower a vector shuffle (V1, V2, V3). V1 and
2280// V2 are the two vectors to select data from, V3 is the permutation.
2281static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
2282 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
2283 SDValue V1 = Op.getOperand(0);
2284 SDValue V2 = Op.getOperand(1);
2285 SDLoc dl(Op);
2286 EVT VT = Op.getValueType();
2287
Sanjay Patel57195842016-03-14 17:28:46 +00002288 if (V2.isUndef())
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002289 V2 = V1;
2290
2291 if (SVN->isSplat()) {
2292 int Lane = SVN->getSplatIndex();
2293 if (Lane == -1) Lane = 0;
2294
2295 // Test if V1 is a SCALAR_TO_VECTOR.
2296 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR)
2297 return createSplat(DAG, dl, VT, V1.getOperand(0));
2298
2299 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
2300 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
2301 // reaches it).
2302 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
2303 !isa<ConstantSDNode>(V1.getOperand(0))) {
2304 bool IsScalarToVector = true;
2305 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
Sanjay Patel75068522016-03-14 18:09:43 +00002306 if (!V1.getOperand(i).isUndef()) {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002307 IsScalarToVector = false;
2308 break;
2309 }
2310 if (IsScalarToVector)
2311 return createSplat(DAG, dl, VT, V1.getOperand(0));
2312 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002313 return createSplat(DAG, dl, VT, DAG.getConstant(Lane, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002314 }
2315
2316 // FIXME: We need to support more general vector shuffles. See
2317 // below the comment from the ARM backend that deals in the general
2318 // case with the vector shuffles. For now, let expand handle these.
2319 return SDValue();
2320
2321 // If the shuffle is not directly supported and it has 4 elements, use
2322 // the PerfectShuffle-generated table to synthesize it from other shuffles.
2323}
2324
2325// If BUILD_VECTOR has same base element repeated several times,
2326// report true.
2327static bool isCommonSplatElement(BuildVectorSDNode *BVN) {
2328 unsigned NElts = BVN->getNumOperands();
2329 SDValue V0 = BVN->getOperand(0);
2330
2331 for (unsigned i = 1, e = NElts; i != e; ++i) {
2332 if (BVN->getOperand(i) != V0)
2333 return false;
2334 }
2335 return true;
2336}
2337
2338// LowerVECTOR_SHIFT - Lower a vector shift. Try to convert
2339// <VT> = SHL/SRA/SRL <VT> by <VT> to Hexagon specific
2340// <VT> = SHL/SRA/SRL <VT> by <IT/i32>.
2341static SDValue LowerVECTOR_SHIFT(SDValue Op, SelectionDAG &DAG) {
2342 BuildVectorSDNode *BVN = 0;
2343 SDValue V1 = Op.getOperand(0);
2344 SDValue V2 = Op.getOperand(1);
2345 SDValue V3;
2346 SDLoc dl(Op);
2347 EVT VT = Op.getValueType();
2348
2349 if ((BVN = dyn_cast<BuildVectorSDNode>(V1.getNode())) &&
2350 isCommonSplatElement(BVN))
2351 V3 = V2;
2352 else if ((BVN = dyn_cast<BuildVectorSDNode>(V2.getNode())) &&
2353 isCommonSplatElement(BVN))
2354 V3 = V1;
2355 else
2356 return SDValue();
2357
2358 SDValue CommonSplat = BVN->getOperand(0);
2359 SDValue Result;
2360
2361 if (VT.getSimpleVT() == MVT::v4i16) {
2362 switch (Op.getOpcode()) {
2363 case ISD::SRA:
2364 Result = DAG.getNode(HexagonISD::VSRAH, dl, VT, V3, CommonSplat);
2365 break;
2366 case ISD::SHL:
2367 Result = DAG.getNode(HexagonISD::VSHLH, dl, VT, V3, CommonSplat);
2368 break;
2369 case ISD::SRL:
2370 Result = DAG.getNode(HexagonISD::VSRLH, dl, VT, V3, CommonSplat);
2371 break;
2372 default:
2373 return SDValue();
2374 }
2375 } else if (VT.getSimpleVT() == MVT::v2i32) {
2376 switch (Op.getOpcode()) {
2377 case ISD::SRA:
2378 Result = DAG.getNode(HexagonISD::VSRAW, dl, VT, V3, CommonSplat);
2379 break;
2380 case ISD::SHL:
2381 Result = DAG.getNode(HexagonISD::VSHLW, dl, VT, V3, CommonSplat);
2382 break;
2383 case ISD::SRL:
2384 Result = DAG.getNode(HexagonISD::VSRLW, dl, VT, V3, CommonSplat);
2385 break;
2386 default:
2387 return SDValue();
2388 }
2389 } else {
2390 return SDValue();
2391 }
2392
2393 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
2394}
2395
2396SDValue
2397HexagonTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
2398 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
2399 SDLoc dl(Op);
2400 EVT VT = Op.getValueType();
2401
2402 unsigned Size = VT.getSizeInBits();
2403
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002404 // Only handle vectors of 64 bits or shorter.
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002405 if (Size > 64)
2406 return SDValue();
2407
2408 APInt APSplatBits, APSplatUndef;
2409 unsigned SplatBitSize;
2410 bool HasAnyUndefs;
2411 unsigned NElts = BVN->getNumOperands();
2412
2413 // Try to generate a SPLAT instruction.
2414 if ((VT.getSimpleVT() == MVT::v4i8 || VT.getSimpleVT() == MVT::v4i16) &&
2415 (BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
2416 HasAnyUndefs, 0, true) && SplatBitSize <= 16)) {
2417 unsigned SplatBits = APSplatBits.getZExtValue();
2418 int32_t SextVal = ((int32_t) (SplatBits << (32 - SplatBitSize)) >>
2419 (32 - SplatBitSize));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002420 return createSplat(DAG, dl, VT, DAG.getConstant(SextVal, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002421 }
2422
2423 // Try to generate COMBINE to build v2i32 vectors.
2424 if (VT.getSimpleVT() == MVT::v2i32) {
2425 SDValue V0 = BVN->getOperand(0);
2426 SDValue V1 = BVN->getOperand(1);
2427
Sanjay Patel57195842016-03-14 17:28:46 +00002428 if (V0.isUndef())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002429 V0 = DAG.getConstant(0, dl, MVT::i32);
Sanjay Patel57195842016-03-14 17:28:46 +00002430 if (V1.isUndef())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002431 V1 = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002432
2433 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(V0);
2434 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(V1);
2435 // If the element isn't a constant, it is in a register:
2436 // generate a COMBINE Register Register instruction.
2437 if (!C0 || !C1)
2438 return DAG.getNode(HexagonISD::COMBINE, dl, VT, V1, V0);
2439
2440 // If one of the operands is an 8 bit integer constant, generate
2441 // a COMBINE Immediate Immediate instruction.
2442 if (isInt<8>(C0->getSExtValue()) ||
2443 isInt<8>(C1->getSExtValue()))
2444 return DAG.getNode(HexagonISD::COMBINE, dl, VT, V1, V0);
2445 }
2446
2447 // Try to generate a S2_packhl to build v2i16 vectors.
2448 if (VT.getSimpleVT() == MVT::v2i16) {
2449 for (unsigned i = 0, e = NElts; i != e; ++i) {
Sanjay Patel57195842016-03-14 17:28:46 +00002450 if (BVN->getOperand(i).isUndef())
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002451 continue;
2452 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(BVN->getOperand(i));
2453 // If the element isn't a constant, it is in a register:
2454 // generate a S2_packhl instruction.
2455 if (!Cst) {
2456 SDValue pack = DAG.getNode(HexagonISD::PACKHL, dl, MVT::v4i16,
2457 BVN->getOperand(1), BVN->getOperand(0));
2458
2459 return DAG.getTargetExtractSubreg(Hexagon::subreg_loreg, dl, MVT::v2i16,
2460 pack);
2461 }
2462 }
2463 }
2464
2465 // In the general case, generate a CONST32 or a CONST64 for constant vectors,
2466 // and insert_vector_elt for all the other cases.
2467 uint64_t Res = 0;
2468 unsigned EltSize = Size / NElts;
2469 SDValue ConstVal;
2470 uint64_t Mask = ~uint64_t(0ULL) >> (64 - EltSize);
2471 bool HasNonConstantElements = false;
2472
2473 for (unsigned i = 0, e = NElts; i != e; ++i) {
2474 // LLVM's BUILD_VECTOR operands are in Little Endian mode, whereas Hexagon's
2475 // combine, const64, etc. are Big Endian.
2476 unsigned OpIdx = NElts - i - 1;
2477 SDValue Operand = BVN->getOperand(OpIdx);
Sanjay Patel57195842016-03-14 17:28:46 +00002478 if (Operand.isUndef())
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002479 continue;
2480
2481 int64_t Val = 0;
2482 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Operand))
2483 Val = Cst->getSExtValue();
2484 else
2485 HasNonConstantElements = true;
2486
2487 Val &= Mask;
2488 Res = (Res << EltSize) | Val;
2489 }
2490
2491 if (Size == 64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002492 ConstVal = DAG.getConstant(Res, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002493 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002494 ConstVal = DAG.getConstant(Res, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002495
2496 // When there are non constant operands, add them with INSERT_VECTOR_ELT to
2497 // ConstVal, the constant part of the vector.
2498 if (HasNonConstantElements) {
2499 EVT EltVT = VT.getVectorElementType();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002500 SDValue Width = DAG.getConstant(EltVT.getSizeInBits(), dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002501 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002502 DAG.getConstant(32, dl, MVT::i64));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002503
2504 for (unsigned i = 0, e = NElts; i != e; ++i) {
2505 // LLVM's BUILD_VECTOR operands are in Little Endian mode, whereas Hexagon
2506 // is Big Endian.
2507 unsigned OpIdx = NElts - i - 1;
2508 SDValue Operand = BVN->getOperand(OpIdx);
Benjamin Kramer619c4e52015-04-10 11:24:51 +00002509 if (isa<ConstantSDNode>(Operand))
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002510 // This operand is already in ConstVal.
2511 continue;
2512
2513 if (VT.getSizeInBits() == 64 &&
2514 Operand.getValueType().getSizeInBits() == 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002515 SDValue C = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002516 Operand = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, Operand);
2517 }
2518
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002519 SDValue Idx = DAG.getConstant(OpIdx, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002520 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i64, Idx, Width);
2521 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2522 const SDValue Ops[] = {ConstVal, Operand, Combined};
2523
2524 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002525 ConstVal = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002526 else
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002527 ConstVal = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002528 }
2529 }
2530
2531 return DAG.getNode(ISD::BITCAST, dl, VT, ConstVal);
2532}
2533
2534SDValue
2535HexagonTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
2536 SelectionDAG &DAG) const {
2537 SDLoc dl(Op);
Krzysztof Parzyszekc168c012015-12-03 16:47:20 +00002538 bool UseHVX = Subtarget.useHVXOps();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002539 EVT VT = Op.getValueType();
2540 unsigned NElts = Op.getNumOperands();
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002541 SDValue Vec0 = Op.getOperand(0);
2542 EVT VecVT = Vec0.getValueType();
2543 unsigned Width = VecVT.getSizeInBits();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002544
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002545 if (NElts == 2) {
2546 MVT ST = VecVT.getSimpleVT();
2547 // We are trying to concat two v2i16 to a single v4i16, or two v4i8
2548 // into a single v8i8.
2549 if (ST == MVT::v2i16 || ST == MVT::v4i8)
2550 return DAG.getNode(HexagonISD::COMBINE, dl, VT, Op.getOperand(1), Vec0);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002551
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002552 if (UseHVX) {
2553 assert((Width == 64*8 && Subtarget.useHVXSglOps()) ||
2554 (Width == 128*8 && Subtarget.useHVXDblOps()));
2555 SDValue Vec1 = Op.getOperand(1);
2556 MVT OpTy = Subtarget.useHVXSglOps() ? MVT::v16i32 : MVT::v32i32;
2557 MVT ReTy = Subtarget.useHVXSglOps() ? MVT::v32i32 : MVT::v64i32;
2558 SDValue B0 = DAG.getNode(ISD::BITCAST, dl, OpTy, Vec0);
2559 SDValue B1 = DAG.getNode(ISD::BITCAST, dl, OpTy, Vec1);
2560 SDValue VC = DAG.getNode(HexagonISD::VCOMBINE, dl, ReTy, B1, B0);
2561 return DAG.getNode(ISD::BITCAST, dl, VT, VC);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002562 }
2563 }
2564
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002565 if (VT.getSizeInBits() != 32 && VT.getSizeInBits() != 64)
2566 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002567
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002568 SDValue C0 = DAG.getConstant(0, dl, MVT::i64);
2569 SDValue C32 = DAG.getConstant(32, dl, MVT::i64);
2570 SDValue W = DAG.getConstant(Width, dl, MVT::i64);
2571 // Create the "width" part of the argument to insert_rp/insertp_rp.
2572 SDValue S = DAG.getNode(ISD::SHL, dl, MVT::i64, W, C32);
2573 SDValue V = C0;
2574
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002575 for (unsigned i = 0, e = NElts; i != e; ++i) {
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002576 unsigned N = NElts-i-1;
2577 SDValue OpN = Op.getOperand(N);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002578
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002579 if (VT.getSizeInBits() == 64 && OpN.getValueType().getSizeInBits() == 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002580 SDValue C = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002581 OpN = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, OpN);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002582 }
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002583 SDValue Idx = DAG.getConstant(N, dl, MVT::i64);
2584 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i64, Idx, W);
2585 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, S, Offset);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002586 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002587 V = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, {V, OpN, Or});
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002588 else
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002589 V = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, {V, OpN, Or});
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002590 }
2591
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002592 return DAG.getNode(ISD::BITCAST, dl, VT, V);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002593}
2594
2595SDValue
2596HexagonTargetLowering::LowerEXTRACT_VECTOR(SDValue Op,
2597 SelectionDAG &DAG) const {
2598 EVT VT = Op.getValueType();
2599 int VTN = VT.isVector() ? VT.getVectorNumElements() : 1;
2600 SDLoc dl(Op);
2601 SDValue Idx = Op.getOperand(1);
2602 SDValue Vec = Op.getOperand(0);
2603 EVT VecVT = Vec.getValueType();
2604 EVT EltVT = VecVT.getVectorElementType();
2605 int EltSize = EltVT.getSizeInBits();
2606 SDValue Width = DAG.getConstant(Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT ?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002607 EltSize : VTN * EltSize, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002608
2609 // Constant element number.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002610 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Idx)) {
2611 uint64_t X = CI->getZExtValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002612 SDValue Offset = DAG.getConstant(X * EltSize, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002613 const SDValue Ops[] = {Vec, Width, Offset};
2614
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002615 ConstantSDNode *CW = dyn_cast<ConstantSDNode>(Width);
2616 assert(CW && "Non constant width in LowerEXTRACT_VECTOR");
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002617
2618 SDValue N;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002619 MVT SVT = VecVT.getSimpleVT();
2620 uint64_t W = CW->getZExtValue();
2621
2622 if (W == 32) {
2623 // Translate this node into EXTRACT_SUBREG.
2624 unsigned Subreg = (X == 0) ? Hexagon::subreg_loreg : 0;
2625
2626 if (X == 0)
2627 Subreg = Hexagon::subreg_loreg;
2628 else if (SVT == MVT::v2i32 && X == 1)
2629 Subreg = Hexagon::subreg_hireg;
2630 else if (SVT == MVT::v4i16 && X == 2)
2631 Subreg = Hexagon::subreg_hireg;
2632 else if (SVT == MVT::v8i8 && X == 4)
2633 Subreg = Hexagon::subreg_hireg;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002634 else
2635 llvm_unreachable("Bad offset");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002636 N = DAG.getTargetExtractSubreg(Subreg, dl, MVT::i32, Vec);
2637
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002638 } else if (VecVT.getSizeInBits() == 32) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002639 N = DAG.getNode(HexagonISD::EXTRACTU, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002640 } else {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002641 N = DAG.getNode(HexagonISD::EXTRACTU, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002642 if (VT.getSizeInBits() == 32)
2643 N = DAG.getTargetExtractSubreg(Hexagon::subreg_loreg, dl, MVT::i32, N);
2644 }
2645
2646 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2647 }
2648
2649 // Variable element number.
2650 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i32, Idx,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002651 DAG.getConstant(EltSize, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002652 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002653 DAG.getConstant(32, dl, MVT::i64));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002654 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2655
2656 const SDValue Ops[] = {Vec, Combined};
2657
2658 SDValue N;
2659 if (VecVT.getSizeInBits() == 32) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002660 N = DAG.getNode(HexagonISD::EXTRACTURP, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002661 } else {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002662 N = DAG.getNode(HexagonISD::EXTRACTURP, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002663 if (VT.getSizeInBits() == 32)
2664 N = DAG.getTargetExtractSubreg(Hexagon::subreg_loreg, dl, MVT::i32, N);
2665 }
2666 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2667}
2668
2669SDValue
2670HexagonTargetLowering::LowerINSERT_VECTOR(SDValue Op,
2671 SelectionDAG &DAG) const {
2672 EVT VT = Op.getValueType();
2673 int VTN = VT.isVector() ? VT.getVectorNumElements() : 1;
2674 SDLoc dl(Op);
2675 SDValue Vec = Op.getOperand(0);
2676 SDValue Val = Op.getOperand(1);
2677 SDValue Idx = Op.getOperand(2);
2678 EVT VecVT = Vec.getValueType();
2679 EVT EltVT = VecVT.getVectorElementType();
2680 int EltSize = EltVT.getSizeInBits();
2681 SDValue Width = DAG.getConstant(Op.getOpcode() == ISD::INSERT_VECTOR_ELT ?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002682 EltSize : VTN * EltSize, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002683
2684 if (ConstantSDNode *C = cast<ConstantSDNode>(Idx)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002685 SDValue Offset = DAG.getConstant(C->getSExtValue() * EltSize, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002686 const SDValue Ops[] = {Vec, Val, Width, Offset};
2687
2688 SDValue N;
2689 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002690 N = DAG.getNode(HexagonISD::INSERT, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002691 else
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002692 N = DAG.getNode(HexagonISD::INSERT, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002693
2694 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2695 }
2696
2697 // Variable element number.
2698 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i32, Idx,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002699 DAG.getConstant(EltSize, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002700 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002701 DAG.getConstant(32, dl, MVT::i64));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002702 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2703
2704 if (VT.getSizeInBits() == 64 &&
2705 Val.getValueType().getSizeInBits() == 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002706 SDValue C = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002707 Val = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, Val);
2708 }
2709
2710 const SDValue Ops[] = {Vec, Val, Combined};
2711
2712 SDValue N;
2713 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002714 N = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002715 else
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002716 N = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002717
2718 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2719}
2720
Tim Northovera4415852013-08-06 09:12:35 +00002721bool
2722HexagonTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
2723 // Assuming the caller does not have either a signext or zeroext modifier, and
2724 // only one value is accepted, any reasonable truncation is allowed.
2725 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
2726 return false;
2727
2728 // FIXME: in principle up to 64-bit could be made safe, but it would be very
2729 // fragile at the moment: any support for multiple value returns would be
2730 // liable to disallow tail calls involving i64 -> iN truncation in many cases.
2731 return Ty1->getPrimitiveSizeInBits() <= 32;
2732}
2733
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002734SDValue
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002735HexagonTargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
2736 SDValue Chain = Op.getOperand(0);
2737 SDValue Offset = Op.getOperand(1);
2738 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002739 SDLoc dl(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002740 auto PtrVT = getPointerTy(DAG.getDataLayout());
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002741
2742 // Mark function as containing a call to EH_RETURN.
2743 HexagonMachineFunctionInfo *FuncInfo =
2744 DAG.getMachineFunction().getInfo<HexagonMachineFunctionInfo>();
2745 FuncInfo->setHasEHReturn();
2746
2747 unsigned OffsetReg = Hexagon::R28;
2748
Mehdi Amini44ede332015-07-09 02:09:04 +00002749 SDValue StoreAddr =
2750 DAG.getNode(ISD::ADD, dl, PtrVT, DAG.getRegister(Hexagon::R30, PtrVT),
2751 DAG.getIntPtrConstant(4, dl));
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002752 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
2753 false, false, 0);
2754 Chain = DAG.getCopyToReg(Chain, dl, OffsetReg, Offset);
2755
2756 // Not needed we already use it as explict input to EH_RETURN.
2757 // MF.getRegInfo().addLiveOut(OffsetReg);
2758
2759 return DAG.getNode(HexagonISD::EH_RETURN, dl, MVT::Other, Chain);
2760}
2761
2762SDValue
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002763HexagonTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002764 unsigned Opc = Op.getOpcode();
2765 switch (Opc) {
2766 default:
2767#ifndef NDEBUG
2768 Op.getNode()->dumpr(&DAG);
2769 if (Opc > HexagonISD::OP_BEGIN && Opc < HexagonISD::OP_END)
2770 errs() << "Check for a non-legal type in this operation\n";
2771#endif
2772 llvm_unreachable("Should not custom lower this!");
2773 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
2774 case ISD::INSERT_SUBVECTOR: return LowerINSERT_VECTOR(Op, DAG);
2775 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR(Op, DAG);
2776 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_VECTOR(Op, DAG);
2777 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR(Op, DAG);
2778 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2779 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002780 case ISD::SRA:
2781 case ISD::SHL:
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002782 case ISD::SRL: return LowerVECTOR_SHIFT(Op, DAG);
2783 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002784 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002785 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
2786 // Frame & Return address. Currently unimplemented.
2787 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
2788 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00002789 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002790 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
2791 case ISD::GlobalAddress: return LowerGLOBALADDRESS(Op, DAG);
2792 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002793 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002794 case ISD::VASTART: return LowerVASTART(Op, DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002795 // Custom lower some vector loads.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002796 case ISD::LOAD: return LowerLOAD(Op, DAG);
2797 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
2798 case ISD::SETCC: return LowerSETCC(Op, DAG);
2799 case ISD::VSELECT: return LowerVSELECT(Op, DAG);
2800 case ISD::CTPOP: return LowerCTPOP(Op, DAG);
2801 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00002802 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002803 case ISD::INLINEASM: return LowerINLINEASM(Op, DAG);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00002804 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002805 }
2806}
2807
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002808/// Returns relocation base for the given PIC jumptable.
2809SDValue
2810HexagonTargetLowering::getPICJumpTableRelocBase(SDValue Table,
2811 SelectionDAG &DAG) const {
2812 int Idx = cast<JumpTableSDNode>(Table)->getIndex();
2813 EVT VT = Table.getValueType();
2814 SDValue T = DAG.getTargetJumpTable(Idx, VT, HexagonII::MO_PCREL);
2815 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Table), VT, T);
2816}
2817
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002818MachineBasicBlock *
2819HexagonTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
2820 MachineBasicBlock *BB)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00002821 const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002822 switch (MI->getOpcode()) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00002823 case Hexagon::ALLOCA: {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002824 MachineFunction *MF = BB->getParent();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00002825 auto *FuncInfo = MF->getInfo<HexagonMachineFunctionInfo>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002826 FuncInfo->addAllocaAdjustInst(MI);
2827 return BB;
2828 }
Craig Toppere55c5562012-02-07 02:50:20 +00002829 default: llvm_unreachable("Unexpected instr type to insert");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002830 } // switch
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002831}
2832
2833//===----------------------------------------------------------------------===//
2834// Inline Assembly Support
2835//===----------------------------------------------------------------------===//
2836
Krzysztof Parzyszekca3b5322016-05-18 14:34:51 +00002837TargetLowering::ConstraintType
2838HexagonTargetLowering::getConstraintType(StringRef Constraint) const {
2839 if (Constraint.size() == 1) {
2840 switch (Constraint[0]) {
2841 case 'q':
2842 case 'v':
2843 if (Subtarget.useHVXOps())
2844 return C_Register;
2845 break;
2846 }
2847 }
2848 return TargetLowering::getConstraintType(Constraint);
2849}
2850
Eric Christopher11e4df72015-02-26 22:38:43 +00002851std::pair<unsigned, const TargetRegisterClass *>
2852HexagonTargetLowering::getRegForInlineAsmConstraint(
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00002853 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002854 bool UseHVX = Subtarget.useHVXOps(), UseHVXDbl = Subtarget.useHVXDblOps();
2855
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002856 if (Constraint.size() == 1) {
2857 switch (Constraint[0]) {
2858 case 'r': // R0-R31
Chad Rosier295bd432013-06-22 18:37:38 +00002859 switch (VT.SimpleTy) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002860 default:
Craig Toppere55c5562012-02-07 02:50:20 +00002861 llvm_unreachable("getRegForInlineAsmConstraint Unhandled data type");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002862 case MVT::i32:
2863 case MVT::i16:
2864 case MVT::i8:
Sirish Pande69295b82012-05-10 20:20:25 +00002865 case MVT::f32:
Craig Topperc7242e02012-04-20 07:30:17 +00002866 return std::make_pair(0U, &Hexagon::IntRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002867 case MVT::i64:
Sirish Pande69295b82012-05-10 20:20:25 +00002868 case MVT::f64:
Craig Topperc7242e02012-04-20 07:30:17 +00002869 return std::make_pair(0U, &Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002870 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002871 case 'q': // q0-q3
2872 switch (VT.SimpleTy) {
2873 default:
2874 llvm_unreachable("getRegForInlineAsmConstraint Unhandled data type");
2875 case MVT::v1024i1:
2876 case MVT::v512i1:
2877 case MVT::v32i16:
2878 case MVT::v16i32:
2879 case MVT::v64i8:
2880 case MVT::v8i64:
2881 return std::make_pair(0U, &Hexagon::VecPredRegsRegClass);
2882 }
2883 case 'v': // V0-V31
2884 switch (VT.SimpleTy) {
2885 default:
2886 llvm_unreachable("getRegForInlineAsmConstraint Unhandled data type");
2887 case MVT::v16i32:
2888 case MVT::v32i16:
2889 case MVT::v64i8:
2890 case MVT::v8i64:
2891 return std::make_pair(0U, &Hexagon::VectorRegsRegClass);
2892 case MVT::v32i32:
2893 case MVT::v64i16:
2894 case MVT::v16i64:
2895 case MVT::v128i8:
2896 if (Subtarget.hasV60TOps() && UseHVX && UseHVXDbl)
2897 return std::make_pair(0U, &Hexagon::VectorRegs128BRegClass);
2898 else
2899 return std::make_pair(0U, &Hexagon::VecDblRegsRegClass);
2900 case MVT::v256i8:
2901 case MVT::v128i16:
2902 case MVT::v64i32:
2903 case MVT::v32i64:
2904 return std::make_pair(0U, &Hexagon::VecDblRegs128BRegClass);
2905 }
2906
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002907 default:
Craig Toppere55c5562012-02-07 02:50:20 +00002908 llvm_unreachable("Unknown asm register class");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002909 }
2910 }
2911
Eric Christopher11e4df72015-02-26 22:38:43 +00002912 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002913}
2914
Sirish Pande69295b82012-05-10 20:20:25 +00002915/// isFPImmLegal - Returns true if the target can instruction select the
2916/// specified FP immediate natively. If false, the legalizer will
2917/// materialize the FP immediate as a load from a constant pool.
2918bool HexagonTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002919 return Subtarget.hasV5TOps();
Sirish Pande69295b82012-05-10 20:20:25 +00002920}
2921
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002922/// isLegalAddressingMode - Return true if the addressing mode represented by
2923/// AM is legal for this target, for a load/store of the specified type.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +00002924bool HexagonTargetLowering::isLegalAddressingMode(const DataLayout &DL,
2925 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +00002926 unsigned AS) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002927 // Allows a signed-extended 11-bit immediate field.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002928 if (AM.BaseOffs <= -(1LL << 13) || AM.BaseOffs >= (1LL << 13)-1)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002929 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002930
2931 // No global is ever allowed as a base.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002932 if (AM.BaseGV)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002933 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002934
2935 int Scale = AM.Scale;
2936 if (Scale < 0) Scale = -Scale;
2937 switch (Scale) {
2938 case 0: // No scale reg, "r+i", "r", or just "i".
2939 break;
2940 default: // No scaled addressing mode.
2941 return false;
2942 }
2943 return true;
2944}
2945
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002946/// Return true if folding a constant offset with the given GlobalAddress is
2947/// legal. It is frequently not legal in PIC relocation models.
2948bool HexagonTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA)
2949 const {
2950 return HTM.getRelocationModel() == Reloc::Static;
2951}
2952
2953
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002954/// isLegalICmpImmediate - Return true if the specified immediate is legal
2955/// icmp immediate, that is the target has icmp instructions which can compare
2956/// a register against the immediate without having to materialize the
2957/// immediate into a register.
2958bool HexagonTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
2959 return Imm >= -512 && Imm <= 511;
2960}
2961
2962/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2963/// for tail call optimization. Targets which want to do tail call
2964/// optimization should implement this function.
2965bool HexagonTargetLowering::IsEligibleForTailCallOptimization(
2966 SDValue Callee,
2967 CallingConv::ID CalleeCC,
2968 bool isVarArg,
2969 bool isCalleeStructRet,
2970 bool isCallerStructRet,
2971 const SmallVectorImpl<ISD::OutputArg> &Outs,
2972 const SmallVectorImpl<SDValue> &OutVals,
2973 const SmallVectorImpl<ISD::InputArg> &Ins,
2974 SelectionDAG& DAG) const {
2975 const Function *CallerF = DAG.getMachineFunction().getFunction();
2976 CallingConv::ID CallerCC = CallerF->getCallingConv();
2977 bool CCMatch = CallerCC == CalleeCC;
2978
2979 // ***************************************************************************
2980 // Look for obvious safe cases to perform tail call optimization that do not
2981 // require ABI changes.
2982 // ***************************************************************************
2983
2984 // If this is a tail call via a function pointer, then don't do it!
Craig Topper66059c92015-11-18 07:07:59 +00002985 if (!(isa<GlobalAddressSDNode>(Callee)) &&
2986 !(isa<ExternalSymbolSDNode>(Callee))) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002987 return false;
2988 }
2989
2990 // Do not optimize if the calling conventions do not match.
2991 if (!CCMatch)
2992 return false;
2993
2994 // Do not tail call optimize vararg calls.
2995 if (isVarArg)
2996 return false;
2997
2998 // Also avoid tail call optimization if either caller or callee uses struct
2999 // return semantics.
3000 if (isCalleeStructRet || isCallerStructRet)
3001 return false;
3002
3003 // In addition to the cases above, we also disable Tail Call Optimization if
3004 // the calling convention code that at least one outgoing argument needs to
3005 // go on the stack. We cannot check that here because at this point that
3006 // information is not available.
3007 return true;
3008}
Colin LeMahieu025f8602014-12-08 21:19:18 +00003009
3010// Return true when the given node fits in a positive half word.
3011bool llvm::isPositiveHalfWord(SDNode *N) {
3012 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N);
3013 if (CN && CN->getSExtValue() > 0 && isInt<16>(CN->getSExtValue()))
3014 return true;
3015
3016 switch (N->getOpcode()) {
3017 default:
3018 return false;
3019 case ISD::SIGN_EXTEND_INREG:
3020 return true;
3021 }
3022}
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003023
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +00003024bool HexagonTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
3025 unsigned AS, unsigned Align, bool *Fast) const {
3026 if (Fast)
3027 *Fast = false;
3028
3029 switch (VT.getSimpleVT().SimpleTy) {
3030 default:
3031 return false;
3032 case MVT::v64i8:
3033 case MVT::v128i8:
3034 case MVT::v256i8:
3035 case MVT::v32i16:
3036 case MVT::v64i16:
3037 case MVT::v128i16:
3038 case MVT::v16i32:
3039 case MVT::v32i32:
3040 case MVT::v64i32:
3041 case MVT::v8i64:
3042 case MVT::v16i64:
3043 case MVT::v32i64:
3044 return true;
3045 }
3046 return false;
3047}
3048
3049
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003050std::pair<const TargetRegisterClass*, uint8_t>
3051HexagonTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
3052 MVT VT) const {
3053 const TargetRegisterClass *RRC = nullptr;
3054
3055 uint8_t Cost = 1;
3056 switch (VT.SimpleTy) {
3057 default:
3058 return TargetLowering::findRepresentativeClass(TRI, VT);
3059 case MVT::v64i8:
3060 case MVT::v32i16:
3061 case MVT::v16i32:
3062 case MVT::v8i64:
3063 RRC = &Hexagon::VectorRegsRegClass;
3064 break;
3065 case MVT::v128i8:
3066 case MVT::v64i16:
3067 case MVT::v32i32:
3068 case MVT::v16i64:
3069 if (Subtarget.hasV60TOps() && Subtarget.useHVXOps() &&
3070 Subtarget.useHVXDblOps())
3071 RRC = &Hexagon::VectorRegs128BRegClass;
3072 else
3073 RRC = &Hexagon::VecDblRegsRegClass;
3074 break;
3075 case MVT::v256i8:
3076 case MVT::v128i16:
3077 case MVT::v64i32:
3078 case MVT::v32i64:
3079 RRC = &Hexagon::VecDblRegs128BRegClass;
3080 break;
3081 }
3082 return std::make_pair(RRC, Cost);
3083}
3084
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003085Value *HexagonTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
3086 AtomicOrdering Ord) const {
3087 BasicBlock *BB = Builder.GetInsertBlock();
3088 Module *M = BB->getParent()->getParent();
3089 Type *Ty = cast<PointerType>(Addr->getType())->getElementType();
3090 unsigned SZ = Ty->getPrimitiveSizeInBits();
3091 assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic loads supported");
3092 Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_L2_loadw_locked
3093 : Intrinsic::hexagon_L4_loadd_locked;
3094 Value *Fn = Intrinsic::getDeclaration(M, IntID);
3095 return Builder.CreateCall(Fn, Addr, "larx");
3096}
3097
3098/// Perform a store-conditional operation to Addr. Return the status of the
3099/// store. This should be 0 if the store succeeded, non-zero otherwise.
3100Value *HexagonTargetLowering::emitStoreConditional(IRBuilder<> &Builder,
3101 Value *Val, Value *Addr, AtomicOrdering Ord) const {
3102 BasicBlock *BB = Builder.GetInsertBlock();
3103 Module *M = BB->getParent()->getParent();
3104 Type *Ty = Val->getType();
3105 unsigned SZ = Ty->getPrimitiveSizeInBits();
3106 assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic stores supported");
3107 Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_S2_storew_locked
3108 : Intrinsic::hexagon_S4_stored_locked;
3109 Value *Fn = Intrinsic::getDeclaration(M, IntID);
3110 Value *Call = Builder.CreateCall(Fn, {Addr, Val}, "stcx");
3111 Value *Cmp = Builder.CreateICmpEQ(Call, Builder.getInt32(0), "");
3112 Value *Ext = Builder.CreateZExt(Cmp, Type::getInt32Ty(M->getContext()));
3113 return Ext;
3114}
3115
Ahmed Bougacha52468672015-09-11 17:08:28 +00003116TargetLowering::AtomicExpansionKind
3117HexagonTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003118 // Do not expand loads and stores that don't exceed 64 bits.
Ahmed Bougacha52468672015-09-11 17:08:28 +00003119 return LI->getType()->getPrimitiveSizeInBits() > 64
Tim Northoverf520eff2015-12-02 18:12:57 +00003120 ? AtomicExpansionKind::LLOnly
Ahmed Bougacha52468672015-09-11 17:08:28 +00003121 : AtomicExpansionKind::None;
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003122}
3123
3124bool HexagonTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
3125 // Do not expand loads and stores that don't exceed 64 bits.
3126 return SI->getValueOperand()->getType()->getPrimitiveSizeInBits() > 64;
3127}
Krzysztof Parzyszekf228c952016-06-22 16:07:10 +00003128
3129bool HexagonTargetLowering::shouldExpandAtomicCmpXchgInIR(
3130 AtomicCmpXchgInst *AI) const {
3131 const DataLayout &DL = AI->getModule()->getDataLayout();
3132 unsigned Size = DL.getTypeStoreSize(AI->getCompareOperand()->getType());
3133 return Size >= 4 && Size <= 8;
3134}