blob: 2362ef8048fc8fa4f5ec5e81e0fd9fc1460ad5cb [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"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000020#include "llvm/CodeGen/CallingConvLower.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/MachineFunction.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
Craig Topperb25fda92012-03-17 18:46:09 +000024#include "llvm/CodeGen/MachineJumpTableInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000025#include "llvm/CodeGen/MachineRegisterInfo.h"
26#include "llvm/CodeGen/SelectionDAGISel.h"
27#include "llvm/CodeGen/ValueTypes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/CallingConv.h"
29#include "llvm/IR/DerivedTypes.h"
30#include "llvm/IR/Function.h"
31#include "llvm/IR/GlobalAlias.h"
32#include "llvm/IR/GlobalVariable.h"
33#include "llvm/IR/InlineAsm.h"
34#include "llvm/IR/Intrinsics.h"
NAKAMURA Takumi54eed762012-04-21 15:31:36 +000035#include "llvm/Support/CommandLine.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000036#include "llvm/Support/Debug.h"
37#include "llvm/Support/ErrorHandling.h"
NAKAMURA Takumie30303f2012-04-21 15:31:45 +000038#include "llvm/Support/raw_ostream.h"
NAKAMURA Takumi54eed762012-04-21 15:31:36 +000039
Craig Topperb25fda92012-03-17 18:46:09 +000040using namespace llvm;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000041
Chandler Carruthe96dd892014-04-21 22:55:11 +000042#define DEBUG_TYPE "hexagon-lowering"
43
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +000044static cl::opt<bool> EmitJumpTables("hexagon-emit-jump-tables",
45 cl::init(true), cl::Hidden,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +000046 cl::desc("Control jump table emission on Hexagon target"));
47
48static cl::opt<bool> EnableHexSDNodeSched("enable-hexagon-sdnode-sched",
49 cl::Hidden, cl::ZeroOrMore, cl::init(false),
50 cl::desc("Enable Hexagon SDNode scheduling"));
51
52static cl::opt<bool> EnableFastMath("ffast-math",
53 cl::Hidden, cl::ZeroOrMore, cl::init(false),
54 cl::desc("Enable Fast Math processing"));
55
56static cl::opt<int> MinimumJumpTables("minimum-jump-tables",
57 cl::Hidden, cl::ZeroOrMore, cl::init(5),
58 cl::desc("Set minimum jump tables"));
59
60static cl::opt<int> MaxStoresPerMemcpyCL("max-store-memcpy",
61 cl::Hidden, cl::ZeroOrMore, cl::init(6),
62 cl::desc("Max #stores to inline memcpy"));
63
64static cl::opt<int> MaxStoresPerMemcpyOptSizeCL("max-store-memcpy-Os",
65 cl::Hidden, cl::ZeroOrMore, cl::init(4),
66 cl::desc("Max #stores to inline memcpy"));
67
68static cl::opt<int> MaxStoresPerMemmoveCL("max-store-memmove",
69 cl::Hidden, cl::ZeroOrMore, cl::init(6),
70 cl::desc("Max #stores to inline memmove"));
71
72static cl::opt<int> MaxStoresPerMemmoveOptSizeCL("max-store-memmove-Os",
73 cl::Hidden, cl::ZeroOrMore, cl::init(4),
74 cl::desc("Max #stores to inline memmove"));
75
76static cl::opt<int> MaxStoresPerMemsetCL("max-store-memset",
77 cl::Hidden, cl::ZeroOrMore, cl::init(8),
78 cl::desc("Max #stores to inline memset"));
79
80static cl::opt<int> MaxStoresPerMemsetOptSizeCL("max-store-memset-Os",
81 cl::Hidden, cl::ZeroOrMore, cl::init(4),
82 cl::desc("Max #stores to inline memset"));
83
Tony Linthicum1213a7a2011-12-12 21:14:40 +000084
Benjamin Kramer602bb4a2013-10-27 11:16:09 +000085namespace {
86class HexagonCCState : public CCState {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +000087 unsigned NumNamedVarArgParams;
Benjamin Kramer602bb4a2013-10-27 11:16:09 +000088
89public:
90 HexagonCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
Eric Christopherb5217502014-08-06 18:45:26 +000091 SmallVectorImpl<CCValAssign> &locs, LLVMContext &C,
92 int NumNamedVarArgParams)
93 : CCState(CC, isVarArg, MF, locs, C),
Benjamin Kramer602bb4a2013-10-27 11:16:09 +000094 NumNamedVarArgParams(NumNamedVarArgParams) {}
95
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +000096 unsigned getNumNamedVarArgParams() const { return NumNamedVarArgParams; }
Benjamin Kramer602bb4a2013-10-27 11:16:09 +000097};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000098}
Tony Linthicum1213a7a2011-12-12 21:14:40 +000099
100// Implement calling convention for Hexagon.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000101
102static bool IsHvxVectorType(MVT ty);
103
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000104static bool
105CC_Hexagon(unsigned ValNo, MVT ValVT,
106 MVT LocVT, CCValAssign::LocInfo LocInfo,
107 ISD::ArgFlagsTy ArgFlags, CCState &State);
108
109static bool
110CC_Hexagon32(unsigned ValNo, MVT ValVT,
111 MVT LocVT, CCValAssign::LocInfo LocInfo,
112 ISD::ArgFlagsTy ArgFlags, CCState &State);
113
114static bool
115CC_Hexagon64(unsigned ValNo, MVT ValVT,
116 MVT LocVT, CCValAssign::LocInfo LocInfo,
117 ISD::ArgFlagsTy ArgFlags, CCState &State);
118
119static bool
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000120CC_HexagonVector(unsigned ValNo, MVT ValVT,
121 MVT LocVT, CCValAssign::LocInfo LocInfo,
122 ISD::ArgFlagsTy ArgFlags, CCState &State);
123
124static bool
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000125RetCC_Hexagon(unsigned ValNo, MVT ValVT,
126 MVT LocVT, CCValAssign::LocInfo LocInfo,
127 ISD::ArgFlagsTy ArgFlags, CCState &State);
128
129static bool
130RetCC_Hexagon32(unsigned ValNo, MVT ValVT,
131 MVT LocVT, CCValAssign::LocInfo LocInfo,
132 ISD::ArgFlagsTy ArgFlags, CCState &State);
133
134static bool
135RetCC_Hexagon64(unsigned ValNo, MVT ValVT,
136 MVT LocVT, CCValAssign::LocInfo LocInfo,
137 ISD::ArgFlagsTy ArgFlags, CCState &State);
138
139static bool
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000140RetCC_HexagonVector(unsigned ValNo, MVT ValVT,
141 MVT LocVT, CCValAssign::LocInfo LocInfo,
142 ISD::ArgFlagsTy ArgFlags, CCState &State);
143
144static bool
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000145CC_Hexagon_VarArg (unsigned ValNo, MVT ValVT,
146 MVT LocVT, CCValAssign::LocInfo LocInfo,
147 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000148 HexagonCCState &HState = static_cast<HexagonCCState &>(State);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000149
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000150 if (ValNo < HState.getNumNamedVarArgParams()) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000151 // Deal with named arguments.
152 return CC_Hexagon(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State);
153 }
154
155 // Deal with un-named arguments.
156 unsigned ofst;
157 if (ArgFlags.isByVal()) {
158 // If pass-by-value, the size allocated on stack is decided
159 // by ArgFlags.getByValSize(), not by the size of LocVT.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000160 ofst = State.AllocateStack(ArgFlags.getByValSize(),
161 ArgFlags.getByValAlign());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000162 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
163 return false;
164 }
Jyotsna Vermac7dcc2f2013-03-07 20:28:34 +0000165 if (LocVT == MVT::i1 || LocVT == MVT::i8 || LocVT == MVT::i16) {
166 LocVT = MVT::i32;
167 ValVT = MVT::i32;
168 if (ArgFlags.isSExt())
169 LocInfo = CCValAssign::SExt;
170 else if (ArgFlags.isZExt())
171 LocInfo = CCValAssign::ZExt;
172 else
173 LocInfo = CCValAssign::AExt;
174 }
Sirish Pande69295b82012-05-10 20:20:25 +0000175 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000176 ofst = State.AllocateStack(4, 4);
177 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
178 return false;
179 }
Sirish Pande69295b82012-05-10 20:20:25 +0000180 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000181 ofst = State.AllocateStack(8, 8);
182 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
183 return false;
184 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000185 if (LocVT == MVT::v2i64 || LocVT == MVT::v4i32 || LocVT == MVT::v8i16 ||
186 LocVT == MVT::v16i8) {
187 ofst = State.AllocateStack(16, 16);
188 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
189 return false;
190 }
191 if (LocVT == MVT::v4i64 || LocVT == MVT::v8i32 || LocVT == MVT::v16i16 ||
192 LocVT == MVT::v32i8) {
193 ofst = State.AllocateStack(32, 32);
194 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
195 return false;
196 }
197 if (LocVT == MVT::v8i64 || LocVT == MVT::v16i32 || LocVT == MVT::v32i16 ||
198 LocVT == MVT::v64i8 || LocVT == MVT::v512i1) {
199 ofst = State.AllocateStack(64, 64);
200 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
201 return false;
202 }
203 if (LocVT == MVT::v16i64 || LocVT == MVT::v32i32 || LocVT == MVT::v64i16 ||
204 LocVT == MVT::v128i8 || LocVT == MVT::v1024i1) {
205 ofst = State.AllocateStack(128, 128);
206 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
207 return false;
208 }
209 if (LocVT == MVT::v32i64 || LocVT == MVT::v64i32 || LocVT == MVT::v128i16 ||
210 LocVT == MVT::v256i8) {
211 ofst = State.AllocateStack(256, 256);
212 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
213 return false;
214 }
215
Craig Toppere73658d2014-04-28 04:05:08 +0000216 llvm_unreachable(nullptr);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000217}
218
219
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000220static bool CC_Hexagon (unsigned ValNo, MVT ValVT, MVT LocVT,
221 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, CCState &State) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000222 if (ArgFlags.isByVal()) {
223 // Passed on stack.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000224 unsigned Offset = State.AllocateStack(ArgFlags.getByValSize(),
225 ArgFlags.getByValAlign());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000226 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
227 return false;
228 }
229
230 if (LocVT == MVT::i1 || LocVT == MVT::i8 || LocVT == MVT::i16) {
231 LocVT = MVT::i32;
232 ValVT = MVT::i32;
233 if (ArgFlags.isSExt())
234 LocInfo = CCValAssign::SExt;
235 else if (ArgFlags.isZExt())
236 LocInfo = CCValAssign::ZExt;
237 else
238 LocInfo = CCValAssign::AExt;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000239 } else if (LocVT == MVT::v4i8 || LocVT == MVT::v2i16) {
240 LocVT = MVT::i32;
241 LocInfo = CCValAssign::BCvt;
242 } else if (LocVT == MVT::v8i8 || LocVT == MVT::v4i16 || LocVT == MVT::v2i32) {
243 LocVT = MVT::i64;
244 LocInfo = CCValAssign::BCvt;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000245 }
246
Sirish Pande69295b82012-05-10 20:20:25 +0000247 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000248 if (!CC_Hexagon32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
249 return false;
250 }
251
Sirish Pande69295b82012-05-10 20:20:25 +0000252 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000253 if (!CC_Hexagon64(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
254 return false;
255 }
256
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000257 if (LocVT == MVT::v8i32 || LocVT == MVT::v16i16 || LocVT == MVT::v32i8) {
258 unsigned Offset = State.AllocateStack(ArgFlags.getByValSize(), 32);
259 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
260 return false;
261 }
262
263 if (IsHvxVectorType(LocVT)) {
264 if (!CC_HexagonVector(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
265 return false;
266 }
267
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000268 return true; // CC didn't match.
269}
270
271
272static bool CC_Hexagon32(unsigned ValNo, MVT ValVT,
273 MVT LocVT, CCValAssign::LocInfo LocInfo,
274 ISD::ArgFlagsTy ArgFlags, CCState &State) {
275
Craig Topper840beec2014-04-04 05:16:06 +0000276 static const MCPhysReg RegList[] = {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000277 Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3, Hexagon::R4,
278 Hexagon::R5
279 };
Tim Northover3b6b7ca2015-02-21 02:11:17 +0000280 if (unsigned Reg = State.AllocateReg(RegList)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000281 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
282 return false;
283 }
284
285 unsigned Offset = State.AllocateStack(4, 4);
286 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
287 return false;
288}
289
290static bool CC_Hexagon64(unsigned ValNo, MVT ValVT,
291 MVT LocVT, CCValAssign::LocInfo LocInfo,
292 ISD::ArgFlagsTy ArgFlags, CCState &State) {
293
294 if (unsigned Reg = State.AllocateReg(Hexagon::D0)) {
295 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
296 return false;
297 }
298
Craig Topper840beec2014-04-04 05:16:06 +0000299 static const MCPhysReg RegList1[] = {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000300 Hexagon::D1, Hexagon::D2
301 };
Craig Topper840beec2014-04-04 05:16:06 +0000302 static const MCPhysReg RegList2[] = {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000303 Hexagon::R1, Hexagon::R3
304 };
Tim Northover3b6b7ca2015-02-21 02:11:17 +0000305 if (unsigned Reg = State.AllocateReg(RegList1, RegList2)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000306 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
307 return false;
308 }
309
310 unsigned Offset = State.AllocateStack(8, 8, Hexagon::D2);
311 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
312 return false;
313}
314
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000315static bool CC_HexagonVector(unsigned ValNo, MVT ValVT,
316 MVT LocVT, CCValAssign::LocInfo LocInfo,
317 ISD::ArgFlagsTy ArgFlags, CCState &State) {
318
Craig Toppere5e035a32015-12-05 07:13:35 +0000319 static const MCPhysReg VecLstS[] = { Hexagon::V0, Hexagon::V1,
320 Hexagon::V2, Hexagon::V3,
321 Hexagon::V4, Hexagon::V5,
322 Hexagon::V6, Hexagon::V7,
323 Hexagon::V8, Hexagon::V9,
324 Hexagon::V10, Hexagon::V11,
325 Hexagon::V12, Hexagon::V13,
326 Hexagon::V14, Hexagon::V15};
327 static const MCPhysReg VecLstD[] = { Hexagon::W0, Hexagon::W1,
328 Hexagon::W2, Hexagon::W3,
329 Hexagon::W4, Hexagon::W5,
330 Hexagon::W6, Hexagon::W7};
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000331 auto &MF = State.getMachineFunction();
332 auto &HST = MF.getSubtarget<HexagonSubtarget>();
333 bool UseHVX = HST.useHVXOps();
334 bool UseHVXDbl = HST.useHVXDblOps();
335
336 if ((UseHVX && !UseHVXDbl) &&
337 (LocVT == MVT::v8i64 || LocVT == MVT::v16i32 || LocVT == MVT::v32i16 ||
338 LocVT == MVT::v64i8 || LocVT == MVT::v512i1)) {
339 if (unsigned Reg = State.AllocateReg(VecLstS)) {
340 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
341 return false;
342 }
343 unsigned Offset = State.AllocateStack(64, 64);
344 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
345 return false;
346 }
347 if ((UseHVX && !UseHVXDbl) &&
348 (LocVT == MVT::v16i64 || LocVT == MVT::v32i32 || LocVT == MVT::v64i16 ||
349 LocVT == MVT::v128i8)) {
350 if (unsigned Reg = State.AllocateReg(VecLstD)) {
351 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
352 return false;
353 }
354 unsigned Offset = State.AllocateStack(128, 128);
355 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
356 return false;
357 }
358 // 128B Mode
359 if ((UseHVX && UseHVXDbl) &&
360 (LocVT == MVT::v32i64 || LocVT == MVT::v64i32 || LocVT == MVT::v128i16 ||
361 LocVT == MVT::v256i8)) {
362 if (unsigned Reg = State.AllocateReg(VecLstD)) {
363 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
364 return false;
365 }
366 unsigned Offset = State.AllocateStack(256, 256);
367 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
368 return false;
369 }
370 if ((UseHVX && UseHVXDbl) &&
371 (LocVT == MVT::v16i64 || LocVT == MVT::v32i32 || LocVT == MVT::v64i16 ||
372 LocVT == MVT::v128i8 || LocVT == MVT::v1024i1)) {
373 if (unsigned Reg = State.AllocateReg(VecLstS)) {
374 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
375 return false;
376 }
377 unsigned Offset = State.AllocateStack(128, 128);
378 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
379 return false;
380 }
381 return true;
382}
383
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000384static bool RetCC_Hexagon(unsigned ValNo, MVT ValVT,
385 MVT LocVT, CCValAssign::LocInfo LocInfo,
386 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000387 auto &MF = State.getMachineFunction();
388 auto &HST = MF.getSubtarget<HexagonSubtarget>();
389 bool UseHVX = HST.useHVXOps();
390 bool UseHVXDbl = HST.useHVXDblOps();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000391
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000392 if (LocVT == MVT::i1) {
393 // Return values of type MVT::i1 still need to be assigned to R0, but
394 // the value type needs to remain i1. LowerCallResult will deal with it,
395 // but it needs to recognize i1 as the value type.
396 LocVT = MVT::i32;
397 } else if (LocVT == MVT::i8 || LocVT == MVT::i16) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000398 LocVT = MVT::i32;
399 ValVT = MVT::i32;
400 if (ArgFlags.isSExt())
401 LocInfo = CCValAssign::SExt;
402 else if (ArgFlags.isZExt())
403 LocInfo = CCValAssign::ZExt;
404 else
405 LocInfo = CCValAssign::AExt;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000406 } else if (LocVT == MVT::v4i8 || LocVT == MVT::v2i16) {
407 LocVT = MVT::i32;
408 LocInfo = CCValAssign::BCvt;
409 } else if (LocVT == MVT::v8i8 || LocVT == MVT::v4i16 || LocVT == MVT::v2i32) {
410 LocVT = MVT::i64;
411 LocInfo = CCValAssign::BCvt;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000412 } else if (LocVT == MVT::v64i8 || LocVT == MVT::v32i16 ||
413 LocVT == MVT::v16i32 || LocVT == MVT::v8i64 ||
414 LocVT == MVT::v512i1) {
415 LocVT = MVT::v16i32;
416 ValVT = MVT::v16i32;
417 LocInfo = CCValAssign::Full;
418 } else if (LocVT == MVT::v128i8 || LocVT == MVT::v64i16 ||
419 LocVT == MVT::v32i32 || LocVT == MVT::v16i64 ||
420 (LocVT == MVT::v1024i1 && UseHVX && UseHVXDbl)) {
421 LocVT = MVT::v32i32;
422 ValVT = MVT::v32i32;
423 LocInfo = CCValAssign::Full;
424 } else if (LocVT == MVT::v256i8 || LocVT == MVT::v128i16 ||
425 LocVT == MVT::v64i32 || LocVT == MVT::v32i64) {
426 LocVT = MVT::v64i32;
427 ValVT = MVT::v64i32;
428 LocInfo = CCValAssign::Full;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000429 }
Sirish Pande69295b82012-05-10 20:20:25 +0000430 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000431 if (!RetCC_Hexagon32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
432 return false;
433 }
434
Sirish Pande69295b82012-05-10 20:20:25 +0000435 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000436 if (!RetCC_Hexagon64(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
437 return false;
438 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000439 if (LocVT == MVT::v16i32 || LocVT == MVT::v32i32 || LocVT == MVT::v64i32) {
440 if (!RetCC_HexagonVector(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
441 return false;
442 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000443 return true; // CC didn't match.
444}
445
446static bool RetCC_Hexagon32(unsigned ValNo, MVT ValVT,
447 MVT LocVT, CCValAssign::LocInfo LocInfo,
448 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Sirish Pande69295b82012-05-10 20:20:25 +0000449 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Krzysztof Parzyszek14412ef2016-07-18 17:36:46 +0000450 // Note that use of registers beyond R1 is not ABI compliant. However there
451 // are (experimental) IR passes which generate internal functions that
452 // return structs using these additional registers.
453 static const uint16_t RegList[] = { Hexagon::R0, Hexagon::R1,
454 Hexagon::R2, Hexagon::R3,
455 Hexagon::R4, Hexagon::R5};
456 if (unsigned Reg = State.AllocateReg(RegList)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000457 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
458 return false;
459 }
460 }
461
462 unsigned Offset = State.AllocateStack(4, 4);
463 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
464 return false;
465}
466
467static bool RetCC_Hexagon64(unsigned ValNo, MVT ValVT,
468 MVT LocVT, CCValAssign::LocInfo LocInfo,
469 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Sirish Pande69295b82012-05-10 20:20:25 +0000470 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000471 if (unsigned Reg = State.AllocateReg(Hexagon::D0)) {
472 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
473 return false;
474 }
475 }
476
477 unsigned Offset = State.AllocateStack(8, 8);
478 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
479 return false;
480}
481
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000482static bool RetCC_HexagonVector(unsigned ValNo, MVT ValVT,
483 MVT LocVT, CCValAssign::LocInfo LocInfo,
484 ISD::ArgFlagsTy ArgFlags, CCState &State) {
485 auto &MF = State.getMachineFunction();
486 auto &HST = MF.getSubtarget<HexagonSubtarget>();
487 bool UseHVX = HST.useHVXOps();
488 bool UseHVXDbl = HST.useHVXDblOps();
489
490 unsigned OffSiz = 64;
491 if (LocVT == MVT::v16i32) {
492 if (unsigned Reg = State.AllocateReg(Hexagon::V0)) {
493 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
494 return false;
495 }
496 } else if (LocVT == MVT::v32i32) {
497 unsigned Req = (UseHVX && UseHVXDbl) ? Hexagon::V0 : Hexagon::W0;
498 if (unsigned Reg = State.AllocateReg(Req)) {
499 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
500 return false;
501 }
502 OffSiz = 128;
503 } else if (LocVT == MVT::v64i32) {
504 if (unsigned Reg = State.AllocateReg(Hexagon::W0)) {
505 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
506 return false;
507 }
508 OffSiz = 256;
509 }
510
511 unsigned Offset = State.AllocateStack(OffSiz, OffSiz);
512 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
513 return false;
514}
515
Craig Topper18e69f42016-04-15 06:20:21 +0000516void HexagonTargetLowering::promoteLdStType(MVT VT, MVT PromotedLdStVT) {
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000517 if (VT != PromotedLdStVT) {
Craig Topper18e69f42016-04-15 06:20:21 +0000518 setOperationAction(ISD::LOAD, VT, Promote);
519 AddPromotedToType(ISD::LOAD, VT, PromotedLdStVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000520
Craig Topper18e69f42016-04-15 06:20:21 +0000521 setOperationAction(ISD::STORE, VT, Promote);
522 AddPromotedToType(ISD::STORE, VT, PromotedLdStVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000523 }
524}
525
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000526SDValue
527HexagonTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG)
528const {
529 return SDValue();
530}
531
532/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
533/// by "Src" to address "Dst" of size "Size". Alignment information is
534/// specified by the specific parameter attribute. The copy will be passed as
535/// a byval function parameter. Sometimes what we are copying is the end of a
536/// larger object, the part that does not fit in registers.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000537static SDValue CreateCopyOfByValArgument(SDValue Src, SDValue Dst,
538 SDValue Chain, ISD::ArgFlagsTy Flags,
539 SelectionDAG &DAG, const SDLoc &dl) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000540
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000541 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000542 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
543 /*isVolatile=*/false, /*AlwaysInline=*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +0000544 /*isTailCall=*/false,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000545 MachinePointerInfo(), MachinePointerInfo());
546}
547
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000548static bool IsHvxVectorType(MVT ty) {
549 return (ty == MVT::v8i64 || ty == MVT::v16i32 || ty == MVT::v32i16 ||
550 ty == MVT::v64i8 ||
551 ty == MVT::v16i64 || ty == MVT::v32i32 || ty == MVT::v64i16 ||
552 ty == MVT::v128i8 ||
553 ty == MVT::v32i64 || ty == MVT::v64i32 || ty == MVT::v128i16 ||
554 ty == MVT::v256i8 ||
555 ty == MVT::v512i1 || ty == MVT::v1024i1);
556}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000557
558// LowerReturn - Lower ISD::RET. If a struct is larger than 8 bytes and is
559// passed by value, the function prototype is modified to return void and
560// the value is stored in memory pointed by a pointer passed by caller.
561SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000562HexagonTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
563 bool isVarArg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000564 const SmallVectorImpl<ISD::OutputArg> &Outs,
565 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000566 const SDLoc &dl, SelectionDAG &DAG) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000567
568 // CCValAssign - represent the assignment of the return value to locations.
569 SmallVector<CCValAssign, 16> RVLocs;
570
571 // CCState - Info about the registers and stack slot.
Eric Christopherb5217502014-08-06 18:45:26 +0000572 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
573 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000574
575 // Analyze return values of ISD::RET
576 CCInfo.AnalyzeReturn(Outs, RetCC_Hexagon);
577
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000578 SDValue Flag;
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000579 SmallVector<SDValue, 4> RetOps(1, Chain);
580
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000581 // Copy the result values into the output registers.
582 for (unsigned i = 0; i != RVLocs.size(); ++i) {
583 CCValAssign &VA = RVLocs[i];
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000584
585 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), OutVals[i], Flag);
586
587 // Guarantee that all emitted copies are stuck together with flags.
588 Flag = Chain.getValue(1);
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000589 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000590 }
591
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000592 RetOps[0] = Chain; // Update chain.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000593
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000594 // Add the flag if we have it.
595 if (Flag.getNode())
596 RetOps.push_back(Flag);
597
Craig Topper48d114b2014-04-26 18:35:24 +0000598 return DAG.getNode(HexagonISD::RET_FLAG, dl, MVT::Other, RetOps);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000599}
600
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000601bool HexagonTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
602 // If either no tail call or told not to tail call at all, don't.
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000603 auto Attr =
604 CI->getParent()->getParent()->getFnAttribute("disable-tail-calls");
605 if (!CI->isTailCall() || Attr.getValueAsString() == "true")
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000606 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000607
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000608 return true;
609}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000610
611/// LowerCallResult - Lower the result values of an ISD::CALL into the
612/// appropriate copies out of appropriate physical registers. This assumes that
613/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
614/// being lowered. Returns a SDNode with the same number of values as the
615/// ISD::CALL.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000616SDValue HexagonTargetLowering::LowerCallResult(
617 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool isVarArg,
618 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
619 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals,
620 const SmallVectorImpl<SDValue> &OutVals, SDValue Callee) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000621 // Assign locations to each value returned by this call.
622 SmallVector<CCValAssign, 16> RVLocs;
623
Eric Christopherb5217502014-08-06 18:45:26 +0000624 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
625 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000626
627 CCInfo.AnalyzeCallResult(Ins, RetCC_Hexagon);
628
629 // Copy all of the result registers out of their specified physreg.
630 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000631 SDValue RetVal;
632 if (RVLocs[i].getValVT() == MVT::i1) {
633 // Return values of type MVT::i1 require special handling. The reason
634 // is that MVT::i1 is associated with the PredRegs register class, but
635 // values of that type are still returned in R0. Generate an explicit
636 // copy into a predicate register from R0, and treat the value of the
637 // predicate register as the call result.
638 auto &MRI = DAG.getMachineFunction().getRegInfo();
639 SDValue FR0 = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
640 MVT::i32, InFlag);
641 // FR0 = (Value, Chain, Glue)
642 unsigned PredR = MRI.createVirtualRegister(&Hexagon::PredRegsRegClass);
643 SDValue TPR = DAG.getCopyToReg(FR0.getValue(1), dl, PredR,
644 FR0.getValue(0), FR0.getValue(2));
645 // TPR = (Chain, Glue)
646 RetVal = DAG.getCopyFromReg(TPR.getValue(0), dl, PredR, MVT::i1,
647 TPR.getValue(1));
648 } else {
649 RetVal = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
650 RVLocs[i].getValVT(), InFlag);
651 }
652 InVals.push_back(RetVal.getValue(0));
653 Chain = RetVal.getValue(1);
654 InFlag = RetVal.getValue(2);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000655 }
656
657 return Chain;
658}
659
660/// LowerCall - Functions arguments are copied from virtual regs to
661/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
662SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +0000663HexagonTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000664 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +0000665 SelectionDAG &DAG = CLI.DAG;
Craig Topperb94011f2013-07-14 04:42:23 +0000666 SDLoc &dl = CLI.DL;
667 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
668 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
669 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000670 SDValue Chain = CLI.Chain;
671 SDValue Callee = CLI.Callee;
672 bool &isTailCall = CLI.IsTailCall;
673 CallingConv::ID CallConv = CLI.CallConv;
674 bool isVarArg = CLI.IsVarArg;
Colin LeMahieu2e3a26d2015-01-16 17:05:27 +0000675 bool doesNotReturn = CLI.DoesNotReturn;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000676
677 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000678 MachineFunction &MF = DAG.getMachineFunction();
Mehdi Amini44ede332015-07-09 02:09:04 +0000679 auto PtrVT = getPointerTy(MF.getDataLayout());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000680
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000681 // Check for varargs.
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000682 int NumNamedVarArgParams = -1;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000683 if (GlobalAddressSDNode *GAN = dyn_cast<GlobalAddressSDNode>(Callee)) {
684 const GlobalValue *GV = GAN->getGlobal();
685 Callee = DAG.getTargetGlobalAddress(GV, dl, MVT::i32);
686 if (const Function* F = dyn_cast<Function>(GV)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000687 // If a function has zero args and is a vararg function, that's
688 // disallowed so it must be an undeclared function. Do not assume
689 // varargs if the callee is undefined.
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000690 if (F->isVarArg() && F->getFunctionType()->getNumParams() != 0)
691 NumNamedVarArgParams = F->getFunctionType()->getNumParams();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000692 }
693 }
694
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000695 // Analyze operands of the call, assigning locations to each operand.
696 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000697 HexagonCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
698 *DAG.getContext(), NumNamedVarArgParams);
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000699
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000700 if (isVarArg)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000701 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon_VarArg);
702 else
703 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon);
704
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000705 auto Attr = MF.getFunction()->getFnAttribute("disable-tail-calls");
706 if (Attr.getValueAsString() == "true")
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000707 isTailCall = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000708
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000709 if (isTailCall) {
710 bool StructAttrFlag = MF.getFunction()->hasStructRetAttr();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000711 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
712 isVarArg, IsStructRet,
713 StructAttrFlag,
714 Outs, OutVals, Ins, DAG);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000715 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000716 CCValAssign &VA = ArgLocs[i];
717 if (VA.isMemLoc()) {
718 isTailCall = false;
719 break;
720 }
721 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000722 DEBUG(dbgs() << (isTailCall ? "Eligible for Tail Call\n"
723 : "Argument must be passed on stack. "
724 "Not eligible for Tail Call\n"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000725 }
726 // Get a count of how many bytes are to be pushed on the stack.
727 unsigned NumBytes = CCInfo.getNextStackOffset();
728 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
729 SmallVector<SDValue, 8> MemOpChains;
730
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000731 auto &HRI = *Subtarget.getRegisterInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +0000732 SDValue StackPtr =
733 DAG.getCopyFromReg(Chain, dl, HRI.getStackRegister(), PtrVT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000734
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000735 bool NeedsArgAlign = false;
736 unsigned LargestAlignSeen = 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000737 // Walk the register/memloc assignments, inserting copies/loads.
738 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
739 CCValAssign &VA = ArgLocs[i];
740 SDValue Arg = OutVals[i];
741 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000742 // Record if we need > 8 byte alignment on an argument.
743 bool ArgAlign = IsHvxVectorType(VA.getValVT());
744 NeedsArgAlign |= ArgAlign;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000745
746 // Promote the value if needed.
747 switch (VA.getLocInfo()) {
748 default:
749 // Loc info must be one of Full, SExt, ZExt, or AExt.
Craig Toppere55c5562012-02-07 02:50:20 +0000750 llvm_unreachable("Unknown loc info!");
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000751 case CCValAssign::BCvt:
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000752 case CCValAssign::Full:
753 break;
754 case CCValAssign::SExt:
755 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
756 break;
757 case CCValAssign::ZExt:
758 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
759 break;
760 case CCValAssign::AExt:
761 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
762 break;
763 }
764
765 if (VA.isMemLoc()) {
766 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000767 SDValue MemAddr = DAG.getConstant(LocMemOffset, dl,
768 StackPtr.getValueType());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000769 MemAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, MemAddr);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000770 if (ArgAlign)
771 LargestAlignSeen = std::max(LargestAlignSeen,
772 VA.getLocVT().getStoreSizeInBits() >> 3);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000773 if (Flags.isByVal()) {
774 // The argument is a struct passed by value. According to LLVM, "Arg"
775 // is is pointer.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000776 MemOpChains.push_back(CreateCopyOfByValArgument(Arg, MemAddr, Chain,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000777 Flags, DAG, dl));
778 } else {
Alex Lorenze40c8a22015-08-11 23:09:45 +0000779 MachinePointerInfo LocPI = MachinePointerInfo::getStack(
780 DAG.getMachineFunction(), LocMemOffset);
Justin Lebar9c375812016-07-15 18:27:10 +0000781 SDValue S = DAG.getStore(Chain, dl, Arg, MemAddr, LocPI);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000782 MemOpChains.push_back(S);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000783 }
784 continue;
785 }
786
787 // Arguments that can be passed on register must be kept at RegsToPass
788 // vector.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000789 if (VA.isRegLoc())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000790 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000791 }
792
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000793 if (NeedsArgAlign && Subtarget.hasV60TOps()) {
794 DEBUG(dbgs() << "Function needs byte stack align due to call args\n");
795 MachineFrameInfo* MFI = DAG.getMachineFunction().getFrameInfo();
796 // V6 vectors passed by value have 64 or 128 byte alignment depending
797 // on whether we are 64 byte vector mode or 128 byte.
798 bool UseHVXDbl = Subtarget.useHVXDblOps();
799 assert(Subtarget.useHVXOps());
800 const unsigned ObjAlign = UseHVXDbl ? 128 : 64;
801 LargestAlignSeen = std::max(LargestAlignSeen, ObjAlign);
802 MFI->ensureMaxAlignment(LargestAlignSeen);
803 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000804 // Transform all store nodes into one single node because all store
805 // nodes are independent of each other.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000806 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000807 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000808
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000809 if (!isTailCall) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000810 SDValue C = DAG.getConstant(NumBytes, dl, PtrVT, true);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000811 Chain = DAG.getCALLSEQ_START(Chain, C, dl);
812 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000813
814 // Build a sequence of copy-to-reg nodes chained together with token
815 // chain and flag operands which copy the outgoing args into registers.
Benjamin Kramerbde91762012-06-02 10:20:22 +0000816 // The InFlag in necessary since all emitted instructions must be
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000817 // stuck together.
818 SDValue InFlag;
819 if (!isTailCall) {
820 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
821 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
822 RegsToPass[i].second, InFlag);
823 InFlag = Chain.getValue(1);
824 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000825 } else {
826 // For tail calls lower the arguments to the 'real' stack slot.
827 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000828 // Force all the incoming stack arguments to be loaded from the stack
829 // before any new outgoing arguments are stored to the stack, because the
830 // outgoing stack slots may alias the incoming argument stack slots, and
831 // the alias isn't otherwise explicit. This is slightly more conservative
832 // than necessary, because it means that each store effectively depends
833 // on every argument instead of just those arguments it would clobber.
834 //
Benjamin Kramerbde91762012-06-02 10:20:22 +0000835 // Do not flag preceding copytoreg stuff together with the following stuff.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000836 InFlag = SDValue();
837 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
838 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
839 RegsToPass[i].second, InFlag);
840 InFlag = Chain.getValue(1);
841 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000842 InFlag = SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000843 }
844
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000845 bool LongCalls = MF.getSubtarget<HexagonSubtarget>().useLongCalls();
846 unsigned Flags = LongCalls ? HexagonII::HMOTF_ConstExtended : 0;
847
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000848 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
849 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
850 // node so that legalize doesn't hack it.
Tobias Edler von Kochb51460c2015-12-16 17:29:37 +0000851 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000852 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, PtrVT, 0, Flags);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000853 } else if (ExternalSymbolSDNode *S =
854 dyn_cast<ExternalSymbolSDNode>(Callee)) {
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000855 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), PtrVT, Flags);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000856 }
857
858 // Returns a chain & a flag for retval copy to use.
859 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
860 SmallVector<SDValue, 8> Ops;
861 Ops.push_back(Chain);
862 Ops.push_back(Callee);
863
864 // Add argument registers to the end of the list so that they are
865 // known live into the call.
866 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
867 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
868 RegsToPass[i].second.getValueType()));
869 }
870
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000871 if (InFlag.getNode())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000872 Ops.push_back(InFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000873
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +0000874 if (isTailCall) {
875 MF.getFrameInfo()->setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +0000876 return DAG.getNode(HexagonISD::TC_RETURN, dl, NodeTys, Ops);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +0000877 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000878
Colin LeMahieu2e3a26d2015-01-16 17:05:27 +0000879 int OpCode = doesNotReturn ? HexagonISD::CALLv3nr : HexagonISD::CALLv3;
880 Chain = DAG.getNode(OpCode, dl, NodeTys, Ops);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000881 InFlag = Chain.getValue(1);
882
883 // Create the CALLSEQ_END node.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000884 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
885 DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000886 InFlag = Chain.getValue(1);
887
888 // Handle result values, copying them out of physregs into vregs that we
889 // return.
890 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
891 InVals, OutVals, Callee);
892}
893
894static bool getIndexedAddressParts(SDNode *Ptr, EVT VT,
895 bool isSEXTLoad, SDValue &Base,
896 SDValue &Offset, bool &isInc,
897 SelectionDAG &DAG) {
898 if (Ptr->getOpcode() != ISD::ADD)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000899 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000900
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000901 auto &HST = static_cast<const HexagonSubtarget&>(DAG.getSubtarget());
902 bool UseHVX = HST.useHVXOps();
903 bool UseHVXDbl = HST.useHVXDblOps();
904
905 bool ValidHVXDblType =
906 (UseHVX && UseHVXDbl) && (VT == MVT::v32i32 || VT == MVT::v16i64 ||
907 VT == MVT::v64i16 || VT == MVT::v128i8);
908 bool ValidHVXType =
909 UseHVX && !UseHVXDbl && (VT == MVT::v16i32 || VT == MVT::v8i64 ||
910 VT == MVT::v32i16 || VT == MVT::v64i8);
911
912 if (ValidHVXDblType || ValidHVXType ||
913 VT == MVT::i64 || VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000914 isInc = (Ptr->getOpcode() == ISD::ADD);
915 Base = Ptr->getOperand(0);
916 Offset = Ptr->getOperand(1);
917 // Ensure that Offset is a constant.
918 return (isa<ConstantSDNode>(Offset));
919 }
920
921 return false;
922}
923
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000924/// getPostIndexedAddressParts - returns true by value, base pointer and
925/// offset pointer and addressing mode by reference if this node can be
926/// combined with a load / store to form a post-indexed load / store.
927bool HexagonTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
928 SDValue &Base,
929 SDValue &Offset,
930 ISD::MemIndexedMode &AM,
931 SelectionDAG &DAG) const
932{
933 EVT VT;
934 SDValue Ptr;
935 bool isSEXTLoad = false;
936
937 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
938 VT = LD->getMemoryVT();
939 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
940 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
941 VT = ST->getMemoryVT();
942 if (ST->getValue().getValueType() == MVT::i64 && ST->isTruncatingStore()) {
943 return false;
944 }
945 } else {
946 return false;
947 }
948
Chad Rosier64dc8aa2012-01-06 20:11:59 +0000949 bool isInc = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000950 bool isLegal = getIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
951 isInc, DAG);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000952 if (isLegal) {
953 auto &HII = *Subtarget.getInstrInfo();
954 int32_t OffsetVal = cast<ConstantSDNode>(Offset.getNode())->getSExtValue();
955 if (HII.isValidAutoIncImm(VT, OffsetVal)) {
956 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
957 return true;
958 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000959 }
960
961 return false;
962}
963
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000964SDValue
965HexagonTargetLowering::LowerINLINEASM(SDValue Op, SelectionDAG &DAG) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000966 SDNode *Node = Op.getNode();
967 MachineFunction &MF = DAG.getMachineFunction();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000968 auto &FuncInfo = *MF.getInfo<HexagonMachineFunctionInfo>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000969 switch (Node->getOpcode()) {
970 case ISD::INLINEASM: {
971 unsigned NumOps = Node->getNumOperands();
972 if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue)
973 --NumOps; // Ignore the flag operand.
974
975 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000976 if (FuncInfo.hasClobberLR())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000977 break;
978 unsigned Flags =
979 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
980 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
981 ++i; // Skip the ID value.
982
983 switch (InlineAsm::getKind(Flags)) {
984 default: llvm_unreachable("Bad flags!");
985 case InlineAsm::Kind_RegDef:
986 case InlineAsm::Kind_RegUse:
987 case InlineAsm::Kind_Imm:
988 case InlineAsm::Kind_Clobber:
989 case InlineAsm::Kind_Mem: {
990 for (; NumVals; --NumVals, ++i) {}
991 break;
992 }
993 case InlineAsm::Kind_RegDefEarlyClobber: {
994 for (; NumVals; --NumVals, ++i) {
995 unsigned Reg =
996 cast<RegisterSDNode>(Node->getOperand(i))->getReg();
997
998 // Check it to be lr
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000999 const HexagonRegisterInfo *QRI = Subtarget.getRegisterInfo();
Eric Christopherdbe1cb02014-06-27 00:13:52 +00001000 if (Reg == QRI->getRARegister()) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001001 FuncInfo.setHasClobberLR(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001002 break;
1003 }
1004 }
1005 break;
1006 }
1007 }
1008 }
1009 }
1010 } // Node->getOpcode
1011 return Op;
1012}
1013
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00001014// Need to transform ISD::PREFETCH into something that doesn't inherit
1015// all of the properties of ISD::PREFETCH, specifically SDNPMayLoad and
1016// SDNPMayStore.
1017SDValue HexagonTargetLowering::LowerPREFETCH(SDValue Op,
1018 SelectionDAG &DAG) const {
1019 SDValue Chain = Op.getOperand(0);
1020 SDValue Addr = Op.getOperand(1);
1021 // Lower it to DCFETCH($reg, #0). A "pat" will try to merge the offset in,
1022 // if the "reg" is fed by an "add".
1023 SDLoc DL(Op);
1024 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
1025 return DAG.getNode(HexagonISD::DCFETCH, DL, MVT::Other, Chain, Addr, Zero);
1026}
1027
1028SDValue HexagonTargetLowering::LowerINTRINSIC_VOID(SDValue Op,
1029 SelectionDAG &DAG) const {
1030 SDValue Chain = Op.getOperand(0);
1031 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1032 // Lower the hexagon_prefetch builtin to DCFETCH, as above.
1033 if (IntNo == Intrinsic::hexagon_prefetch) {
1034 SDValue Addr = Op.getOperand(2);
1035 SDLoc DL(Op);
1036 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
1037 return DAG.getNode(HexagonISD::DCFETCH, DL, MVT::Other, Chain, Addr, Zero);
1038 }
1039 return SDValue();
1040}
1041
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001042SDValue
1043HexagonTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
1044 SelectionDAG &DAG) const {
1045 SDValue Chain = Op.getOperand(0);
1046 SDValue Size = Op.getOperand(1);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001047 SDValue Align = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001048 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001049
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001050 ConstantSDNode *AlignConst = dyn_cast<ConstantSDNode>(Align);
1051 assert(AlignConst && "Non-constant Align in LowerDYNAMIC_STACKALLOC");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001052
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001053 unsigned A = AlignConst->getSExtValue();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001054 auto &HFI = *Subtarget.getFrameLowering();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001055 // "Zero" means natural stack alignment.
1056 if (A == 0)
1057 A = HFI.getStackAlignment();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001058
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001059 DEBUG({
Krzysztof Parzyszek9ee04e42015-04-22 17:19:44 +00001060 dbgs () << LLVM_FUNCTION_NAME << " Align: " << A << " Size: ";
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001061 Size.getNode()->dump(&DAG);
1062 dbgs() << "\n";
1063 });
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001064
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001065 SDValue AC = DAG.getConstant(A, dl, MVT::i32);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001066 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00001067 SDValue AA = DAG.getNode(HexagonISD::ALLOCA, dl, VTs, Chain, Size, AC);
Nirav Davebfdb4832016-06-23 17:52:57 +00001068
1069 DAG.ReplaceAllUsesOfValueWith(Op, AA);
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00001070 return AA;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001071}
1072
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001073SDValue HexagonTargetLowering::LowerFormalArguments(
1074 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
1075 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
1076 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001077
1078 MachineFunction &MF = DAG.getMachineFunction();
1079 MachineFrameInfo *MFI = MF.getFrameInfo();
1080 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001081 auto &FuncInfo = *MF.getInfo<HexagonMachineFunctionInfo>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001082
1083 // Assign locations to all of the incoming arguments.
1084 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001085 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1086 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001087
1088 CCInfo.AnalyzeFormalArguments(Ins, CC_Hexagon);
1089
1090 // For LLVM, in the case when returning a struct by value (>8byte),
1091 // the first argument is a pointer that points to the location on caller's
1092 // stack where the return value will be stored. For Hexagon, the location on
1093 // caller's stack is passed only when the struct size is smaller than (and
1094 // equal to) 8 bytes. If not, no address will be passed into callee and
1095 // callee return the result direclty through R0/R1.
1096
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001097 SmallVector<SDValue, 8> MemOps;
1098 bool UseHVX = Subtarget.useHVXOps(), UseHVXDbl = Subtarget.useHVXDblOps();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001099
1100 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1101 CCValAssign &VA = ArgLocs[i];
1102 ISD::ArgFlagsTy Flags = Ins[i].Flags;
1103 unsigned ObjSize;
1104 unsigned StackLocation;
1105 int FI;
1106
1107 if ( (VA.isRegLoc() && !Flags.isByVal())
1108 || (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() > 8)) {
1109 // Arguments passed in registers
1110 // 1. int, long long, ptr args that get allocated in register.
1111 // 2. Large struct that gets an register to put its address in.
1112 EVT RegVT = VA.getLocVT();
Sirish Pande69295b82012-05-10 20:20:25 +00001113 if (RegVT == MVT::i8 || RegVT == MVT::i16 ||
1114 RegVT == MVT::i32 || RegVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001115 unsigned VReg =
Craig Topperc7242e02012-04-20 07:30:17 +00001116 RegInfo.createVirtualRegister(&Hexagon::IntRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001117 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1118 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Colin LeMahieu4379d102015-01-28 22:08:16 +00001119 } else if (RegVT == MVT::i64 || RegVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001120 unsigned VReg =
Craig Topperc7242e02012-04-20 07:30:17 +00001121 RegInfo.createVirtualRegister(&Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001122 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1123 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001124
1125 // Single Vector
1126 } else if ((RegVT == MVT::v8i64 || RegVT == MVT::v16i32 ||
1127 RegVT == MVT::v32i16 || RegVT == MVT::v64i8)) {
1128 unsigned VReg =
1129 RegInfo.createVirtualRegister(&Hexagon::VectorRegsRegClass);
1130 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1131 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1132 } else if (UseHVX && UseHVXDbl &&
1133 ((RegVT == MVT::v16i64 || RegVT == MVT::v32i32 ||
1134 RegVT == MVT::v64i16 || RegVT == MVT::v128i8))) {
1135 unsigned VReg =
1136 RegInfo.createVirtualRegister(&Hexagon::VectorRegs128BRegClass);
1137 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1138 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1139
1140 // Double Vector
1141 } else if ((RegVT == MVT::v16i64 || RegVT == MVT::v32i32 ||
1142 RegVT == MVT::v64i16 || RegVT == MVT::v128i8)) {
1143 unsigned VReg =
1144 RegInfo.createVirtualRegister(&Hexagon::VecDblRegsRegClass);
1145 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1146 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1147 } else if (UseHVX && UseHVXDbl &&
1148 ((RegVT == MVT::v32i64 || RegVT == MVT::v64i32 ||
1149 RegVT == MVT::v128i16 || RegVT == MVT::v256i8))) {
1150 unsigned VReg =
1151 RegInfo.createVirtualRegister(&Hexagon::VecDblRegs128BRegClass);
1152 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1153 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1154 } else if (RegVT == MVT::v512i1 || RegVT == MVT::v1024i1) {
1155 assert(0 && "need to support VecPred regs");
1156 unsigned VReg =
1157 RegInfo.createVirtualRegister(&Hexagon::VecPredRegsRegClass);
1158 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1159 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001160 } else {
1161 assert (0);
1162 }
1163 } else if (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() <= 8) {
1164 assert (0 && "ByValSize must be bigger than 8 bytes");
1165 } else {
1166 // Sanity check.
1167 assert(VA.isMemLoc());
1168
1169 if (Flags.isByVal()) {
1170 // If it's a byval parameter, then we need to compute the
1171 // "real" size, not the size of the pointer.
1172 ObjSize = Flags.getByValSize();
1173 } else {
1174 ObjSize = VA.getLocVT().getStoreSizeInBits() >> 3;
1175 }
1176
1177 StackLocation = HEXAGON_LRFP_SIZE + VA.getLocMemOffset();
1178 // Create the frame index object for this incoming parameter...
1179 FI = MFI->CreateFixedObject(ObjSize, StackLocation, true);
1180
1181 // Create the SelectionDAG nodes cordl, responding to a load
1182 // from this parameter.
1183 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
1184
1185 if (Flags.isByVal()) {
1186 // If it's a pass-by-value aggregate, then do not dereference the stack
1187 // location. Instead, we should generate a reference to the stack
1188 // location.
1189 InVals.push_back(FIN);
1190 } else {
Justin Lebar9c375812016-07-15 18:27:10 +00001191 InVals.push_back(
1192 DAG.getLoad(VA.getLocVT(), dl, Chain, FIN, MachinePointerInfo()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001193 }
1194 }
1195 }
1196
1197 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001198 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001199
1200 if (isVarArg) {
1201 // This will point to the next argument passed via stack.
1202 int FrameIndex = MFI->CreateFixedObject(Hexagon_PointerSize,
1203 HEXAGON_LRFP_SIZE +
1204 CCInfo.getNextStackOffset(),
1205 true);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001206 FuncInfo.setVarArgsFrameIndex(FrameIndex);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001207 }
1208
1209 return Chain;
1210}
1211
1212SDValue
1213HexagonTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
1214 // VASTART stores the address of the VarArgsFrameIndex slot into the
1215 // memory location argument.
1216 MachineFunction &MF = DAG.getMachineFunction();
1217 HexagonMachineFunctionInfo *QFI = MF.getInfo<HexagonMachineFunctionInfo>();
1218 SDValue Addr = DAG.getFrameIndex(QFI->getVarArgsFrameIndex(), MVT::i32);
1219 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Justin Lebar9c375812016-07-15 18:27:10 +00001220 return DAG.getStore(Op.getOperand(0), SDLoc(Op), Addr, Op.getOperand(1),
1221 MachinePointerInfo(SV));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001222}
1223
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001224// Creates a SPLAT instruction for a constant value VAL.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001225static SDValue createSplat(SelectionDAG &DAG, const SDLoc &dl, EVT VT,
1226 SDValue Val) {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001227 if (VT.getSimpleVT() == MVT::v4i8)
1228 return DAG.getNode(HexagonISD::VSPLATB, dl, VT, Val);
1229
1230 if (VT.getSimpleVT() == MVT::v4i16)
1231 return DAG.getNode(HexagonISD::VSPLATH, dl, VT, Val);
1232
1233 return SDValue();
1234}
1235
1236static bool isSExtFree(SDValue N) {
1237 // A sign-extend of a truncate of a sign-extend is free.
1238 if (N.getOpcode() == ISD::TRUNCATE &&
1239 N.getOperand(0).getOpcode() == ISD::AssertSext)
1240 return true;
1241 // We have sign-extended loads.
1242 if (N.getOpcode() == ISD::LOAD)
1243 return true;
1244 return false;
1245}
1246
1247SDValue HexagonTargetLowering::LowerCTPOP(SDValue Op, SelectionDAG &DAG) const {
1248 SDLoc dl(Op);
1249 SDValue InpVal = Op.getOperand(0);
1250 if (isa<ConstantSDNode>(InpVal)) {
1251 uint64_t V = cast<ConstantSDNode>(InpVal)->getZExtValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001252 return DAG.getTargetConstant(countPopulation(V), dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001253 }
1254 SDValue PopOut = DAG.getNode(HexagonISD::POPCOUNT, dl, MVT::i32, InpVal);
1255 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, PopOut);
1256}
1257
1258SDValue HexagonTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1259 SDLoc dl(Op);
1260
1261 SDValue LHS = Op.getOperand(0);
1262 SDValue RHS = Op.getOperand(1);
1263 SDValue Cmp = Op.getOperand(2);
1264 ISD::CondCode CC = cast<CondCodeSDNode>(Cmp)->get();
1265
1266 EVT VT = Op.getValueType();
1267 EVT LHSVT = LHS.getValueType();
1268 EVT RHSVT = RHS.getValueType();
1269
1270 if (LHSVT == MVT::v2i16) {
1271 assert(ISD::isSignedIntSetCC(CC) || ISD::isUnsignedIntSetCC(CC));
1272 unsigned ExtOpc = ISD::isSignedIntSetCC(CC) ? ISD::SIGN_EXTEND
1273 : ISD::ZERO_EXTEND;
1274 SDValue LX = DAG.getNode(ExtOpc, dl, MVT::v2i32, LHS);
1275 SDValue RX = DAG.getNode(ExtOpc, dl, MVT::v2i32, RHS);
1276 SDValue SC = DAG.getNode(ISD::SETCC, dl, MVT::v2i1, LX, RX, Cmp);
1277 return SC;
1278 }
1279
1280 // Treat all other vector types as legal.
1281 if (VT.isVector())
1282 return Op;
1283
1284 // Equals and not equals should use sign-extend, not zero-extend, since
1285 // we can represent small negative values in the compare instructions.
1286 // The LLVM default is to use zero-extend arbitrarily in these cases.
1287 if ((CC == ISD::SETEQ || CC == ISD::SETNE) &&
1288 (RHSVT == MVT::i8 || RHSVT == MVT::i16) &&
1289 (LHSVT == MVT::i8 || LHSVT == MVT::i16)) {
1290 ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS);
1291 if (C && C->getAPIntValue().isNegative()) {
1292 LHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, LHS);
1293 RHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, RHS);
1294 return DAG.getNode(ISD::SETCC, dl, Op.getValueType(),
1295 LHS, RHS, Op.getOperand(2));
1296 }
1297 if (isSExtFree(LHS) || isSExtFree(RHS)) {
1298 LHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, LHS);
1299 RHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, RHS);
1300 return DAG.getNode(ISD::SETCC, dl, Op.getValueType(),
1301 LHS, RHS, Op.getOperand(2));
1302 }
1303 }
1304 return SDValue();
1305}
1306
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001307SDValue
1308HexagonTargetLowering::LowerVSELECT(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001309 SDValue PredOp = Op.getOperand(0);
1310 SDValue Op1 = Op.getOperand(1), Op2 = Op.getOperand(2);
1311 EVT OpVT = Op1.getValueType();
1312 SDLoc DL(Op);
1313
1314 if (OpVT == MVT::v2i16) {
1315 SDValue X1 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op1);
1316 SDValue X2 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op2);
1317 SDValue SL = DAG.getNode(ISD::VSELECT, DL, MVT::v2i32, PredOp, X1, X2);
1318 SDValue TR = DAG.getNode(ISD::TRUNCATE, DL, MVT::v2i16, SL);
1319 return TR;
1320 }
1321
1322 return SDValue();
1323}
1324
1325// Handle only specific vector loads.
1326SDValue HexagonTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1327 EVT VT = Op.getValueType();
1328 SDLoc DL(Op);
1329 LoadSDNode *LoadNode = cast<LoadSDNode>(Op);
1330 SDValue Chain = LoadNode->getChain();
1331 SDValue Ptr = Op.getOperand(1);
1332 SDValue LoweredLoad;
1333 SDValue Result;
1334 SDValue Base = LoadNode->getBasePtr();
1335 ISD::LoadExtType Ext = LoadNode->getExtensionType();
1336 unsigned Alignment = LoadNode->getAlignment();
1337 SDValue LoadChain;
1338
1339 if(Ext == ISD::NON_EXTLOAD)
1340 Ext = ISD::ZEXTLOAD;
1341
1342 if (VT == MVT::v4i16) {
1343 if (Alignment == 2) {
1344 SDValue Loads[4];
1345 // Base load.
1346 Loads[0] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Base,
Justin Lebar9c375812016-07-15 18:27:10 +00001347 LoadNode->getPointerInfo(), MVT::i16, Alignment,
1348 LoadNode->getMemOperand()->getFlags());
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001349 // Base+2 load.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001350 SDValue Increment = DAG.getConstant(2, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001351 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1352 Loads[1] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
Justin Lebar9c375812016-07-15 18:27:10 +00001353 LoadNode->getPointerInfo(), MVT::i16, Alignment,
1354 LoadNode->getMemOperand()->getFlags());
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001355 // SHL 16, then OR base and base+2.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001356 SDValue ShiftAmount = DAG.getConstant(16, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001357 SDValue Tmp1 = DAG.getNode(ISD::SHL, DL, MVT::i32, Loads[1], ShiftAmount);
1358 SDValue Tmp2 = DAG.getNode(ISD::OR, DL, MVT::i32, Tmp1, Loads[0]);
1359 // Base + 4.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001360 Increment = DAG.getConstant(4, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001361 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1362 Loads[2] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
Justin Lebar9c375812016-07-15 18:27:10 +00001363 LoadNode->getPointerInfo(), MVT::i16, Alignment,
1364 LoadNode->getMemOperand()->getFlags());
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001365 // Base + 6.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001366 Increment = DAG.getConstant(6, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001367 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1368 Loads[3] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
Justin Lebar9c375812016-07-15 18:27:10 +00001369 LoadNode->getPointerInfo(), MVT::i16, Alignment,
1370 LoadNode->getMemOperand()->getFlags());
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001371 // SHL 16, then OR base+4 and base+6.
1372 Tmp1 = DAG.getNode(ISD::SHL, DL, MVT::i32, Loads[3], ShiftAmount);
1373 SDValue Tmp4 = DAG.getNode(ISD::OR, DL, MVT::i32, Tmp1, Loads[2]);
1374 // Combine to i64. This could be optimised out later if we can
1375 // affect reg allocation of this code.
1376 Result = DAG.getNode(HexagonISD::COMBINE, DL, MVT::i64, Tmp4, Tmp2);
1377 LoadChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
1378 Loads[0].getValue(1), Loads[1].getValue(1),
1379 Loads[2].getValue(1), Loads[3].getValue(1));
1380 } else {
1381 // Perform default type expansion.
1382 Result = DAG.getLoad(MVT::i64, DL, Chain, Ptr, LoadNode->getPointerInfo(),
Justin Lebar9c375812016-07-15 18:27:10 +00001383 LoadNode->getAlignment(),
1384 LoadNode->getMemOperand()->getFlags());
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001385 LoadChain = Result.getValue(1);
1386 }
1387 } else
1388 llvm_unreachable("Custom lowering unsupported load");
1389
1390 Result = DAG.getNode(ISD::BITCAST, DL, VT, Result);
1391 // Since we pretend to lower a load, we need the original chain
1392 // info attached to the result.
1393 SDValue Ops[] = { Result, LoadChain };
1394
1395 return DAG.getMergeValues(Ops, DL);
1396}
1397
1398
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001399SDValue
Sirish Pande69295b82012-05-10 20:20:25 +00001400HexagonTargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
1401 EVT ValTy = Op.getValueType();
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001402 ConstantPoolSDNode *CPN = cast<ConstantPoolSDNode>(Op);
1403 unsigned Align = CPN->getAlignment();
Rafael Espindola405e25a2016-06-26 22:24:01 +00001404 bool IsPositionIndependent = isPositionIndependent();
1405 unsigned char TF = IsPositionIndependent ? HexagonII::MO_PCREL : 0;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001406
1407 SDValue T;
1408 if (CPN->isMachineConstantPoolEntry())
1409 T = DAG.getTargetConstantPool(CPN->getMachineCPVal(), ValTy, Align, TF);
Sirish Pande69295b82012-05-10 20:20:25 +00001410 else
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001411 T = DAG.getTargetConstantPool(CPN->getConstVal(), ValTy, Align, TF);
Rafael Espindola405e25a2016-06-26 22:24:01 +00001412 if (IsPositionIndependent)
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001413 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), ValTy, T);
1414 return DAG.getNode(HexagonISD::CP, SDLoc(Op), ValTy, T);
1415}
1416
1417SDValue
1418HexagonTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
1419 EVT VT = Op.getValueType();
1420 int Idx = cast<JumpTableSDNode>(Op)->getIndex();
Rafael Espindola405e25a2016-06-26 22:24:01 +00001421 if (isPositionIndependent()) {
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001422 SDValue T = DAG.getTargetJumpTable(Idx, VT, HexagonII::MO_PCREL);
1423 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), VT, T);
1424 }
1425
1426 SDValue T = DAG.getTargetJumpTable(Idx, VT);
1427 return DAG.getNode(HexagonISD::JT, SDLoc(Op), VT, T);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001428}
1429
1430SDValue
1431HexagonTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001432 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001433 MachineFunction &MF = DAG.getMachineFunction();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001434 MachineFrameInfo &MFI = *MF.getFrameInfo();
1435 MFI.setReturnAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001436
Bill Wendling908bf812014-01-06 00:43:20 +00001437 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001438 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001439
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001440 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001441 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001442 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1443 if (Depth) {
1444 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001445 SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001446 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
1447 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Justin Lebar9c375812016-07-15 18:27:10 +00001448 MachinePointerInfo());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001449 }
1450
1451 // Return LR, which contains the return address. Mark it an implicit live-in.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001452 unsigned Reg = MF.addLiveIn(HRI.getRARegister(), getRegClassFor(MVT::i32));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001453 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
1454}
1455
1456SDValue
1457HexagonTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001458 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
1459 MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
1460 MFI.setFrameAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001461
1462 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001463 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001464 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1465 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001466 HRI.getFrameRegister(), VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001467 while (Depth--)
1468 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00001469 MachinePointerInfo());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001470 return FrameAddr;
1471}
1472
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001473SDValue
1474HexagonTargetLowering::LowerATOMIC_FENCE(SDValue Op, SelectionDAG& DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001475 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001476 return DAG.getNode(HexagonISD::BARRIER, dl, MVT::Other, Op.getOperand(0));
1477}
1478
1479
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001480SDValue
1481HexagonTargetLowering::LowerGLOBALADDRESS(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001482 SDLoc dl(Op);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001483 auto *GAN = cast<GlobalAddressSDNode>(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00001484 auto PtrVT = getPointerTy(DAG.getDataLayout());
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001485 auto *GV = GAN->getGlobal();
1486 int64_t Offset = GAN->getOffset();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001487
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001488 auto &HLOF = *HTM.getObjFileLowering();
1489 Reloc::Model RM = HTM.getRelocationModel();
1490
1491 if (RM == Reloc::Static) {
1492 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset);
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +00001493 if (HLOF.isGlobalInSmallSection(GV, HTM))
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001494 return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, GA);
1495 return DAG.getNode(HexagonISD::CONST32, dl, PtrVT, GA);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001496 }
1497
Rafael Espindola3beef8d2016-06-27 23:15:57 +00001498 bool UsePCRel = getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001499 if (UsePCRel) {
1500 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset,
1501 HexagonII::MO_PCREL);
1502 return DAG.getNode(HexagonISD::AT_PCREL, dl, PtrVT, GA);
1503 }
1504
1505 // Use GOT index.
1506 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
1507 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, HexagonII::MO_GOT);
1508 SDValue Off = DAG.getConstant(Offset, dl, MVT::i32);
1509 return DAG.getNode(HexagonISD::AT_GOT, dl, PtrVT, GOT, GA, Off);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001510}
1511
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001512// Specifies that for loads and stores VT can be promoted to PromotedLdStVT.
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001513SDValue
1514HexagonTargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
1515 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001516 SDLoc dl(Op);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001517 EVT PtrVT = getPointerTy(DAG.getDataLayout());
1518
1519 Reloc::Model RM = HTM.getRelocationModel();
1520 if (RM == Reloc::Static) {
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001521 SDValue A = DAG.getTargetBlockAddress(BA, PtrVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001522 return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, A);
1523 }
1524
1525 SDValue A = DAG.getTargetBlockAddress(BA, PtrVT, 0, HexagonII::MO_PCREL);
1526 return DAG.getNode(HexagonISD::AT_PCREL, dl, PtrVT, A);
1527}
1528
1529SDValue
1530HexagonTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op, SelectionDAG &DAG)
1531 const {
1532 EVT PtrVT = getPointerTy(DAG.getDataLayout());
1533 SDValue GOTSym = DAG.getTargetExternalSymbol(HEXAGON_GOT_SYM_NAME, PtrVT,
1534 HexagonII::MO_PCREL);
1535 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), PtrVT, GOTSym);
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001536}
1537
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001538SDValue
1539HexagonTargetLowering::GetDynamicTLSAddr(SelectionDAG &DAG, SDValue Chain,
1540 GlobalAddressSDNode *GA, SDValue *InFlag, EVT PtrVT, unsigned ReturnReg,
1541 unsigned char OperandFlags) const {
1542 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1543 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1544 SDLoc dl(GA);
1545 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
1546 GA->getValueType(0),
1547 GA->getOffset(),
1548 OperandFlags);
1549 // Create Operands for the call.The Operands should have the following:
1550 // 1. Chain SDValue
1551 // 2. Callee which in this case is the Global address value.
1552 // 3. Registers live into the call.In this case its R0, as we
1553 // have just one argument to be passed.
1554 // 4. InFlag if there is any.
1555 // Note: The order is important.
1556
1557 if (InFlag) {
1558 SDValue Ops[] = { Chain, TGA,
1559 DAG.getRegister(Hexagon::R0, PtrVT), *InFlag };
1560 Chain = DAG.getNode(HexagonISD::CALLv3, dl, NodeTys, Ops);
1561 } else {
1562 SDValue Ops[] = { Chain, TGA, DAG.getRegister(Hexagon::R0, PtrVT)};
1563 Chain = DAG.getNode(HexagonISD::CALLv3, dl, NodeTys, Ops);
1564 }
1565
1566 // Inform MFI that function has calls.
1567 MFI->setAdjustsStack(true);
1568
1569 SDValue Flag = Chain.getValue(1);
1570 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
1571}
1572
1573//
1574// Lower using the intial executable model for TLS addresses
1575//
1576SDValue
1577HexagonTargetLowering::LowerToTLSInitialExecModel(GlobalAddressSDNode *GA,
1578 SelectionDAG &DAG) const {
1579 SDLoc dl(GA);
1580 int64_t Offset = GA->getOffset();
1581 auto PtrVT = getPointerTy(DAG.getDataLayout());
1582
1583 // Get the thread pointer.
1584 SDValue TP = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Hexagon::UGP, PtrVT);
1585
Rafael Espindola405e25a2016-06-26 22:24:01 +00001586 bool IsPositionIndependent = isPositionIndependent();
1587 unsigned char TF =
1588 IsPositionIndependent ? HexagonII::MO_IEGOT : HexagonII::MO_IE;
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001589
1590 // First generate the TLS symbol address
1591 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT,
1592 Offset, TF);
1593
1594 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1595
Rafael Espindola405e25a2016-06-26 22:24:01 +00001596 if (IsPositionIndependent) {
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001597 // Generate the GOT pointer in case of position independent code
1598 SDValue GOT = LowerGLOBAL_OFFSET_TABLE(Sym, DAG);
1599
1600 // Add the TLS Symbol address to GOT pointer.This gives
1601 // GOT relative relocation for the symbol.
1602 Sym = DAG.getNode(ISD::ADD, dl, PtrVT, GOT, Sym);
1603 }
1604
1605 // Load the offset value for TLS symbol.This offset is relative to
1606 // thread pointer.
Justin Lebar9c375812016-07-15 18:27:10 +00001607 SDValue LoadOffset =
1608 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Sym, MachinePointerInfo());
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001609
1610 // Address of the thread local variable is the add of thread
1611 // pointer and the offset of the variable.
1612 return DAG.getNode(ISD::ADD, dl, PtrVT, TP, LoadOffset);
1613}
1614
1615//
1616// Lower using the local executable model for TLS addresses
1617//
1618SDValue
1619HexagonTargetLowering::LowerToTLSLocalExecModel(GlobalAddressSDNode *GA,
1620 SelectionDAG &DAG) const {
1621 SDLoc dl(GA);
1622 int64_t Offset = GA->getOffset();
1623 auto PtrVT = getPointerTy(DAG.getDataLayout());
1624
1625 // Get the thread pointer.
1626 SDValue TP = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Hexagon::UGP, PtrVT);
1627 // Generate the TLS symbol address
1628 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT, Offset,
1629 HexagonII::MO_TPREL);
1630 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1631
1632 // Address of the thread local variable is the add of thread
1633 // pointer and the offset of the variable.
1634 return DAG.getNode(ISD::ADD, dl, PtrVT, TP, Sym);
1635}
1636
1637//
1638// Lower using the general dynamic model for TLS addresses
1639//
1640SDValue
1641HexagonTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
1642 SelectionDAG &DAG) const {
1643 SDLoc dl(GA);
1644 int64_t Offset = GA->getOffset();
1645 auto PtrVT = getPointerTy(DAG.getDataLayout());
1646
1647 // First generate the TLS symbol address
1648 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT, Offset,
1649 HexagonII::MO_GDGOT);
1650
1651 // Then, generate the GOT pointer
1652 SDValue GOT = LowerGLOBAL_OFFSET_TABLE(TGA, DAG);
1653
1654 // Add the TLS symbol and the GOT pointer
1655 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1656 SDValue Chain = DAG.getNode(ISD::ADD, dl, PtrVT, GOT, Sym);
1657
1658 // Copy over the argument to R0
1659 SDValue InFlag;
1660 Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, Hexagon::R0, Chain, InFlag);
1661 InFlag = Chain.getValue(1);
1662
1663 return GetDynamicTLSAddr(DAG, Chain, GA, &InFlag, PtrVT,
1664 Hexagon::R0, HexagonII::MO_GDPLT);
1665}
1666
1667//
1668// Lower TLS addresses.
1669//
1670// For now for dynamic models, we only support the general dynamic model.
1671//
1672SDValue
1673HexagonTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1674 SelectionDAG &DAG) const {
1675 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1676
1677 switch (HTM.getTLSModel(GA->getGlobal())) {
1678 case TLSModel::GeneralDynamic:
1679 case TLSModel::LocalDynamic:
1680 return LowerToTLSGeneralDynamicModel(GA, DAG);
1681 case TLSModel::InitialExec:
1682 return LowerToTLSInitialExecModel(GA, DAG);
1683 case TLSModel::LocalExec:
1684 return LowerToTLSLocalExecModel(GA, DAG);
1685 }
1686 llvm_unreachable("Bogus TLS model");
1687}
1688
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001689//===----------------------------------------------------------------------===//
1690// TargetLowering Implementation
1691//===----------------------------------------------------------------------===//
1692
Eric Christopherd737b762015-02-02 22:11:36 +00001693HexagonTargetLowering::HexagonTargetLowering(const TargetMachine &TM,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001694 const HexagonSubtarget &ST)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001695 : TargetLowering(TM), HTM(static_cast<const HexagonTargetMachine&>(TM)),
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001696 Subtarget(ST) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001697 bool IsV4 = !Subtarget.hasV5TOps();
1698 auto &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00001699 bool UseHVX = Subtarget.useHVXOps();
1700 bool UseHVXSgl = Subtarget.useHVXSglOps();
1701 bool UseHVXDbl = Subtarget.useHVXDblOps();
Sirish Pande69295b82012-05-10 20:20:25 +00001702
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001703 setPrefLoopAlignment(4);
1704 setPrefFunctionAlignment(4);
1705 setMinFunctionAlignment(2);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001706 setStackPointerRegisterToSaveRestore(HRI.getStackRegister());
1707
Krzysztof Parzyszekf228c952016-06-22 16:07:10 +00001708 setMaxAtomicSizeInBitsSupported(64);
1709 setMinCmpXchgSizeInBits(32);
1710
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001711 if (EnableHexSDNodeSched)
1712 setSchedulingPreference(Sched::VLIW);
1713 else
1714 setSchedulingPreference(Sched::Source);
1715
1716 // Limits for inline expansion of memcpy/memmove
1717 MaxStoresPerMemcpy = MaxStoresPerMemcpyCL;
1718 MaxStoresPerMemcpyOptSize = MaxStoresPerMemcpyOptSizeCL;
1719 MaxStoresPerMemmove = MaxStoresPerMemmoveCL;
1720 MaxStoresPerMemmoveOptSize = MaxStoresPerMemmoveOptSizeCL;
1721 MaxStoresPerMemset = MaxStoresPerMemsetCL;
1722 MaxStoresPerMemsetOptSize = MaxStoresPerMemsetOptSizeCL;
1723
1724 //
1725 // Set up register classes.
1726 //
1727
1728 addRegisterClass(MVT::i1, &Hexagon::PredRegsRegClass);
1729 addRegisterClass(MVT::v2i1, &Hexagon::PredRegsRegClass); // bbbbaaaa
1730 addRegisterClass(MVT::v4i1, &Hexagon::PredRegsRegClass); // ddccbbaa
1731 addRegisterClass(MVT::v8i1, &Hexagon::PredRegsRegClass); // hgfedcba
1732 addRegisterClass(MVT::i32, &Hexagon::IntRegsRegClass);
1733 addRegisterClass(MVT::v4i8, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001734 addRegisterClass(MVT::v2i16, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001735 addRegisterClass(MVT::i64, &Hexagon::DoubleRegsRegClass);
1736 addRegisterClass(MVT::v8i8, &Hexagon::DoubleRegsRegClass);
1737 addRegisterClass(MVT::v4i16, &Hexagon::DoubleRegsRegClass);
1738 addRegisterClass(MVT::v2i32, &Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001739
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001740 if (Subtarget.hasV5TOps()) {
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001741 addRegisterClass(MVT::f32, &Hexagon::IntRegsRegClass);
1742 addRegisterClass(MVT::f64, &Hexagon::DoubleRegsRegClass);
1743 }
Sirish Pande69295b82012-05-10 20:20:25 +00001744
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001745 if (Subtarget.hasV60TOps()) {
1746 if (Subtarget.useHVXSglOps()) {
1747 addRegisterClass(MVT::v64i8, &Hexagon::VectorRegsRegClass);
1748 addRegisterClass(MVT::v32i16, &Hexagon::VectorRegsRegClass);
1749 addRegisterClass(MVT::v16i32, &Hexagon::VectorRegsRegClass);
1750 addRegisterClass(MVT::v8i64, &Hexagon::VectorRegsRegClass);
1751 addRegisterClass(MVT::v128i8, &Hexagon::VecDblRegsRegClass);
1752 addRegisterClass(MVT::v64i16, &Hexagon::VecDblRegsRegClass);
1753 addRegisterClass(MVT::v32i32, &Hexagon::VecDblRegsRegClass);
1754 addRegisterClass(MVT::v16i64, &Hexagon::VecDblRegsRegClass);
1755 addRegisterClass(MVT::v512i1, &Hexagon::VecPredRegsRegClass);
1756 } else if (Subtarget.useHVXDblOps()) {
1757 addRegisterClass(MVT::v128i8, &Hexagon::VectorRegs128BRegClass);
1758 addRegisterClass(MVT::v64i16, &Hexagon::VectorRegs128BRegClass);
1759 addRegisterClass(MVT::v32i32, &Hexagon::VectorRegs128BRegClass);
1760 addRegisterClass(MVT::v16i64, &Hexagon::VectorRegs128BRegClass);
1761 addRegisterClass(MVT::v256i8, &Hexagon::VecDblRegs128BRegClass);
1762 addRegisterClass(MVT::v128i16, &Hexagon::VecDblRegs128BRegClass);
1763 addRegisterClass(MVT::v64i32, &Hexagon::VecDblRegs128BRegClass);
1764 addRegisterClass(MVT::v32i64, &Hexagon::VecDblRegs128BRegClass);
1765 addRegisterClass(MVT::v1024i1, &Hexagon::VecPredRegs128BRegClass);
1766 }
1767
1768 }
1769
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001770 //
1771 // Handling of scalar operations.
1772 //
1773 // All operations default to "legal", except:
1774 // - indexed loads and stores (pre-/post-incremented),
1775 // - ANY_EXTEND_VECTOR_INREG, ATOMIC_CMP_SWAP_WITH_SUCCESS, CONCAT_VECTORS,
1776 // ConstantFP, DEBUGTRAP, FCEIL, FCOPYSIGN, FEXP, FEXP2, FFLOOR, FGETSIGN,
1777 // FLOG, FLOG2, FLOG10, FMAXNUM, FMINNUM, FNEARBYINT, FRINT, FROUND, TRAP,
1778 // FTRUNC, PREFETCH, SIGN_EXTEND_VECTOR_INREG, ZERO_EXTEND_VECTOR_INREG,
1779 // which default to "expand" for at least one type.
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001780
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001781 // Misc operations.
1782 setOperationAction(ISD::ConstantFP, MVT::f32, Legal); // Default: expand
1783 setOperationAction(ISD::ConstantFP, MVT::f64, Legal); // Default: expand
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001784
1785 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001786 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001787 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001788 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
1789 setOperationAction(ISD::INLINEASM, MVT::Other, Custom);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00001790 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
1791 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001792 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001793 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001794 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001795 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001796
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001797 // Custom legalize GlobalAddress nodes into CONST32.
1798 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001799 setOperationAction(ISD::GlobalAddress, MVT::i8, Custom);
1800 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001801
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001802 // Hexagon needs to optimize cases with negative constants.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001803 setOperationAction(ISD::SETCC, MVT::i8, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001804 setOperationAction(ISD::SETCC, MVT::i16, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001805
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001806 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
1807 setOperationAction(ISD::VASTART, MVT::Other, Custom);
1808 setOperationAction(ISD::VAEND, MVT::Other, Expand);
1809 setOperationAction(ISD::VAARG, MVT::Other, Expand);
1810
1811 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
1812 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
1813 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
1814
1815 if (EmitJumpTables)
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001816 setMinimumJumpTableEntries(MinimumJumpTables);
Krzysztof Parzyszeka61f7da2016-01-13 21:43:13 +00001817 else
1818 setMinimumJumpTableEntries(INT_MAX);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001819 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001820
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001821 // Hexagon has instructions for add/sub with carry. The problem with
1822 // modeling these instructions is that they produce 2 results: Rdd and Px.
1823 // To model the update of Px, we will have to use Defs[p0..p3] which will
1824 // cause any predicate live range to spill. So, we pretend we dont't have
1825 // these instructions.
1826 setOperationAction(ISD::ADDE, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001827 setOperationAction(ISD::ADDE, MVT::i16, Expand);
1828 setOperationAction(ISD::ADDE, MVT::i32, Expand);
1829 setOperationAction(ISD::ADDE, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001830 setOperationAction(ISD::SUBE, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001831 setOperationAction(ISD::SUBE, MVT::i16, Expand);
1832 setOperationAction(ISD::SUBE, MVT::i32, Expand);
1833 setOperationAction(ISD::SUBE, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001834 setOperationAction(ISD::ADDC, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001835 setOperationAction(ISD::ADDC, MVT::i16, Expand);
1836 setOperationAction(ISD::ADDC, MVT::i32, Expand);
1837 setOperationAction(ISD::ADDC, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001838 setOperationAction(ISD::SUBC, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001839 setOperationAction(ISD::SUBC, MVT::i16, Expand);
1840 setOperationAction(ISD::SUBC, MVT::i32, Expand);
1841 setOperationAction(ISD::SUBC, MVT::i64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001842
Krzysztof Parzyszek2c4487d2015-04-13 20:37:01 +00001843 // Only add and sub that detect overflow are the saturating ones.
1844 for (MVT VT : MVT::integer_valuetypes()) {
1845 setOperationAction(ISD::UADDO, VT, Expand);
1846 setOperationAction(ISD::SADDO, VT, Expand);
1847 setOperationAction(ISD::USUBO, VT, Expand);
1848 setOperationAction(ISD::SSUBO, VT, Expand);
1849 }
1850
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001851 setOperationAction(ISD::CTLZ, MVT::i8, Promote);
1852 setOperationAction(ISD::CTLZ, MVT::i16, Promote);
1853 setOperationAction(ISD::CTTZ, MVT::i8, Promote);
1854 setOperationAction(ISD::CTTZ, MVT::i16, Promote);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001855
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001856 // In V5, popcount can count # of 1s in i64 but returns i32.
1857 // On V4 it will be expanded (set later).
1858 setOperationAction(ISD::CTPOP, MVT::i8, Promote);
1859 setOperationAction(ISD::CTPOP, MVT::i16, Promote);
1860 setOperationAction(ISD::CTPOP, MVT::i32, Promote);
1861 setOperationAction(ISD::CTPOP, MVT::i64, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001862
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001863 // We custom lower i64 to i64 mul, so that it is not considered as a legal
1864 // operation. There is a pattern that will match i64 mul and transform it
1865 // to a series of instructions.
1866 setOperationAction(ISD::MUL, MVT::i64, Expand);
Colin LeMahieude68b662015-02-05 21:13:25 +00001867 setOperationAction(ISD::MULHS, MVT::i64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001868
Benjamin Kramer62460692015-04-25 14:46:53 +00001869 for (unsigned IntExpOp :
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001870 { ISD::SDIV, ISD::UDIV, ISD::SREM, ISD::UREM,
1871 ISD::SDIVREM, ISD::UDIVREM, ISD::ROTL, ISD::ROTR,
1872 ISD::BSWAP, ISD::SHL_PARTS, ISD::SRA_PARTS, ISD::SRL_PARTS,
1873 ISD::SMUL_LOHI, ISD::UMUL_LOHI }) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001874 setOperationAction(IntExpOp, MVT::i32, Expand);
1875 setOperationAction(IntExpOp, MVT::i64, Expand);
1876 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001877
Benjamin Kramer62460692015-04-25 14:46:53 +00001878 for (unsigned FPExpOp :
1879 {ISD::FDIV, ISD::FREM, ISD::FSQRT, ISD::FSIN, ISD::FCOS, ISD::FSINCOS,
1880 ISD::FPOW, ISD::FCOPYSIGN}) {
1881 setOperationAction(FPExpOp, MVT::f32, Expand);
1882 setOperationAction(FPExpOp, MVT::f64, Expand);
1883 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001884
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001885 // No extending loads from i32.
1886 for (MVT VT : MVT::integer_valuetypes()) {
1887 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
1888 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
1889 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
1890 }
1891 // Turn FP truncstore into trunc + store.
1892 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
1893 // Turn FP extload into load/fextend.
1894 for (MVT VT : MVT::fp_valuetypes())
1895 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001896
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001897 // Expand BR_CC and SELECT_CC for all integer and fp types.
1898 for (MVT VT : MVT::integer_valuetypes()) {
1899 setOperationAction(ISD::BR_CC, VT, Expand);
1900 setOperationAction(ISD::SELECT_CC, VT, Expand);
1901 }
1902 for (MVT VT : MVT::fp_valuetypes()) {
1903 setOperationAction(ISD::BR_CC, VT, Expand);
1904 setOperationAction(ISD::SELECT_CC, VT, Expand);
1905 }
1906 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001907
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001908 //
1909 // Handling of vector operations.
1910 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001911
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001912 // Custom lower v4i16 load only. Let v4i16 store to be
1913 // promoted for now.
1914 promoteLdStType(MVT::v4i8, MVT::i32);
1915 promoteLdStType(MVT::v2i16, MVT::i32);
1916 promoteLdStType(MVT::v8i8, MVT::i64);
1917 promoteLdStType(MVT::v2i32, MVT::i64);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001918
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001919 setOperationAction(ISD::LOAD, MVT::v4i16, Custom);
1920 setOperationAction(ISD::STORE, MVT::v4i16, Promote);
1921 AddPromotedToType(ISD::LOAD, MVT::v4i16, MVT::i64);
1922 AddPromotedToType(ISD::STORE, MVT::v4i16, MVT::i64);
1923
1924 // Set the action for vector operations to "expand", then override it with
1925 // either "custom" or "legal" for specific cases.
Craig Topper26260942015-10-18 05:15:34 +00001926 static const unsigned VectExpOps[] = {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001927 // Integer arithmetic:
1928 ISD::ADD, ISD::SUB, ISD::MUL, ISD::SDIV, ISD::UDIV,
1929 ISD::SREM, ISD::UREM, ISD::SDIVREM, ISD::UDIVREM, ISD::ADDC,
1930 ISD::SUBC, ISD::SADDO, ISD::UADDO, ISD::SSUBO, ISD::USUBO,
1931 ISD::SMUL_LOHI, ISD::UMUL_LOHI,
1932 // Logical/bit:
1933 ISD::AND, ISD::OR, ISD::XOR, ISD::ROTL, ISD::ROTR,
Craig Topper33772c52016-04-28 03:34:31 +00001934 ISD::CTPOP, ISD::CTLZ, ISD::CTTZ,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001935 // Floating point arithmetic/math functions:
1936 ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FMA, ISD::FDIV,
1937 ISD::FREM, ISD::FNEG, ISD::FABS, ISD::FSQRT, ISD::FSIN,
1938 ISD::FCOS, ISD::FPOWI, ISD::FPOW, ISD::FLOG, ISD::FLOG2,
1939 ISD::FLOG10, ISD::FEXP, ISD::FEXP2, ISD::FCEIL, ISD::FTRUNC,
1940 ISD::FRINT, ISD::FNEARBYINT, ISD::FROUND, ISD::FFLOOR,
1941 ISD::FMINNUM, ISD::FMAXNUM, ISD::FSINCOS,
1942 // Misc:
1943 ISD::SELECT, ISD::ConstantPool,
1944 // Vector:
1945 ISD::BUILD_VECTOR, ISD::SCALAR_TO_VECTOR,
1946 ISD::EXTRACT_VECTOR_ELT, ISD::INSERT_VECTOR_ELT,
1947 ISD::EXTRACT_SUBVECTOR, ISD::INSERT_SUBVECTOR,
1948 ISD::CONCAT_VECTORS, ISD::VECTOR_SHUFFLE
1949 };
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001950
1951 for (MVT VT : MVT::vector_valuetypes()) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001952 for (unsigned VectExpOp : VectExpOps)
1953 setOperationAction(VectExpOp, VT, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001954
1955 // Expand all extended loads and truncating stores:
1956 for (MVT TargetVT : MVT::vector_valuetypes()) {
1957 setLoadExtAction(ISD::EXTLOAD, TargetVT, VT, Expand);
1958 setTruncStoreAction(VT, TargetVT, Expand);
1959 }
1960
1961 setOperationAction(ISD::SRA, VT, Custom);
1962 setOperationAction(ISD::SHL, VT, Custom);
1963 setOperationAction(ISD::SRL, VT, Custom);
1964 }
1965
1966 // Types natively supported:
Benjamin Kramer62460692015-04-25 14:46:53 +00001967 for (MVT NativeVT : {MVT::v2i1, MVT::v4i1, MVT::v8i1, MVT::v32i1, MVT::v64i1,
1968 MVT::v4i8, MVT::v8i8, MVT::v2i16, MVT::v4i16, MVT::v1i32,
1969 MVT::v2i32, MVT::v1i64}) {
1970 setOperationAction(ISD::BUILD_VECTOR, NativeVT, Custom);
1971 setOperationAction(ISD::EXTRACT_VECTOR_ELT, NativeVT, Custom);
1972 setOperationAction(ISD::INSERT_VECTOR_ELT, NativeVT, Custom);
1973 setOperationAction(ISD::EXTRACT_SUBVECTOR, NativeVT, Custom);
1974 setOperationAction(ISD::INSERT_SUBVECTOR, NativeVT, Custom);
1975 setOperationAction(ISD::CONCAT_VECTORS, NativeVT, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001976
Benjamin Kramer62460692015-04-25 14:46:53 +00001977 setOperationAction(ISD::ADD, NativeVT, Legal);
1978 setOperationAction(ISD::SUB, NativeVT, Legal);
1979 setOperationAction(ISD::MUL, NativeVT, Legal);
1980 setOperationAction(ISD::AND, NativeVT, Legal);
1981 setOperationAction(ISD::OR, NativeVT, Legal);
1982 setOperationAction(ISD::XOR, NativeVT, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001983 }
1984
1985 setOperationAction(ISD::SETCC, MVT::v2i16, Custom);
1986 setOperationAction(ISD::VSELECT, MVT::v2i16, Custom);
1987 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
1988 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001989 if (UseHVX) {
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00001990 if (UseHVXSgl) {
1991 setOperationAction(ISD::CONCAT_VECTORS, MVT::v128i8, Custom);
1992 setOperationAction(ISD::CONCAT_VECTORS, MVT::v64i16, Custom);
1993 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i32, Custom);
1994 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i64, Custom);
1995 } else if (UseHVXDbl) {
1996 setOperationAction(ISD::CONCAT_VECTORS, MVT::v256i8, Custom);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001997 setOperationAction(ISD::CONCAT_VECTORS, MVT::v128i16, Custom);
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00001998 setOperationAction(ISD::CONCAT_VECTORS, MVT::v64i32, Custom);
1999 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i64, Custom);
2000 } else {
2001 llvm_unreachable("Unrecognized HVX mode");
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002002 }
2003 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002004 // Subtarget-specific operation actions.
2005 //
2006 if (Subtarget.hasV5TOps()) {
2007 setOperationAction(ISD::FMA, MVT::f64, Expand);
2008 setOperationAction(ISD::FADD, MVT::f64, Expand);
2009 setOperationAction(ISD::FSUB, MVT::f64, Expand);
2010 setOperationAction(ISD::FMUL, MVT::f64, Expand);
2011
2012 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Promote);
2013 setOperationAction(ISD::FP_TO_UINT, MVT::i8, Promote);
2014 setOperationAction(ISD::FP_TO_UINT, MVT::i16, Promote);
2015 setOperationAction(ISD::FP_TO_SINT, MVT::i1, Promote);
2016 setOperationAction(ISD::FP_TO_SINT, MVT::i8, Promote);
2017 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
2018 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote);
2019 setOperationAction(ISD::UINT_TO_FP, MVT::i8, Promote);
2020 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote);
2021 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote);
2022 setOperationAction(ISD::SINT_TO_FP, MVT::i8, Promote);
2023 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
2024
2025 } else { // V4
2026 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
2027 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Expand);
2028 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
2029 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
2030 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Expand);
2031 setOperationAction(ISD::FP_TO_SINT, MVT::f32, Expand);
2032 setOperationAction(ISD::FP_EXTEND, MVT::f32, Expand);
2033 setOperationAction(ISD::FP_ROUND, MVT::f64, Expand);
2034 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
2035
2036 setOperationAction(ISD::CTPOP, MVT::i8, Expand);
2037 setOperationAction(ISD::CTPOP, MVT::i16, Expand);
2038 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
2039 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
2040
2041 // Expand these operations for both f32 and f64:
Benjamin Kramer62460692015-04-25 14:46:53 +00002042 for (unsigned FPExpOpV4 :
2043 {ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FABS, ISD::FNEG, ISD::FMA}) {
2044 setOperationAction(FPExpOpV4, MVT::f32, Expand);
2045 setOperationAction(FPExpOpV4, MVT::f64, Expand);
2046 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002047
Benjamin Kramer62460692015-04-25 14:46:53 +00002048 for (ISD::CondCode FPExpCCV4 :
2049 {ISD::SETOEQ, ISD::SETOGT, ISD::SETOLT, ISD::SETOGE, ISD::SETOLE,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002050 ISD::SETUO, ISD::SETO}) {
Benjamin Kramer62460692015-04-25 14:46:53 +00002051 setCondCodeAction(FPExpCCV4, MVT::f32, Expand);
2052 setCondCodeAction(FPExpCCV4, MVT::f64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002053 }
2054 }
2055
2056 // Handling of indexed loads/stores: default is "expand".
2057 //
Benjamin Kramer62460692015-04-25 14:46:53 +00002058 for (MVT LSXTy : {MVT::i8, MVT::i16, MVT::i32, MVT::i64}) {
2059 setIndexedLoadAction(ISD::POST_INC, LSXTy, Legal);
2060 setIndexedStoreAction(ISD::POST_INC, LSXTy, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002061 }
2062
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002063 if (UseHVXDbl) {
2064 for (MVT VT : {MVT::v128i8, MVT::v64i16, MVT::v32i32, MVT::v16i64}) {
2065 setIndexedLoadAction(ISD::POST_INC, VT, Legal);
2066 setIndexedStoreAction(ISD::POST_INC, VT, Legal);
2067 }
2068 }
2069
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002070 computeRegisterProperties(&HRI);
2071
2072 //
2073 // Library calls for unsupported operations
2074 //
2075 bool FastMath = EnableFastMath;
2076
Benjamin Kramera37c8092015-04-25 14:46:46 +00002077 setLibcallName(RTLIB::SDIV_I32, "__hexagon_divsi3");
2078 setLibcallName(RTLIB::SDIV_I64, "__hexagon_divdi3");
2079 setLibcallName(RTLIB::UDIV_I32, "__hexagon_udivsi3");
2080 setLibcallName(RTLIB::UDIV_I64, "__hexagon_udivdi3");
2081 setLibcallName(RTLIB::SREM_I32, "__hexagon_modsi3");
2082 setLibcallName(RTLIB::SREM_I64, "__hexagon_moddi3");
2083 setLibcallName(RTLIB::UREM_I32, "__hexagon_umodsi3");
2084 setLibcallName(RTLIB::UREM_I64, "__hexagon_umoddi3");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002085
Benjamin Kramera37c8092015-04-25 14:46:46 +00002086 setLibcallName(RTLIB::SINTTOFP_I128_F64, "__hexagon_floattidf");
2087 setLibcallName(RTLIB::SINTTOFP_I128_F32, "__hexagon_floattisf");
2088 setLibcallName(RTLIB::FPTOUINT_F32_I128, "__hexagon_fixunssfti");
2089 setLibcallName(RTLIB::FPTOUINT_F64_I128, "__hexagon_fixunsdfti");
2090 setLibcallName(RTLIB::FPTOSINT_F32_I128, "__hexagon_fixsfti");
2091 setLibcallName(RTLIB::FPTOSINT_F64_I128, "__hexagon_fixdfti");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002092
2093 if (IsV4) {
2094 // Handle single-precision floating point operations on V4.
Benjamin Kramera37c8092015-04-25 14:46:46 +00002095 if (FastMath) {
2096 setLibcallName(RTLIB::ADD_F32, "__hexagon_fast_addsf3");
2097 setLibcallName(RTLIB::SUB_F32, "__hexagon_fast_subsf3");
2098 setLibcallName(RTLIB::MUL_F32, "__hexagon_fast_mulsf3");
2099 setLibcallName(RTLIB::OGT_F32, "__hexagon_fast_gtsf2");
2100 setLibcallName(RTLIB::OLT_F32, "__hexagon_fast_ltsf2");
2101 // Double-precision compares.
2102 setLibcallName(RTLIB::OGT_F64, "__hexagon_fast_gtdf2");
2103 setLibcallName(RTLIB::OLT_F64, "__hexagon_fast_ltdf2");
2104 } else {
2105 setLibcallName(RTLIB::ADD_F32, "__hexagon_addsf3");
2106 setLibcallName(RTLIB::SUB_F32, "__hexagon_subsf3");
2107 setLibcallName(RTLIB::MUL_F32, "__hexagon_mulsf3");
2108 setLibcallName(RTLIB::OGT_F32, "__hexagon_gtsf2");
2109 setLibcallName(RTLIB::OLT_F32, "__hexagon_ltsf2");
2110 // Double-precision compares.
2111 setLibcallName(RTLIB::OGT_F64, "__hexagon_gtdf2");
2112 setLibcallName(RTLIB::OLT_F64, "__hexagon_ltdf2");
2113 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002114 }
2115
2116 // This is the only fast library function for sqrtd.
2117 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00002118 setLibcallName(RTLIB::SQRT_F64, "__hexagon_fast2_sqrtdf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002119
Benjamin Kramera37c8092015-04-25 14:46:46 +00002120 // Prefix is: nothing for "slow-math",
2121 // "fast2_" for V4 fast-math and V5+ fast-math double-precision
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002122 // (actually, keep fast-math and fast-math2 separate for now)
Benjamin Kramera37c8092015-04-25 14:46:46 +00002123 if (FastMath) {
2124 setLibcallName(RTLIB::ADD_F64, "__hexagon_fast_adddf3");
2125 setLibcallName(RTLIB::SUB_F64, "__hexagon_fast_subdf3");
2126 setLibcallName(RTLIB::MUL_F64, "__hexagon_fast_muldf3");
2127 setLibcallName(RTLIB::DIV_F64, "__hexagon_fast_divdf3");
2128 // Calling __hexagon_fast2_divsf3 with fast-math on V5 (ok).
2129 setLibcallName(RTLIB::DIV_F32, "__hexagon_fast_divsf3");
2130 } else {
2131 setLibcallName(RTLIB::ADD_F64, "__hexagon_adddf3");
2132 setLibcallName(RTLIB::SUB_F64, "__hexagon_subdf3");
2133 setLibcallName(RTLIB::MUL_F64, "__hexagon_muldf3");
2134 setLibcallName(RTLIB::DIV_F64, "__hexagon_divdf3");
2135 setLibcallName(RTLIB::DIV_F32, "__hexagon_divsf3");
2136 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002137
2138 if (Subtarget.hasV5TOps()) {
2139 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00002140 setLibcallName(RTLIB::SQRT_F32, "__hexagon_fast2_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002141 else
Benjamin Kramera37c8092015-04-25 14:46:46 +00002142 setLibcallName(RTLIB::SQRT_F32, "__hexagon_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002143 } else {
2144 // V4
Benjamin Kramera37c8092015-04-25 14:46:46 +00002145 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__hexagon_floatsisf");
2146 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__hexagon_floatsidf");
2147 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__hexagon_floatdisf");
2148 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__hexagon_floatdidf");
2149 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__hexagon_floatunsisf");
2150 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__hexagon_floatunsidf");
2151 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__hexagon_floatundisf");
2152 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__hexagon_floatundidf");
2153 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__hexagon_fixunssfsi");
2154 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__hexagon_fixunssfdi");
2155 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__hexagon_fixunsdfsi");
2156 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__hexagon_fixunsdfdi");
2157 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__hexagon_fixsfsi");
2158 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__hexagon_fixsfdi");
2159 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__hexagon_fixdfsi");
2160 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__hexagon_fixdfdi");
2161 setLibcallName(RTLIB::FPEXT_F32_F64, "__hexagon_extendsfdf2");
2162 setLibcallName(RTLIB::FPROUND_F64_F32, "__hexagon_truncdfsf2");
2163 setLibcallName(RTLIB::OEQ_F32, "__hexagon_eqsf2");
2164 setLibcallName(RTLIB::OEQ_F64, "__hexagon_eqdf2");
2165 setLibcallName(RTLIB::OGE_F32, "__hexagon_gesf2");
2166 setLibcallName(RTLIB::OGE_F64, "__hexagon_gedf2");
2167 setLibcallName(RTLIB::OLE_F32, "__hexagon_lesf2");
2168 setLibcallName(RTLIB::OLE_F64, "__hexagon_ledf2");
2169 setLibcallName(RTLIB::UNE_F32, "__hexagon_nesf2");
2170 setLibcallName(RTLIB::UNE_F64, "__hexagon_nedf2");
2171 setLibcallName(RTLIB::UO_F32, "__hexagon_unordsf2");
2172 setLibcallName(RTLIB::UO_F64, "__hexagon_unorddf2");
2173 setLibcallName(RTLIB::O_F32, "__hexagon_unordsf2");
2174 setLibcallName(RTLIB::O_F64, "__hexagon_unorddf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002175 }
2176
2177 // These cause problems when the shift amount is non-constant.
2178 setLibcallName(RTLIB::SHL_I128, nullptr);
2179 setLibcallName(RTLIB::SRL_I128, nullptr);
2180 setLibcallName(RTLIB::SRA_I128, nullptr);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002181}
2182
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002183
2184const char* HexagonTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00002185 switch ((HexagonISD::NodeType)Opcode) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002186 case HexagonISD::ALLOCA: return "HexagonISD::ALLOCA";
2187 case HexagonISD::ARGEXTEND: return "HexagonISD::ARGEXTEND";
2188 case HexagonISD::AT_GOT: return "HexagonISD::AT_GOT";
2189 case HexagonISD::AT_PCREL: return "HexagonISD::AT_PCREL";
2190 case HexagonISD::BARRIER: return "HexagonISD::BARRIER";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002191 case HexagonISD::CALLR: return "HexagonISD::CALLR";
2192 case HexagonISD::CALLv3nr: return "HexagonISD::CALLv3nr";
2193 case HexagonISD::CALLv3: return "HexagonISD::CALLv3";
2194 case HexagonISD::COMBINE: return "HexagonISD::COMBINE";
2195 case HexagonISD::CONST32_GP: return "HexagonISD::CONST32_GP";
2196 case HexagonISD::CONST32: return "HexagonISD::CONST32";
2197 case HexagonISD::CP: return "HexagonISD::CP";
2198 case HexagonISD::DCFETCH: return "HexagonISD::DCFETCH";
2199 case HexagonISD::EH_RETURN: return "HexagonISD::EH_RETURN";
2200 case HexagonISD::EXTRACTU: return "HexagonISD::EXTRACTU";
2201 case HexagonISD::EXTRACTURP: return "HexagonISD::EXTRACTURP";
2202 case HexagonISD::FCONST32: return "HexagonISD::FCONST32";
2203 case HexagonISD::INSERT: return "HexagonISD::INSERT";
2204 case HexagonISD::INSERTRP: return "HexagonISD::INSERTRP";
2205 case HexagonISD::JT: return "HexagonISD::JT";
2206 case HexagonISD::PACKHL: return "HexagonISD::PACKHL";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002207 case HexagonISD::POPCOUNT: return "HexagonISD::POPCOUNT";
2208 case HexagonISD::RET_FLAG: return "HexagonISD::RET_FLAG";
2209 case HexagonISD::SHUFFEB: return "HexagonISD::SHUFFEB";
2210 case HexagonISD::SHUFFEH: return "HexagonISD::SHUFFEH";
2211 case HexagonISD::SHUFFOB: return "HexagonISD::SHUFFOB";
2212 case HexagonISD::SHUFFOH: return "HexagonISD::SHUFFOH";
2213 case HexagonISD::TC_RETURN: return "HexagonISD::TC_RETURN";
2214 case HexagonISD::VCMPBEQ: return "HexagonISD::VCMPBEQ";
2215 case HexagonISD::VCMPBGT: return "HexagonISD::VCMPBGT";
2216 case HexagonISD::VCMPBGTU: return "HexagonISD::VCMPBGTU";
2217 case HexagonISD::VCMPHEQ: return "HexagonISD::VCMPHEQ";
2218 case HexagonISD::VCMPHGT: return "HexagonISD::VCMPHGT";
2219 case HexagonISD::VCMPHGTU: return "HexagonISD::VCMPHGTU";
2220 case HexagonISD::VCMPWEQ: return "HexagonISD::VCMPWEQ";
2221 case HexagonISD::VCMPWGT: return "HexagonISD::VCMPWGT";
2222 case HexagonISD::VCMPWGTU: return "HexagonISD::VCMPWGTU";
Krzysztof Parzyszekc168c012015-12-03 16:47:20 +00002223 case HexagonISD::VCOMBINE: return "HexagonISD::VCOMBINE";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002224 case HexagonISD::VSHLH: return "HexagonISD::VSHLH";
2225 case HexagonISD::VSHLW: return "HexagonISD::VSHLW";
2226 case HexagonISD::VSPLATB: return "HexagonISD::VSPLTB";
2227 case HexagonISD::VSPLATH: return "HexagonISD::VSPLATH";
2228 case HexagonISD::VSRAH: return "HexagonISD::VSRAH";
2229 case HexagonISD::VSRAW: return "HexagonISD::VSRAW";
2230 case HexagonISD::VSRLH: return "HexagonISD::VSRLH";
2231 case HexagonISD::VSRLW: return "HexagonISD::VSRLW";
2232 case HexagonISD::VSXTBH: return "HexagonISD::VSXTBH";
2233 case HexagonISD::VSXTBW: return "HexagonISD::VSXTBW";
Matthias Braund04893f2015-05-07 21:33:59 +00002234 case HexagonISD::OP_END: break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002235 }
Matthias Braund04893f2015-05-07 21:33:59 +00002236 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002237}
2238
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002239bool HexagonTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002240 EVT MTy1 = EVT::getEVT(Ty1);
2241 EVT MTy2 = EVT::getEVT(Ty2);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002242 if (!MTy1.isSimple() || !MTy2.isSimple())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002243 return false;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002244 return (MTy1.getSimpleVT() == MVT::i64) && (MTy2.getSimpleVT() == MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002245}
2246
2247bool HexagonTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002248 if (!VT1.isSimple() || !VT2.isSimple())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002249 return false;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002250 return (VT1.getSimpleVT() == MVT::i64) && (VT2.getSimpleVT() == MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002251}
2252
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002253// shouldExpandBuildVectorWithShuffles
2254// Should we expand the build vector with shuffles?
2255bool
2256HexagonTargetLowering::shouldExpandBuildVectorWithShuffles(EVT VT,
2257 unsigned DefinedValues) const {
2258
2259 // Hexagon vector shuffle operates on element sizes of bytes or halfwords
2260 EVT EltVT = VT.getVectorElementType();
2261 int EltBits = EltVT.getSizeInBits();
2262 if ((EltBits != 8) && (EltBits != 16))
2263 return false;
2264
2265 return TargetLowering::shouldExpandBuildVectorWithShuffles(VT, DefinedValues);
2266}
2267
2268// LowerVECTOR_SHUFFLE - Lower a vector shuffle (V1, V2, V3). V1 and
2269// V2 are the two vectors to select data from, V3 is the permutation.
2270static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
2271 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
2272 SDValue V1 = Op.getOperand(0);
2273 SDValue V2 = Op.getOperand(1);
2274 SDLoc dl(Op);
2275 EVT VT = Op.getValueType();
2276
Sanjay Patel57195842016-03-14 17:28:46 +00002277 if (V2.isUndef())
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002278 V2 = V1;
2279
2280 if (SVN->isSplat()) {
2281 int Lane = SVN->getSplatIndex();
2282 if (Lane == -1) Lane = 0;
2283
2284 // Test if V1 is a SCALAR_TO_VECTOR.
2285 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR)
2286 return createSplat(DAG, dl, VT, V1.getOperand(0));
2287
2288 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
2289 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
2290 // reaches it).
2291 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
2292 !isa<ConstantSDNode>(V1.getOperand(0))) {
2293 bool IsScalarToVector = true;
2294 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
Sanjay Patel75068522016-03-14 18:09:43 +00002295 if (!V1.getOperand(i).isUndef()) {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002296 IsScalarToVector = false;
2297 break;
2298 }
2299 if (IsScalarToVector)
2300 return createSplat(DAG, dl, VT, V1.getOperand(0));
2301 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002302 return createSplat(DAG, dl, VT, DAG.getConstant(Lane, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002303 }
2304
2305 // FIXME: We need to support more general vector shuffles. See
2306 // below the comment from the ARM backend that deals in the general
2307 // case with the vector shuffles. For now, let expand handle these.
2308 return SDValue();
2309
2310 // If the shuffle is not directly supported and it has 4 elements, use
2311 // the PerfectShuffle-generated table to synthesize it from other shuffles.
2312}
2313
2314// If BUILD_VECTOR has same base element repeated several times,
2315// report true.
2316static bool isCommonSplatElement(BuildVectorSDNode *BVN) {
2317 unsigned NElts = BVN->getNumOperands();
2318 SDValue V0 = BVN->getOperand(0);
2319
2320 for (unsigned i = 1, e = NElts; i != e; ++i) {
2321 if (BVN->getOperand(i) != V0)
2322 return false;
2323 }
2324 return true;
2325}
2326
2327// LowerVECTOR_SHIFT - Lower a vector shift. Try to convert
2328// <VT> = SHL/SRA/SRL <VT> by <VT> to Hexagon specific
2329// <VT> = SHL/SRA/SRL <VT> by <IT/i32>.
2330static SDValue LowerVECTOR_SHIFT(SDValue Op, SelectionDAG &DAG) {
2331 BuildVectorSDNode *BVN = 0;
2332 SDValue V1 = Op.getOperand(0);
2333 SDValue V2 = Op.getOperand(1);
2334 SDValue V3;
2335 SDLoc dl(Op);
2336 EVT VT = Op.getValueType();
2337
2338 if ((BVN = dyn_cast<BuildVectorSDNode>(V1.getNode())) &&
2339 isCommonSplatElement(BVN))
2340 V3 = V2;
2341 else if ((BVN = dyn_cast<BuildVectorSDNode>(V2.getNode())) &&
2342 isCommonSplatElement(BVN))
2343 V3 = V1;
2344 else
2345 return SDValue();
2346
2347 SDValue CommonSplat = BVN->getOperand(0);
2348 SDValue Result;
2349
2350 if (VT.getSimpleVT() == MVT::v4i16) {
2351 switch (Op.getOpcode()) {
2352 case ISD::SRA:
2353 Result = DAG.getNode(HexagonISD::VSRAH, dl, VT, V3, CommonSplat);
2354 break;
2355 case ISD::SHL:
2356 Result = DAG.getNode(HexagonISD::VSHLH, dl, VT, V3, CommonSplat);
2357 break;
2358 case ISD::SRL:
2359 Result = DAG.getNode(HexagonISD::VSRLH, dl, VT, V3, CommonSplat);
2360 break;
2361 default:
2362 return SDValue();
2363 }
2364 } else if (VT.getSimpleVT() == MVT::v2i32) {
2365 switch (Op.getOpcode()) {
2366 case ISD::SRA:
2367 Result = DAG.getNode(HexagonISD::VSRAW, dl, VT, V3, CommonSplat);
2368 break;
2369 case ISD::SHL:
2370 Result = DAG.getNode(HexagonISD::VSHLW, dl, VT, V3, CommonSplat);
2371 break;
2372 case ISD::SRL:
2373 Result = DAG.getNode(HexagonISD::VSRLW, dl, VT, V3, CommonSplat);
2374 break;
2375 default:
2376 return SDValue();
2377 }
2378 } else {
2379 return SDValue();
2380 }
2381
2382 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
2383}
2384
2385SDValue
2386HexagonTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
2387 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
2388 SDLoc dl(Op);
2389 EVT VT = Op.getValueType();
2390
2391 unsigned Size = VT.getSizeInBits();
2392
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002393 // Only handle vectors of 64 bits or shorter.
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002394 if (Size > 64)
2395 return SDValue();
2396
2397 APInt APSplatBits, APSplatUndef;
2398 unsigned SplatBitSize;
2399 bool HasAnyUndefs;
2400 unsigned NElts = BVN->getNumOperands();
2401
2402 // Try to generate a SPLAT instruction.
2403 if ((VT.getSimpleVT() == MVT::v4i8 || VT.getSimpleVT() == MVT::v4i16) &&
2404 (BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
2405 HasAnyUndefs, 0, true) && SplatBitSize <= 16)) {
2406 unsigned SplatBits = APSplatBits.getZExtValue();
2407 int32_t SextVal = ((int32_t) (SplatBits << (32 - SplatBitSize)) >>
2408 (32 - SplatBitSize));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002409 return createSplat(DAG, dl, VT, DAG.getConstant(SextVal, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002410 }
2411
2412 // Try to generate COMBINE to build v2i32 vectors.
2413 if (VT.getSimpleVT() == MVT::v2i32) {
2414 SDValue V0 = BVN->getOperand(0);
2415 SDValue V1 = BVN->getOperand(1);
2416
Sanjay Patel57195842016-03-14 17:28:46 +00002417 if (V0.isUndef())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002418 V0 = DAG.getConstant(0, dl, MVT::i32);
Sanjay Patel57195842016-03-14 17:28:46 +00002419 if (V1.isUndef())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002420 V1 = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002421
2422 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(V0);
2423 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(V1);
2424 // If the element isn't a constant, it is in a register:
2425 // generate a COMBINE Register Register instruction.
2426 if (!C0 || !C1)
2427 return DAG.getNode(HexagonISD::COMBINE, dl, VT, V1, V0);
2428
2429 // If one of the operands is an 8 bit integer constant, generate
2430 // a COMBINE Immediate Immediate instruction.
2431 if (isInt<8>(C0->getSExtValue()) ||
2432 isInt<8>(C1->getSExtValue()))
2433 return DAG.getNode(HexagonISD::COMBINE, dl, VT, V1, V0);
2434 }
2435
2436 // Try to generate a S2_packhl to build v2i16 vectors.
2437 if (VT.getSimpleVT() == MVT::v2i16) {
2438 for (unsigned i = 0, e = NElts; i != e; ++i) {
Sanjay Patel57195842016-03-14 17:28:46 +00002439 if (BVN->getOperand(i).isUndef())
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002440 continue;
2441 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(BVN->getOperand(i));
2442 // If the element isn't a constant, it is in a register:
2443 // generate a S2_packhl instruction.
2444 if (!Cst) {
2445 SDValue pack = DAG.getNode(HexagonISD::PACKHL, dl, MVT::v4i16,
2446 BVN->getOperand(1), BVN->getOperand(0));
2447
2448 return DAG.getTargetExtractSubreg(Hexagon::subreg_loreg, dl, MVT::v2i16,
2449 pack);
2450 }
2451 }
2452 }
2453
2454 // In the general case, generate a CONST32 or a CONST64 for constant vectors,
2455 // and insert_vector_elt for all the other cases.
2456 uint64_t Res = 0;
2457 unsigned EltSize = Size / NElts;
2458 SDValue ConstVal;
2459 uint64_t Mask = ~uint64_t(0ULL) >> (64 - EltSize);
2460 bool HasNonConstantElements = false;
2461
2462 for (unsigned i = 0, e = NElts; i != e; ++i) {
2463 // LLVM's BUILD_VECTOR operands are in Little Endian mode, whereas Hexagon's
2464 // combine, const64, etc. are Big Endian.
2465 unsigned OpIdx = NElts - i - 1;
2466 SDValue Operand = BVN->getOperand(OpIdx);
Sanjay Patel57195842016-03-14 17:28:46 +00002467 if (Operand.isUndef())
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002468 continue;
2469
2470 int64_t Val = 0;
2471 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Operand))
2472 Val = Cst->getSExtValue();
2473 else
2474 HasNonConstantElements = true;
2475
2476 Val &= Mask;
2477 Res = (Res << EltSize) | Val;
2478 }
2479
2480 if (Size == 64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002481 ConstVal = DAG.getConstant(Res, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002482 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002483 ConstVal = DAG.getConstant(Res, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002484
2485 // When there are non constant operands, add them with INSERT_VECTOR_ELT to
2486 // ConstVal, the constant part of the vector.
2487 if (HasNonConstantElements) {
2488 EVT EltVT = VT.getVectorElementType();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002489 SDValue Width = DAG.getConstant(EltVT.getSizeInBits(), dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002490 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002491 DAG.getConstant(32, dl, MVT::i64));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002492
2493 for (unsigned i = 0, e = NElts; i != e; ++i) {
2494 // LLVM's BUILD_VECTOR operands are in Little Endian mode, whereas Hexagon
2495 // is Big Endian.
2496 unsigned OpIdx = NElts - i - 1;
2497 SDValue Operand = BVN->getOperand(OpIdx);
Benjamin Kramer619c4e52015-04-10 11:24:51 +00002498 if (isa<ConstantSDNode>(Operand))
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002499 // This operand is already in ConstVal.
2500 continue;
2501
2502 if (VT.getSizeInBits() == 64 &&
2503 Operand.getValueType().getSizeInBits() == 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002504 SDValue C = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002505 Operand = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, Operand);
2506 }
2507
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002508 SDValue Idx = DAG.getConstant(OpIdx, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002509 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i64, Idx, Width);
2510 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2511 const SDValue Ops[] = {ConstVal, Operand, Combined};
2512
2513 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002514 ConstVal = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002515 else
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002516 ConstVal = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002517 }
2518 }
2519
2520 return DAG.getNode(ISD::BITCAST, dl, VT, ConstVal);
2521}
2522
2523SDValue
2524HexagonTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
2525 SelectionDAG &DAG) const {
2526 SDLoc dl(Op);
Krzysztof Parzyszekc168c012015-12-03 16:47:20 +00002527 bool UseHVX = Subtarget.useHVXOps();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002528 EVT VT = Op.getValueType();
2529 unsigned NElts = Op.getNumOperands();
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002530 SDValue Vec0 = Op.getOperand(0);
2531 EVT VecVT = Vec0.getValueType();
2532 unsigned Width = VecVT.getSizeInBits();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002533
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002534 if (NElts == 2) {
2535 MVT ST = VecVT.getSimpleVT();
2536 // We are trying to concat two v2i16 to a single v4i16, or two v4i8
2537 // into a single v8i8.
2538 if (ST == MVT::v2i16 || ST == MVT::v4i8)
2539 return DAG.getNode(HexagonISD::COMBINE, dl, VT, Op.getOperand(1), Vec0);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002540
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002541 if (UseHVX) {
2542 assert((Width == 64*8 && Subtarget.useHVXSglOps()) ||
2543 (Width == 128*8 && Subtarget.useHVXDblOps()));
2544 SDValue Vec1 = Op.getOperand(1);
2545 MVT OpTy = Subtarget.useHVXSglOps() ? MVT::v16i32 : MVT::v32i32;
2546 MVT ReTy = Subtarget.useHVXSglOps() ? MVT::v32i32 : MVT::v64i32;
2547 SDValue B0 = DAG.getNode(ISD::BITCAST, dl, OpTy, Vec0);
2548 SDValue B1 = DAG.getNode(ISD::BITCAST, dl, OpTy, Vec1);
2549 SDValue VC = DAG.getNode(HexagonISD::VCOMBINE, dl, ReTy, B1, B0);
2550 return DAG.getNode(ISD::BITCAST, dl, VT, VC);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002551 }
2552 }
2553
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002554 if (VT.getSizeInBits() != 32 && VT.getSizeInBits() != 64)
2555 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002556
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002557 SDValue C0 = DAG.getConstant(0, dl, MVT::i64);
2558 SDValue C32 = DAG.getConstant(32, dl, MVT::i64);
2559 SDValue W = DAG.getConstant(Width, dl, MVT::i64);
2560 // Create the "width" part of the argument to insert_rp/insertp_rp.
2561 SDValue S = DAG.getNode(ISD::SHL, dl, MVT::i64, W, C32);
2562 SDValue V = C0;
2563
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002564 for (unsigned i = 0, e = NElts; i != e; ++i) {
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002565 unsigned N = NElts-i-1;
2566 SDValue OpN = Op.getOperand(N);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002567
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002568 if (VT.getSizeInBits() == 64 && OpN.getValueType().getSizeInBits() == 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002569 SDValue C = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002570 OpN = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, OpN);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002571 }
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002572 SDValue Idx = DAG.getConstant(N, dl, MVT::i64);
2573 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i64, Idx, W);
2574 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, S, Offset);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002575 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002576 V = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, {V, OpN, Or});
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002577 else
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002578 V = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, {V, OpN, Or});
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002579 }
2580
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002581 return DAG.getNode(ISD::BITCAST, dl, VT, V);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002582}
2583
2584SDValue
2585HexagonTargetLowering::LowerEXTRACT_VECTOR(SDValue Op,
2586 SelectionDAG &DAG) const {
2587 EVT VT = Op.getValueType();
2588 int VTN = VT.isVector() ? VT.getVectorNumElements() : 1;
2589 SDLoc dl(Op);
2590 SDValue Idx = Op.getOperand(1);
2591 SDValue Vec = Op.getOperand(0);
2592 EVT VecVT = Vec.getValueType();
2593 EVT EltVT = VecVT.getVectorElementType();
2594 int EltSize = EltVT.getSizeInBits();
2595 SDValue Width = DAG.getConstant(Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT ?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002596 EltSize : VTN * EltSize, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002597
2598 // Constant element number.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002599 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Idx)) {
2600 uint64_t X = CI->getZExtValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002601 SDValue Offset = DAG.getConstant(X * EltSize, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002602 const SDValue Ops[] = {Vec, Width, Offset};
2603
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002604 ConstantSDNode *CW = dyn_cast<ConstantSDNode>(Width);
2605 assert(CW && "Non constant width in LowerEXTRACT_VECTOR");
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002606
2607 SDValue N;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002608 MVT SVT = VecVT.getSimpleVT();
2609 uint64_t W = CW->getZExtValue();
2610
2611 if (W == 32) {
2612 // Translate this node into EXTRACT_SUBREG.
2613 unsigned Subreg = (X == 0) ? Hexagon::subreg_loreg : 0;
2614
2615 if (X == 0)
2616 Subreg = Hexagon::subreg_loreg;
2617 else if (SVT == MVT::v2i32 && X == 1)
2618 Subreg = Hexagon::subreg_hireg;
2619 else if (SVT == MVT::v4i16 && X == 2)
2620 Subreg = Hexagon::subreg_hireg;
2621 else if (SVT == MVT::v8i8 && X == 4)
2622 Subreg = Hexagon::subreg_hireg;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002623 else
2624 llvm_unreachable("Bad offset");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002625 N = DAG.getTargetExtractSubreg(Subreg, dl, MVT::i32, Vec);
2626
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002627 } else if (VecVT.getSizeInBits() == 32) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002628 N = DAG.getNode(HexagonISD::EXTRACTU, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002629 } else {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002630 N = DAG.getNode(HexagonISD::EXTRACTU, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002631 if (VT.getSizeInBits() == 32)
2632 N = DAG.getTargetExtractSubreg(Hexagon::subreg_loreg, dl, MVT::i32, N);
2633 }
2634
2635 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2636 }
2637
2638 // Variable element number.
2639 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i32, Idx,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002640 DAG.getConstant(EltSize, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002641 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002642 DAG.getConstant(32, dl, MVT::i64));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002643 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2644
2645 const SDValue Ops[] = {Vec, Combined};
2646
2647 SDValue N;
2648 if (VecVT.getSizeInBits() == 32) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002649 N = DAG.getNode(HexagonISD::EXTRACTURP, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002650 } else {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002651 N = DAG.getNode(HexagonISD::EXTRACTURP, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002652 if (VT.getSizeInBits() == 32)
2653 N = DAG.getTargetExtractSubreg(Hexagon::subreg_loreg, dl, MVT::i32, N);
2654 }
2655 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2656}
2657
2658SDValue
2659HexagonTargetLowering::LowerINSERT_VECTOR(SDValue Op,
2660 SelectionDAG &DAG) const {
2661 EVT VT = Op.getValueType();
2662 int VTN = VT.isVector() ? VT.getVectorNumElements() : 1;
2663 SDLoc dl(Op);
2664 SDValue Vec = Op.getOperand(0);
2665 SDValue Val = Op.getOperand(1);
2666 SDValue Idx = Op.getOperand(2);
2667 EVT VecVT = Vec.getValueType();
2668 EVT EltVT = VecVT.getVectorElementType();
2669 int EltSize = EltVT.getSizeInBits();
2670 SDValue Width = DAG.getConstant(Op.getOpcode() == ISD::INSERT_VECTOR_ELT ?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002671 EltSize : VTN * EltSize, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002672
2673 if (ConstantSDNode *C = cast<ConstantSDNode>(Idx)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002674 SDValue Offset = DAG.getConstant(C->getSExtValue() * EltSize, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002675 const SDValue Ops[] = {Vec, Val, Width, Offset};
2676
2677 SDValue N;
2678 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002679 N = DAG.getNode(HexagonISD::INSERT, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002680 else
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002681 N = DAG.getNode(HexagonISD::INSERT, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002682
2683 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2684 }
2685
2686 // Variable element number.
2687 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i32, Idx,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002688 DAG.getConstant(EltSize, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002689 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002690 DAG.getConstant(32, dl, MVT::i64));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002691 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2692
2693 if (VT.getSizeInBits() == 64 &&
2694 Val.getValueType().getSizeInBits() == 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002695 SDValue C = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002696 Val = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, Val);
2697 }
2698
2699 const SDValue Ops[] = {Vec, Val, Combined};
2700
2701 SDValue N;
2702 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002703 N = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002704 else
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002705 N = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002706
2707 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2708}
2709
Tim Northovera4415852013-08-06 09:12:35 +00002710bool
2711HexagonTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
2712 // Assuming the caller does not have either a signext or zeroext modifier, and
2713 // only one value is accepted, any reasonable truncation is allowed.
2714 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
2715 return false;
2716
2717 // FIXME: in principle up to 64-bit could be made safe, but it would be very
2718 // fragile at the moment: any support for multiple value returns would be
2719 // liable to disallow tail calls involving i64 -> iN truncation in many cases.
2720 return Ty1->getPrimitiveSizeInBits() <= 32;
2721}
2722
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002723SDValue
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002724HexagonTargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
2725 SDValue Chain = Op.getOperand(0);
2726 SDValue Offset = Op.getOperand(1);
2727 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002728 SDLoc dl(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002729 auto PtrVT = getPointerTy(DAG.getDataLayout());
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002730
2731 // Mark function as containing a call to EH_RETURN.
2732 HexagonMachineFunctionInfo *FuncInfo =
2733 DAG.getMachineFunction().getInfo<HexagonMachineFunctionInfo>();
2734 FuncInfo->setHasEHReturn();
2735
2736 unsigned OffsetReg = Hexagon::R28;
2737
Mehdi Amini44ede332015-07-09 02:09:04 +00002738 SDValue StoreAddr =
2739 DAG.getNode(ISD::ADD, dl, PtrVT, DAG.getRegister(Hexagon::R30, PtrVT),
2740 DAG.getIntPtrConstant(4, dl));
Justin Lebar9c375812016-07-15 18:27:10 +00002741 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo());
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002742 Chain = DAG.getCopyToReg(Chain, dl, OffsetReg, Offset);
2743
2744 // Not needed we already use it as explict input to EH_RETURN.
2745 // MF.getRegInfo().addLiveOut(OffsetReg);
2746
2747 return DAG.getNode(HexagonISD::EH_RETURN, dl, MVT::Other, Chain);
2748}
2749
2750SDValue
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002751HexagonTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002752 unsigned Opc = Op.getOpcode();
2753 switch (Opc) {
2754 default:
2755#ifndef NDEBUG
2756 Op.getNode()->dumpr(&DAG);
2757 if (Opc > HexagonISD::OP_BEGIN && Opc < HexagonISD::OP_END)
2758 errs() << "Check for a non-legal type in this operation\n";
2759#endif
2760 llvm_unreachable("Should not custom lower this!");
2761 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
2762 case ISD::INSERT_SUBVECTOR: return LowerINSERT_VECTOR(Op, DAG);
2763 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR(Op, DAG);
2764 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_VECTOR(Op, DAG);
2765 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR(Op, DAG);
2766 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2767 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002768 case ISD::SRA:
2769 case ISD::SHL:
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002770 case ISD::SRL: return LowerVECTOR_SHIFT(Op, DAG);
2771 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002772 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002773 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
2774 // Frame & Return address. Currently unimplemented.
2775 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
2776 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00002777 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002778 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
2779 case ISD::GlobalAddress: return LowerGLOBALADDRESS(Op, DAG);
2780 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002781 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002782 case ISD::VASTART: return LowerVASTART(Op, DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002783 // Custom lower some vector loads.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002784 case ISD::LOAD: return LowerLOAD(Op, DAG);
2785 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
2786 case ISD::SETCC: return LowerSETCC(Op, DAG);
2787 case ISD::VSELECT: return LowerVSELECT(Op, DAG);
2788 case ISD::CTPOP: return LowerCTPOP(Op, DAG);
2789 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00002790 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002791 case ISD::INLINEASM: return LowerINLINEASM(Op, DAG);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00002792 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002793 }
2794}
2795
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002796/// Returns relocation base for the given PIC jumptable.
2797SDValue
2798HexagonTargetLowering::getPICJumpTableRelocBase(SDValue Table,
2799 SelectionDAG &DAG) const {
2800 int Idx = cast<JumpTableSDNode>(Table)->getIndex();
2801 EVT VT = Table.getValueType();
2802 SDValue T = DAG.getTargetJumpTable(Idx, VT, HexagonII::MO_PCREL);
2803 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Table), VT, T);
2804}
2805
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00002806MachineBasicBlock *HexagonTargetLowering::EmitInstrWithCustomInserter(
2807 MachineInstr &MI, MachineBasicBlock *BB) const {
2808 switch (MI.getOpcode()) {
2809 case Hexagon::ALLOCA: {
2810 MachineFunction *MF = BB->getParent();
2811 auto *FuncInfo = MF->getInfo<HexagonMachineFunctionInfo>();
2812 FuncInfo->addAllocaAdjustInst(&MI);
2813 return BB;
2814 }
2815 default:
2816 llvm_unreachable("Unexpected instr type to insert");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002817 } // switch
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002818}
2819
2820//===----------------------------------------------------------------------===//
2821// Inline Assembly Support
2822//===----------------------------------------------------------------------===//
2823
Krzysztof Parzyszekca3b5322016-05-18 14:34:51 +00002824TargetLowering::ConstraintType
2825HexagonTargetLowering::getConstraintType(StringRef Constraint) const {
2826 if (Constraint.size() == 1) {
2827 switch (Constraint[0]) {
2828 case 'q':
2829 case 'v':
2830 if (Subtarget.useHVXOps())
2831 return C_Register;
2832 break;
2833 }
2834 }
2835 return TargetLowering::getConstraintType(Constraint);
2836}
2837
Eric Christopher11e4df72015-02-26 22:38:43 +00002838std::pair<unsigned, const TargetRegisterClass *>
2839HexagonTargetLowering::getRegForInlineAsmConstraint(
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00002840 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002841 bool UseHVX = Subtarget.useHVXOps(), UseHVXDbl = Subtarget.useHVXDblOps();
2842
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002843 if (Constraint.size() == 1) {
2844 switch (Constraint[0]) {
2845 case 'r': // R0-R31
Chad Rosier295bd432013-06-22 18:37:38 +00002846 switch (VT.SimpleTy) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002847 default:
Craig Toppere55c5562012-02-07 02:50:20 +00002848 llvm_unreachable("getRegForInlineAsmConstraint Unhandled data type");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002849 case MVT::i32:
2850 case MVT::i16:
2851 case MVT::i8:
Sirish Pande69295b82012-05-10 20:20:25 +00002852 case MVT::f32:
Craig Topperc7242e02012-04-20 07:30:17 +00002853 return std::make_pair(0U, &Hexagon::IntRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002854 case MVT::i64:
Sirish Pande69295b82012-05-10 20:20:25 +00002855 case MVT::f64:
Craig Topperc7242e02012-04-20 07:30:17 +00002856 return std::make_pair(0U, &Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002857 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002858 case 'q': // q0-q3
2859 switch (VT.SimpleTy) {
2860 default:
2861 llvm_unreachable("getRegForInlineAsmConstraint Unhandled data type");
2862 case MVT::v1024i1:
2863 case MVT::v512i1:
2864 case MVT::v32i16:
2865 case MVT::v16i32:
2866 case MVT::v64i8:
2867 case MVT::v8i64:
2868 return std::make_pair(0U, &Hexagon::VecPredRegsRegClass);
2869 }
2870 case 'v': // V0-V31
2871 switch (VT.SimpleTy) {
2872 default:
2873 llvm_unreachable("getRegForInlineAsmConstraint Unhandled data type");
2874 case MVT::v16i32:
2875 case MVT::v32i16:
2876 case MVT::v64i8:
2877 case MVT::v8i64:
2878 return std::make_pair(0U, &Hexagon::VectorRegsRegClass);
2879 case MVT::v32i32:
2880 case MVT::v64i16:
2881 case MVT::v16i64:
2882 case MVT::v128i8:
2883 if (Subtarget.hasV60TOps() && UseHVX && UseHVXDbl)
2884 return std::make_pair(0U, &Hexagon::VectorRegs128BRegClass);
2885 else
2886 return std::make_pair(0U, &Hexagon::VecDblRegsRegClass);
2887 case MVT::v256i8:
2888 case MVT::v128i16:
2889 case MVT::v64i32:
2890 case MVT::v32i64:
2891 return std::make_pair(0U, &Hexagon::VecDblRegs128BRegClass);
2892 }
2893
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002894 default:
Craig Toppere55c5562012-02-07 02:50:20 +00002895 llvm_unreachable("Unknown asm register class");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002896 }
2897 }
2898
Eric Christopher11e4df72015-02-26 22:38:43 +00002899 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002900}
2901
Sirish Pande69295b82012-05-10 20:20:25 +00002902/// isFPImmLegal - Returns true if the target can instruction select the
2903/// specified FP immediate natively. If false, the legalizer will
2904/// materialize the FP immediate as a load from a constant pool.
2905bool HexagonTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002906 return Subtarget.hasV5TOps();
Sirish Pande69295b82012-05-10 20:20:25 +00002907}
2908
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002909/// isLegalAddressingMode - Return true if the addressing mode represented by
2910/// AM is legal for this target, for a load/store of the specified type.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +00002911bool HexagonTargetLowering::isLegalAddressingMode(const DataLayout &DL,
2912 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +00002913 unsigned AS) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002914 // Allows a signed-extended 11-bit immediate field.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002915 if (AM.BaseOffs <= -(1LL << 13) || AM.BaseOffs >= (1LL << 13)-1)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002916 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002917
2918 // No global is ever allowed as a base.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002919 if (AM.BaseGV)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002920 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002921
2922 int Scale = AM.Scale;
2923 if (Scale < 0) Scale = -Scale;
2924 switch (Scale) {
2925 case 0: // No scale reg, "r+i", "r", or just "i".
2926 break;
2927 default: // No scaled addressing mode.
2928 return false;
2929 }
2930 return true;
2931}
2932
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002933/// Return true if folding a constant offset with the given GlobalAddress is
2934/// legal. It is frequently not legal in PIC relocation models.
2935bool HexagonTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA)
2936 const {
2937 return HTM.getRelocationModel() == Reloc::Static;
2938}
2939
2940
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002941/// isLegalICmpImmediate - Return true if the specified immediate is legal
2942/// icmp immediate, that is the target has icmp instructions which can compare
2943/// a register against the immediate without having to materialize the
2944/// immediate into a register.
2945bool HexagonTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
2946 return Imm >= -512 && Imm <= 511;
2947}
2948
2949/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2950/// for tail call optimization. Targets which want to do tail call
2951/// optimization should implement this function.
2952bool HexagonTargetLowering::IsEligibleForTailCallOptimization(
2953 SDValue Callee,
2954 CallingConv::ID CalleeCC,
2955 bool isVarArg,
2956 bool isCalleeStructRet,
2957 bool isCallerStructRet,
2958 const SmallVectorImpl<ISD::OutputArg> &Outs,
2959 const SmallVectorImpl<SDValue> &OutVals,
2960 const SmallVectorImpl<ISD::InputArg> &Ins,
2961 SelectionDAG& DAG) const {
2962 const Function *CallerF = DAG.getMachineFunction().getFunction();
2963 CallingConv::ID CallerCC = CallerF->getCallingConv();
2964 bool CCMatch = CallerCC == CalleeCC;
2965
2966 // ***************************************************************************
2967 // Look for obvious safe cases to perform tail call optimization that do not
2968 // require ABI changes.
2969 // ***************************************************************************
2970
2971 // If this is a tail call via a function pointer, then don't do it!
Craig Topper66059c92015-11-18 07:07:59 +00002972 if (!(isa<GlobalAddressSDNode>(Callee)) &&
2973 !(isa<ExternalSymbolSDNode>(Callee))) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002974 return false;
2975 }
2976
2977 // Do not optimize if the calling conventions do not match.
2978 if (!CCMatch)
2979 return false;
2980
2981 // Do not tail call optimize vararg calls.
2982 if (isVarArg)
2983 return false;
2984
2985 // Also avoid tail call optimization if either caller or callee uses struct
2986 // return semantics.
2987 if (isCalleeStructRet || isCallerStructRet)
2988 return false;
2989
2990 // In addition to the cases above, we also disable Tail Call Optimization if
2991 // the calling convention code that at least one outgoing argument needs to
2992 // go on the stack. We cannot check that here because at this point that
2993 // information is not available.
2994 return true;
2995}
Colin LeMahieu025f8602014-12-08 21:19:18 +00002996
2997// Return true when the given node fits in a positive half word.
2998bool llvm::isPositiveHalfWord(SDNode *N) {
2999 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N);
3000 if (CN && CN->getSExtValue() > 0 && isInt<16>(CN->getSExtValue()))
3001 return true;
3002
3003 switch (N->getOpcode()) {
3004 default:
3005 return false;
3006 case ISD::SIGN_EXTEND_INREG:
3007 return true;
3008 }
3009}
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003010
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +00003011bool HexagonTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
3012 unsigned AS, unsigned Align, bool *Fast) const {
3013 if (Fast)
3014 *Fast = false;
3015
3016 switch (VT.getSimpleVT().SimpleTy) {
3017 default:
3018 return false;
3019 case MVT::v64i8:
3020 case MVT::v128i8:
3021 case MVT::v256i8:
3022 case MVT::v32i16:
3023 case MVT::v64i16:
3024 case MVT::v128i16:
3025 case MVT::v16i32:
3026 case MVT::v32i32:
3027 case MVT::v64i32:
3028 case MVT::v8i64:
3029 case MVT::v16i64:
3030 case MVT::v32i64:
3031 return true;
3032 }
3033 return false;
3034}
3035
3036
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003037std::pair<const TargetRegisterClass*, uint8_t>
3038HexagonTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
3039 MVT VT) const {
3040 const TargetRegisterClass *RRC = nullptr;
3041
3042 uint8_t Cost = 1;
3043 switch (VT.SimpleTy) {
3044 default:
3045 return TargetLowering::findRepresentativeClass(TRI, VT);
3046 case MVT::v64i8:
3047 case MVT::v32i16:
3048 case MVT::v16i32:
3049 case MVT::v8i64:
3050 RRC = &Hexagon::VectorRegsRegClass;
3051 break;
3052 case MVT::v128i8:
3053 case MVT::v64i16:
3054 case MVT::v32i32:
3055 case MVT::v16i64:
3056 if (Subtarget.hasV60TOps() && Subtarget.useHVXOps() &&
3057 Subtarget.useHVXDblOps())
3058 RRC = &Hexagon::VectorRegs128BRegClass;
3059 else
3060 RRC = &Hexagon::VecDblRegsRegClass;
3061 break;
3062 case MVT::v256i8:
3063 case MVT::v128i16:
3064 case MVT::v64i32:
3065 case MVT::v32i64:
3066 RRC = &Hexagon::VecDblRegs128BRegClass;
3067 break;
3068 }
3069 return std::make_pair(RRC, Cost);
3070}
3071
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003072Value *HexagonTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
3073 AtomicOrdering Ord) const {
3074 BasicBlock *BB = Builder.GetInsertBlock();
3075 Module *M = BB->getParent()->getParent();
3076 Type *Ty = cast<PointerType>(Addr->getType())->getElementType();
3077 unsigned SZ = Ty->getPrimitiveSizeInBits();
3078 assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic loads supported");
3079 Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_L2_loadw_locked
3080 : Intrinsic::hexagon_L4_loadd_locked;
3081 Value *Fn = Intrinsic::getDeclaration(M, IntID);
3082 return Builder.CreateCall(Fn, Addr, "larx");
3083}
3084
3085/// Perform a store-conditional operation to Addr. Return the status of the
3086/// store. This should be 0 if the store succeeded, non-zero otherwise.
3087Value *HexagonTargetLowering::emitStoreConditional(IRBuilder<> &Builder,
3088 Value *Val, Value *Addr, AtomicOrdering Ord) const {
3089 BasicBlock *BB = Builder.GetInsertBlock();
3090 Module *M = BB->getParent()->getParent();
3091 Type *Ty = Val->getType();
3092 unsigned SZ = Ty->getPrimitiveSizeInBits();
3093 assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic stores supported");
3094 Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_S2_storew_locked
3095 : Intrinsic::hexagon_S4_stored_locked;
3096 Value *Fn = Intrinsic::getDeclaration(M, IntID);
3097 Value *Call = Builder.CreateCall(Fn, {Addr, Val}, "stcx");
3098 Value *Cmp = Builder.CreateICmpEQ(Call, Builder.getInt32(0), "");
3099 Value *Ext = Builder.CreateZExt(Cmp, Type::getInt32Ty(M->getContext()));
3100 return Ext;
3101}
3102
Ahmed Bougacha52468672015-09-11 17:08:28 +00003103TargetLowering::AtomicExpansionKind
3104HexagonTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003105 // Do not expand loads and stores that don't exceed 64 bits.
Ahmed Bougacha52468672015-09-11 17:08:28 +00003106 return LI->getType()->getPrimitiveSizeInBits() > 64
Tim Northoverf520eff2015-12-02 18:12:57 +00003107 ? AtomicExpansionKind::LLOnly
Ahmed Bougacha52468672015-09-11 17:08:28 +00003108 : AtomicExpansionKind::None;
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003109}
3110
3111bool HexagonTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
3112 // Do not expand loads and stores that don't exceed 64 bits.
3113 return SI->getValueOperand()->getType()->getPrimitiveSizeInBits() > 64;
3114}
Krzysztof Parzyszekf228c952016-06-22 16:07:10 +00003115
3116bool HexagonTargetLowering::shouldExpandAtomicCmpXchgInIR(
3117 AtomicCmpXchgInst *AI) const {
3118 const DataLayout &DL = AI->getModule()->getDataLayout();
3119 unsigned Size = DL.getTypeStoreSize(AI->getCompareOperand()->getType());
3120 return Size >= 4 && Size <= 8;
3121}