blob: 3edd97a6b21dcacc4e60962bd67edba0aeb32f2c [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) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000450 if (unsigned Reg = State.AllocateReg(Hexagon::R0)) {
451 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
452 return false;
453 }
454 }
455
456 unsigned Offset = State.AllocateStack(4, 4);
457 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
458 return false;
459}
460
461static bool RetCC_Hexagon64(unsigned ValNo, MVT ValVT,
462 MVT LocVT, CCValAssign::LocInfo LocInfo,
463 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Sirish Pande69295b82012-05-10 20:20:25 +0000464 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000465 if (unsigned Reg = State.AllocateReg(Hexagon::D0)) {
466 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
467 return false;
468 }
469 }
470
471 unsigned Offset = State.AllocateStack(8, 8);
472 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
473 return false;
474}
475
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000476static bool RetCC_HexagonVector(unsigned ValNo, MVT ValVT,
477 MVT LocVT, CCValAssign::LocInfo LocInfo,
478 ISD::ArgFlagsTy ArgFlags, CCState &State) {
479 auto &MF = State.getMachineFunction();
480 auto &HST = MF.getSubtarget<HexagonSubtarget>();
481 bool UseHVX = HST.useHVXOps();
482 bool UseHVXDbl = HST.useHVXDblOps();
483
484 unsigned OffSiz = 64;
485 if (LocVT == MVT::v16i32) {
486 if (unsigned Reg = State.AllocateReg(Hexagon::V0)) {
487 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
488 return false;
489 }
490 } else if (LocVT == MVT::v32i32) {
491 unsigned Req = (UseHVX && UseHVXDbl) ? Hexagon::V0 : Hexagon::W0;
492 if (unsigned Reg = State.AllocateReg(Req)) {
493 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
494 return false;
495 }
496 OffSiz = 128;
497 } else if (LocVT == MVT::v64i32) {
498 if (unsigned Reg = State.AllocateReg(Hexagon::W0)) {
499 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
500 return false;
501 }
502 OffSiz = 256;
503 }
504
505 unsigned Offset = State.AllocateStack(OffSiz, OffSiz);
506 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
507 return false;
508}
509
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000510void HexagonTargetLowering::promoteLdStType(EVT VT, EVT PromotedLdStVT) {
511 if (VT != PromotedLdStVT) {
512 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
513 AddPromotedToType(ISD::LOAD, VT.getSimpleVT(),
514 PromotedLdStVT.getSimpleVT());
515
516 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
517 AddPromotedToType(ISD::STORE, VT.getSimpleVT(),
518 PromotedLdStVT.getSimpleVT());
519 }
520}
521
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000522SDValue
523HexagonTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG)
524const {
525 return SDValue();
526}
527
528/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
529/// by "Src" to address "Dst" of size "Size". Alignment information is
530/// specified by the specific parameter attribute. The copy will be passed as
531/// a byval function parameter. Sometimes what we are copying is the end of a
532/// larger object, the part that does not fit in registers.
533static SDValue
534CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
535 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000536 SDLoc dl) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000537
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000538 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000539 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
540 /*isVolatile=*/false, /*AlwaysInline=*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +0000541 /*isTailCall=*/false,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000542 MachinePointerInfo(), MachinePointerInfo());
543}
544
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000545static bool IsHvxVectorType(MVT ty) {
546 return (ty == MVT::v8i64 || ty == MVT::v16i32 || ty == MVT::v32i16 ||
547 ty == MVT::v64i8 ||
548 ty == MVT::v16i64 || ty == MVT::v32i32 || ty == MVT::v64i16 ||
549 ty == MVT::v128i8 ||
550 ty == MVT::v32i64 || ty == MVT::v64i32 || ty == MVT::v128i16 ||
551 ty == MVT::v256i8 ||
552 ty == MVT::v512i1 || ty == MVT::v1024i1);
553}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000554
555// LowerReturn - Lower ISD::RET. If a struct is larger than 8 bytes and is
556// passed by value, the function prototype is modified to return void and
557// the value is stored in memory pointed by a pointer passed by caller.
558SDValue
559HexagonTargetLowering::LowerReturn(SDValue Chain,
560 CallingConv::ID CallConv, bool isVarArg,
561 const SmallVectorImpl<ISD::OutputArg> &Outs,
562 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000563 SDLoc dl, SelectionDAG &DAG) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000564
565 // CCValAssign - represent the assignment of the return value to locations.
566 SmallVector<CCValAssign, 16> RVLocs;
567
568 // CCState - Info about the registers and stack slot.
Eric Christopherb5217502014-08-06 18:45:26 +0000569 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
570 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000571
572 // Analyze return values of ISD::RET
573 CCInfo.AnalyzeReturn(Outs, RetCC_Hexagon);
574
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000575 SDValue Flag;
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000576 SmallVector<SDValue, 4> RetOps(1, Chain);
577
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000578 // Copy the result values into the output registers.
579 for (unsigned i = 0; i != RVLocs.size(); ++i) {
580 CCValAssign &VA = RVLocs[i];
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000581
582 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), OutVals[i], Flag);
583
584 // Guarantee that all emitted copies are stuck together with flags.
585 Flag = Chain.getValue(1);
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000586 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000587 }
588
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000589 RetOps[0] = Chain; // Update chain.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000590
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000591 // Add the flag if we have it.
592 if (Flag.getNode())
593 RetOps.push_back(Flag);
594
Craig Topper48d114b2014-04-26 18:35:24 +0000595 return DAG.getNode(HexagonISD::RET_FLAG, dl, MVT::Other, RetOps);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000596}
597
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000598bool HexagonTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
599 // If either no tail call or told not to tail call at all, don't.
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000600 auto Attr =
601 CI->getParent()->getParent()->getFnAttribute("disable-tail-calls");
602 if (!CI->isTailCall() || Attr.getValueAsString() == "true")
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000603 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000604
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000605 return true;
606}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000607
608/// LowerCallResult - Lower the result values of an ISD::CALL into the
609/// appropriate copies out of appropriate physical registers. This assumes that
610/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
611/// being lowered. Returns a SDNode with the same number of values as the
612/// ISD::CALL.
613SDValue
614HexagonTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
615 CallingConv::ID CallConv, bool isVarArg,
616 const
617 SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000618 SDLoc dl, SelectionDAG &DAG,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000619 SmallVectorImpl<SDValue> &InVals,
620 const SmallVectorImpl<SDValue> &OutVals,
621 SDValue Callee) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000622 // Assign locations to each value returned by this call.
623 SmallVector<CCValAssign, 16> RVLocs;
624
Eric Christopherb5217502014-08-06 18:45:26 +0000625 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
626 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000627
628 CCInfo.AnalyzeCallResult(Ins, RetCC_Hexagon);
629
630 // Copy all of the result registers out of their specified physreg.
631 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000632 SDValue RetVal;
633 if (RVLocs[i].getValVT() == MVT::i1) {
634 // Return values of type MVT::i1 require special handling. The reason
635 // is that MVT::i1 is associated with the PredRegs register class, but
636 // values of that type are still returned in R0. Generate an explicit
637 // copy into a predicate register from R0, and treat the value of the
638 // predicate register as the call result.
639 auto &MRI = DAG.getMachineFunction().getRegInfo();
640 SDValue FR0 = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
641 MVT::i32, InFlag);
642 // FR0 = (Value, Chain, Glue)
643 unsigned PredR = MRI.createVirtualRegister(&Hexagon::PredRegsRegClass);
644 SDValue TPR = DAG.getCopyToReg(FR0.getValue(1), dl, PredR,
645 FR0.getValue(0), FR0.getValue(2));
646 // TPR = (Chain, Glue)
647 RetVal = DAG.getCopyFromReg(TPR.getValue(0), dl, PredR, MVT::i1,
648 TPR.getValue(1));
649 } else {
650 RetVal = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
651 RVLocs[i].getValVT(), InFlag);
652 }
653 InVals.push_back(RetVal.getValue(0));
654 Chain = RetVal.getValue(1);
655 InFlag = RetVal.getValue(2);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000656 }
657
658 return Chain;
659}
660
661/// LowerCall - Functions arguments are copied from virtual regs to
662/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
663SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +0000664HexagonTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000665 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +0000666 SelectionDAG &DAG = CLI.DAG;
Craig Topperb94011f2013-07-14 04:42:23 +0000667 SDLoc &dl = CLI.DL;
668 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
669 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
670 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000671 SDValue Chain = CLI.Chain;
672 SDValue Callee = CLI.Callee;
673 bool &isTailCall = CLI.IsTailCall;
674 CallingConv::ID CallConv = CLI.CallConv;
675 bool isVarArg = CLI.IsVarArg;
Colin LeMahieu2e3a26d2015-01-16 17:05:27 +0000676 bool doesNotReturn = CLI.DoesNotReturn;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000677
678 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000679 MachineFunction &MF = DAG.getMachineFunction();
Mehdi Amini44ede332015-07-09 02:09:04 +0000680 auto PtrVT = getPointerTy(MF.getDataLayout());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000681
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000682 // Check for varargs.
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000683 int NumNamedVarArgParams = -1;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000684 if (GlobalAddressSDNode *GAN = dyn_cast<GlobalAddressSDNode>(Callee)) {
685 const GlobalValue *GV = GAN->getGlobal();
686 Callee = DAG.getTargetGlobalAddress(GV, dl, MVT::i32);
687 if (const Function* F = dyn_cast<Function>(GV)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000688 // If a function has zero args and is a vararg function, that's
689 // disallowed so it must be an undeclared function. Do not assume
690 // varargs if the callee is undefined.
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000691 if (F->isVarArg() && F->getFunctionType()->getNumParams() != 0)
692 NumNamedVarArgParams = F->getFunctionType()->getNumParams();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000693 }
694 }
695
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000696 // Analyze operands of the call, assigning locations to each operand.
697 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000698 HexagonCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
699 *DAG.getContext(), NumNamedVarArgParams);
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000700
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000701 if (isVarArg)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000702 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon_VarArg);
703 else
704 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon);
705
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000706 auto Attr = MF.getFunction()->getFnAttribute("disable-tail-calls");
707 if (Attr.getValueAsString() == "true")
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000708 isTailCall = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000709
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000710 if (isTailCall) {
711 bool StructAttrFlag = MF.getFunction()->hasStructRetAttr();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000712 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
713 isVarArg, IsStructRet,
714 StructAttrFlag,
715 Outs, OutVals, Ins, DAG);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000716 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000717 CCValAssign &VA = ArgLocs[i];
718 if (VA.isMemLoc()) {
719 isTailCall = false;
720 break;
721 }
722 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000723 DEBUG(dbgs() << (isTailCall ? "Eligible for Tail Call\n"
724 : "Argument must be passed on stack. "
725 "Not eligible for Tail Call\n"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000726 }
727 // Get a count of how many bytes are to be pushed on the stack.
728 unsigned NumBytes = CCInfo.getNextStackOffset();
729 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
730 SmallVector<SDValue, 8> MemOpChains;
731
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000732 auto &HRI = *Subtarget.getRegisterInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +0000733 SDValue StackPtr =
734 DAG.getCopyFromReg(Chain, dl, HRI.getStackRegister(), PtrVT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000735
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000736 bool NeedsArgAlign = false;
737 unsigned LargestAlignSeen = 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000738 // Walk the register/memloc assignments, inserting copies/loads.
739 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
740 CCValAssign &VA = ArgLocs[i];
741 SDValue Arg = OutVals[i];
742 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000743 // Record if we need > 8 byte alignment on an argument.
744 bool ArgAlign = IsHvxVectorType(VA.getValVT());
745 NeedsArgAlign |= ArgAlign;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000746
747 // Promote the value if needed.
748 switch (VA.getLocInfo()) {
749 default:
750 // Loc info must be one of Full, SExt, ZExt, or AExt.
Craig Toppere55c5562012-02-07 02:50:20 +0000751 llvm_unreachable("Unknown loc info!");
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000752 case CCValAssign::BCvt:
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000753 case CCValAssign::Full:
754 break;
755 case CCValAssign::SExt:
756 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
757 break;
758 case CCValAssign::ZExt:
759 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
760 break;
761 case CCValAssign::AExt:
762 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
763 break;
764 }
765
766 if (VA.isMemLoc()) {
767 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000768 SDValue MemAddr = DAG.getConstant(LocMemOffset, dl,
769 StackPtr.getValueType());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000770 MemAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, MemAddr);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000771 if (ArgAlign)
772 LargestAlignSeen = std::max(LargestAlignSeen,
773 VA.getLocVT().getStoreSizeInBits() >> 3);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000774 if (Flags.isByVal()) {
775 // The argument is a struct passed by value. According to LLVM, "Arg"
776 // is is pointer.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000777 MemOpChains.push_back(CreateCopyOfByValArgument(Arg, MemAddr, Chain,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000778 Flags, DAG, dl));
779 } else {
Alex Lorenze40c8a22015-08-11 23:09:45 +0000780 MachinePointerInfo LocPI = MachinePointerInfo::getStack(
781 DAG.getMachineFunction(), LocMemOffset);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000782 SDValue S = DAG.getStore(Chain, dl, Arg, MemAddr, LocPI, false,
783 false, 0);
784 MemOpChains.push_back(S);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000785 }
786 continue;
787 }
788
789 // Arguments that can be passed on register must be kept at RegsToPass
790 // vector.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000791 if (VA.isRegLoc())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000792 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000793 }
794
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000795 if (NeedsArgAlign && Subtarget.hasV60TOps()) {
796 DEBUG(dbgs() << "Function needs byte stack align due to call args\n");
797 MachineFrameInfo* MFI = DAG.getMachineFunction().getFrameInfo();
798 // V6 vectors passed by value have 64 or 128 byte alignment depending
799 // on whether we are 64 byte vector mode or 128 byte.
800 bool UseHVXDbl = Subtarget.useHVXDblOps();
801 assert(Subtarget.useHVXOps());
802 const unsigned ObjAlign = UseHVXDbl ? 128 : 64;
803 LargestAlignSeen = std::max(LargestAlignSeen, ObjAlign);
804 MFI->ensureMaxAlignment(LargestAlignSeen);
805 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000806 // Transform all store nodes into one single node because all store
807 // nodes are independent of each other.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000808 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000809 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000810
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000811 if (!isTailCall) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000812 SDValue C = DAG.getConstant(NumBytes, dl, PtrVT, true);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000813 Chain = DAG.getCALLSEQ_START(Chain, C, dl);
814 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000815
816 // Build a sequence of copy-to-reg nodes chained together with token
817 // chain and flag operands which copy the outgoing args into registers.
Benjamin Kramerbde91762012-06-02 10:20:22 +0000818 // The InFlag in necessary since all emitted instructions must be
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000819 // stuck together.
820 SDValue InFlag;
821 if (!isTailCall) {
822 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
823 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
824 RegsToPass[i].second, InFlag);
825 InFlag = Chain.getValue(1);
826 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000827 } else {
828 // For tail calls lower the arguments to the 'real' stack slot.
829 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000830 // Force all the incoming stack arguments to be loaded from the stack
831 // before any new outgoing arguments are stored to the stack, because the
832 // outgoing stack slots may alias the incoming argument stack slots, and
833 // the alias isn't otherwise explicit. This is slightly more conservative
834 // than necessary, because it means that each store effectively depends
835 // on every argument instead of just those arguments it would clobber.
836 //
Benjamin Kramerbde91762012-06-02 10:20:22 +0000837 // Do not flag preceding copytoreg stuff together with the following stuff.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000838 InFlag = SDValue();
839 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
840 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
841 RegsToPass[i].second, InFlag);
842 InFlag = Chain.getValue(1);
843 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000844 InFlag = SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000845 }
846
847 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
848 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
849 // node so that legalize doesn't hack it.
Tobias Edler von Kochb51460c2015-12-16 17:29:37 +0000850 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000851 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, PtrVT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000852 } else if (ExternalSymbolSDNode *S =
853 dyn_cast<ExternalSymbolSDNode>(Callee)) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000854 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), PtrVT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000855 }
856
857 // Returns a chain & a flag for retval copy to use.
858 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
859 SmallVector<SDValue, 8> Ops;
860 Ops.push_back(Chain);
861 Ops.push_back(Callee);
862
863 // Add argument registers to the end of the list so that they are
864 // known live into the call.
865 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
866 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
867 RegsToPass[i].second.getValueType()));
868 }
869
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000870 if (InFlag.getNode())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000871 Ops.push_back(InFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000872
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +0000873 if (isTailCall) {
874 MF.getFrameInfo()->setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +0000875 return DAG.getNode(HexagonISD::TC_RETURN, dl, NodeTys, Ops);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +0000876 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000877
Colin LeMahieu2e3a26d2015-01-16 17:05:27 +0000878 int OpCode = doesNotReturn ? HexagonISD::CALLv3nr : HexagonISD::CALLv3;
879 Chain = DAG.getNode(OpCode, dl, NodeTys, Ops);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000880 InFlag = Chain.getValue(1);
881
882 // Create the CALLSEQ_END node.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000883 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
884 DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000885 InFlag = Chain.getValue(1);
886
887 // Handle result values, copying them out of physregs into vregs that we
888 // return.
889 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
890 InVals, OutVals, Callee);
891}
892
893static bool getIndexedAddressParts(SDNode *Ptr, EVT VT,
894 bool isSEXTLoad, SDValue &Base,
895 SDValue &Offset, bool &isInc,
896 SelectionDAG &DAG) {
897 if (Ptr->getOpcode() != ISD::ADD)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000898 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000899
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000900 auto &HST = static_cast<const HexagonSubtarget&>(DAG.getSubtarget());
901 bool UseHVX = HST.useHVXOps();
902 bool UseHVXDbl = HST.useHVXDblOps();
903
904 bool ValidHVXDblType =
905 (UseHVX && UseHVXDbl) && (VT == MVT::v32i32 || VT == MVT::v16i64 ||
906 VT == MVT::v64i16 || VT == MVT::v128i8);
907 bool ValidHVXType =
908 UseHVX && !UseHVXDbl && (VT == MVT::v16i32 || VT == MVT::v8i64 ||
909 VT == MVT::v32i16 || VT == MVT::v64i8);
910
911 if (ValidHVXDblType || ValidHVXType ||
912 VT == MVT::i64 || VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000913 isInc = (Ptr->getOpcode() == ISD::ADD);
914 Base = Ptr->getOperand(0);
915 Offset = Ptr->getOperand(1);
916 // Ensure that Offset is a constant.
917 return (isa<ConstantSDNode>(Offset));
918 }
919
920 return false;
921}
922
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000923/// getPostIndexedAddressParts - returns true by value, base pointer and
924/// offset pointer and addressing mode by reference if this node can be
925/// combined with a load / store to form a post-indexed load / store.
926bool HexagonTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
927 SDValue &Base,
928 SDValue &Offset,
929 ISD::MemIndexedMode &AM,
930 SelectionDAG &DAG) const
931{
932 EVT VT;
933 SDValue Ptr;
934 bool isSEXTLoad = false;
935
936 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
937 VT = LD->getMemoryVT();
938 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
939 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
940 VT = ST->getMemoryVT();
941 if (ST->getValue().getValueType() == MVT::i64 && ST->isTruncatingStore()) {
942 return false;
943 }
944 } else {
945 return false;
946 }
947
Chad Rosier64dc8aa2012-01-06 20:11:59 +0000948 bool isInc = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000949 bool isLegal = getIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
950 isInc, DAG);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000951 if (isLegal) {
952 auto &HII = *Subtarget.getInstrInfo();
953 int32_t OffsetVal = cast<ConstantSDNode>(Offset.getNode())->getSExtValue();
954 if (HII.isValidAutoIncImm(VT, OffsetVal)) {
955 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
956 return true;
957 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000958 }
959
960 return false;
961}
962
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000963SDValue
964HexagonTargetLowering::LowerINLINEASM(SDValue Op, SelectionDAG &DAG) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000965 SDNode *Node = Op.getNode();
966 MachineFunction &MF = DAG.getMachineFunction();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000967 auto &FuncInfo = *MF.getInfo<HexagonMachineFunctionInfo>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000968 switch (Node->getOpcode()) {
969 case ISD::INLINEASM: {
970 unsigned NumOps = Node->getNumOperands();
971 if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue)
972 --NumOps; // Ignore the flag operand.
973
974 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000975 if (FuncInfo.hasClobberLR())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000976 break;
977 unsigned Flags =
978 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
979 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
980 ++i; // Skip the ID value.
981
982 switch (InlineAsm::getKind(Flags)) {
983 default: llvm_unreachable("Bad flags!");
984 case InlineAsm::Kind_RegDef:
985 case InlineAsm::Kind_RegUse:
986 case InlineAsm::Kind_Imm:
987 case InlineAsm::Kind_Clobber:
988 case InlineAsm::Kind_Mem: {
989 for (; NumVals; --NumVals, ++i) {}
990 break;
991 }
992 case InlineAsm::Kind_RegDefEarlyClobber: {
993 for (; NumVals; --NumVals, ++i) {
994 unsigned Reg =
995 cast<RegisterSDNode>(Node->getOperand(i))->getReg();
996
997 // Check it to be lr
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000998 const HexagonRegisterInfo *QRI = Subtarget.getRegisterInfo();
Eric Christopherdbe1cb02014-06-27 00:13:52 +0000999 if (Reg == QRI->getRARegister()) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001000 FuncInfo.setHasClobberLR(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001001 break;
1002 }
1003 }
1004 break;
1005 }
1006 }
1007 }
1008 }
1009 } // Node->getOpcode
1010 return Op;
1011}
1012
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00001013// Need to transform ISD::PREFETCH into something that doesn't inherit
1014// all of the properties of ISD::PREFETCH, specifically SDNPMayLoad and
1015// SDNPMayStore.
1016SDValue HexagonTargetLowering::LowerPREFETCH(SDValue Op,
1017 SelectionDAG &DAG) const {
1018 SDValue Chain = Op.getOperand(0);
1019 SDValue Addr = Op.getOperand(1);
1020 // Lower it to DCFETCH($reg, #0). A "pat" will try to merge the offset in,
1021 // if the "reg" is fed by an "add".
1022 SDLoc DL(Op);
1023 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
1024 return DAG.getNode(HexagonISD::DCFETCH, DL, MVT::Other, Chain, Addr, Zero);
1025}
1026
1027SDValue HexagonTargetLowering::LowerINTRINSIC_VOID(SDValue Op,
1028 SelectionDAG &DAG) const {
1029 SDValue Chain = Op.getOperand(0);
1030 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1031 // Lower the hexagon_prefetch builtin to DCFETCH, as above.
1032 if (IntNo == Intrinsic::hexagon_prefetch) {
1033 SDValue Addr = Op.getOperand(2);
1034 SDLoc DL(Op);
1035 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
1036 return DAG.getNode(HexagonISD::DCFETCH, DL, MVT::Other, Chain, Addr, Zero);
1037 }
1038 return SDValue();
1039}
1040
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001041SDValue
1042HexagonTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
1043 SelectionDAG &DAG) const {
1044 SDValue Chain = Op.getOperand(0);
1045 SDValue Size = Op.getOperand(1);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001046 SDValue Align = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001047 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001048
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001049 ConstantSDNode *AlignConst = dyn_cast<ConstantSDNode>(Align);
1050 assert(AlignConst && "Non-constant Align in LowerDYNAMIC_STACKALLOC");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001051
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001052 unsigned A = AlignConst->getSExtValue();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001053 auto &HFI = *Subtarget.getFrameLowering();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001054 // "Zero" means natural stack alignment.
1055 if (A == 0)
1056 A = HFI.getStackAlignment();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001057
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001058 DEBUG({
Krzysztof Parzyszek9ee04e42015-04-22 17:19:44 +00001059 dbgs () << LLVM_FUNCTION_NAME << " Align: " << A << " Size: ";
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001060 Size.getNode()->dump(&DAG);
1061 dbgs() << "\n";
1062 });
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001063
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001064 SDValue AC = DAG.getConstant(A, dl, MVT::i32);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001065 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00001066 SDValue AA = DAG.getNode(HexagonISD::ALLOCA, dl, VTs, Chain, Size, AC);
1067 if (Op.getNode()->getHasDebugValue())
1068 DAG.TransferDbgValues(Op, AA);
1069 return AA;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001070}
1071
1072SDValue
1073HexagonTargetLowering::LowerFormalArguments(SDValue Chain,
1074 CallingConv::ID CallConv,
1075 bool isVarArg,
1076 const
1077 SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001078 SDLoc dl, SelectionDAG &DAG,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001079 SmallVectorImpl<SDValue> &InVals)
1080const {
1081
1082 MachineFunction &MF = DAG.getMachineFunction();
1083 MachineFrameInfo *MFI = MF.getFrameInfo();
1084 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001085 auto &FuncInfo = *MF.getInfo<HexagonMachineFunctionInfo>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001086
1087 // Assign locations to all of the incoming arguments.
1088 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001089 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1090 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001091
1092 CCInfo.AnalyzeFormalArguments(Ins, CC_Hexagon);
1093
1094 // For LLVM, in the case when returning a struct by value (>8byte),
1095 // the first argument is a pointer that points to the location on caller's
1096 // stack where the return value will be stored. For Hexagon, the location on
1097 // caller's stack is passed only when the struct size is smaller than (and
1098 // equal to) 8 bytes. If not, no address will be passed into callee and
1099 // callee return the result direclty through R0/R1.
1100
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001101 SmallVector<SDValue, 8> MemOps;
1102 bool UseHVX = Subtarget.useHVXOps(), UseHVXDbl = Subtarget.useHVXDblOps();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001103
1104 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1105 CCValAssign &VA = ArgLocs[i];
1106 ISD::ArgFlagsTy Flags = Ins[i].Flags;
1107 unsigned ObjSize;
1108 unsigned StackLocation;
1109 int FI;
1110
1111 if ( (VA.isRegLoc() && !Flags.isByVal())
1112 || (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() > 8)) {
1113 // Arguments passed in registers
1114 // 1. int, long long, ptr args that get allocated in register.
1115 // 2. Large struct that gets an register to put its address in.
1116 EVT RegVT = VA.getLocVT();
Sirish Pande69295b82012-05-10 20:20:25 +00001117 if (RegVT == MVT::i8 || RegVT == MVT::i16 ||
1118 RegVT == MVT::i32 || RegVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001119 unsigned VReg =
Craig Topperc7242e02012-04-20 07:30:17 +00001120 RegInfo.createVirtualRegister(&Hexagon::IntRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001121 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1122 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Colin LeMahieu4379d102015-01-28 22:08:16 +00001123 } else if (RegVT == MVT::i64 || RegVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001124 unsigned VReg =
Craig Topperc7242e02012-04-20 07:30:17 +00001125 RegInfo.createVirtualRegister(&Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001126 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1127 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001128
1129 // Single Vector
1130 } else if ((RegVT == MVT::v8i64 || RegVT == MVT::v16i32 ||
1131 RegVT == MVT::v32i16 || RegVT == MVT::v64i8)) {
1132 unsigned VReg =
1133 RegInfo.createVirtualRegister(&Hexagon::VectorRegsRegClass);
1134 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1135 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1136 } else if (UseHVX && UseHVXDbl &&
1137 ((RegVT == MVT::v16i64 || RegVT == MVT::v32i32 ||
1138 RegVT == MVT::v64i16 || RegVT == MVT::v128i8))) {
1139 unsigned VReg =
1140 RegInfo.createVirtualRegister(&Hexagon::VectorRegs128BRegClass);
1141 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1142 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1143
1144 // Double Vector
1145 } else if ((RegVT == MVT::v16i64 || RegVT == MVT::v32i32 ||
1146 RegVT == MVT::v64i16 || RegVT == MVT::v128i8)) {
1147 unsigned VReg =
1148 RegInfo.createVirtualRegister(&Hexagon::VecDblRegsRegClass);
1149 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1150 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1151 } else if (UseHVX && UseHVXDbl &&
1152 ((RegVT == MVT::v32i64 || RegVT == MVT::v64i32 ||
1153 RegVT == MVT::v128i16 || RegVT == MVT::v256i8))) {
1154 unsigned VReg =
1155 RegInfo.createVirtualRegister(&Hexagon::VecDblRegs128BRegClass);
1156 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1157 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1158 } else if (RegVT == MVT::v512i1 || RegVT == MVT::v1024i1) {
1159 assert(0 && "need to support VecPred regs");
1160 unsigned VReg =
1161 RegInfo.createVirtualRegister(&Hexagon::VecPredRegsRegClass);
1162 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1163 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001164 } else {
1165 assert (0);
1166 }
1167 } else if (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() <= 8) {
1168 assert (0 && "ByValSize must be bigger than 8 bytes");
1169 } else {
1170 // Sanity check.
1171 assert(VA.isMemLoc());
1172
1173 if (Flags.isByVal()) {
1174 // If it's a byval parameter, then we need to compute the
1175 // "real" size, not the size of the pointer.
1176 ObjSize = Flags.getByValSize();
1177 } else {
1178 ObjSize = VA.getLocVT().getStoreSizeInBits() >> 3;
1179 }
1180
1181 StackLocation = HEXAGON_LRFP_SIZE + VA.getLocMemOffset();
1182 // Create the frame index object for this incoming parameter...
1183 FI = MFI->CreateFixedObject(ObjSize, StackLocation, true);
1184
1185 // Create the SelectionDAG nodes cordl, responding to a load
1186 // from this parameter.
1187 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
1188
1189 if (Flags.isByVal()) {
1190 // If it's a pass-by-value aggregate, then do not dereference the stack
1191 // location. Instead, we should generate a reference to the stack
1192 // location.
1193 InVals.push_back(FIN);
1194 } else {
1195 InVals.push_back(DAG.getLoad(VA.getLocVT(), dl, Chain, FIN,
1196 MachinePointerInfo(), false, false,
1197 false, 0));
1198 }
1199 }
1200 }
1201
1202 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001203 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001204
1205 if (isVarArg) {
1206 // This will point to the next argument passed via stack.
1207 int FrameIndex = MFI->CreateFixedObject(Hexagon_PointerSize,
1208 HEXAGON_LRFP_SIZE +
1209 CCInfo.getNextStackOffset(),
1210 true);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001211 FuncInfo.setVarArgsFrameIndex(FrameIndex);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001212 }
1213
1214 return Chain;
1215}
1216
1217SDValue
1218HexagonTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
1219 // VASTART stores the address of the VarArgsFrameIndex slot into the
1220 // memory location argument.
1221 MachineFunction &MF = DAG.getMachineFunction();
1222 HexagonMachineFunctionInfo *QFI = MF.getInfo<HexagonMachineFunctionInfo>();
1223 SDValue Addr = DAG.getFrameIndex(QFI->getVarArgsFrameIndex(), MVT::i32);
1224 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001225 return DAG.getStore(Op.getOperand(0), SDLoc(Op), Addr,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001226 Op.getOperand(1), MachinePointerInfo(SV), false,
1227 false, 0);
1228}
1229
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001230// Creates a SPLAT instruction for a constant value VAL.
1231static SDValue createSplat(SelectionDAG &DAG, SDLoc dl, EVT VT, SDValue Val) {
1232 if (VT.getSimpleVT() == MVT::v4i8)
1233 return DAG.getNode(HexagonISD::VSPLATB, dl, VT, Val);
1234
1235 if (VT.getSimpleVT() == MVT::v4i16)
1236 return DAG.getNode(HexagonISD::VSPLATH, dl, VT, Val);
1237
1238 return SDValue();
1239}
1240
1241static bool isSExtFree(SDValue N) {
1242 // A sign-extend of a truncate of a sign-extend is free.
1243 if (N.getOpcode() == ISD::TRUNCATE &&
1244 N.getOperand(0).getOpcode() == ISD::AssertSext)
1245 return true;
1246 // We have sign-extended loads.
1247 if (N.getOpcode() == ISD::LOAD)
1248 return true;
1249 return false;
1250}
1251
1252SDValue HexagonTargetLowering::LowerCTPOP(SDValue Op, SelectionDAG &DAG) const {
1253 SDLoc dl(Op);
1254 SDValue InpVal = Op.getOperand(0);
1255 if (isa<ConstantSDNode>(InpVal)) {
1256 uint64_t V = cast<ConstantSDNode>(InpVal)->getZExtValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001257 return DAG.getTargetConstant(countPopulation(V), dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001258 }
1259 SDValue PopOut = DAG.getNode(HexagonISD::POPCOUNT, dl, MVT::i32, InpVal);
1260 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, PopOut);
1261}
1262
1263SDValue HexagonTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1264 SDLoc dl(Op);
1265
1266 SDValue LHS = Op.getOperand(0);
1267 SDValue RHS = Op.getOperand(1);
1268 SDValue Cmp = Op.getOperand(2);
1269 ISD::CondCode CC = cast<CondCodeSDNode>(Cmp)->get();
1270
1271 EVT VT = Op.getValueType();
1272 EVT LHSVT = LHS.getValueType();
1273 EVT RHSVT = RHS.getValueType();
1274
1275 if (LHSVT == MVT::v2i16) {
1276 assert(ISD::isSignedIntSetCC(CC) || ISD::isUnsignedIntSetCC(CC));
1277 unsigned ExtOpc = ISD::isSignedIntSetCC(CC) ? ISD::SIGN_EXTEND
1278 : ISD::ZERO_EXTEND;
1279 SDValue LX = DAG.getNode(ExtOpc, dl, MVT::v2i32, LHS);
1280 SDValue RX = DAG.getNode(ExtOpc, dl, MVT::v2i32, RHS);
1281 SDValue SC = DAG.getNode(ISD::SETCC, dl, MVT::v2i1, LX, RX, Cmp);
1282 return SC;
1283 }
1284
1285 // Treat all other vector types as legal.
1286 if (VT.isVector())
1287 return Op;
1288
1289 // Equals and not equals should use sign-extend, not zero-extend, since
1290 // we can represent small negative values in the compare instructions.
1291 // The LLVM default is to use zero-extend arbitrarily in these cases.
1292 if ((CC == ISD::SETEQ || CC == ISD::SETNE) &&
1293 (RHSVT == MVT::i8 || RHSVT == MVT::i16) &&
1294 (LHSVT == MVT::i8 || LHSVT == MVT::i16)) {
1295 ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS);
1296 if (C && C->getAPIntValue().isNegative()) {
1297 LHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, LHS);
1298 RHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, RHS);
1299 return DAG.getNode(ISD::SETCC, dl, Op.getValueType(),
1300 LHS, RHS, Op.getOperand(2));
1301 }
1302 if (isSExtFree(LHS) || isSExtFree(RHS)) {
1303 LHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, LHS);
1304 RHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, RHS);
1305 return DAG.getNode(ISD::SETCC, dl, Op.getValueType(),
1306 LHS, RHS, Op.getOperand(2));
1307 }
1308 }
1309 return SDValue();
1310}
1311
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001312SDValue
1313HexagonTargetLowering::LowerVSELECT(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001314 SDValue PredOp = Op.getOperand(0);
1315 SDValue Op1 = Op.getOperand(1), Op2 = Op.getOperand(2);
1316 EVT OpVT = Op1.getValueType();
1317 SDLoc DL(Op);
1318
1319 if (OpVT == MVT::v2i16) {
1320 SDValue X1 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op1);
1321 SDValue X2 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op2);
1322 SDValue SL = DAG.getNode(ISD::VSELECT, DL, MVT::v2i32, PredOp, X1, X2);
1323 SDValue TR = DAG.getNode(ISD::TRUNCATE, DL, MVT::v2i16, SL);
1324 return TR;
1325 }
1326
1327 return SDValue();
1328}
1329
1330// Handle only specific vector loads.
1331SDValue HexagonTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1332 EVT VT = Op.getValueType();
1333 SDLoc DL(Op);
1334 LoadSDNode *LoadNode = cast<LoadSDNode>(Op);
1335 SDValue Chain = LoadNode->getChain();
1336 SDValue Ptr = Op.getOperand(1);
1337 SDValue LoweredLoad;
1338 SDValue Result;
1339 SDValue Base = LoadNode->getBasePtr();
1340 ISD::LoadExtType Ext = LoadNode->getExtensionType();
1341 unsigned Alignment = LoadNode->getAlignment();
1342 SDValue LoadChain;
1343
1344 if(Ext == ISD::NON_EXTLOAD)
1345 Ext = ISD::ZEXTLOAD;
1346
1347 if (VT == MVT::v4i16) {
1348 if (Alignment == 2) {
1349 SDValue Loads[4];
1350 // Base load.
1351 Loads[0] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Base,
1352 LoadNode->getPointerInfo(), MVT::i16,
1353 LoadNode->isVolatile(),
1354 LoadNode->isNonTemporal(),
1355 LoadNode->isInvariant(),
1356 Alignment);
1357 // Base+2 load.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001358 SDValue Increment = DAG.getConstant(2, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001359 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1360 Loads[1] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
1361 LoadNode->getPointerInfo(), MVT::i16,
1362 LoadNode->isVolatile(),
1363 LoadNode->isNonTemporal(),
1364 LoadNode->isInvariant(),
1365 Alignment);
1366 // SHL 16, then OR base and base+2.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001367 SDValue ShiftAmount = DAG.getConstant(16, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001368 SDValue Tmp1 = DAG.getNode(ISD::SHL, DL, MVT::i32, Loads[1], ShiftAmount);
1369 SDValue Tmp2 = DAG.getNode(ISD::OR, DL, MVT::i32, Tmp1, Loads[0]);
1370 // Base + 4.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001371 Increment = DAG.getConstant(4, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001372 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1373 Loads[2] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
1374 LoadNode->getPointerInfo(), MVT::i16,
1375 LoadNode->isVolatile(),
1376 LoadNode->isNonTemporal(),
1377 LoadNode->isInvariant(),
1378 Alignment);
1379 // Base + 6.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001380 Increment = DAG.getConstant(6, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001381 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1382 Loads[3] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
1383 LoadNode->getPointerInfo(), MVT::i16,
1384 LoadNode->isVolatile(),
1385 LoadNode->isNonTemporal(),
1386 LoadNode->isInvariant(),
1387 Alignment);
1388 // SHL 16, then OR base+4 and base+6.
1389 Tmp1 = DAG.getNode(ISD::SHL, DL, MVT::i32, Loads[3], ShiftAmount);
1390 SDValue Tmp4 = DAG.getNode(ISD::OR, DL, MVT::i32, Tmp1, Loads[2]);
1391 // Combine to i64. This could be optimised out later if we can
1392 // affect reg allocation of this code.
1393 Result = DAG.getNode(HexagonISD::COMBINE, DL, MVT::i64, Tmp4, Tmp2);
1394 LoadChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
1395 Loads[0].getValue(1), Loads[1].getValue(1),
1396 Loads[2].getValue(1), Loads[3].getValue(1));
1397 } else {
1398 // Perform default type expansion.
1399 Result = DAG.getLoad(MVT::i64, DL, Chain, Ptr, LoadNode->getPointerInfo(),
1400 LoadNode->isVolatile(), LoadNode->isNonTemporal(),
1401 LoadNode->isInvariant(), LoadNode->getAlignment());
1402 LoadChain = Result.getValue(1);
1403 }
1404 } else
1405 llvm_unreachable("Custom lowering unsupported load");
1406
1407 Result = DAG.getNode(ISD::BITCAST, DL, VT, Result);
1408 // Since we pretend to lower a load, we need the original chain
1409 // info attached to the result.
1410 SDValue Ops[] = { Result, LoadChain };
1411
1412 return DAG.getMergeValues(Ops, DL);
1413}
1414
1415
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001416SDValue
Sirish Pande69295b82012-05-10 20:20:25 +00001417HexagonTargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
1418 EVT ValTy = Op.getValueType();
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001419 ConstantPoolSDNode *CPN = cast<ConstantPoolSDNode>(Op);
1420 unsigned Align = CPN->getAlignment();
1421 Reloc::Model RM = HTM.getRelocationModel();
1422 unsigned char TF = (RM == Reloc::PIC_) ? HexagonII::MO_PCREL : 0;
1423
1424 SDValue T;
1425 if (CPN->isMachineConstantPoolEntry())
1426 T = DAG.getTargetConstantPool(CPN->getMachineCPVal(), ValTy, Align, TF);
Sirish Pande69295b82012-05-10 20:20:25 +00001427 else
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001428 T = DAG.getTargetConstantPool(CPN->getConstVal(), ValTy, Align, TF);
1429 if (RM == Reloc::PIC_)
1430 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), ValTy, T);
1431 return DAG.getNode(HexagonISD::CP, SDLoc(Op), ValTy, T);
1432}
1433
1434SDValue
1435HexagonTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
1436 EVT VT = Op.getValueType();
1437 int Idx = cast<JumpTableSDNode>(Op)->getIndex();
1438 Reloc::Model RM = HTM.getRelocationModel();
1439 if (RM == Reloc::PIC_) {
1440 SDValue T = DAG.getTargetJumpTable(Idx, VT, HexagonII::MO_PCREL);
1441 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), VT, T);
1442 }
1443
1444 SDValue T = DAG.getTargetJumpTable(Idx, VT);
1445 return DAG.getNode(HexagonISD::JT, SDLoc(Op), VT, T);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001446}
1447
1448SDValue
1449HexagonTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001450 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001451 MachineFunction &MF = DAG.getMachineFunction();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001452 MachineFrameInfo &MFI = *MF.getFrameInfo();
1453 MFI.setReturnAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001454
Bill Wendling908bf812014-01-06 00:43:20 +00001455 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001456 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001457
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001458 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001459 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001460 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1461 if (Depth) {
1462 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001463 SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001464 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
1465 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
1466 MachinePointerInfo(), false, false, false, 0);
1467 }
1468
1469 // Return LR, which contains the return address. Mark it an implicit live-in.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001470 unsigned Reg = MF.addLiveIn(HRI.getRARegister(), getRegClassFor(MVT::i32));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001471 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
1472}
1473
1474SDValue
1475HexagonTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001476 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
1477 MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
1478 MFI.setFrameAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001479
1480 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001481 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001482 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1483 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001484 HRI.getFrameRegister(), VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001485 while (Depth--)
1486 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
1487 MachinePointerInfo(),
1488 false, false, false, 0);
1489 return FrameAddr;
1490}
1491
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001492SDValue
1493HexagonTargetLowering::LowerATOMIC_FENCE(SDValue Op, SelectionDAG& DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001494 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001495 return DAG.getNode(HexagonISD::BARRIER, dl, MVT::Other, Op.getOperand(0));
1496}
1497
1498
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001499SDValue
1500HexagonTargetLowering::LowerGLOBALADDRESS(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001501 SDLoc dl(Op);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001502 auto *GAN = cast<GlobalAddressSDNode>(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00001503 auto PtrVT = getPointerTy(DAG.getDataLayout());
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001504 auto *GV = GAN->getGlobal();
1505 int64_t Offset = GAN->getOffset();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001506
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001507 auto &HLOF = *HTM.getObjFileLowering();
1508 Reloc::Model RM = HTM.getRelocationModel();
1509
1510 if (RM == Reloc::Static) {
1511 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset);
1512 if (HLOF.IsGlobalInSmallSection(GV, HTM))
1513 return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, GA);
1514 return DAG.getNode(HexagonISD::CONST32, dl, PtrVT, GA);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001515 }
1516
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001517 bool UsePCRel = GV->hasInternalLinkage() || GV->hasHiddenVisibility() ||
1518 (GV->hasLocalLinkage() && !isa<Function>(GV));
1519 if (UsePCRel) {
1520 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset,
1521 HexagonII::MO_PCREL);
1522 return DAG.getNode(HexagonISD::AT_PCREL, dl, PtrVT, GA);
1523 }
1524
1525 // Use GOT index.
1526 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
1527 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, HexagonII::MO_GOT);
1528 SDValue Off = DAG.getConstant(Offset, dl, MVT::i32);
1529 return DAG.getNode(HexagonISD::AT_GOT, dl, PtrVT, GOT, GA, Off);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001530}
1531
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001532// Specifies that for loads and stores VT can be promoted to PromotedLdStVT.
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001533SDValue
1534HexagonTargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
1535 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001536 SDLoc dl(Op);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001537 EVT PtrVT = getPointerTy(DAG.getDataLayout());
1538
1539 Reloc::Model RM = HTM.getRelocationModel();
1540 if (RM == Reloc::Static) {
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001541 SDValue A = DAG.getTargetBlockAddress(BA, PtrVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001542 return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, A);
1543 }
1544
1545 SDValue A = DAG.getTargetBlockAddress(BA, PtrVT, 0, HexagonII::MO_PCREL);
1546 return DAG.getNode(HexagonISD::AT_PCREL, dl, PtrVT, A);
1547}
1548
1549SDValue
1550HexagonTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op, SelectionDAG &DAG)
1551 const {
1552 EVT PtrVT = getPointerTy(DAG.getDataLayout());
1553 SDValue GOTSym = DAG.getTargetExternalSymbol(HEXAGON_GOT_SYM_NAME, PtrVT,
1554 HexagonII::MO_PCREL);
1555 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), PtrVT, GOTSym);
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001556}
1557
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001558SDValue
1559HexagonTargetLowering::GetDynamicTLSAddr(SelectionDAG &DAG, SDValue Chain,
1560 GlobalAddressSDNode *GA, SDValue *InFlag, EVT PtrVT, unsigned ReturnReg,
1561 unsigned char OperandFlags) const {
1562 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1563 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1564 SDLoc dl(GA);
1565 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
1566 GA->getValueType(0),
1567 GA->getOffset(),
1568 OperandFlags);
1569 // Create Operands for the call.The Operands should have the following:
1570 // 1. Chain SDValue
1571 // 2. Callee which in this case is the Global address value.
1572 // 3. Registers live into the call.In this case its R0, as we
1573 // have just one argument to be passed.
1574 // 4. InFlag if there is any.
1575 // Note: The order is important.
1576
1577 if (InFlag) {
1578 SDValue Ops[] = { Chain, TGA,
1579 DAG.getRegister(Hexagon::R0, PtrVT), *InFlag };
1580 Chain = DAG.getNode(HexagonISD::CALLv3, dl, NodeTys, Ops);
1581 } else {
1582 SDValue Ops[] = { Chain, TGA, DAG.getRegister(Hexagon::R0, PtrVT)};
1583 Chain = DAG.getNode(HexagonISD::CALLv3, dl, NodeTys, Ops);
1584 }
1585
1586 // Inform MFI that function has calls.
1587 MFI->setAdjustsStack(true);
1588
1589 SDValue Flag = Chain.getValue(1);
1590 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
1591}
1592
1593//
1594// Lower using the intial executable model for TLS addresses
1595//
1596SDValue
1597HexagonTargetLowering::LowerToTLSInitialExecModel(GlobalAddressSDNode *GA,
1598 SelectionDAG &DAG) const {
1599 SDLoc dl(GA);
1600 int64_t Offset = GA->getOffset();
1601 auto PtrVT = getPointerTy(DAG.getDataLayout());
1602
1603 // Get the thread pointer.
1604 SDValue TP = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Hexagon::UGP, PtrVT);
1605
1606 Reloc::Model RM = HTM.getRelocationModel();
1607 unsigned char TF = (RM == Reloc::PIC_) ? HexagonII::MO_IEGOT
1608 : HexagonII::MO_IE;
1609
1610 // First generate the TLS symbol address
1611 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT,
1612 Offset, TF);
1613
1614 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1615
1616 if (RM == Reloc::PIC_) {
1617 // Generate the GOT pointer in case of position independent code
1618 SDValue GOT = LowerGLOBAL_OFFSET_TABLE(Sym, DAG);
1619
1620 // Add the TLS Symbol address to GOT pointer.This gives
1621 // GOT relative relocation for the symbol.
1622 Sym = DAG.getNode(ISD::ADD, dl, PtrVT, GOT, Sym);
1623 }
1624
1625 // Load the offset value for TLS symbol.This offset is relative to
1626 // thread pointer.
1627 SDValue LoadOffset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Sym,
1628 MachinePointerInfo(),
1629 false, false, false, 0);
1630
1631 // Address of the thread local variable is the add of thread
1632 // pointer and the offset of the variable.
1633 return DAG.getNode(ISD::ADD, dl, PtrVT, TP, LoadOffset);
1634}
1635
1636//
1637// Lower using the local executable model for TLS addresses
1638//
1639SDValue
1640HexagonTargetLowering::LowerToTLSLocalExecModel(GlobalAddressSDNode *GA,
1641 SelectionDAG &DAG) const {
1642 SDLoc dl(GA);
1643 int64_t Offset = GA->getOffset();
1644 auto PtrVT = getPointerTy(DAG.getDataLayout());
1645
1646 // Get the thread pointer.
1647 SDValue TP = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Hexagon::UGP, PtrVT);
1648 // Generate the TLS symbol address
1649 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT, Offset,
1650 HexagonII::MO_TPREL);
1651 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1652
1653 // Address of the thread local variable is the add of thread
1654 // pointer and the offset of the variable.
1655 return DAG.getNode(ISD::ADD, dl, PtrVT, TP, Sym);
1656}
1657
1658//
1659// Lower using the general dynamic model for TLS addresses
1660//
1661SDValue
1662HexagonTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
1663 SelectionDAG &DAG) const {
1664 SDLoc dl(GA);
1665 int64_t Offset = GA->getOffset();
1666 auto PtrVT = getPointerTy(DAG.getDataLayout());
1667
1668 // First generate the TLS symbol address
1669 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT, Offset,
1670 HexagonII::MO_GDGOT);
1671
1672 // Then, generate the GOT pointer
1673 SDValue GOT = LowerGLOBAL_OFFSET_TABLE(TGA, DAG);
1674
1675 // Add the TLS symbol and the GOT pointer
1676 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1677 SDValue Chain = DAG.getNode(ISD::ADD, dl, PtrVT, GOT, Sym);
1678
1679 // Copy over the argument to R0
1680 SDValue InFlag;
1681 Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, Hexagon::R0, Chain, InFlag);
1682 InFlag = Chain.getValue(1);
1683
1684 return GetDynamicTLSAddr(DAG, Chain, GA, &InFlag, PtrVT,
1685 Hexagon::R0, HexagonII::MO_GDPLT);
1686}
1687
1688//
1689// Lower TLS addresses.
1690//
1691// For now for dynamic models, we only support the general dynamic model.
1692//
1693SDValue
1694HexagonTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1695 SelectionDAG &DAG) const {
1696 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1697
1698 switch (HTM.getTLSModel(GA->getGlobal())) {
1699 case TLSModel::GeneralDynamic:
1700 case TLSModel::LocalDynamic:
1701 return LowerToTLSGeneralDynamicModel(GA, DAG);
1702 case TLSModel::InitialExec:
1703 return LowerToTLSInitialExecModel(GA, DAG);
1704 case TLSModel::LocalExec:
1705 return LowerToTLSLocalExecModel(GA, DAG);
1706 }
1707 llvm_unreachable("Bogus TLS model");
1708}
1709
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001710//===----------------------------------------------------------------------===//
1711// TargetLowering Implementation
1712//===----------------------------------------------------------------------===//
1713
Eric Christopherd737b762015-02-02 22:11:36 +00001714HexagonTargetLowering::HexagonTargetLowering(const TargetMachine &TM,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001715 const HexagonSubtarget &ST)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001716 : TargetLowering(TM), HTM(static_cast<const HexagonTargetMachine&>(TM)),
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001717 Subtarget(ST) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001718 bool IsV4 = !Subtarget.hasV5TOps();
1719 auto &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00001720 bool UseHVX = Subtarget.useHVXOps();
1721 bool UseHVXSgl = Subtarget.useHVXSglOps();
1722 bool UseHVXDbl = Subtarget.useHVXDblOps();
Sirish Pande69295b82012-05-10 20:20:25 +00001723
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001724 setPrefLoopAlignment(4);
1725 setPrefFunctionAlignment(4);
1726 setMinFunctionAlignment(2);
1727 setInsertFencesForAtomic(false);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001728 setStackPointerRegisterToSaveRestore(HRI.getStackRegister());
1729
1730 if (EnableHexSDNodeSched)
1731 setSchedulingPreference(Sched::VLIW);
1732 else
1733 setSchedulingPreference(Sched::Source);
1734
1735 // Limits for inline expansion of memcpy/memmove
1736 MaxStoresPerMemcpy = MaxStoresPerMemcpyCL;
1737 MaxStoresPerMemcpyOptSize = MaxStoresPerMemcpyOptSizeCL;
1738 MaxStoresPerMemmove = MaxStoresPerMemmoveCL;
1739 MaxStoresPerMemmoveOptSize = MaxStoresPerMemmoveOptSizeCL;
1740 MaxStoresPerMemset = MaxStoresPerMemsetCL;
1741 MaxStoresPerMemsetOptSize = MaxStoresPerMemsetOptSizeCL;
1742
1743 //
1744 // Set up register classes.
1745 //
1746
1747 addRegisterClass(MVT::i1, &Hexagon::PredRegsRegClass);
1748 addRegisterClass(MVT::v2i1, &Hexagon::PredRegsRegClass); // bbbbaaaa
1749 addRegisterClass(MVT::v4i1, &Hexagon::PredRegsRegClass); // ddccbbaa
1750 addRegisterClass(MVT::v8i1, &Hexagon::PredRegsRegClass); // hgfedcba
1751 addRegisterClass(MVT::i32, &Hexagon::IntRegsRegClass);
1752 addRegisterClass(MVT::v4i8, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001753 addRegisterClass(MVT::v2i16, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001754 addRegisterClass(MVT::i64, &Hexagon::DoubleRegsRegClass);
1755 addRegisterClass(MVT::v8i8, &Hexagon::DoubleRegsRegClass);
1756 addRegisterClass(MVT::v4i16, &Hexagon::DoubleRegsRegClass);
1757 addRegisterClass(MVT::v2i32, &Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001758
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001759 if (Subtarget.hasV5TOps()) {
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001760 addRegisterClass(MVT::f32, &Hexagon::IntRegsRegClass);
1761 addRegisterClass(MVT::f64, &Hexagon::DoubleRegsRegClass);
1762 }
Sirish Pande69295b82012-05-10 20:20:25 +00001763
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001764 if (Subtarget.hasV60TOps()) {
1765 if (Subtarget.useHVXSglOps()) {
1766 addRegisterClass(MVT::v64i8, &Hexagon::VectorRegsRegClass);
1767 addRegisterClass(MVT::v32i16, &Hexagon::VectorRegsRegClass);
1768 addRegisterClass(MVT::v16i32, &Hexagon::VectorRegsRegClass);
1769 addRegisterClass(MVT::v8i64, &Hexagon::VectorRegsRegClass);
1770 addRegisterClass(MVT::v128i8, &Hexagon::VecDblRegsRegClass);
1771 addRegisterClass(MVT::v64i16, &Hexagon::VecDblRegsRegClass);
1772 addRegisterClass(MVT::v32i32, &Hexagon::VecDblRegsRegClass);
1773 addRegisterClass(MVT::v16i64, &Hexagon::VecDblRegsRegClass);
1774 addRegisterClass(MVT::v512i1, &Hexagon::VecPredRegsRegClass);
1775 } else if (Subtarget.useHVXDblOps()) {
1776 addRegisterClass(MVT::v128i8, &Hexagon::VectorRegs128BRegClass);
1777 addRegisterClass(MVT::v64i16, &Hexagon::VectorRegs128BRegClass);
1778 addRegisterClass(MVT::v32i32, &Hexagon::VectorRegs128BRegClass);
1779 addRegisterClass(MVT::v16i64, &Hexagon::VectorRegs128BRegClass);
1780 addRegisterClass(MVT::v256i8, &Hexagon::VecDblRegs128BRegClass);
1781 addRegisterClass(MVT::v128i16, &Hexagon::VecDblRegs128BRegClass);
1782 addRegisterClass(MVT::v64i32, &Hexagon::VecDblRegs128BRegClass);
1783 addRegisterClass(MVT::v32i64, &Hexagon::VecDblRegs128BRegClass);
1784 addRegisterClass(MVT::v1024i1, &Hexagon::VecPredRegs128BRegClass);
1785 }
1786
1787 }
1788
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001789 //
1790 // Handling of scalar operations.
1791 //
1792 // All operations default to "legal", except:
1793 // - indexed loads and stores (pre-/post-incremented),
1794 // - ANY_EXTEND_VECTOR_INREG, ATOMIC_CMP_SWAP_WITH_SUCCESS, CONCAT_VECTORS,
1795 // ConstantFP, DEBUGTRAP, FCEIL, FCOPYSIGN, FEXP, FEXP2, FFLOOR, FGETSIGN,
1796 // FLOG, FLOG2, FLOG10, FMAXNUM, FMINNUM, FNEARBYINT, FRINT, FROUND, TRAP,
1797 // FTRUNC, PREFETCH, SIGN_EXTEND_VECTOR_INREG, ZERO_EXTEND_VECTOR_INREG,
1798 // which default to "expand" for at least one type.
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001799
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001800 // Misc operations.
1801 setOperationAction(ISD::ConstantFP, MVT::f32, Legal); // Default: expand
1802 setOperationAction(ISD::ConstantFP, MVT::f64, Legal); // Default: expand
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001803
1804 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001805 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001806 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001807 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
1808 setOperationAction(ISD::INLINEASM, MVT::Other, Custom);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00001809 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
1810 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001811 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001812 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001813 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001814 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001815
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001816 // Custom legalize GlobalAddress nodes into CONST32.
1817 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001818 setOperationAction(ISD::GlobalAddress, MVT::i8, Custom);
1819 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001820
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001821 // Hexagon needs to optimize cases with negative constants.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001822 setOperationAction(ISD::SETCC, MVT::i8, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001823 setOperationAction(ISD::SETCC, MVT::i16, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001824
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001825 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
1826 setOperationAction(ISD::VASTART, MVT::Other, Custom);
1827 setOperationAction(ISD::VAEND, MVT::Other, Expand);
1828 setOperationAction(ISD::VAARG, MVT::Other, Expand);
1829
1830 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
1831 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
1832 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
1833
1834 if (EmitJumpTables)
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001835 setMinimumJumpTableEntries(MinimumJumpTables);
Krzysztof Parzyszeka61f7da2016-01-13 21:43:13 +00001836 else
1837 setMinimumJumpTableEntries(INT_MAX);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001838 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001839
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001840 // Hexagon has instructions for add/sub with carry. The problem with
1841 // modeling these instructions is that they produce 2 results: Rdd and Px.
1842 // To model the update of Px, we will have to use Defs[p0..p3] which will
1843 // cause any predicate live range to spill. So, we pretend we dont't have
1844 // these instructions.
1845 setOperationAction(ISD::ADDE, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001846 setOperationAction(ISD::ADDE, MVT::i16, Expand);
1847 setOperationAction(ISD::ADDE, MVT::i32, Expand);
1848 setOperationAction(ISD::ADDE, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001849 setOperationAction(ISD::SUBE, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001850 setOperationAction(ISD::SUBE, MVT::i16, Expand);
1851 setOperationAction(ISD::SUBE, MVT::i32, Expand);
1852 setOperationAction(ISD::SUBE, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001853 setOperationAction(ISD::ADDC, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001854 setOperationAction(ISD::ADDC, MVT::i16, Expand);
1855 setOperationAction(ISD::ADDC, MVT::i32, Expand);
1856 setOperationAction(ISD::ADDC, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001857 setOperationAction(ISD::SUBC, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001858 setOperationAction(ISD::SUBC, MVT::i16, Expand);
1859 setOperationAction(ISD::SUBC, MVT::i32, Expand);
1860 setOperationAction(ISD::SUBC, MVT::i64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001861
Krzysztof Parzyszek2c4487d2015-04-13 20:37:01 +00001862 // Only add and sub that detect overflow are the saturating ones.
1863 for (MVT VT : MVT::integer_valuetypes()) {
1864 setOperationAction(ISD::UADDO, VT, Expand);
1865 setOperationAction(ISD::SADDO, VT, Expand);
1866 setOperationAction(ISD::USUBO, VT, Expand);
1867 setOperationAction(ISD::SSUBO, VT, Expand);
1868 }
1869
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001870 setOperationAction(ISD::CTLZ, MVT::i8, Promote);
1871 setOperationAction(ISD::CTLZ, MVT::i16, Promote);
1872 setOperationAction(ISD::CTTZ, MVT::i8, Promote);
1873 setOperationAction(ISD::CTTZ, MVT::i16, Promote);
1874 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8, Promote);
1875 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16, Promote);
1876 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i8, Promote);
1877 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16, Promote);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001878
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001879 // In V5, popcount can count # of 1s in i64 but returns i32.
1880 // On V4 it will be expanded (set later).
1881 setOperationAction(ISD::CTPOP, MVT::i8, Promote);
1882 setOperationAction(ISD::CTPOP, MVT::i16, Promote);
1883 setOperationAction(ISD::CTPOP, MVT::i32, Promote);
1884 setOperationAction(ISD::CTPOP, MVT::i64, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001885
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001886 // We custom lower i64 to i64 mul, so that it is not considered as a legal
1887 // operation. There is a pattern that will match i64 mul and transform it
1888 // to a series of instructions.
1889 setOperationAction(ISD::MUL, MVT::i64, Expand);
Colin LeMahieude68b662015-02-05 21:13:25 +00001890 setOperationAction(ISD::MULHS, MVT::i64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001891
Benjamin Kramer62460692015-04-25 14:46:53 +00001892 for (unsigned IntExpOp :
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001893 { ISD::SDIV, ISD::UDIV, ISD::SREM, ISD::UREM,
1894 ISD::SDIVREM, ISD::UDIVREM, ISD::ROTL, ISD::ROTR,
1895 ISD::BSWAP, ISD::SHL_PARTS, ISD::SRA_PARTS, ISD::SRL_PARTS,
1896 ISD::SMUL_LOHI, ISD::UMUL_LOHI }) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001897 setOperationAction(IntExpOp, MVT::i32, Expand);
1898 setOperationAction(IntExpOp, MVT::i64, Expand);
1899 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001900
Benjamin Kramer62460692015-04-25 14:46:53 +00001901 for (unsigned FPExpOp :
1902 {ISD::FDIV, ISD::FREM, ISD::FSQRT, ISD::FSIN, ISD::FCOS, ISD::FSINCOS,
1903 ISD::FPOW, ISD::FCOPYSIGN}) {
1904 setOperationAction(FPExpOp, MVT::f32, Expand);
1905 setOperationAction(FPExpOp, MVT::f64, Expand);
1906 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001907
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001908 // No extending loads from i32.
1909 for (MVT VT : MVT::integer_valuetypes()) {
1910 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
1911 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
1912 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
1913 }
1914 // Turn FP truncstore into trunc + store.
1915 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
1916 // Turn FP extload into load/fextend.
1917 for (MVT VT : MVT::fp_valuetypes())
1918 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001919
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001920 // Expand BR_CC and SELECT_CC for all integer and fp types.
1921 for (MVT VT : MVT::integer_valuetypes()) {
1922 setOperationAction(ISD::BR_CC, VT, Expand);
1923 setOperationAction(ISD::SELECT_CC, VT, Expand);
1924 }
1925 for (MVT VT : MVT::fp_valuetypes()) {
1926 setOperationAction(ISD::BR_CC, VT, Expand);
1927 setOperationAction(ISD::SELECT_CC, VT, Expand);
1928 }
1929 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001930
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001931 //
1932 // Handling of vector operations.
1933 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001934
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001935 // Custom lower v4i16 load only. Let v4i16 store to be
1936 // promoted for now.
1937 promoteLdStType(MVT::v4i8, MVT::i32);
1938 promoteLdStType(MVT::v2i16, MVT::i32);
1939 promoteLdStType(MVT::v8i8, MVT::i64);
1940 promoteLdStType(MVT::v2i32, MVT::i64);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001941
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001942 setOperationAction(ISD::LOAD, MVT::v4i16, Custom);
1943 setOperationAction(ISD::STORE, MVT::v4i16, Promote);
1944 AddPromotedToType(ISD::LOAD, MVT::v4i16, MVT::i64);
1945 AddPromotedToType(ISD::STORE, MVT::v4i16, MVT::i64);
1946
1947 // Set the action for vector operations to "expand", then override it with
1948 // either "custom" or "legal" for specific cases.
Craig Topper26260942015-10-18 05:15:34 +00001949 static const unsigned VectExpOps[] = {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001950 // Integer arithmetic:
1951 ISD::ADD, ISD::SUB, ISD::MUL, ISD::SDIV, ISD::UDIV,
1952 ISD::SREM, ISD::UREM, ISD::SDIVREM, ISD::UDIVREM, ISD::ADDC,
1953 ISD::SUBC, ISD::SADDO, ISD::UADDO, ISD::SSUBO, ISD::USUBO,
1954 ISD::SMUL_LOHI, ISD::UMUL_LOHI,
1955 // Logical/bit:
1956 ISD::AND, ISD::OR, ISD::XOR, ISD::ROTL, ISD::ROTR,
1957 ISD::CTPOP, ISD::CTLZ, ISD::CTTZ, ISD::CTLZ_ZERO_UNDEF,
1958 ISD::CTTZ_ZERO_UNDEF,
1959 // Floating point arithmetic/math functions:
1960 ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FMA, ISD::FDIV,
1961 ISD::FREM, ISD::FNEG, ISD::FABS, ISD::FSQRT, ISD::FSIN,
1962 ISD::FCOS, ISD::FPOWI, ISD::FPOW, ISD::FLOG, ISD::FLOG2,
1963 ISD::FLOG10, ISD::FEXP, ISD::FEXP2, ISD::FCEIL, ISD::FTRUNC,
1964 ISD::FRINT, ISD::FNEARBYINT, ISD::FROUND, ISD::FFLOOR,
1965 ISD::FMINNUM, ISD::FMAXNUM, ISD::FSINCOS,
1966 // Misc:
1967 ISD::SELECT, ISD::ConstantPool,
1968 // Vector:
1969 ISD::BUILD_VECTOR, ISD::SCALAR_TO_VECTOR,
1970 ISD::EXTRACT_VECTOR_ELT, ISD::INSERT_VECTOR_ELT,
1971 ISD::EXTRACT_SUBVECTOR, ISD::INSERT_SUBVECTOR,
1972 ISD::CONCAT_VECTORS, ISD::VECTOR_SHUFFLE
1973 };
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001974
1975 for (MVT VT : MVT::vector_valuetypes()) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001976 for (unsigned VectExpOp : VectExpOps)
1977 setOperationAction(VectExpOp, VT, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001978
1979 // Expand all extended loads and truncating stores:
1980 for (MVT TargetVT : MVT::vector_valuetypes()) {
1981 setLoadExtAction(ISD::EXTLOAD, TargetVT, VT, Expand);
1982 setTruncStoreAction(VT, TargetVT, Expand);
1983 }
1984
1985 setOperationAction(ISD::SRA, VT, Custom);
1986 setOperationAction(ISD::SHL, VT, Custom);
1987 setOperationAction(ISD::SRL, VT, Custom);
1988 }
1989
1990 // Types natively supported:
Benjamin Kramer62460692015-04-25 14:46:53 +00001991 for (MVT NativeVT : {MVT::v2i1, MVT::v4i1, MVT::v8i1, MVT::v32i1, MVT::v64i1,
1992 MVT::v4i8, MVT::v8i8, MVT::v2i16, MVT::v4i16, MVT::v1i32,
1993 MVT::v2i32, MVT::v1i64}) {
1994 setOperationAction(ISD::BUILD_VECTOR, NativeVT, Custom);
1995 setOperationAction(ISD::EXTRACT_VECTOR_ELT, NativeVT, Custom);
1996 setOperationAction(ISD::INSERT_VECTOR_ELT, NativeVT, Custom);
1997 setOperationAction(ISD::EXTRACT_SUBVECTOR, NativeVT, Custom);
1998 setOperationAction(ISD::INSERT_SUBVECTOR, NativeVT, Custom);
1999 setOperationAction(ISD::CONCAT_VECTORS, NativeVT, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002000
Benjamin Kramer62460692015-04-25 14:46:53 +00002001 setOperationAction(ISD::ADD, NativeVT, Legal);
2002 setOperationAction(ISD::SUB, NativeVT, Legal);
2003 setOperationAction(ISD::MUL, NativeVT, Legal);
2004 setOperationAction(ISD::AND, NativeVT, Legal);
2005 setOperationAction(ISD::OR, NativeVT, Legal);
2006 setOperationAction(ISD::XOR, NativeVT, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002007 }
2008
2009 setOperationAction(ISD::SETCC, MVT::v2i16, Custom);
2010 setOperationAction(ISD::VSELECT, MVT::v2i16, Custom);
2011 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
2012 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002013 if (UseHVX) {
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002014 if (UseHVXSgl) {
2015 setOperationAction(ISD::CONCAT_VECTORS, MVT::v128i8, Custom);
2016 setOperationAction(ISD::CONCAT_VECTORS, MVT::v64i16, Custom);
2017 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i32, Custom);
2018 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i64, Custom);
2019 } else if (UseHVXDbl) {
2020 setOperationAction(ISD::CONCAT_VECTORS, MVT::v256i8, Custom);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002021 setOperationAction(ISD::CONCAT_VECTORS, MVT::v128i16, Custom);
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002022 setOperationAction(ISD::CONCAT_VECTORS, MVT::v64i32, Custom);
2023 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i64, Custom);
2024 } else {
2025 llvm_unreachable("Unrecognized HVX mode");
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002026 }
2027 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002028 // Subtarget-specific operation actions.
2029 //
2030 if (Subtarget.hasV5TOps()) {
2031 setOperationAction(ISD::FMA, MVT::f64, Expand);
2032 setOperationAction(ISD::FADD, MVT::f64, Expand);
2033 setOperationAction(ISD::FSUB, MVT::f64, Expand);
2034 setOperationAction(ISD::FMUL, MVT::f64, Expand);
2035
2036 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Promote);
2037 setOperationAction(ISD::FP_TO_UINT, MVT::i8, Promote);
2038 setOperationAction(ISD::FP_TO_UINT, MVT::i16, Promote);
2039 setOperationAction(ISD::FP_TO_SINT, MVT::i1, Promote);
2040 setOperationAction(ISD::FP_TO_SINT, MVT::i8, Promote);
2041 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
2042 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote);
2043 setOperationAction(ISD::UINT_TO_FP, MVT::i8, Promote);
2044 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote);
2045 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote);
2046 setOperationAction(ISD::SINT_TO_FP, MVT::i8, Promote);
2047 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
2048
2049 } else { // V4
2050 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
2051 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Expand);
2052 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
2053 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
2054 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Expand);
2055 setOperationAction(ISD::FP_TO_SINT, MVT::f32, Expand);
2056 setOperationAction(ISD::FP_EXTEND, MVT::f32, Expand);
2057 setOperationAction(ISD::FP_ROUND, MVT::f64, Expand);
2058 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
2059
2060 setOperationAction(ISD::CTPOP, MVT::i8, Expand);
2061 setOperationAction(ISD::CTPOP, MVT::i16, Expand);
2062 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
2063 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
2064
2065 // Expand these operations for both f32 and f64:
Benjamin Kramer62460692015-04-25 14:46:53 +00002066 for (unsigned FPExpOpV4 :
2067 {ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FABS, ISD::FNEG, ISD::FMA}) {
2068 setOperationAction(FPExpOpV4, MVT::f32, Expand);
2069 setOperationAction(FPExpOpV4, MVT::f64, Expand);
2070 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002071
Benjamin Kramer62460692015-04-25 14:46:53 +00002072 for (ISD::CondCode FPExpCCV4 :
2073 {ISD::SETOEQ, ISD::SETOGT, ISD::SETOLT, ISD::SETOGE, ISD::SETOLE,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002074 ISD::SETUO, ISD::SETO}) {
Benjamin Kramer62460692015-04-25 14:46:53 +00002075 setCondCodeAction(FPExpCCV4, MVT::f32, Expand);
2076 setCondCodeAction(FPExpCCV4, MVT::f64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002077 }
2078 }
2079
2080 // Handling of indexed loads/stores: default is "expand".
2081 //
Benjamin Kramer62460692015-04-25 14:46:53 +00002082 for (MVT LSXTy : {MVT::i8, MVT::i16, MVT::i32, MVT::i64}) {
2083 setIndexedLoadAction(ISD::POST_INC, LSXTy, Legal);
2084 setIndexedStoreAction(ISD::POST_INC, LSXTy, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002085 }
2086
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002087 if (UseHVXDbl) {
2088 for (MVT VT : {MVT::v128i8, MVT::v64i16, MVT::v32i32, MVT::v16i64}) {
2089 setIndexedLoadAction(ISD::POST_INC, VT, Legal);
2090 setIndexedStoreAction(ISD::POST_INC, VT, Legal);
2091 }
2092 }
2093
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002094 computeRegisterProperties(&HRI);
2095
2096 //
2097 // Library calls for unsupported operations
2098 //
2099 bool FastMath = EnableFastMath;
2100
Benjamin Kramera37c8092015-04-25 14:46:46 +00002101 setLibcallName(RTLIB::SDIV_I32, "__hexagon_divsi3");
2102 setLibcallName(RTLIB::SDIV_I64, "__hexagon_divdi3");
2103 setLibcallName(RTLIB::UDIV_I32, "__hexagon_udivsi3");
2104 setLibcallName(RTLIB::UDIV_I64, "__hexagon_udivdi3");
2105 setLibcallName(RTLIB::SREM_I32, "__hexagon_modsi3");
2106 setLibcallName(RTLIB::SREM_I64, "__hexagon_moddi3");
2107 setLibcallName(RTLIB::UREM_I32, "__hexagon_umodsi3");
2108 setLibcallName(RTLIB::UREM_I64, "__hexagon_umoddi3");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002109
Benjamin Kramera37c8092015-04-25 14:46:46 +00002110 setLibcallName(RTLIB::SINTTOFP_I128_F64, "__hexagon_floattidf");
2111 setLibcallName(RTLIB::SINTTOFP_I128_F32, "__hexagon_floattisf");
2112 setLibcallName(RTLIB::FPTOUINT_F32_I128, "__hexagon_fixunssfti");
2113 setLibcallName(RTLIB::FPTOUINT_F64_I128, "__hexagon_fixunsdfti");
2114 setLibcallName(RTLIB::FPTOSINT_F32_I128, "__hexagon_fixsfti");
2115 setLibcallName(RTLIB::FPTOSINT_F64_I128, "__hexagon_fixdfti");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002116
2117 if (IsV4) {
2118 // Handle single-precision floating point operations on V4.
Benjamin Kramera37c8092015-04-25 14:46:46 +00002119 if (FastMath) {
2120 setLibcallName(RTLIB::ADD_F32, "__hexagon_fast_addsf3");
2121 setLibcallName(RTLIB::SUB_F32, "__hexagon_fast_subsf3");
2122 setLibcallName(RTLIB::MUL_F32, "__hexagon_fast_mulsf3");
2123 setLibcallName(RTLIB::OGT_F32, "__hexagon_fast_gtsf2");
2124 setLibcallName(RTLIB::OLT_F32, "__hexagon_fast_ltsf2");
2125 // Double-precision compares.
2126 setLibcallName(RTLIB::OGT_F64, "__hexagon_fast_gtdf2");
2127 setLibcallName(RTLIB::OLT_F64, "__hexagon_fast_ltdf2");
2128 } else {
2129 setLibcallName(RTLIB::ADD_F32, "__hexagon_addsf3");
2130 setLibcallName(RTLIB::SUB_F32, "__hexagon_subsf3");
2131 setLibcallName(RTLIB::MUL_F32, "__hexagon_mulsf3");
2132 setLibcallName(RTLIB::OGT_F32, "__hexagon_gtsf2");
2133 setLibcallName(RTLIB::OLT_F32, "__hexagon_ltsf2");
2134 // Double-precision compares.
2135 setLibcallName(RTLIB::OGT_F64, "__hexagon_gtdf2");
2136 setLibcallName(RTLIB::OLT_F64, "__hexagon_ltdf2");
2137 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002138 }
2139
2140 // This is the only fast library function for sqrtd.
2141 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00002142 setLibcallName(RTLIB::SQRT_F64, "__hexagon_fast2_sqrtdf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002143
Benjamin Kramera37c8092015-04-25 14:46:46 +00002144 // Prefix is: nothing for "slow-math",
2145 // "fast2_" for V4 fast-math and V5+ fast-math double-precision
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002146 // (actually, keep fast-math and fast-math2 separate for now)
Benjamin Kramera37c8092015-04-25 14:46:46 +00002147 if (FastMath) {
2148 setLibcallName(RTLIB::ADD_F64, "__hexagon_fast_adddf3");
2149 setLibcallName(RTLIB::SUB_F64, "__hexagon_fast_subdf3");
2150 setLibcallName(RTLIB::MUL_F64, "__hexagon_fast_muldf3");
2151 setLibcallName(RTLIB::DIV_F64, "__hexagon_fast_divdf3");
2152 // Calling __hexagon_fast2_divsf3 with fast-math on V5 (ok).
2153 setLibcallName(RTLIB::DIV_F32, "__hexagon_fast_divsf3");
2154 } else {
2155 setLibcallName(RTLIB::ADD_F64, "__hexagon_adddf3");
2156 setLibcallName(RTLIB::SUB_F64, "__hexagon_subdf3");
2157 setLibcallName(RTLIB::MUL_F64, "__hexagon_muldf3");
2158 setLibcallName(RTLIB::DIV_F64, "__hexagon_divdf3");
2159 setLibcallName(RTLIB::DIV_F32, "__hexagon_divsf3");
2160 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002161
2162 if (Subtarget.hasV5TOps()) {
2163 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00002164 setLibcallName(RTLIB::SQRT_F32, "__hexagon_fast2_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002165 else
Benjamin Kramera37c8092015-04-25 14:46:46 +00002166 setLibcallName(RTLIB::SQRT_F32, "__hexagon_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002167 } else {
2168 // V4
Benjamin Kramera37c8092015-04-25 14:46:46 +00002169 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__hexagon_floatsisf");
2170 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__hexagon_floatsidf");
2171 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__hexagon_floatdisf");
2172 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__hexagon_floatdidf");
2173 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__hexagon_floatunsisf");
2174 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__hexagon_floatunsidf");
2175 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__hexagon_floatundisf");
2176 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__hexagon_floatundidf");
2177 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__hexagon_fixunssfsi");
2178 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__hexagon_fixunssfdi");
2179 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__hexagon_fixunsdfsi");
2180 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__hexagon_fixunsdfdi");
2181 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__hexagon_fixsfsi");
2182 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__hexagon_fixsfdi");
2183 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__hexagon_fixdfsi");
2184 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__hexagon_fixdfdi");
2185 setLibcallName(RTLIB::FPEXT_F32_F64, "__hexagon_extendsfdf2");
2186 setLibcallName(RTLIB::FPROUND_F64_F32, "__hexagon_truncdfsf2");
2187 setLibcallName(RTLIB::OEQ_F32, "__hexagon_eqsf2");
2188 setLibcallName(RTLIB::OEQ_F64, "__hexagon_eqdf2");
2189 setLibcallName(RTLIB::OGE_F32, "__hexagon_gesf2");
2190 setLibcallName(RTLIB::OGE_F64, "__hexagon_gedf2");
2191 setLibcallName(RTLIB::OLE_F32, "__hexagon_lesf2");
2192 setLibcallName(RTLIB::OLE_F64, "__hexagon_ledf2");
2193 setLibcallName(RTLIB::UNE_F32, "__hexagon_nesf2");
2194 setLibcallName(RTLIB::UNE_F64, "__hexagon_nedf2");
2195 setLibcallName(RTLIB::UO_F32, "__hexagon_unordsf2");
2196 setLibcallName(RTLIB::UO_F64, "__hexagon_unorddf2");
2197 setLibcallName(RTLIB::O_F32, "__hexagon_unordsf2");
2198 setLibcallName(RTLIB::O_F64, "__hexagon_unorddf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002199 }
2200
2201 // These cause problems when the shift amount is non-constant.
2202 setLibcallName(RTLIB::SHL_I128, nullptr);
2203 setLibcallName(RTLIB::SRL_I128, nullptr);
2204 setLibcallName(RTLIB::SRA_I128, nullptr);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002205}
2206
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002207
2208const char* HexagonTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00002209 switch ((HexagonISD::NodeType)Opcode) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002210 case HexagonISD::ALLOCA: return "HexagonISD::ALLOCA";
2211 case HexagonISD::ARGEXTEND: return "HexagonISD::ARGEXTEND";
2212 case HexagonISD::AT_GOT: return "HexagonISD::AT_GOT";
2213 case HexagonISD::AT_PCREL: return "HexagonISD::AT_PCREL";
2214 case HexagonISD::BARRIER: return "HexagonISD::BARRIER";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002215 case HexagonISD::CALLR: return "HexagonISD::CALLR";
2216 case HexagonISD::CALLv3nr: return "HexagonISD::CALLv3nr";
2217 case HexagonISD::CALLv3: return "HexagonISD::CALLv3";
2218 case HexagonISD::COMBINE: return "HexagonISD::COMBINE";
2219 case HexagonISD::CONST32_GP: return "HexagonISD::CONST32_GP";
2220 case HexagonISD::CONST32: return "HexagonISD::CONST32";
2221 case HexagonISD::CP: return "HexagonISD::CP";
2222 case HexagonISD::DCFETCH: return "HexagonISD::DCFETCH";
2223 case HexagonISD::EH_RETURN: return "HexagonISD::EH_RETURN";
2224 case HexagonISD::EXTRACTU: return "HexagonISD::EXTRACTU";
2225 case HexagonISD::EXTRACTURP: return "HexagonISD::EXTRACTURP";
2226 case HexagonISD::FCONST32: return "HexagonISD::FCONST32";
2227 case HexagonISD::INSERT: return "HexagonISD::INSERT";
2228 case HexagonISD::INSERTRP: return "HexagonISD::INSERTRP";
2229 case HexagonISD::JT: return "HexagonISD::JT";
2230 case HexagonISD::PACKHL: return "HexagonISD::PACKHL";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002231 case HexagonISD::POPCOUNT: return "HexagonISD::POPCOUNT";
2232 case HexagonISD::RET_FLAG: return "HexagonISD::RET_FLAG";
2233 case HexagonISD::SHUFFEB: return "HexagonISD::SHUFFEB";
2234 case HexagonISD::SHUFFEH: return "HexagonISD::SHUFFEH";
2235 case HexagonISD::SHUFFOB: return "HexagonISD::SHUFFOB";
2236 case HexagonISD::SHUFFOH: return "HexagonISD::SHUFFOH";
2237 case HexagonISD::TC_RETURN: return "HexagonISD::TC_RETURN";
2238 case HexagonISD::VCMPBEQ: return "HexagonISD::VCMPBEQ";
2239 case HexagonISD::VCMPBGT: return "HexagonISD::VCMPBGT";
2240 case HexagonISD::VCMPBGTU: return "HexagonISD::VCMPBGTU";
2241 case HexagonISD::VCMPHEQ: return "HexagonISD::VCMPHEQ";
2242 case HexagonISD::VCMPHGT: return "HexagonISD::VCMPHGT";
2243 case HexagonISD::VCMPHGTU: return "HexagonISD::VCMPHGTU";
2244 case HexagonISD::VCMPWEQ: return "HexagonISD::VCMPWEQ";
2245 case HexagonISD::VCMPWGT: return "HexagonISD::VCMPWGT";
2246 case HexagonISD::VCMPWGTU: return "HexagonISD::VCMPWGTU";
Krzysztof Parzyszekc168c012015-12-03 16:47:20 +00002247 case HexagonISD::VCOMBINE: return "HexagonISD::VCOMBINE";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002248 case HexagonISD::VSHLH: return "HexagonISD::VSHLH";
2249 case HexagonISD::VSHLW: return "HexagonISD::VSHLW";
2250 case HexagonISD::VSPLATB: return "HexagonISD::VSPLTB";
2251 case HexagonISD::VSPLATH: return "HexagonISD::VSPLATH";
2252 case HexagonISD::VSRAH: return "HexagonISD::VSRAH";
2253 case HexagonISD::VSRAW: return "HexagonISD::VSRAW";
2254 case HexagonISD::VSRLH: return "HexagonISD::VSRLH";
2255 case HexagonISD::VSRLW: return "HexagonISD::VSRLW";
2256 case HexagonISD::VSXTBH: return "HexagonISD::VSXTBH";
2257 case HexagonISD::VSXTBW: return "HexagonISD::VSXTBW";
Matthias Braund04893f2015-05-07 21:33:59 +00002258 case HexagonISD::OP_END: break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002259 }
Matthias Braund04893f2015-05-07 21:33:59 +00002260 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002261}
2262
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002263bool HexagonTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002264 EVT MTy1 = EVT::getEVT(Ty1);
2265 EVT MTy2 = EVT::getEVT(Ty2);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002266 if (!MTy1.isSimple() || !MTy2.isSimple())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002267 return false;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002268 return (MTy1.getSimpleVT() == MVT::i64) && (MTy2.getSimpleVT() == MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002269}
2270
2271bool HexagonTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002272 if (!VT1.isSimple() || !VT2.isSimple())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002273 return false;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002274 return (VT1.getSimpleVT() == MVT::i64) && (VT2.getSimpleVT() == MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002275}
2276
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002277// shouldExpandBuildVectorWithShuffles
2278// Should we expand the build vector with shuffles?
2279bool
2280HexagonTargetLowering::shouldExpandBuildVectorWithShuffles(EVT VT,
2281 unsigned DefinedValues) const {
2282
2283 // Hexagon vector shuffle operates on element sizes of bytes or halfwords
2284 EVT EltVT = VT.getVectorElementType();
2285 int EltBits = EltVT.getSizeInBits();
2286 if ((EltBits != 8) && (EltBits != 16))
2287 return false;
2288
2289 return TargetLowering::shouldExpandBuildVectorWithShuffles(VT, DefinedValues);
2290}
2291
2292// LowerVECTOR_SHUFFLE - Lower a vector shuffle (V1, V2, V3). V1 and
2293// V2 are the two vectors to select data from, V3 is the permutation.
2294static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
2295 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
2296 SDValue V1 = Op.getOperand(0);
2297 SDValue V2 = Op.getOperand(1);
2298 SDLoc dl(Op);
2299 EVT VT = Op.getValueType();
2300
2301 if (V2.getOpcode() == ISD::UNDEF)
2302 V2 = V1;
2303
2304 if (SVN->isSplat()) {
2305 int Lane = SVN->getSplatIndex();
2306 if (Lane == -1) Lane = 0;
2307
2308 // Test if V1 is a SCALAR_TO_VECTOR.
2309 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR)
2310 return createSplat(DAG, dl, VT, V1.getOperand(0));
2311
2312 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
2313 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
2314 // reaches it).
2315 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
2316 !isa<ConstantSDNode>(V1.getOperand(0))) {
2317 bool IsScalarToVector = true;
2318 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
2319 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
2320 IsScalarToVector = false;
2321 break;
2322 }
2323 if (IsScalarToVector)
2324 return createSplat(DAG, dl, VT, V1.getOperand(0));
2325 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002326 return createSplat(DAG, dl, VT, DAG.getConstant(Lane, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002327 }
2328
2329 // FIXME: We need to support more general vector shuffles. See
2330 // below the comment from the ARM backend that deals in the general
2331 // case with the vector shuffles. For now, let expand handle these.
2332 return SDValue();
2333
2334 // If the shuffle is not directly supported and it has 4 elements, use
2335 // the PerfectShuffle-generated table to synthesize it from other shuffles.
2336}
2337
2338// If BUILD_VECTOR has same base element repeated several times,
2339// report true.
2340static bool isCommonSplatElement(BuildVectorSDNode *BVN) {
2341 unsigned NElts = BVN->getNumOperands();
2342 SDValue V0 = BVN->getOperand(0);
2343
2344 for (unsigned i = 1, e = NElts; i != e; ++i) {
2345 if (BVN->getOperand(i) != V0)
2346 return false;
2347 }
2348 return true;
2349}
2350
2351// LowerVECTOR_SHIFT - Lower a vector shift. Try to convert
2352// <VT> = SHL/SRA/SRL <VT> by <VT> to Hexagon specific
2353// <VT> = SHL/SRA/SRL <VT> by <IT/i32>.
2354static SDValue LowerVECTOR_SHIFT(SDValue Op, SelectionDAG &DAG) {
2355 BuildVectorSDNode *BVN = 0;
2356 SDValue V1 = Op.getOperand(0);
2357 SDValue V2 = Op.getOperand(1);
2358 SDValue V3;
2359 SDLoc dl(Op);
2360 EVT VT = Op.getValueType();
2361
2362 if ((BVN = dyn_cast<BuildVectorSDNode>(V1.getNode())) &&
2363 isCommonSplatElement(BVN))
2364 V3 = V2;
2365 else if ((BVN = dyn_cast<BuildVectorSDNode>(V2.getNode())) &&
2366 isCommonSplatElement(BVN))
2367 V3 = V1;
2368 else
2369 return SDValue();
2370
2371 SDValue CommonSplat = BVN->getOperand(0);
2372 SDValue Result;
2373
2374 if (VT.getSimpleVT() == MVT::v4i16) {
2375 switch (Op.getOpcode()) {
2376 case ISD::SRA:
2377 Result = DAG.getNode(HexagonISD::VSRAH, dl, VT, V3, CommonSplat);
2378 break;
2379 case ISD::SHL:
2380 Result = DAG.getNode(HexagonISD::VSHLH, dl, VT, V3, CommonSplat);
2381 break;
2382 case ISD::SRL:
2383 Result = DAG.getNode(HexagonISD::VSRLH, dl, VT, V3, CommonSplat);
2384 break;
2385 default:
2386 return SDValue();
2387 }
2388 } else if (VT.getSimpleVT() == MVT::v2i32) {
2389 switch (Op.getOpcode()) {
2390 case ISD::SRA:
2391 Result = DAG.getNode(HexagonISD::VSRAW, dl, VT, V3, CommonSplat);
2392 break;
2393 case ISD::SHL:
2394 Result = DAG.getNode(HexagonISD::VSHLW, dl, VT, V3, CommonSplat);
2395 break;
2396 case ISD::SRL:
2397 Result = DAG.getNode(HexagonISD::VSRLW, dl, VT, V3, CommonSplat);
2398 break;
2399 default:
2400 return SDValue();
2401 }
2402 } else {
2403 return SDValue();
2404 }
2405
2406 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
2407}
2408
2409SDValue
2410HexagonTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
2411 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
2412 SDLoc dl(Op);
2413 EVT VT = Op.getValueType();
2414
2415 unsigned Size = VT.getSizeInBits();
2416
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002417 // Only handle vectors of 64 bits or shorter.
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002418 if (Size > 64)
2419 return SDValue();
2420
2421 APInt APSplatBits, APSplatUndef;
2422 unsigned SplatBitSize;
2423 bool HasAnyUndefs;
2424 unsigned NElts = BVN->getNumOperands();
2425
2426 // Try to generate a SPLAT instruction.
2427 if ((VT.getSimpleVT() == MVT::v4i8 || VT.getSimpleVT() == MVT::v4i16) &&
2428 (BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
2429 HasAnyUndefs, 0, true) && SplatBitSize <= 16)) {
2430 unsigned SplatBits = APSplatBits.getZExtValue();
2431 int32_t SextVal = ((int32_t) (SplatBits << (32 - SplatBitSize)) >>
2432 (32 - SplatBitSize));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002433 return createSplat(DAG, dl, VT, DAG.getConstant(SextVal, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002434 }
2435
2436 // Try to generate COMBINE to build v2i32 vectors.
2437 if (VT.getSimpleVT() == MVT::v2i32) {
2438 SDValue V0 = BVN->getOperand(0);
2439 SDValue V1 = BVN->getOperand(1);
2440
2441 if (V0.getOpcode() == ISD::UNDEF)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002442 V0 = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002443 if (V1.getOpcode() == ISD::UNDEF)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002444 V1 = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002445
2446 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(V0);
2447 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(V1);
2448 // If the element isn't a constant, it is in a register:
2449 // generate a COMBINE Register Register instruction.
2450 if (!C0 || !C1)
2451 return DAG.getNode(HexagonISD::COMBINE, dl, VT, V1, V0);
2452
2453 // If one of the operands is an 8 bit integer constant, generate
2454 // a COMBINE Immediate Immediate instruction.
2455 if (isInt<8>(C0->getSExtValue()) ||
2456 isInt<8>(C1->getSExtValue()))
2457 return DAG.getNode(HexagonISD::COMBINE, dl, VT, V1, V0);
2458 }
2459
2460 // Try to generate a S2_packhl to build v2i16 vectors.
2461 if (VT.getSimpleVT() == MVT::v2i16) {
2462 for (unsigned i = 0, e = NElts; i != e; ++i) {
2463 if (BVN->getOperand(i).getOpcode() == ISD::UNDEF)
2464 continue;
2465 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(BVN->getOperand(i));
2466 // If the element isn't a constant, it is in a register:
2467 // generate a S2_packhl instruction.
2468 if (!Cst) {
2469 SDValue pack = DAG.getNode(HexagonISD::PACKHL, dl, MVT::v4i16,
2470 BVN->getOperand(1), BVN->getOperand(0));
2471
2472 return DAG.getTargetExtractSubreg(Hexagon::subreg_loreg, dl, MVT::v2i16,
2473 pack);
2474 }
2475 }
2476 }
2477
2478 // In the general case, generate a CONST32 or a CONST64 for constant vectors,
2479 // and insert_vector_elt for all the other cases.
2480 uint64_t Res = 0;
2481 unsigned EltSize = Size / NElts;
2482 SDValue ConstVal;
2483 uint64_t Mask = ~uint64_t(0ULL) >> (64 - EltSize);
2484 bool HasNonConstantElements = false;
2485
2486 for (unsigned i = 0, e = NElts; i != e; ++i) {
2487 // LLVM's BUILD_VECTOR operands are in Little Endian mode, whereas Hexagon's
2488 // combine, const64, etc. are Big Endian.
2489 unsigned OpIdx = NElts - i - 1;
2490 SDValue Operand = BVN->getOperand(OpIdx);
2491 if (Operand.getOpcode() == ISD::UNDEF)
2492 continue;
2493
2494 int64_t Val = 0;
2495 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Operand))
2496 Val = Cst->getSExtValue();
2497 else
2498 HasNonConstantElements = true;
2499
2500 Val &= Mask;
2501 Res = (Res << EltSize) | Val;
2502 }
2503
2504 if (Size == 64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002505 ConstVal = DAG.getConstant(Res, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002506 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002507 ConstVal = DAG.getConstant(Res, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002508
2509 // When there are non constant operands, add them with INSERT_VECTOR_ELT to
2510 // ConstVal, the constant part of the vector.
2511 if (HasNonConstantElements) {
2512 EVT EltVT = VT.getVectorElementType();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002513 SDValue Width = DAG.getConstant(EltVT.getSizeInBits(), dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002514 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002515 DAG.getConstant(32, dl, MVT::i64));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002516
2517 for (unsigned i = 0, e = NElts; i != e; ++i) {
2518 // LLVM's BUILD_VECTOR operands are in Little Endian mode, whereas Hexagon
2519 // is Big Endian.
2520 unsigned OpIdx = NElts - i - 1;
2521 SDValue Operand = BVN->getOperand(OpIdx);
Benjamin Kramer619c4e52015-04-10 11:24:51 +00002522 if (isa<ConstantSDNode>(Operand))
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002523 // This operand is already in ConstVal.
2524 continue;
2525
2526 if (VT.getSizeInBits() == 64 &&
2527 Operand.getValueType().getSizeInBits() == 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002528 SDValue C = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002529 Operand = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, Operand);
2530 }
2531
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002532 SDValue Idx = DAG.getConstant(OpIdx, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002533 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i64, Idx, Width);
2534 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2535 const SDValue Ops[] = {ConstVal, Operand, Combined};
2536
2537 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002538 ConstVal = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002539 else
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002540 ConstVal = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002541 }
2542 }
2543
2544 return DAG.getNode(ISD::BITCAST, dl, VT, ConstVal);
2545}
2546
2547SDValue
2548HexagonTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
2549 SelectionDAG &DAG) const {
2550 SDLoc dl(Op);
Krzysztof Parzyszekc168c012015-12-03 16:47:20 +00002551 bool UseHVX = Subtarget.useHVXOps();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002552 EVT VT = Op.getValueType();
2553 unsigned NElts = Op.getNumOperands();
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002554 SDValue Vec0 = Op.getOperand(0);
2555 EVT VecVT = Vec0.getValueType();
2556 unsigned Width = VecVT.getSizeInBits();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002557
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002558 if (NElts == 2) {
2559 MVT ST = VecVT.getSimpleVT();
2560 // We are trying to concat two v2i16 to a single v4i16, or two v4i8
2561 // into a single v8i8.
2562 if (ST == MVT::v2i16 || ST == MVT::v4i8)
2563 return DAG.getNode(HexagonISD::COMBINE, dl, VT, Op.getOperand(1), Vec0);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002564
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002565 if (UseHVX) {
2566 assert((Width == 64*8 && Subtarget.useHVXSglOps()) ||
2567 (Width == 128*8 && Subtarget.useHVXDblOps()));
2568 SDValue Vec1 = Op.getOperand(1);
2569 MVT OpTy = Subtarget.useHVXSglOps() ? MVT::v16i32 : MVT::v32i32;
2570 MVT ReTy = Subtarget.useHVXSglOps() ? MVT::v32i32 : MVT::v64i32;
2571 SDValue B0 = DAG.getNode(ISD::BITCAST, dl, OpTy, Vec0);
2572 SDValue B1 = DAG.getNode(ISD::BITCAST, dl, OpTy, Vec1);
2573 SDValue VC = DAG.getNode(HexagonISD::VCOMBINE, dl, ReTy, B1, B0);
2574 return DAG.getNode(ISD::BITCAST, dl, VT, VC);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002575 }
2576 }
2577
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002578 if (VT.getSizeInBits() != 32 && VT.getSizeInBits() != 64)
2579 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002580
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002581 SDValue C0 = DAG.getConstant(0, dl, MVT::i64);
2582 SDValue C32 = DAG.getConstant(32, dl, MVT::i64);
2583 SDValue W = DAG.getConstant(Width, dl, MVT::i64);
2584 // Create the "width" part of the argument to insert_rp/insertp_rp.
2585 SDValue S = DAG.getNode(ISD::SHL, dl, MVT::i64, W, C32);
2586 SDValue V = C0;
2587
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002588 for (unsigned i = 0, e = NElts; i != e; ++i) {
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002589 unsigned N = NElts-i-1;
2590 SDValue OpN = Op.getOperand(N);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002591
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002592 if (VT.getSizeInBits() == 64 && OpN.getValueType().getSizeInBits() == 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002593 SDValue C = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002594 OpN = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, OpN);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002595 }
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002596 SDValue Idx = DAG.getConstant(N, dl, MVT::i64);
2597 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i64, Idx, W);
2598 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, S, Offset);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002599 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002600 V = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, {V, OpN, Or});
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002601 else
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002602 V = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, {V, OpN, Or});
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002603 }
2604
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002605 return DAG.getNode(ISD::BITCAST, dl, VT, V);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002606}
2607
2608SDValue
2609HexagonTargetLowering::LowerEXTRACT_VECTOR(SDValue Op,
2610 SelectionDAG &DAG) const {
2611 EVT VT = Op.getValueType();
2612 int VTN = VT.isVector() ? VT.getVectorNumElements() : 1;
2613 SDLoc dl(Op);
2614 SDValue Idx = Op.getOperand(1);
2615 SDValue Vec = Op.getOperand(0);
2616 EVT VecVT = Vec.getValueType();
2617 EVT EltVT = VecVT.getVectorElementType();
2618 int EltSize = EltVT.getSizeInBits();
2619 SDValue Width = DAG.getConstant(Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT ?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002620 EltSize : VTN * EltSize, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002621
2622 // Constant element number.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002623 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Idx)) {
2624 uint64_t X = CI->getZExtValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002625 SDValue Offset = DAG.getConstant(X * EltSize, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002626 const SDValue Ops[] = {Vec, Width, Offset};
2627
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002628 ConstantSDNode *CW = dyn_cast<ConstantSDNode>(Width);
2629 assert(CW && "Non constant width in LowerEXTRACT_VECTOR");
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002630
2631 SDValue N;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002632 MVT SVT = VecVT.getSimpleVT();
2633 uint64_t W = CW->getZExtValue();
2634
2635 if (W == 32) {
2636 // Translate this node into EXTRACT_SUBREG.
2637 unsigned Subreg = (X == 0) ? Hexagon::subreg_loreg : 0;
2638
2639 if (X == 0)
2640 Subreg = Hexagon::subreg_loreg;
2641 else if (SVT == MVT::v2i32 && X == 1)
2642 Subreg = Hexagon::subreg_hireg;
2643 else if (SVT == MVT::v4i16 && X == 2)
2644 Subreg = Hexagon::subreg_hireg;
2645 else if (SVT == MVT::v8i8 && X == 4)
2646 Subreg = Hexagon::subreg_hireg;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002647 else
2648 llvm_unreachable("Bad offset");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002649 N = DAG.getTargetExtractSubreg(Subreg, dl, MVT::i32, Vec);
2650
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002651 } else if (VecVT.getSizeInBits() == 32) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002652 N = DAG.getNode(HexagonISD::EXTRACTU, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002653 } else {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002654 N = DAG.getNode(HexagonISD::EXTRACTU, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002655 if (VT.getSizeInBits() == 32)
2656 N = DAG.getTargetExtractSubreg(Hexagon::subreg_loreg, dl, MVT::i32, N);
2657 }
2658
2659 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2660 }
2661
2662 // Variable element number.
2663 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i32, Idx,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002664 DAG.getConstant(EltSize, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002665 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002666 DAG.getConstant(32, dl, MVT::i64));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002667 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2668
2669 const SDValue Ops[] = {Vec, Combined};
2670
2671 SDValue N;
2672 if (VecVT.getSizeInBits() == 32) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002673 N = DAG.getNode(HexagonISD::EXTRACTURP, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002674 } else {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002675 N = DAG.getNode(HexagonISD::EXTRACTURP, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002676 if (VT.getSizeInBits() == 32)
2677 N = DAG.getTargetExtractSubreg(Hexagon::subreg_loreg, dl, MVT::i32, N);
2678 }
2679 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2680}
2681
2682SDValue
2683HexagonTargetLowering::LowerINSERT_VECTOR(SDValue Op,
2684 SelectionDAG &DAG) const {
2685 EVT VT = Op.getValueType();
2686 int VTN = VT.isVector() ? VT.getVectorNumElements() : 1;
2687 SDLoc dl(Op);
2688 SDValue Vec = Op.getOperand(0);
2689 SDValue Val = Op.getOperand(1);
2690 SDValue Idx = Op.getOperand(2);
2691 EVT VecVT = Vec.getValueType();
2692 EVT EltVT = VecVT.getVectorElementType();
2693 int EltSize = EltVT.getSizeInBits();
2694 SDValue Width = DAG.getConstant(Op.getOpcode() == ISD::INSERT_VECTOR_ELT ?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002695 EltSize : VTN * EltSize, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002696
2697 if (ConstantSDNode *C = cast<ConstantSDNode>(Idx)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002698 SDValue Offset = DAG.getConstant(C->getSExtValue() * EltSize, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002699 const SDValue Ops[] = {Vec, Val, Width, Offset};
2700
2701 SDValue N;
2702 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002703 N = DAG.getNode(HexagonISD::INSERT, 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::INSERT, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002706
2707 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2708 }
2709
2710 // Variable element number.
2711 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i32, Idx,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002712 DAG.getConstant(EltSize, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002713 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002714 DAG.getConstant(32, dl, MVT::i64));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002715 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2716
2717 if (VT.getSizeInBits() == 64 &&
2718 Val.getValueType().getSizeInBits() == 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002719 SDValue C = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002720 Val = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, Val);
2721 }
2722
2723 const SDValue Ops[] = {Vec, Val, Combined};
2724
2725 SDValue N;
2726 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002727 N = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002728 else
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002729 N = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002730
2731 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2732}
2733
Tim Northovera4415852013-08-06 09:12:35 +00002734bool
2735HexagonTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
2736 // Assuming the caller does not have either a signext or zeroext modifier, and
2737 // only one value is accepted, any reasonable truncation is allowed.
2738 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
2739 return false;
2740
2741 // FIXME: in principle up to 64-bit could be made safe, but it would be very
2742 // fragile at the moment: any support for multiple value returns would be
2743 // liable to disallow tail calls involving i64 -> iN truncation in many cases.
2744 return Ty1->getPrimitiveSizeInBits() <= 32;
2745}
2746
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002747SDValue
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002748HexagonTargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
2749 SDValue Chain = Op.getOperand(0);
2750 SDValue Offset = Op.getOperand(1);
2751 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002752 SDLoc dl(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002753 auto PtrVT = getPointerTy(DAG.getDataLayout());
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002754
2755 // Mark function as containing a call to EH_RETURN.
2756 HexagonMachineFunctionInfo *FuncInfo =
2757 DAG.getMachineFunction().getInfo<HexagonMachineFunctionInfo>();
2758 FuncInfo->setHasEHReturn();
2759
2760 unsigned OffsetReg = Hexagon::R28;
2761
Mehdi Amini44ede332015-07-09 02:09:04 +00002762 SDValue StoreAddr =
2763 DAG.getNode(ISD::ADD, dl, PtrVT, DAG.getRegister(Hexagon::R30, PtrVT),
2764 DAG.getIntPtrConstant(4, dl));
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002765 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
2766 false, false, 0);
2767 Chain = DAG.getCopyToReg(Chain, dl, OffsetReg, Offset);
2768
2769 // Not needed we already use it as explict input to EH_RETURN.
2770 // MF.getRegInfo().addLiveOut(OffsetReg);
2771
2772 return DAG.getNode(HexagonISD::EH_RETURN, dl, MVT::Other, Chain);
2773}
2774
2775SDValue
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002776HexagonTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002777 unsigned Opc = Op.getOpcode();
2778 switch (Opc) {
2779 default:
2780#ifndef NDEBUG
2781 Op.getNode()->dumpr(&DAG);
2782 if (Opc > HexagonISD::OP_BEGIN && Opc < HexagonISD::OP_END)
2783 errs() << "Check for a non-legal type in this operation\n";
2784#endif
2785 llvm_unreachable("Should not custom lower this!");
2786 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
2787 case ISD::INSERT_SUBVECTOR: return LowerINSERT_VECTOR(Op, DAG);
2788 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR(Op, DAG);
2789 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_VECTOR(Op, DAG);
2790 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR(Op, DAG);
2791 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2792 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002793 case ISD::SRA:
2794 case ISD::SHL:
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002795 case ISD::SRL: return LowerVECTOR_SHIFT(Op, DAG);
2796 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002797 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002798 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
2799 // Frame & Return address. Currently unimplemented.
2800 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
2801 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00002802 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002803 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
2804 case ISD::GlobalAddress: return LowerGLOBALADDRESS(Op, DAG);
2805 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002806 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002807 case ISD::VASTART: return LowerVASTART(Op, DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002808 // Custom lower some vector loads.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002809 case ISD::LOAD: return LowerLOAD(Op, DAG);
2810 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
2811 case ISD::SETCC: return LowerSETCC(Op, DAG);
2812 case ISD::VSELECT: return LowerVSELECT(Op, DAG);
2813 case ISD::CTPOP: return LowerCTPOP(Op, DAG);
2814 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00002815 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002816 case ISD::INLINEASM: return LowerINLINEASM(Op, DAG);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00002817 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002818 }
2819}
2820
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002821/// Returns relocation base for the given PIC jumptable.
2822SDValue
2823HexagonTargetLowering::getPICJumpTableRelocBase(SDValue Table,
2824 SelectionDAG &DAG) const {
2825 int Idx = cast<JumpTableSDNode>(Table)->getIndex();
2826 EVT VT = Table.getValueType();
2827 SDValue T = DAG.getTargetJumpTable(Idx, VT, HexagonII::MO_PCREL);
2828 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Table), VT, T);
2829}
2830
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002831MachineBasicBlock *
2832HexagonTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
2833 MachineBasicBlock *BB)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00002834 const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002835 switch (MI->getOpcode()) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00002836 case Hexagon::ALLOCA: {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002837 MachineFunction *MF = BB->getParent();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00002838 auto *FuncInfo = MF->getInfo<HexagonMachineFunctionInfo>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002839 FuncInfo->addAllocaAdjustInst(MI);
2840 return BB;
2841 }
Craig Toppere55c5562012-02-07 02:50:20 +00002842 default: llvm_unreachable("Unexpected instr type to insert");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002843 } // switch
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002844}
2845
2846//===----------------------------------------------------------------------===//
2847// Inline Assembly Support
2848//===----------------------------------------------------------------------===//
2849
Eric Christopher11e4df72015-02-26 22:38:43 +00002850std::pair<unsigned, const TargetRegisterClass *>
2851HexagonTargetLowering::getRegForInlineAsmConstraint(
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00002852 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002853 bool UseHVX = Subtarget.useHVXOps(), UseHVXDbl = Subtarget.useHVXDblOps();
2854
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002855 if (Constraint.size() == 1) {
2856 switch (Constraint[0]) {
2857 case 'r': // R0-R31
Chad Rosier295bd432013-06-22 18:37:38 +00002858 switch (VT.SimpleTy) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002859 default:
Craig Toppere55c5562012-02-07 02:50:20 +00002860 llvm_unreachable("getRegForInlineAsmConstraint Unhandled data type");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002861 case MVT::i32:
2862 case MVT::i16:
2863 case MVT::i8:
Sirish Pande69295b82012-05-10 20:20:25 +00002864 case MVT::f32:
Craig Topperc7242e02012-04-20 07:30:17 +00002865 return std::make_pair(0U, &Hexagon::IntRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002866 case MVT::i64:
Sirish Pande69295b82012-05-10 20:20:25 +00002867 case MVT::f64:
Craig Topperc7242e02012-04-20 07:30:17 +00002868 return std::make_pair(0U, &Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002869 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002870 case 'q': // q0-q3
2871 switch (VT.SimpleTy) {
2872 default:
2873 llvm_unreachable("getRegForInlineAsmConstraint Unhandled data type");
2874 case MVT::v1024i1:
2875 case MVT::v512i1:
2876 case MVT::v32i16:
2877 case MVT::v16i32:
2878 case MVT::v64i8:
2879 case MVT::v8i64:
2880 return std::make_pair(0U, &Hexagon::VecPredRegsRegClass);
2881 }
2882 case 'v': // V0-V31
2883 switch (VT.SimpleTy) {
2884 default:
2885 llvm_unreachable("getRegForInlineAsmConstraint Unhandled data type");
2886 case MVT::v16i32:
2887 case MVT::v32i16:
2888 case MVT::v64i8:
2889 case MVT::v8i64:
2890 return std::make_pair(0U, &Hexagon::VectorRegsRegClass);
2891 case MVT::v32i32:
2892 case MVT::v64i16:
2893 case MVT::v16i64:
2894 case MVT::v128i8:
2895 if (Subtarget.hasV60TOps() && UseHVX && UseHVXDbl)
2896 return std::make_pair(0U, &Hexagon::VectorRegs128BRegClass);
2897 else
2898 return std::make_pair(0U, &Hexagon::VecDblRegsRegClass);
2899 case MVT::v256i8:
2900 case MVT::v128i16:
2901 case MVT::v64i32:
2902 case MVT::v32i64:
2903 return std::make_pair(0U, &Hexagon::VecDblRegs128BRegClass);
2904 }
2905
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002906 default:
Craig Toppere55c5562012-02-07 02:50:20 +00002907 llvm_unreachable("Unknown asm register class");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002908 }
2909 }
2910
Eric Christopher11e4df72015-02-26 22:38:43 +00002911 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002912}
2913
Sirish Pande69295b82012-05-10 20:20:25 +00002914/// isFPImmLegal - Returns true if the target can instruction select the
2915/// specified FP immediate natively. If false, the legalizer will
2916/// materialize the FP immediate as a load from a constant pool.
2917bool HexagonTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002918 return Subtarget.hasV5TOps();
Sirish Pande69295b82012-05-10 20:20:25 +00002919}
2920
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002921/// isLegalAddressingMode - Return true if the addressing mode represented by
2922/// AM is legal for this target, for a load/store of the specified type.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +00002923bool HexagonTargetLowering::isLegalAddressingMode(const DataLayout &DL,
2924 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +00002925 unsigned AS) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002926 // Allows a signed-extended 11-bit immediate field.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002927 if (AM.BaseOffs <= -(1LL << 13) || AM.BaseOffs >= (1LL << 13)-1)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002928 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002929
2930 // No global is ever allowed as a base.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002931 if (AM.BaseGV)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002932 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002933
2934 int Scale = AM.Scale;
2935 if (Scale < 0) Scale = -Scale;
2936 switch (Scale) {
2937 case 0: // No scale reg, "r+i", "r", or just "i".
2938 break;
2939 default: // No scaled addressing mode.
2940 return false;
2941 }
2942 return true;
2943}
2944
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002945/// Return true if folding a constant offset with the given GlobalAddress is
2946/// legal. It is frequently not legal in PIC relocation models.
2947bool HexagonTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA)
2948 const {
2949 return HTM.getRelocationModel() == Reloc::Static;
2950}
2951
2952
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002953/// isLegalICmpImmediate - Return true if the specified immediate is legal
2954/// icmp immediate, that is the target has icmp instructions which can compare
2955/// a register against the immediate without having to materialize the
2956/// immediate into a register.
2957bool HexagonTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
2958 return Imm >= -512 && Imm <= 511;
2959}
2960
2961/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2962/// for tail call optimization. Targets which want to do tail call
2963/// optimization should implement this function.
2964bool HexagonTargetLowering::IsEligibleForTailCallOptimization(
2965 SDValue Callee,
2966 CallingConv::ID CalleeCC,
2967 bool isVarArg,
2968 bool isCalleeStructRet,
2969 bool isCallerStructRet,
2970 const SmallVectorImpl<ISD::OutputArg> &Outs,
2971 const SmallVectorImpl<SDValue> &OutVals,
2972 const SmallVectorImpl<ISD::InputArg> &Ins,
2973 SelectionDAG& DAG) const {
2974 const Function *CallerF = DAG.getMachineFunction().getFunction();
2975 CallingConv::ID CallerCC = CallerF->getCallingConv();
2976 bool CCMatch = CallerCC == CalleeCC;
2977
2978 // ***************************************************************************
2979 // Look for obvious safe cases to perform tail call optimization that do not
2980 // require ABI changes.
2981 // ***************************************************************************
2982
2983 // If this is a tail call via a function pointer, then don't do it!
Craig Topper66059c92015-11-18 07:07:59 +00002984 if (!(isa<GlobalAddressSDNode>(Callee)) &&
2985 !(isa<ExternalSymbolSDNode>(Callee))) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002986 return false;
2987 }
2988
2989 // Do not optimize if the calling conventions do not match.
2990 if (!CCMatch)
2991 return false;
2992
2993 // Do not tail call optimize vararg calls.
2994 if (isVarArg)
2995 return false;
2996
2997 // Also avoid tail call optimization if either caller or callee uses struct
2998 // return semantics.
2999 if (isCalleeStructRet || isCallerStructRet)
3000 return false;
3001
3002 // In addition to the cases above, we also disable Tail Call Optimization if
3003 // the calling convention code that at least one outgoing argument needs to
3004 // go on the stack. We cannot check that here because at this point that
3005 // information is not available.
3006 return true;
3007}
Colin LeMahieu025f8602014-12-08 21:19:18 +00003008
3009// Return true when the given node fits in a positive half word.
3010bool llvm::isPositiveHalfWord(SDNode *N) {
3011 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N);
3012 if (CN && CN->getSExtValue() > 0 && isInt<16>(CN->getSExtValue()))
3013 return true;
3014
3015 switch (N->getOpcode()) {
3016 default:
3017 return false;
3018 case ISD::SIGN_EXTEND_INREG:
3019 return true;
3020 }
3021}
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003022
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003023std::pair<const TargetRegisterClass*, uint8_t>
3024HexagonTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
3025 MVT VT) const {
3026 const TargetRegisterClass *RRC = nullptr;
3027
3028 uint8_t Cost = 1;
3029 switch (VT.SimpleTy) {
3030 default:
3031 return TargetLowering::findRepresentativeClass(TRI, VT);
3032 case MVT::v64i8:
3033 case MVT::v32i16:
3034 case MVT::v16i32:
3035 case MVT::v8i64:
3036 RRC = &Hexagon::VectorRegsRegClass;
3037 break;
3038 case MVT::v128i8:
3039 case MVT::v64i16:
3040 case MVT::v32i32:
3041 case MVT::v16i64:
3042 if (Subtarget.hasV60TOps() && Subtarget.useHVXOps() &&
3043 Subtarget.useHVXDblOps())
3044 RRC = &Hexagon::VectorRegs128BRegClass;
3045 else
3046 RRC = &Hexagon::VecDblRegsRegClass;
3047 break;
3048 case MVT::v256i8:
3049 case MVT::v128i16:
3050 case MVT::v64i32:
3051 case MVT::v32i64:
3052 RRC = &Hexagon::VecDblRegs128BRegClass;
3053 break;
3054 }
3055 return std::make_pair(RRC, Cost);
3056}
3057
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003058Value *HexagonTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
3059 AtomicOrdering Ord) const {
3060 BasicBlock *BB = Builder.GetInsertBlock();
3061 Module *M = BB->getParent()->getParent();
3062 Type *Ty = cast<PointerType>(Addr->getType())->getElementType();
3063 unsigned SZ = Ty->getPrimitiveSizeInBits();
3064 assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic loads supported");
3065 Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_L2_loadw_locked
3066 : Intrinsic::hexagon_L4_loadd_locked;
3067 Value *Fn = Intrinsic::getDeclaration(M, IntID);
3068 return Builder.CreateCall(Fn, Addr, "larx");
3069}
3070
3071/// Perform a store-conditional operation to Addr. Return the status of the
3072/// store. This should be 0 if the store succeeded, non-zero otherwise.
3073Value *HexagonTargetLowering::emitStoreConditional(IRBuilder<> &Builder,
3074 Value *Val, Value *Addr, AtomicOrdering Ord) const {
3075 BasicBlock *BB = Builder.GetInsertBlock();
3076 Module *M = BB->getParent()->getParent();
3077 Type *Ty = Val->getType();
3078 unsigned SZ = Ty->getPrimitiveSizeInBits();
3079 assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic stores supported");
3080 Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_S2_storew_locked
3081 : Intrinsic::hexagon_S4_stored_locked;
3082 Value *Fn = Intrinsic::getDeclaration(M, IntID);
3083 Value *Call = Builder.CreateCall(Fn, {Addr, Val}, "stcx");
3084 Value *Cmp = Builder.CreateICmpEQ(Call, Builder.getInt32(0), "");
3085 Value *Ext = Builder.CreateZExt(Cmp, Type::getInt32Ty(M->getContext()));
3086 return Ext;
3087}
3088
Ahmed Bougacha52468672015-09-11 17:08:28 +00003089TargetLowering::AtomicExpansionKind
3090HexagonTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003091 // Do not expand loads and stores that don't exceed 64 bits.
Ahmed Bougacha52468672015-09-11 17:08:28 +00003092 return LI->getType()->getPrimitiveSizeInBits() > 64
Tim Northoverf520eff2015-12-02 18:12:57 +00003093 ? AtomicExpansionKind::LLOnly
Ahmed Bougacha52468672015-09-11 17:08:28 +00003094 : AtomicExpansionKind::None;
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003095}
3096
3097bool HexagonTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
3098 // Do not expand loads and stores that don't exceed 64 bits.
3099 return SI->getValueOperand()->getType()->getPrimitiveSizeInBits() > 64;
3100}