blob: 4c454f427101f9ab685c911d98090d7bc6723b1f [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
392 if (LocVT == MVT::i1 ||
393 LocVT == MVT::i8 ||
394 LocVT == MVT::i16) {
395 LocVT = MVT::i32;
396 ValVT = MVT::i32;
397 if (ArgFlags.isSExt())
398 LocInfo = CCValAssign::SExt;
399 else if (ArgFlags.isZExt())
400 LocInfo = CCValAssign::ZExt;
401 else
402 LocInfo = CCValAssign::AExt;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000403 } else if (LocVT == MVT::v4i8 || LocVT == MVT::v2i16) {
404 LocVT = MVT::i32;
405 LocInfo = CCValAssign::BCvt;
406 } else if (LocVT == MVT::v8i8 || LocVT == MVT::v4i16 || LocVT == MVT::v2i32) {
407 LocVT = MVT::i64;
408 LocInfo = CCValAssign::BCvt;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000409 } else if (LocVT == MVT::v64i8 || LocVT == MVT::v32i16 ||
410 LocVT == MVT::v16i32 || LocVT == MVT::v8i64 ||
411 LocVT == MVT::v512i1) {
412 LocVT = MVT::v16i32;
413 ValVT = MVT::v16i32;
414 LocInfo = CCValAssign::Full;
415 } else if (LocVT == MVT::v128i8 || LocVT == MVT::v64i16 ||
416 LocVT == MVT::v32i32 || LocVT == MVT::v16i64 ||
417 (LocVT == MVT::v1024i1 && UseHVX && UseHVXDbl)) {
418 LocVT = MVT::v32i32;
419 ValVT = MVT::v32i32;
420 LocInfo = CCValAssign::Full;
421 } else if (LocVT == MVT::v256i8 || LocVT == MVT::v128i16 ||
422 LocVT == MVT::v64i32 || LocVT == MVT::v32i64) {
423 LocVT = MVT::v64i32;
424 ValVT = MVT::v64i32;
425 LocInfo = CCValAssign::Full;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000426 }
Sirish Pande69295b82012-05-10 20:20:25 +0000427 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000428 if (!RetCC_Hexagon32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
429 return false;
430 }
431
Sirish Pande69295b82012-05-10 20:20:25 +0000432 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000433 if (!RetCC_Hexagon64(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
434 return false;
435 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000436 if (LocVT == MVT::v16i32 || LocVT == MVT::v32i32 || LocVT == MVT::v64i32) {
437 if (!RetCC_HexagonVector(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
438 return false;
439 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000440 return true; // CC didn't match.
441}
442
443static bool RetCC_Hexagon32(unsigned ValNo, MVT ValVT,
444 MVT LocVT, CCValAssign::LocInfo LocInfo,
445 ISD::ArgFlagsTy ArgFlags, CCState &State) {
446
Sirish Pande69295b82012-05-10 20:20:25 +0000447 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000448 if (unsigned Reg = State.AllocateReg(Hexagon::R0)) {
449 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
450 return false;
451 }
452 }
453
454 unsigned Offset = State.AllocateStack(4, 4);
455 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
456 return false;
457}
458
459static bool RetCC_Hexagon64(unsigned ValNo, MVT ValVT,
460 MVT LocVT, CCValAssign::LocInfo LocInfo,
461 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Sirish Pande69295b82012-05-10 20:20:25 +0000462 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000463 if (unsigned Reg = State.AllocateReg(Hexagon::D0)) {
464 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
465 return false;
466 }
467 }
468
469 unsigned Offset = State.AllocateStack(8, 8);
470 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
471 return false;
472}
473
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000474static bool RetCC_HexagonVector(unsigned ValNo, MVT ValVT,
475 MVT LocVT, CCValAssign::LocInfo LocInfo,
476 ISD::ArgFlagsTy ArgFlags, CCState &State) {
477 auto &MF = State.getMachineFunction();
478 auto &HST = MF.getSubtarget<HexagonSubtarget>();
479 bool UseHVX = HST.useHVXOps();
480 bool UseHVXDbl = HST.useHVXDblOps();
481
482 unsigned OffSiz = 64;
483 if (LocVT == MVT::v16i32) {
484 if (unsigned Reg = State.AllocateReg(Hexagon::V0)) {
485 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
486 return false;
487 }
488 } else if (LocVT == MVT::v32i32) {
489 unsigned Req = (UseHVX && UseHVXDbl) ? Hexagon::V0 : Hexagon::W0;
490 if (unsigned Reg = State.AllocateReg(Req)) {
491 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
492 return false;
493 }
494 OffSiz = 128;
495 } else if (LocVT == MVT::v64i32) {
496 if (unsigned Reg = State.AllocateReg(Hexagon::W0)) {
497 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
498 return false;
499 }
500 OffSiz = 256;
501 }
502
503 unsigned Offset = State.AllocateStack(OffSiz, OffSiz);
504 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
505 return false;
506}
507
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000508void HexagonTargetLowering::promoteLdStType(EVT VT, EVT PromotedLdStVT) {
509 if (VT != PromotedLdStVT) {
510 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
511 AddPromotedToType(ISD::LOAD, VT.getSimpleVT(),
512 PromotedLdStVT.getSimpleVT());
513
514 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
515 AddPromotedToType(ISD::STORE, VT.getSimpleVT(),
516 PromotedLdStVT.getSimpleVT());
517 }
518}
519
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000520SDValue
521HexagonTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG)
522const {
523 return SDValue();
524}
525
526/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
527/// by "Src" to address "Dst" of size "Size". Alignment information is
528/// specified by the specific parameter attribute. The copy will be passed as
529/// a byval function parameter. Sometimes what we are copying is the end of a
530/// larger object, the part that does not fit in registers.
531static SDValue
532CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
533 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000534 SDLoc dl) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000535
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000536 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000537 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
538 /*isVolatile=*/false, /*AlwaysInline=*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +0000539 /*isTailCall=*/false,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000540 MachinePointerInfo(), MachinePointerInfo());
541}
542
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000543static bool IsHvxVectorType(MVT ty) {
544 return (ty == MVT::v8i64 || ty == MVT::v16i32 || ty == MVT::v32i16 ||
545 ty == MVT::v64i8 ||
546 ty == MVT::v16i64 || ty == MVT::v32i32 || ty == MVT::v64i16 ||
547 ty == MVT::v128i8 ||
548 ty == MVT::v32i64 || ty == MVT::v64i32 || ty == MVT::v128i16 ||
549 ty == MVT::v256i8 ||
550 ty == MVT::v512i1 || ty == MVT::v1024i1);
551}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000552
553// LowerReturn - Lower ISD::RET. If a struct is larger than 8 bytes and is
554// passed by value, the function prototype is modified to return void and
555// the value is stored in memory pointed by a pointer passed by caller.
556SDValue
557HexagonTargetLowering::LowerReturn(SDValue Chain,
558 CallingConv::ID CallConv, bool isVarArg,
559 const SmallVectorImpl<ISD::OutputArg> &Outs,
560 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000561 SDLoc dl, SelectionDAG &DAG) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000562
563 // CCValAssign - represent the assignment of the return value to locations.
564 SmallVector<CCValAssign, 16> RVLocs;
565
566 // CCState - Info about the registers and stack slot.
Eric Christopherb5217502014-08-06 18:45:26 +0000567 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
568 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000569
570 // Analyze return values of ISD::RET
571 CCInfo.AnalyzeReturn(Outs, RetCC_Hexagon);
572
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000573 SDValue Flag;
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000574 SmallVector<SDValue, 4> RetOps(1, Chain);
575
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000576 // Copy the result values into the output registers.
577 for (unsigned i = 0; i != RVLocs.size(); ++i) {
578 CCValAssign &VA = RVLocs[i];
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000579
580 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), OutVals[i], Flag);
581
582 // Guarantee that all emitted copies are stuck together with flags.
583 Flag = Chain.getValue(1);
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000584 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000585 }
586
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000587 RetOps[0] = Chain; // Update chain.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000588
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000589 // Add the flag if we have it.
590 if (Flag.getNode())
591 RetOps.push_back(Flag);
592
Craig Topper48d114b2014-04-26 18:35:24 +0000593 return DAG.getNode(HexagonISD::RET_FLAG, dl, MVT::Other, RetOps);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000594}
595
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000596bool HexagonTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
597 // If either no tail call or told not to tail call at all, don't.
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000598 auto Attr =
599 CI->getParent()->getParent()->getFnAttribute("disable-tail-calls");
600 if (!CI->isTailCall() || Attr.getValueAsString() == "true")
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000601 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000602
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000603 return true;
604}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000605
606/// LowerCallResult - Lower the result values of an ISD::CALL into the
607/// appropriate copies out of appropriate physical registers. This assumes that
608/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
609/// being lowered. Returns a SDNode with the same number of values as the
610/// ISD::CALL.
611SDValue
612HexagonTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
613 CallingConv::ID CallConv, bool isVarArg,
614 const
615 SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000616 SDLoc dl, SelectionDAG &DAG,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000617 SmallVectorImpl<SDValue> &InVals,
618 const SmallVectorImpl<SDValue> &OutVals,
619 SDValue Callee) const {
620
621 // Assign locations to each value returned by this call.
622 SmallVector<CCValAssign, 16> RVLocs;
623
Eric Christopherb5217502014-08-06 18:45:26 +0000624 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
625 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000626
627 CCInfo.AnalyzeCallResult(Ins, RetCC_Hexagon);
628
629 // Copy all of the result registers out of their specified physreg.
630 for (unsigned i = 0; i != RVLocs.size(); ++i) {
631 Chain = DAG.getCopyFromReg(Chain, dl,
632 RVLocs[i].getLocReg(),
633 RVLocs[i].getValVT(), InFlag).getValue(1);
634 InFlag = Chain.getValue(2);
635 InVals.push_back(Chain.getValue(0));
636 }
637
638 return Chain;
639}
640
641/// LowerCall - Functions arguments are copied from virtual regs to
642/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
643SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +0000644HexagonTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000645 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +0000646 SelectionDAG &DAG = CLI.DAG;
Craig Topperb94011f2013-07-14 04:42:23 +0000647 SDLoc &dl = CLI.DL;
648 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
649 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
650 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000651 SDValue Chain = CLI.Chain;
652 SDValue Callee = CLI.Callee;
653 bool &isTailCall = CLI.IsTailCall;
654 CallingConv::ID CallConv = CLI.CallConv;
655 bool isVarArg = CLI.IsVarArg;
Colin LeMahieu2e3a26d2015-01-16 17:05:27 +0000656 bool doesNotReturn = CLI.DoesNotReturn;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000657
658 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000659 MachineFunction &MF = DAG.getMachineFunction();
Mehdi Amini44ede332015-07-09 02:09:04 +0000660 auto PtrVT = getPointerTy(MF.getDataLayout());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000661
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000662 // Check for varargs.
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000663 int NumNamedVarArgParams = -1;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000664 if (GlobalAddressSDNode *GAN = dyn_cast<GlobalAddressSDNode>(Callee)) {
665 const GlobalValue *GV = GAN->getGlobal();
666 Callee = DAG.getTargetGlobalAddress(GV, dl, MVT::i32);
667 if (const Function* F = dyn_cast<Function>(GV)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000668 // If a function has zero args and is a vararg function, that's
669 // disallowed so it must be an undeclared function. Do not assume
670 // varargs if the callee is undefined.
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000671 if (F->isVarArg() && F->getFunctionType()->getNumParams() != 0)
672 NumNamedVarArgParams = F->getFunctionType()->getNumParams();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000673 }
674 }
675
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000676 // Analyze operands of the call, assigning locations to each operand.
677 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000678 HexagonCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
679 *DAG.getContext(), NumNamedVarArgParams);
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000680
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000681 if (isVarArg)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000682 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon_VarArg);
683 else
684 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon);
685
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000686 auto Attr = MF.getFunction()->getFnAttribute("disable-tail-calls");
687 if (Attr.getValueAsString() == "true")
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000688 isTailCall = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000689
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000690 if (isTailCall) {
691 bool StructAttrFlag = MF.getFunction()->hasStructRetAttr();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000692 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
693 isVarArg, IsStructRet,
694 StructAttrFlag,
695 Outs, OutVals, Ins, DAG);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000696 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000697 CCValAssign &VA = ArgLocs[i];
698 if (VA.isMemLoc()) {
699 isTailCall = false;
700 break;
701 }
702 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000703 DEBUG(dbgs() << (isTailCall ? "Eligible for Tail Call\n"
704 : "Argument must be passed on stack. "
705 "Not eligible for Tail Call\n"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000706 }
707 // Get a count of how many bytes are to be pushed on the stack.
708 unsigned NumBytes = CCInfo.getNextStackOffset();
709 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
710 SmallVector<SDValue, 8> MemOpChains;
711
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000712 auto &HRI = *Subtarget.getRegisterInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +0000713 SDValue StackPtr =
714 DAG.getCopyFromReg(Chain, dl, HRI.getStackRegister(), PtrVT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000715
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000716 bool NeedsArgAlign = false;
717 unsigned LargestAlignSeen = 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000718 // Walk the register/memloc assignments, inserting copies/loads.
719 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
720 CCValAssign &VA = ArgLocs[i];
721 SDValue Arg = OutVals[i];
722 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000723 // Record if we need > 8 byte alignment on an argument.
724 bool ArgAlign = IsHvxVectorType(VA.getValVT());
725 NeedsArgAlign |= ArgAlign;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000726
727 // Promote the value if needed.
728 switch (VA.getLocInfo()) {
729 default:
730 // Loc info must be one of Full, SExt, ZExt, or AExt.
Craig Toppere55c5562012-02-07 02:50:20 +0000731 llvm_unreachable("Unknown loc info!");
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000732 case CCValAssign::BCvt:
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000733 case CCValAssign::Full:
734 break;
735 case CCValAssign::SExt:
736 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
737 break;
738 case CCValAssign::ZExt:
739 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
740 break;
741 case CCValAssign::AExt:
742 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
743 break;
744 }
745
746 if (VA.isMemLoc()) {
747 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000748 SDValue MemAddr = DAG.getConstant(LocMemOffset, dl,
749 StackPtr.getValueType());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000750 MemAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, MemAddr);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000751 if (ArgAlign)
752 LargestAlignSeen = std::max(LargestAlignSeen,
753 VA.getLocVT().getStoreSizeInBits() >> 3);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000754 if (Flags.isByVal()) {
755 // The argument is a struct passed by value. According to LLVM, "Arg"
756 // is is pointer.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000757 MemOpChains.push_back(CreateCopyOfByValArgument(Arg, MemAddr, Chain,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000758 Flags, DAG, dl));
759 } else {
Alex Lorenze40c8a22015-08-11 23:09:45 +0000760 MachinePointerInfo LocPI = MachinePointerInfo::getStack(
761 DAG.getMachineFunction(), LocMemOffset);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000762 SDValue S = DAG.getStore(Chain, dl, Arg, MemAddr, LocPI, false,
763 false, 0);
764 MemOpChains.push_back(S);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000765 }
766 continue;
767 }
768
769 // Arguments that can be passed on register must be kept at RegsToPass
770 // vector.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000771 if (VA.isRegLoc())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000772 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000773 }
774
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000775 if (NeedsArgAlign && Subtarget.hasV60TOps()) {
776 DEBUG(dbgs() << "Function needs byte stack align due to call args\n");
777 MachineFrameInfo* MFI = DAG.getMachineFunction().getFrameInfo();
778 // V6 vectors passed by value have 64 or 128 byte alignment depending
779 // on whether we are 64 byte vector mode or 128 byte.
780 bool UseHVXDbl = Subtarget.useHVXDblOps();
781 assert(Subtarget.useHVXOps());
782 const unsigned ObjAlign = UseHVXDbl ? 128 : 64;
783 LargestAlignSeen = std::max(LargestAlignSeen, ObjAlign);
784 MFI->ensureMaxAlignment(LargestAlignSeen);
785 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000786 // Transform all store nodes into one single node because all store
787 // nodes are independent of each other.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000788 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000789 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000790
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000791 if (!isTailCall) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000792 SDValue C = DAG.getConstant(NumBytes, dl, PtrVT, true);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000793 Chain = DAG.getCALLSEQ_START(Chain, C, dl);
794 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000795
796 // Build a sequence of copy-to-reg nodes chained together with token
797 // chain and flag operands which copy the outgoing args into registers.
Benjamin Kramerbde91762012-06-02 10:20:22 +0000798 // The InFlag in necessary since all emitted instructions must be
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000799 // stuck together.
800 SDValue InFlag;
801 if (!isTailCall) {
802 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
803 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
804 RegsToPass[i].second, InFlag);
805 InFlag = Chain.getValue(1);
806 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000807 } else {
808 // For tail calls lower the arguments to the 'real' stack slot.
809 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000810 // Force all the incoming stack arguments to be loaded from the stack
811 // before any new outgoing arguments are stored to the stack, because the
812 // outgoing stack slots may alias the incoming argument stack slots, and
813 // the alias isn't otherwise explicit. This is slightly more conservative
814 // than necessary, because it means that each store effectively depends
815 // on every argument instead of just those arguments it would clobber.
816 //
Benjamin Kramerbde91762012-06-02 10:20:22 +0000817 // Do not flag preceding copytoreg stuff together with the following stuff.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000818 InFlag = SDValue();
819 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
820 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
821 RegsToPass[i].second, InFlag);
822 InFlag = Chain.getValue(1);
823 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000824 InFlag = SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000825 }
826
827 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
828 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
829 // node so that legalize doesn't hack it.
Tobias Edler von Kochb51460c2015-12-16 17:29:37 +0000830 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000831 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, PtrVT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000832 } else if (ExternalSymbolSDNode *S =
833 dyn_cast<ExternalSymbolSDNode>(Callee)) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000834 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), PtrVT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000835 }
836
837 // Returns a chain & a flag for retval copy to use.
838 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
839 SmallVector<SDValue, 8> Ops;
840 Ops.push_back(Chain);
841 Ops.push_back(Callee);
842
843 // Add argument registers to the end of the list so that they are
844 // known live into the call.
845 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
846 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
847 RegsToPass[i].second.getValueType()));
848 }
849
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000850 if (InFlag.getNode())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000851 Ops.push_back(InFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000852
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +0000853 if (isTailCall) {
854 MF.getFrameInfo()->setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +0000855 return DAG.getNode(HexagonISD::TC_RETURN, dl, NodeTys, Ops);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +0000856 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000857
Colin LeMahieu2e3a26d2015-01-16 17:05:27 +0000858 int OpCode = doesNotReturn ? HexagonISD::CALLv3nr : HexagonISD::CALLv3;
859 Chain = DAG.getNode(OpCode, dl, NodeTys, Ops);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000860 InFlag = Chain.getValue(1);
861
862 // Create the CALLSEQ_END node.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000863 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
864 DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000865 InFlag = Chain.getValue(1);
866
867 // Handle result values, copying them out of physregs into vregs that we
868 // return.
869 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
870 InVals, OutVals, Callee);
871}
872
873static bool getIndexedAddressParts(SDNode *Ptr, EVT VT,
874 bool isSEXTLoad, SDValue &Base,
875 SDValue &Offset, bool &isInc,
876 SelectionDAG &DAG) {
877 if (Ptr->getOpcode() != ISD::ADD)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000878 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000879
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000880 auto &HST = static_cast<const HexagonSubtarget&>(DAG.getSubtarget());
881 bool UseHVX = HST.useHVXOps();
882 bool UseHVXDbl = HST.useHVXDblOps();
883
884 bool ValidHVXDblType =
885 (UseHVX && UseHVXDbl) && (VT == MVT::v32i32 || VT == MVT::v16i64 ||
886 VT == MVT::v64i16 || VT == MVT::v128i8);
887 bool ValidHVXType =
888 UseHVX && !UseHVXDbl && (VT == MVT::v16i32 || VT == MVT::v8i64 ||
889 VT == MVT::v32i16 || VT == MVT::v64i8);
890
891 if (ValidHVXDblType || ValidHVXType ||
892 VT == MVT::i64 || VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000893 isInc = (Ptr->getOpcode() == ISD::ADD);
894 Base = Ptr->getOperand(0);
895 Offset = Ptr->getOperand(1);
896 // Ensure that Offset is a constant.
897 return (isa<ConstantSDNode>(Offset));
898 }
899
900 return false;
901}
902
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000903/// getPostIndexedAddressParts - returns true by value, base pointer and
904/// offset pointer and addressing mode by reference if this node can be
905/// combined with a load / store to form a post-indexed load / store.
906bool HexagonTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
907 SDValue &Base,
908 SDValue &Offset,
909 ISD::MemIndexedMode &AM,
910 SelectionDAG &DAG) const
911{
912 EVT VT;
913 SDValue Ptr;
914 bool isSEXTLoad = false;
915
916 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
917 VT = LD->getMemoryVT();
918 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
919 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
920 VT = ST->getMemoryVT();
921 if (ST->getValue().getValueType() == MVT::i64 && ST->isTruncatingStore()) {
922 return false;
923 }
924 } else {
925 return false;
926 }
927
Chad Rosier64dc8aa2012-01-06 20:11:59 +0000928 bool isInc = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000929 bool isLegal = getIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
930 isInc, DAG);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000931 if (isLegal) {
932 auto &HII = *Subtarget.getInstrInfo();
933 int32_t OffsetVal = cast<ConstantSDNode>(Offset.getNode())->getSExtValue();
934 if (HII.isValidAutoIncImm(VT, OffsetVal)) {
935 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
936 return true;
937 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000938 }
939
940 return false;
941}
942
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000943SDValue
944HexagonTargetLowering::LowerINLINEASM(SDValue Op, SelectionDAG &DAG) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000945 SDNode *Node = Op.getNode();
946 MachineFunction &MF = DAG.getMachineFunction();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000947 auto &FuncInfo = *MF.getInfo<HexagonMachineFunctionInfo>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000948 switch (Node->getOpcode()) {
949 case ISD::INLINEASM: {
950 unsigned NumOps = Node->getNumOperands();
951 if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue)
952 --NumOps; // Ignore the flag operand.
953
954 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000955 if (FuncInfo.hasClobberLR())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000956 break;
957 unsigned Flags =
958 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
959 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
960 ++i; // Skip the ID value.
961
962 switch (InlineAsm::getKind(Flags)) {
963 default: llvm_unreachable("Bad flags!");
964 case InlineAsm::Kind_RegDef:
965 case InlineAsm::Kind_RegUse:
966 case InlineAsm::Kind_Imm:
967 case InlineAsm::Kind_Clobber:
968 case InlineAsm::Kind_Mem: {
969 for (; NumVals; --NumVals, ++i) {}
970 break;
971 }
972 case InlineAsm::Kind_RegDefEarlyClobber: {
973 for (; NumVals; --NumVals, ++i) {
974 unsigned Reg =
975 cast<RegisterSDNode>(Node->getOperand(i))->getReg();
976
977 // Check it to be lr
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000978 const HexagonRegisterInfo *QRI = Subtarget.getRegisterInfo();
Eric Christopherdbe1cb02014-06-27 00:13:52 +0000979 if (Reg == QRI->getRARegister()) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000980 FuncInfo.setHasClobberLR(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000981 break;
982 }
983 }
984 break;
985 }
986 }
987 }
988 }
989 } // Node->getOpcode
990 return Op;
991}
992
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +0000993// Need to transform ISD::PREFETCH into something that doesn't inherit
994// all of the properties of ISD::PREFETCH, specifically SDNPMayLoad and
995// SDNPMayStore.
996SDValue HexagonTargetLowering::LowerPREFETCH(SDValue Op,
997 SelectionDAG &DAG) const {
998 SDValue Chain = Op.getOperand(0);
999 SDValue Addr = Op.getOperand(1);
1000 // Lower it to DCFETCH($reg, #0). A "pat" will try to merge the offset in,
1001 // if the "reg" is fed by an "add".
1002 SDLoc DL(Op);
1003 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
1004 return DAG.getNode(HexagonISD::DCFETCH, DL, MVT::Other, Chain, Addr, Zero);
1005}
1006
1007SDValue HexagonTargetLowering::LowerINTRINSIC_VOID(SDValue Op,
1008 SelectionDAG &DAG) const {
1009 SDValue Chain = Op.getOperand(0);
1010 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1011 // Lower the hexagon_prefetch builtin to DCFETCH, as above.
1012 if (IntNo == Intrinsic::hexagon_prefetch) {
1013 SDValue Addr = Op.getOperand(2);
1014 SDLoc DL(Op);
1015 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
1016 return DAG.getNode(HexagonISD::DCFETCH, DL, MVT::Other, Chain, Addr, Zero);
1017 }
1018 return SDValue();
1019}
1020
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001021SDValue
1022HexagonTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
1023 SelectionDAG &DAG) const {
1024 SDValue Chain = Op.getOperand(0);
1025 SDValue Size = Op.getOperand(1);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001026 SDValue Align = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001027 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001028
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001029 ConstantSDNode *AlignConst = dyn_cast<ConstantSDNode>(Align);
1030 assert(AlignConst && "Non-constant Align in LowerDYNAMIC_STACKALLOC");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001031
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001032 unsigned A = AlignConst->getSExtValue();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001033 auto &HFI = *Subtarget.getFrameLowering();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001034 // "Zero" means natural stack alignment.
1035 if (A == 0)
1036 A = HFI.getStackAlignment();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001037
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001038 DEBUG({
Krzysztof Parzyszek9ee04e42015-04-22 17:19:44 +00001039 dbgs () << LLVM_FUNCTION_NAME << " Align: " << A << " Size: ";
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001040 Size.getNode()->dump(&DAG);
1041 dbgs() << "\n";
1042 });
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001043
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001044 SDValue AC = DAG.getConstant(A, dl, MVT::i32);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001045 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00001046 SDValue AA = DAG.getNode(HexagonISD::ALLOCA, dl, VTs, Chain, Size, AC);
1047 if (Op.getNode()->getHasDebugValue())
1048 DAG.TransferDbgValues(Op, AA);
1049 return AA;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001050}
1051
1052SDValue
1053HexagonTargetLowering::LowerFormalArguments(SDValue Chain,
1054 CallingConv::ID CallConv,
1055 bool isVarArg,
1056 const
1057 SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001058 SDLoc dl, SelectionDAG &DAG,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001059 SmallVectorImpl<SDValue> &InVals)
1060const {
1061
1062 MachineFunction &MF = DAG.getMachineFunction();
1063 MachineFrameInfo *MFI = MF.getFrameInfo();
1064 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001065 auto &FuncInfo = *MF.getInfo<HexagonMachineFunctionInfo>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001066
1067 // Assign locations to all of the incoming arguments.
1068 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001069 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1070 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001071
1072 CCInfo.AnalyzeFormalArguments(Ins, CC_Hexagon);
1073
1074 // For LLVM, in the case when returning a struct by value (>8byte),
1075 // the first argument is a pointer that points to the location on caller's
1076 // stack where the return value will be stored. For Hexagon, the location on
1077 // caller's stack is passed only when the struct size is smaller than (and
1078 // equal to) 8 bytes. If not, no address will be passed into callee and
1079 // callee return the result direclty through R0/R1.
1080
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001081 SmallVector<SDValue, 8> MemOps;
1082 bool UseHVX = Subtarget.useHVXOps(), UseHVXDbl = Subtarget.useHVXDblOps();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001083
1084 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1085 CCValAssign &VA = ArgLocs[i];
1086 ISD::ArgFlagsTy Flags = Ins[i].Flags;
1087 unsigned ObjSize;
1088 unsigned StackLocation;
1089 int FI;
1090
1091 if ( (VA.isRegLoc() && !Flags.isByVal())
1092 || (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() > 8)) {
1093 // Arguments passed in registers
1094 // 1. int, long long, ptr args that get allocated in register.
1095 // 2. Large struct that gets an register to put its address in.
1096 EVT RegVT = VA.getLocVT();
Sirish Pande69295b82012-05-10 20:20:25 +00001097 if (RegVT == MVT::i8 || RegVT == MVT::i16 ||
1098 RegVT == MVT::i32 || RegVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001099 unsigned VReg =
Craig Topperc7242e02012-04-20 07:30:17 +00001100 RegInfo.createVirtualRegister(&Hexagon::IntRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001101 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1102 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Colin LeMahieu4379d102015-01-28 22:08:16 +00001103 } else if (RegVT == MVT::i64 || RegVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001104 unsigned VReg =
Craig Topperc7242e02012-04-20 07:30:17 +00001105 RegInfo.createVirtualRegister(&Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001106 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1107 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001108
1109 // Single Vector
1110 } else if ((RegVT == MVT::v8i64 || RegVT == MVT::v16i32 ||
1111 RegVT == MVT::v32i16 || RegVT == MVT::v64i8)) {
1112 unsigned VReg =
1113 RegInfo.createVirtualRegister(&Hexagon::VectorRegsRegClass);
1114 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1115 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1116 } else if (UseHVX && UseHVXDbl &&
1117 ((RegVT == MVT::v16i64 || RegVT == MVT::v32i32 ||
1118 RegVT == MVT::v64i16 || RegVT == MVT::v128i8))) {
1119 unsigned VReg =
1120 RegInfo.createVirtualRegister(&Hexagon::VectorRegs128BRegClass);
1121 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1122 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1123
1124 // Double Vector
1125 } else if ((RegVT == MVT::v16i64 || RegVT == MVT::v32i32 ||
1126 RegVT == MVT::v64i16 || RegVT == MVT::v128i8)) {
1127 unsigned VReg =
1128 RegInfo.createVirtualRegister(&Hexagon::VecDblRegsRegClass);
1129 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1130 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1131 } else if (UseHVX && UseHVXDbl &&
1132 ((RegVT == MVT::v32i64 || RegVT == MVT::v64i32 ||
1133 RegVT == MVT::v128i16 || RegVT == MVT::v256i8))) {
1134 unsigned VReg =
1135 RegInfo.createVirtualRegister(&Hexagon::VecDblRegs128BRegClass);
1136 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1137 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1138 } else if (RegVT == MVT::v512i1 || RegVT == MVT::v1024i1) {
1139 assert(0 && "need to support VecPred regs");
1140 unsigned VReg =
1141 RegInfo.createVirtualRegister(&Hexagon::VecPredRegsRegClass);
1142 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1143 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001144 } else {
1145 assert (0);
1146 }
1147 } else if (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() <= 8) {
1148 assert (0 && "ByValSize must be bigger than 8 bytes");
1149 } else {
1150 // Sanity check.
1151 assert(VA.isMemLoc());
1152
1153 if (Flags.isByVal()) {
1154 // If it's a byval parameter, then we need to compute the
1155 // "real" size, not the size of the pointer.
1156 ObjSize = Flags.getByValSize();
1157 } else {
1158 ObjSize = VA.getLocVT().getStoreSizeInBits() >> 3;
1159 }
1160
1161 StackLocation = HEXAGON_LRFP_SIZE + VA.getLocMemOffset();
1162 // Create the frame index object for this incoming parameter...
1163 FI = MFI->CreateFixedObject(ObjSize, StackLocation, true);
1164
1165 // Create the SelectionDAG nodes cordl, responding to a load
1166 // from this parameter.
1167 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
1168
1169 if (Flags.isByVal()) {
1170 // If it's a pass-by-value aggregate, then do not dereference the stack
1171 // location. Instead, we should generate a reference to the stack
1172 // location.
1173 InVals.push_back(FIN);
1174 } else {
1175 InVals.push_back(DAG.getLoad(VA.getLocVT(), dl, Chain, FIN,
1176 MachinePointerInfo(), false, false,
1177 false, 0));
1178 }
1179 }
1180 }
1181
1182 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001183 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001184
1185 if (isVarArg) {
1186 // This will point to the next argument passed via stack.
1187 int FrameIndex = MFI->CreateFixedObject(Hexagon_PointerSize,
1188 HEXAGON_LRFP_SIZE +
1189 CCInfo.getNextStackOffset(),
1190 true);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001191 FuncInfo.setVarArgsFrameIndex(FrameIndex);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001192 }
1193
1194 return Chain;
1195}
1196
1197SDValue
1198HexagonTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
1199 // VASTART stores the address of the VarArgsFrameIndex slot into the
1200 // memory location argument.
1201 MachineFunction &MF = DAG.getMachineFunction();
1202 HexagonMachineFunctionInfo *QFI = MF.getInfo<HexagonMachineFunctionInfo>();
1203 SDValue Addr = DAG.getFrameIndex(QFI->getVarArgsFrameIndex(), MVT::i32);
1204 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001205 return DAG.getStore(Op.getOperand(0), SDLoc(Op), Addr,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001206 Op.getOperand(1), MachinePointerInfo(SV), false,
1207 false, 0);
1208}
1209
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001210// Creates a SPLAT instruction for a constant value VAL.
1211static SDValue createSplat(SelectionDAG &DAG, SDLoc dl, EVT VT, SDValue Val) {
1212 if (VT.getSimpleVT() == MVT::v4i8)
1213 return DAG.getNode(HexagonISD::VSPLATB, dl, VT, Val);
1214
1215 if (VT.getSimpleVT() == MVT::v4i16)
1216 return DAG.getNode(HexagonISD::VSPLATH, dl, VT, Val);
1217
1218 return SDValue();
1219}
1220
1221static bool isSExtFree(SDValue N) {
1222 // A sign-extend of a truncate of a sign-extend is free.
1223 if (N.getOpcode() == ISD::TRUNCATE &&
1224 N.getOperand(0).getOpcode() == ISD::AssertSext)
1225 return true;
1226 // We have sign-extended loads.
1227 if (N.getOpcode() == ISD::LOAD)
1228 return true;
1229 return false;
1230}
1231
1232SDValue HexagonTargetLowering::LowerCTPOP(SDValue Op, SelectionDAG &DAG) const {
1233 SDLoc dl(Op);
1234 SDValue InpVal = Op.getOperand(0);
1235 if (isa<ConstantSDNode>(InpVal)) {
1236 uint64_t V = cast<ConstantSDNode>(InpVal)->getZExtValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001237 return DAG.getTargetConstant(countPopulation(V), dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001238 }
1239 SDValue PopOut = DAG.getNode(HexagonISD::POPCOUNT, dl, MVT::i32, InpVal);
1240 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, PopOut);
1241}
1242
1243SDValue HexagonTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1244 SDLoc dl(Op);
1245
1246 SDValue LHS = Op.getOperand(0);
1247 SDValue RHS = Op.getOperand(1);
1248 SDValue Cmp = Op.getOperand(2);
1249 ISD::CondCode CC = cast<CondCodeSDNode>(Cmp)->get();
1250
1251 EVT VT = Op.getValueType();
1252 EVT LHSVT = LHS.getValueType();
1253 EVT RHSVT = RHS.getValueType();
1254
1255 if (LHSVT == MVT::v2i16) {
1256 assert(ISD::isSignedIntSetCC(CC) || ISD::isUnsignedIntSetCC(CC));
1257 unsigned ExtOpc = ISD::isSignedIntSetCC(CC) ? ISD::SIGN_EXTEND
1258 : ISD::ZERO_EXTEND;
1259 SDValue LX = DAG.getNode(ExtOpc, dl, MVT::v2i32, LHS);
1260 SDValue RX = DAG.getNode(ExtOpc, dl, MVT::v2i32, RHS);
1261 SDValue SC = DAG.getNode(ISD::SETCC, dl, MVT::v2i1, LX, RX, Cmp);
1262 return SC;
1263 }
1264
1265 // Treat all other vector types as legal.
1266 if (VT.isVector())
1267 return Op;
1268
1269 // Equals and not equals should use sign-extend, not zero-extend, since
1270 // we can represent small negative values in the compare instructions.
1271 // The LLVM default is to use zero-extend arbitrarily in these cases.
1272 if ((CC == ISD::SETEQ || CC == ISD::SETNE) &&
1273 (RHSVT == MVT::i8 || RHSVT == MVT::i16) &&
1274 (LHSVT == MVT::i8 || LHSVT == MVT::i16)) {
1275 ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS);
1276 if (C && C->getAPIntValue().isNegative()) {
1277 LHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, LHS);
1278 RHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, RHS);
1279 return DAG.getNode(ISD::SETCC, dl, Op.getValueType(),
1280 LHS, RHS, Op.getOperand(2));
1281 }
1282 if (isSExtFree(LHS) || isSExtFree(RHS)) {
1283 LHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, LHS);
1284 RHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, RHS);
1285 return DAG.getNode(ISD::SETCC, dl, Op.getValueType(),
1286 LHS, RHS, Op.getOperand(2));
1287 }
1288 }
1289 return SDValue();
1290}
1291
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001292SDValue
1293HexagonTargetLowering::LowerVSELECT(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001294 SDValue PredOp = Op.getOperand(0);
1295 SDValue Op1 = Op.getOperand(1), Op2 = Op.getOperand(2);
1296 EVT OpVT = Op1.getValueType();
1297 SDLoc DL(Op);
1298
1299 if (OpVT == MVT::v2i16) {
1300 SDValue X1 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op1);
1301 SDValue X2 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op2);
1302 SDValue SL = DAG.getNode(ISD::VSELECT, DL, MVT::v2i32, PredOp, X1, X2);
1303 SDValue TR = DAG.getNode(ISD::TRUNCATE, DL, MVT::v2i16, SL);
1304 return TR;
1305 }
1306
1307 return SDValue();
1308}
1309
1310// Handle only specific vector loads.
1311SDValue HexagonTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1312 EVT VT = Op.getValueType();
1313 SDLoc DL(Op);
1314 LoadSDNode *LoadNode = cast<LoadSDNode>(Op);
1315 SDValue Chain = LoadNode->getChain();
1316 SDValue Ptr = Op.getOperand(1);
1317 SDValue LoweredLoad;
1318 SDValue Result;
1319 SDValue Base = LoadNode->getBasePtr();
1320 ISD::LoadExtType Ext = LoadNode->getExtensionType();
1321 unsigned Alignment = LoadNode->getAlignment();
1322 SDValue LoadChain;
1323
1324 if(Ext == ISD::NON_EXTLOAD)
1325 Ext = ISD::ZEXTLOAD;
1326
1327 if (VT == MVT::v4i16) {
1328 if (Alignment == 2) {
1329 SDValue Loads[4];
1330 // Base load.
1331 Loads[0] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Base,
1332 LoadNode->getPointerInfo(), MVT::i16,
1333 LoadNode->isVolatile(),
1334 LoadNode->isNonTemporal(),
1335 LoadNode->isInvariant(),
1336 Alignment);
1337 // Base+2 load.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001338 SDValue Increment = DAG.getConstant(2, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001339 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1340 Loads[1] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
1341 LoadNode->getPointerInfo(), MVT::i16,
1342 LoadNode->isVolatile(),
1343 LoadNode->isNonTemporal(),
1344 LoadNode->isInvariant(),
1345 Alignment);
1346 // SHL 16, then OR base and base+2.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001347 SDValue ShiftAmount = DAG.getConstant(16, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001348 SDValue Tmp1 = DAG.getNode(ISD::SHL, DL, MVT::i32, Loads[1], ShiftAmount);
1349 SDValue Tmp2 = DAG.getNode(ISD::OR, DL, MVT::i32, Tmp1, Loads[0]);
1350 // Base + 4.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001351 Increment = DAG.getConstant(4, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001352 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1353 Loads[2] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
1354 LoadNode->getPointerInfo(), MVT::i16,
1355 LoadNode->isVolatile(),
1356 LoadNode->isNonTemporal(),
1357 LoadNode->isInvariant(),
1358 Alignment);
1359 // Base + 6.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001360 Increment = DAG.getConstant(6, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001361 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1362 Loads[3] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
1363 LoadNode->getPointerInfo(), MVT::i16,
1364 LoadNode->isVolatile(),
1365 LoadNode->isNonTemporal(),
1366 LoadNode->isInvariant(),
1367 Alignment);
1368 // SHL 16, then OR base+4 and base+6.
1369 Tmp1 = DAG.getNode(ISD::SHL, DL, MVT::i32, Loads[3], ShiftAmount);
1370 SDValue Tmp4 = DAG.getNode(ISD::OR, DL, MVT::i32, Tmp1, Loads[2]);
1371 // Combine to i64. This could be optimised out later if we can
1372 // affect reg allocation of this code.
1373 Result = DAG.getNode(HexagonISD::COMBINE, DL, MVT::i64, Tmp4, Tmp2);
1374 LoadChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
1375 Loads[0].getValue(1), Loads[1].getValue(1),
1376 Loads[2].getValue(1), Loads[3].getValue(1));
1377 } else {
1378 // Perform default type expansion.
1379 Result = DAG.getLoad(MVT::i64, DL, Chain, Ptr, LoadNode->getPointerInfo(),
1380 LoadNode->isVolatile(), LoadNode->isNonTemporal(),
1381 LoadNode->isInvariant(), LoadNode->getAlignment());
1382 LoadChain = Result.getValue(1);
1383 }
1384 } else
1385 llvm_unreachable("Custom lowering unsupported load");
1386
1387 Result = DAG.getNode(ISD::BITCAST, DL, VT, Result);
1388 // Since we pretend to lower a load, we need the original chain
1389 // info attached to the result.
1390 SDValue Ops[] = { Result, LoadChain };
1391
1392 return DAG.getMergeValues(Ops, DL);
1393}
1394
1395
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001396SDValue
Sirish Pande69295b82012-05-10 20:20:25 +00001397HexagonTargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
1398 EVT ValTy = Op.getValueType();
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001399 ConstantPoolSDNode *CPN = cast<ConstantPoolSDNode>(Op);
1400 unsigned Align = CPN->getAlignment();
1401 Reloc::Model RM = HTM.getRelocationModel();
1402 unsigned char TF = (RM == Reloc::PIC_) ? HexagonII::MO_PCREL : 0;
1403
1404 SDValue T;
1405 if (CPN->isMachineConstantPoolEntry())
1406 T = DAG.getTargetConstantPool(CPN->getMachineCPVal(), ValTy, Align, TF);
Sirish Pande69295b82012-05-10 20:20:25 +00001407 else
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001408 T = DAG.getTargetConstantPool(CPN->getConstVal(), ValTy, Align, TF);
1409 if (RM == Reloc::PIC_)
1410 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), ValTy, T);
1411 return DAG.getNode(HexagonISD::CP, SDLoc(Op), ValTy, T);
1412}
1413
1414SDValue
1415HexagonTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
1416 EVT VT = Op.getValueType();
1417 int Idx = cast<JumpTableSDNode>(Op)->getIndex();
1418 Reloc::Model RM = HTM.getRelocationModel();
1419 if (RM == Reloc::PIC_) {
1420 SDValue T = DAG.getTargetJumpTable(Idx, VT, HexagonII::MO_PCREL);
1421 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), VT, T);
1422 }
1423
1424 SDValue T = DAG.getTargetJumpTable(Idx, VT);
1425 return DAG.getNode(HexagonISD::JT, SDLoc(Op), VT, T);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001426}
1427
1428SDValue
1429HexagonTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001430 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001431 MachineFunction &MF = DAG.getMachineFunction();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001432 MachineFrameInfo &MFI = *MF.getFrameInfo();
1433 MFI.setReturnAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001434
Bill Wendling908bf812014-01-06 00:43:20 +00001435 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001436 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001437
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001438 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001439 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001440 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1441 if (Depth) {
1442 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001443 SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001444 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
1445 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
1446 MachinePointerInfo(), false, false, false, 0);
1447 }
1448
1449 // Return LR, which contains the return address. Mark it an implicit live-in.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001450 unsigned Reg = MF.addLiveIn(HRI.getRARegister(), getRegClassFor(MVT::i32));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001451 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
1452}
1453
1454SDValue
1455HexagonTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001456 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
1457 MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
1458 MFI.setFrameAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001459
1460 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001461 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001462 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1463 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001464 HRI.getFrameRegister(), VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001465 while (Depth--)
1466 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
1467 MachinePointerInfo(),
1468 false, false, false, 0);
1469 return FrameAddr;
1470}
1471
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001472SDValue
1473HexagonTargetLowering::LowerATOMIC_FENCE(SDValue Op, SelectionDAG& DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001474 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001475 return DAG.getNode(HexagonISD::BARRIER, dl, MVT::Other, Op.getOperand(0));
1476}
1477
1478
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001479SDValue
1480HexagonTargetLowering::LowerGLOBALADDRESS(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001481 SDLoc dl(Op);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001482 auto *GAN = cast<GlobalAddressSDNode>(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00001483 auto PtrVT = getPointerTy(DAG.getDataLayout());
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001484 auto *GV = GAN->getGlobal();
1485 int64_t Offset = GAN->getOffset();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001486
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001487 auto &HLOF = *HTM.getObjFileLowering();
1488 Reloc::Model RM = HTM.getRelocationModel();
1489
1490 if (RM == Reloc::Static) {
1491 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset);
1492 if (HLOF.IsGlobalInSmallSection(GV, HTM))
1493 return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, GA);
1494 return DAG.getNode(HexagonISD::CONST32, dl, PtrVT, GA);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001495 }
1496
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001497 bool UsePCRel = GV->hasInternalLinkage() || GV->hasHiddenVisibility() ||
1498 (GV->hasLocalLinkage() && !isa<Function>(GV));
1499 if (UsePCRel) {
1500 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset,
1501 HexagonII::MO_PCREL);
1502 return DAG.getNode(HexagonISD::AT_PCREL, dl, PtrVT, GA);
1503 }
1504
1505 // Use GOT index.
1506 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
1507 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, HexagonII::MO_GOT);
1508 SDValue Off = DAG.getConstant(Offset, dl, MVT::i32);
1509 return DAG.getNode(HexagonISD::AT_GOT, dl, PtrVT, GOT, GA, Off);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001510}
1511
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001512// Specifies that for loads and stores VT can be promoted to PromotedLdStVT.
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001513SDValue
1514HexagonTargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
1515 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001516 SDLoc dl(Op);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001517 EVT PtrVT = getPointerTy(DAG.getDataLayout());
1518
1519 Reloc::Model RM = HTM.getRelocationModel();
1520 if (RM == Reloc::Static) {
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001521 SDValue A = DAG.getTargetBlockAddress(BA, PtrVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001522 return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, A);
1523 }
1524
1525 SDValue A = DAG.getTargetBlockAddress(BA, PtrVT, 0, HexagonII::MO_PCREL);
1526 return DAG.getNode(HexagonISD::AT_PCREL, dl, PtrVT, A);
1527}
1528
1529SDValue
1530HexagonTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op, SelectionDAG &DAG)
1531 const {
1532 EVT PtrVT = getPointerTy(DAG.getDataLayout());
1533 SDValue GOTSym = DAG.getTargetExternalSymbol(HEXAGON_GOT_SYM_NAME, PtrVT,
1534 HexagonII::MO_PCREL);
1535 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), PtrVT, GOTSym);
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001536}
1537
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001538SDValue
1539HexagonTargetLowering::GetDynamicTLSAddr(SelectionDAG &DAG, SDValue Chain,
1540 GlobalAddressSDNode *GA, SDValue *InFlag, EVT PtrVT, unsigned ReturnReg,
1541 unsigned char OperandFlags) const {
1542 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1543 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1544 SDLoc dl(GA);
1545 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
1546 GA->getValueType(0),
1547 GA->getOffset(),
1548 OperandFlags);
1549 // Create Operands for the call.The Operands should have the following:
1550 // 1. Chain SDValue
1551 // 2. Callee which in this case is the Global address value.
1552 // 3. Registers live into the call.In this case its R0, as we
1553 // have just one argument to be passed.
1554 // 4. InFlag if there is any.
1555 // Note: The order is important.
1556
1557 if (InFlag) {
1558 SDValue Ops[] = { Chain, TGA,
1559 DAG.getRegister(Hexagon::R0, PtrVT), *InFlag };
1560 Chain = DAG.getNode(HexagonISD::CALLv3, dl, NodeTys, Ops);
1561 } else {
1562 SDValue Ops[] = { Chain, TGA, DAG.getRegister(Hexagon::R0, PtrVT)};
1563 Chain = DAG.getNode(HexagonISD::CALLv3, dl, NodeTys, Ops);
1564 }
1565
1566 // Inform MFI that function has calls.
1567 MFI->setAdjustsStack(true);
1568
1569 SDValue Flag = Chain.getValue(1);
1570 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
1571}
1572
1573//
1574// Lower using the intial executable model for TLS addresses
1575//
1576SDValue
1577HexagonTargetLowering::LowerToTLSInitialExecModel(GlobalAddressSDNode *GA,
1578 SelectionDAG &DAG) const {
1579 SDLoc dl(GA);
1580 int64_t Offset = GA->getOffset();
1581 auto PtrVT = getPointerTy(DAG.getDataLayout());
1582
1583 // Get the thread pointer.
1584 SDValue TP = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Hexagon::UGP, PtrVT);
1585
1586 Reloc::Model RM = HTM.getRelocationModel();
1587 unsigned char TF = (RM == Reloc::PIC_) ? HexagonII::MO_IEGOT
1588 : HexagonII::MO_IE;
1589
1590 // First generate the TLS symbol address
1591 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT,
1592 Offset, TF);
1593
1594 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1595
1596 if (RM == Reloc::PIC_) {
1597 // Generate the GOT pointer in case of position independent code
1598 SDValue GOT = LowerGLOBAL_OFFSET_TABLE(Sym, DAG);
1599
1600 // Add the TLS Symbol address to GOT pointer.This gives
1601 // GOT relative relocation for the symbol.
1602 Sym = DAG.getNode(ISD::ADD, dl, PtrVT, GOT, Sym);
1603 }
1604
1605 // Load the offset value for TLS symbol.This offset is relative to
1606 // thread pointer.
1607 SDValue LoadOffset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Sym,
1608 MachinePointerInfo(),
1609 false, false, false, 0);
1610
1611 // Address of the thread local variable is the add of thread
1612 // pointer and the offset of the variable.
1613 return DAG.getNode(ISD::ADD, dl, PtrVT, TP, LoadOffset);
1614}
1615
1616//
1617// Lower using the local executable model for TLS addresses
1618//
1619SDValue
1620HexagonTargetLowering::LowerToTLSLocalExecModel(GlobalAddressSDNode *GA,
1621 SelectionDAG &DAG) const {
1622 SDLoc dl(GA);
1623 int64_t Offset = GA->getOffset();
1624 auto PtrVT = getPointerTy(DAG.getDataLayout());
1625
1626 // Get the thread pointer.
1627 SDValue TP = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Hexagon::UGP, PtrVT);
1628 // Generate the TLS symbol address
1629 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT, Offset,
1630 HexagonII::MO_TPREL);
1631 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1632
1633 // Address of the thread local variable is the add of thread
1634 // pointer and the offset of the variable.
1635 return DAG.getNode(ISD::ADD, dl, PtrVT, TP, Sym);
1636}
1637
1638//
1639// Lower using the general dynamic model for TLS addresses
1640//
1641SDValue
1642HexagonTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
1643 SelectionDAG &DAG) const {
1644 SDLoc dl(GA);
1645 int64_t Offset = GA->getOffset();
1646 auto PtrVT = getPointerTy(DAG.getDataLayout());
1647
1648 // First generate the TLS symbol address
1649 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT, Offset,
1650 HexagonII::MO_GDGOT);
1651
1652 // Then, generate the GOT pointer
1653 SDValue GOT = LowerGLOBAL_OFFSET_TABLE(TGA, DAG);
1654
1655 // Add the TLS symbol and the GOT pointer
1656 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1657 SDValue Chain = DAG.getNode(ISD::ADD, dl, PtrVT, GOT, Sym);
1658
1659 // Copy over the argument to R0
1660 SDValue InFlag;
1661 Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, Hexagon::R0, Chain, InFlag);
1662 InFlag = Chain.getValue(1);
1663
1664 return GetDynamicTLSAddr(DAG, Chain, GA, &InFlag, PtrVT,
1665 Hexagon::R0, HexagonII::MO_GDPLT);
1666}
1667
1668//
1669// Lower TLS addresses.
1670//
1671// For now for dynamic models, we only support the general dynamic model.
1672//
1673SDValue
1674HexagonTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1675 SelectionDAG &DAG) const {
1676 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1677
1678 switch (HTM.getTLSModel(GA->getGlobal())) {
1679 case TLSModel::GeneralDynamic:
1680 case TLSModel::LocalDynamic:
1681 return LowerToTLSGeneralDynamicModel(GA, DAG);
1682 case TLSModel::InitialExec:
1683 return LowerToTLSInitialExecModel(GA, DAG);
1684 case TLSModel::LocalExec:
1685 return LowerToTLSLocalExecModel(GA, DAG);
1686 }
1687 llvm_unreachable("Bogus TLS model");
1688}
1689
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001690//===----------------------------------------------------------------------===//
1691// TargetLowering Implementation
1692//===----------------------------------------------------------------------===//
1693
Eric Christopherd737b762015-02-02 22:11:36 +00001694HexagonTargetLowering::HexagonTargetLowering(const TargetMachine &TM,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001695 const HexagonSubtarget &ST)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001696 : TargetLowering(TM), HTM(static_cast<const HexagonTargetMachine&>(TM)),
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001697 Subtarget(ST) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001698 bool IsV4 = !Subtarget.hasV5TOps();
1699 auto &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00001700 bool UseHVX = Subtarget.useHVXOps();
1701 bool UseHVXSgl = Subtarget.useHVXSglOps();
1702 bool UseHVXDbl = Subtarget.useHVXDblOps();
Sirish Pande69295b82012-05-10 20:20:25 +00001703
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001704 setPrefLoopAlignment(4);
1705 setPrefFunctionAlignment(4);
1706 setMinFunctionAlignment(2);
1707 setInsertFencesForAtomic(false);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001708 setStackPointerRegisterToSaveRestore(HRI.getStackRegister());
1709
1710 if (EnableHexSDNodeSched)
1711 setSchedulingPreference(Sched::VLIW);
1712 else
1713 setSchedulingPreference(Sched::Source);
1714
1715 // Limits for inline expansion of memcpy/memmove
1716 MaxStoresPerMemcpy = MaxStoresPerMemcpyCL;
1717 MaxStoresPerMemcpyOptSize = MaxStoresPerMemcpyOptSizeCL;
1718 MaxStoresPerMemmove = MaxStoresPerMemmoveCL;
1719 MaxStoresPerMemmoveOptSize = MaxStoresPerMemmoveOptSizeCL;
1720 MaxStoresPerMemset = MaxStoresPerMemsetCL;
1721 MaxStoresPerMemsetOptSize = MaxStoresPerMemsetOptSizeCL;
1722
1723 //
1724 // Set up register classes.
1725 //
1726
1727 addRegisterClass(MVT::i1, &Hexagon::PredRegsRegClass);
1728 addRegisterClass(MVT::v2i1, &Hexagon::PredRegsRegClass); // bbbbaaaa
1729 addRegisterClass(MVT::v4i1, &Hexagon::PredRegsRegClass); // ddccbbaa
1730 addRegisterClass(MVT::v8i1, &Hexagon::PredRegsRegClass); // hgfedcba
1731 addRegisterClass(MVT::i32, &Hexagon::IntRegsRegClass);
1732 addRegisterClass(MVT::v4i8, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001733 addRegisterClass(MVT::v2i16, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001734 addRegisterClass(MVT::i64, &Hexagon::DoubleRegsRegClass);
1735 addRegisterClass(MVT::v8i8, &Hexagon::DoubleRegsRegClass);
1736 addRegisterClass(MVT::v4i16, &Hexagon::DoubleRegsRegClass);
1737 addRegisterClass(MVT::v2i32, &Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001738
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001739 if (Subtarget.hasV5TOps()) {
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001740 addRegisterClass(MVT::f32, &Hexagon::IntRegsRegClass);
1741 addRegisterClass(MVT::f64, &Hexagon::DoubleRegsRegClass);
1742 }
Sirish Pande69295b82012-05-10 20:20:25 +00001743
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001744 if (Subtarget.hasV60TOps()) {
1745 if (Subtarget.useHVXSglOps()) {
1746 addRegisterClass(MVT::v64i8, &Hexagon::VectorRegsRegClass);
1747 addRegisterClass(MVT::v32i16, &Hexagon::VectorRegsRegClass);
1748 addRegisterClass(MVT::v16i32, &Hexagon::VectorRegsRegClass);
1749 addRegisterClass(MVT::v8i64, &Hexagon::VectorRegsRegClass);
1750 addRegisterClass(MVT::v128i8, &Hexagon::VecDblRegsRegClass);
1751 addRegisterClass(MVT::v64i16, &Hexagon::VecDblRegsRegClass);
1752 addRegisterClass(MVT::v32i32, &Hexagon::VecDblRegsRegClass);
1753 addRegisterClass(MVT::v16i64, &Hexagon::VecDblRegsRegClass);
1754 addRegisterClass(MVT::v512i1, &Hexagon::VecPredRegsRegClass);
1755 } else if (Subtarget.useHVXDblOps()) {
1756 addRegisterClass(MVT::v128i8, &Hexagon::VectorRegs128BRegClass);
1757 addRegisterClass(MVT::v64i16, &Hexagon::VectorRegs128BRegClass);
1758 addRegisterClass(MVT::v32i32, &Hexagon::VectorRegs128BRegClass);
1759 addRegisterClass(MVT::v16i64, &Hexagon::VectorRegs128BRegClass);
1760 addRegisterClass(MVT::v256i8, &Hexagon::VecDblRegs128BRegClass);
1761 addRegisterClass(MVT::v128i16, &Hexagon::VecDblRegs128BRegClass);
1762 addRegisterClass(MVT::v64i32, &Hexagon::VecDblRegs128BRegClass);
1763 addRegisterClass(MVT::v32i64, &Hexagon::VecDblRegs128BRegClass);
1764 addRegisterClass(MVT::v1024i1, &Hexagon::VecPredRegs128BRegClass);
1765 }
1766
1767 }
1768
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001769 //
1770 // Handling of scalar operations.
1771 //
1772 // All operations default to "legal", except:
1773 // - indexed loads and stores (pre-/post-incremented),
1774 // - ANY_EXTEND_VECTOR_INREG, ATOMIC_CMP_SWAP_WITH_SUCCESS, CONCAT_VECTORS,
1775 // ConstantFP, DEBUGTRAP, FCEIL, FCOPYSIGN, FEXP, FEXP2, FFLOOR, FGETSIGN,
1776 // FLOG, FLOG2, FLOG10, FMAXNUM, FMINNUM, FNEARBYINT, FRINT, FROUND, TRAP,
1777 // FTRUNC, PREFETCH, SIGN_EXTEND_VECTOR_INREG, ZERO_EXTEND_VECTOR_INREG,
1778 // which default to "expand" for at least one type.
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001779
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001780 // Misc operations.
1781 setOperationAction(ISD::ConstantFP, MVT::f32, Legal); // Default: expand
1782 setOperationAction(ISD::ConstantFP, MVT::f64, Legal); // Default: expand
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001783
1784 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001785 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001786 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001787 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
1788 setOperationAction(ISD::INLINEASM, MVT::Other, Custom);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00001789 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
1790 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001791 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001792 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001793 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001794 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001795
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001796 // Custom legalize GlobalAddress nodes into CONST32.
1797 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001798 setOperationAction(ISD::GlobalAddress, MVT::i8, Custom);
1799 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001800
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001801 // Hexagon needs to optimize cases with negative constants.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001802 setOperationAction(ISD::SETCC, MVT::i8, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001803 setOperationAction(ISD::SETCC, MVT::i16, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001804
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001805 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
1806 setOperationAction(ISD::VASTART, MVT::Other, Custom);
1807 setOperationAction(ISD::VAEND, MVT::Other, Expand);
1808 setOperationAction(ISD::VAARG, MVT::Other, Expand);
1809
1810 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
1811 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
1812 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
1813
1814 if (EmitJumpTables)
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001815 setMinimumJumpTableEntries(MinimumJumpTables);
Krzysztof Parzyszeka61f7da2016-01-13 21:43:13 +00001816 else
1817 setMinimumJumpTableEntries(INT_MAX);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001818 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001819
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001820 // Hexagon has instructions for add/sub with carry. The problem with
1821 // modeling these instructions is that they produce 2 results: Rdd and Px.
1822 // To model the update of Px, we will have to use Defs[p0..p3] which will
1823 // cause any predicate live range to spill. So, we pretend we dont't have
1824 // these instructions.
1825 setOperationAction(ISD::ADDE, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001826 setOperationAction(ISD::ADDE, MVT::i16, Expand);
1827 setOperationAction(ISD::ADDE, MVT::i32, Expand);
1828 setOperationAction(ISD::ADDE, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001829 setOperationAction(ISD::SUBE, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001830 setOperationAction(ISD::SUBE, MVT::i16, Expand);
1831 setOperationAction(ISD::SUBE, MVT::i32, Expand);
1832 setOperationAction(ISD::SUBE, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001833 setOperationAction(ISD::ADDC, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001834 setOperationAction(ISD::ADDC, MVT::i16, Expand);
1835 setOperationAction(ISD::ADDC, MVT::i32, Expand);
1836 setOperationAction(ISD::ADDC, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001837 setOperationAction(ISD::SUBC, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001838 setOperationAction(ISD::SUBC, MVT::i16, Expand);
1839 setOperationAction(ISD::SUBC, MVT::i32, Expand);
1840 setOperationAction(ISD::SUBC, MVT::i64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001841
Krzysztof Parzyszek2c4487d2015-04-13 20:37:01 +00001842 // Only add and sub that detect overflow are the saturating ones.
1843 for (MVT VT : MVT::integer_valuetypes()) {
1844 setOperationAction(ISD::UADDO, VT, Expand);
1845 setOperationAction(ISD::SADDO, VT, Expand);
1846 setOperationAction(ISD::USUBO, VT, Expand);
1847 setOperationAction(ISD::SSUBO, VT, Expand);
1848 }
1849
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001850 setOperationAction(ISD::CTLZ, MVT::i8, Promote);
1851 setOperationAction(ISD::CTLZ, MVT::i16, Promote);
1852 setOperationAction(ISD::CTTZ, MVT::i8, Promote);
1853 setOperationAction(ISD::CTTZ, MVT::i16, Promote);
1854 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8, Promote);
1855 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16, Promote);
1856 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i8, Promote);
1857 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16, Promote);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001858
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001859 // In V5, popcount can count # of 1s in i64 but returns i32.
1860 // On V4 it will be expanded (set later).
1861 setOperationAction(ISD::CTPOP, MVT::i8, Promote);
1862 setOperationAction(ISD::CTPOP, MVT::i16, Promote);
1863 setOperationAction(ISD::CTPOP, MVT::i32, Promote);
1864 setOperationAction(ISD::CTPOP, MVT::i64, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001865
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001866 // We custom lower i64 to i64 mul, so that it is not considered as a legal
1867 // operation. There is a pattern that will match i64 mul and transform it
1868 // to a series of instructions.
1869 setOperationAction(ISD::MUL, MVT::i64, Expand);
Colin LeMahieude68b662015-02-05 21:13:25 +00001870 setOperationAction(ISD::MULHS, MVT::i64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001871
Benjamin Kramer62460692015-04-25 14:46:53 +00001872 for (unsigned IntExpOp :
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001873 { ISD::SDIV, ISD::UDIV, ISD::SREM, ISD::UREM,
1874 ISD::SDIVREM, ISD::UDIVREM, ISD::ROTL, ISD::ROTR,
1875 ISD::BSWAP, ISD::SHL_PARTS, ISD::SRA_PARTS, ISD::SRL_PARTS,
1876 ISD::SMUL_LOHI, ISD::UMUL_LOHI }) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001877 setOperationAction(IntExpOp, MVT::i32, Expand);
1878 setOperationAction(IntExpOp, MVT::i64, Expand);
1879 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001880
Benjamin Kramer62460692015-04-25 14:46:53 +00001881 for (unsigned FPExpOp :
1882 {ISD::FDIV, ISD::FREM, ISD::FSQRT, ISD::FSIN, ISD::FCOS, ISD::FSINCOS,
1883 ISD::FPOW, ISD::FCOPYSIGN}) {
1884 setOperationAction(FPExpOp, MVT::f32, Expand);
1885 setOperationAction(FPExpOp, MVT::f64, Expand);
1886 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001887
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001888 // No extending loads from i32.
1889 for (MVT VT : MVT::integer_valuetypes()) {
1890 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
1891 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
1892 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
1893 }
1894 // Turn FP truncstore into trunc + store.
1895 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
1896 // Turn FP extload into load/fextend.
1897 for (MVT VT : MVT::fp_valuetypes())
1898 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001899
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001900 // Expand BR_CC and SELECT_CC for all integer and fp types.
1901 for (MVT VT : MVT::integer_valuetypes()) {
1902 setOperationAction(ISD::BR_CC, VT, Expand);
1903 setOperationAction(ISD::SELECT_CC, VT, Expand);
1904 }
1905 for (MVT VT : MVT::fp_valuetypes()) {
1906 setOperationAction(ISD::BR_CC, VT, Expand);
1907 setOperationAction(ISD::SELECT_CC, VT, Expand);
1908 }
1909 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001910
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001911 //
1912 // Handling of vector operations.
1913 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001914
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001915 // Custom lower v4i16 load only. Let v4i16 store to be
1916 // promoted for now.
1917 promoteLdStType(MVT::v4i8, MVT::i32);
1918 promoteLdStType(MVT::v2i16, MVT::i32);
1919 promoteLdStType(MVT::v8i8, MVT::i64);
1920 promoteLdStType(MVT::v2i32, MVT::i64);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001921
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001922 setOperationAction(ISD::LOAD, MVT::v4i16, Custom);
1923 setOperationAction(ISD::STORE, MVT::v4i16, Promote);
1924 AddPromotedToType(ISD::LOAD, MVT::v4i16, MVT::i64);
1925 AddPromotedToType(ISD::STORE, MVT::v4i16, MVT::i64);
1926
1927 // Set the action for vector operations to "expand", then override it with
1928 // either "custom" or "legal" for specific cases.
Craig Topper26260942015-10-18 05:15:34 +00001929 static const unsigned VectExpOps[] = {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001930 // Integer arithmetic:
1931 ISD::ADD, ISD::SUB, ISD::MUL, ISD::SDIV, ISD::UDIV,
1932 ISD::SREM, ISD::UREM, ISD::SDIVREM, ISD::UDIVREM, ISD::ADDC,
1933 ISD::SUBC, ISD::SADDO, ISD::UADDO, ISD::SSUBO, ISD::USUBO,
1934 ISD::SMUL_LOHI, ISD::UMUL_LOHI,
1935 // Logical/bit:
1936 ISD::AND, ISD::OR, ISD::XOR, ISD::ROTL, ISD::ROTR,
1937 ISD::CTPOP, ISD::CTLZ, ISD::CTTZ, ISD::CTLZ_ZERO_UNDEF,
1938 ISD::CTTZ_ZERO_UNDEF,
1939 // Floating point arithmetic/math functions:
1940 ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FMA, ISD::FDIV,
1941 ISD::FREM, ISD::FNEG, ISD::FABS, ISD::FSQRT, ISD::FSIN,
1942 ISD::FCOS, ISD::FPOWI, ISD::FPOW, ISD::FLOG, ISD::FLOG2,
1943 ISD::FLOG10, ISD::FEXP, ISD::FEXP2, ISD::FCEIL, ISD::FTRUNC,
1944 ISD::FRINT, ISD::FNEARBYINT, ISD::FROUND, ISD::FFLOOR,
1945 ISD::FMINNUM, ISD::FMAXNUM, ISD::FSINCOS,
1946 // Misc:
1947 ISD::SELECT, ISD::ConstantPool,
1948 // Vector:
1949 ISD::BUILD_VECTOR, ISD::SCALAR_TO_VECTOR,
1950 ISD::EXTRACT_VECTOR_ELT, ISD::INSERT_VECTOR_ELT,
1951 ISD::EXTRACT_SUBVECTOR, ISD::INSERT_SUBVECTOR,
1952 ISD::CONCAT_VECTORS, ISD::VECTOR_SHUFFLE
1953 };
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001954
1955 for (MVT VT : MVT::vector_valuetypes()) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001956 for (unsigned VectExpOp : VectExpOps)
1957 setOperationAction(VectExpOp, VT, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001958
1959 // Expand all extended loads and truncating stores:
1960 for (MVT TargetVT : MVT::vector_valuetypes()) {
1961 setLoadExtAction(ISD::EXTLOAD, TargetVT, VT, Expand);
1962 setTruncStoreAction(VT, TargetVT, Expand);
1963 }
1964
1965 setOperationAction(ISD::SRA, VT, Custom);
1966 setOperationAction(ISD::SHL, VT, Custom);
1967 setOperationAction(ISD::SRL, VT, Custom);
1968 }
1969
1970 // Types natively supported:
Benjamin Kramer62460692015-04-25 14:46:53 +00001971 for (MVT NativeVT : {MVT::v2i1, MVT::v4i1, MVT::v8i1, MVT::v32i1, MVT::v64i1,
1972 MVT::v4i8, MVT::v8i8, MVT::v2i16, MVT::v4i16, MVT::v1i32,
1973 MVT::v2i32, MVT::v1i64}) {
1974 setOperationAction(ISD::BUILD_VECTOR, NativeVT, Custom);
1975 setOperationAction(ISD::EXTRACT_VECTOR_ELT, NativeVT, Custom);
1976 setOperationAction(ISD::INSERT_VECTOR_ELT, NativeVT, Custom);
1977 setOperationAction(ISD::EXTRACT_SUBVECTOR, NativeVT, Custom);
1978 setOperationAction(ISD::INSERT_SUBVECTOR, NativeVT, Custom);
1979 setOperationAction(ISD::CONCAT_VECTORS, NativeVT, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001980
Benjamin Kramer62460692015-04-25 14:46:53 +00001981 setOperationAction(ISD::ADD, NativeVT, Legal);
1982 setOperationAction(ISD::SUB, NativeVT, Legal);
1983 setOperationAction(ISD::MUL, NativeVT, Legal);
1984 setOperationAction(ISD::AND, NativeVT, Legal);
1985 setOperationAction(ISD::OR, NativeVT, Legal);
1986 setOperationAction(ISD::XOR, NativeVT, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001987 }
1988
1989 setOperationAction(ISD::SETCC, MVT::v2i16, Custom);
1990 setOperationAction(ISD::VSELECT, MVT::v2i16, Custom);
1991 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
1992 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001993 if (UseHVX) {
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00001994 if (UseHVXSgl) {
1995 setOperationAction(ISD::CONCAT_VECTORS, MVT::v128i8, Custom);
1996 setOperationAction(ISD::CONCAT_VECTORS, MVT::v64i16, Custom);
1997 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i32, Custom);
1998 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i64, Custom);
1999 } else if (UseHVXDbl) {
2000 setOperationAction(ISD::CONCAT_VECTORS, MVT::v256i8, Custom);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002001 setOperationAction(ISD::CONCAT_VECTORS, MVT::v128i16, Custom);
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002002 setOperationAction(ISD::CONCAT_VECTORS, MVT::v64i32, Custom);
2003 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i64, Custom);
2004 } else {
2005 llvm_unreachable("Unrecognized HVX mode");
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002006 }
2007 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002008 // Subtarget-specific operation actions.
2009 //
2010 if (Subtarget.hasV5TOps()) {
2011 setOperationAction(ISD::FMA, MVT::f64, Expand);
2012 setOperationAction(ISD::FADD, MVT::f64, Expand);
2013 setOperationAction(ISD::FSUB, MVT::f64, Expand);
2014 setOperationAction(ISD::FMUL, MVT::f64, Expand);
2015
2016 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Promote);
2017 setOperationAction(ISD::FP_TO_UINT, MVT::i8, Promote);
2018 setOperationAction(ISD::FP_TO_UINT, MVT::i16, Promote);
2019 setOperationAction(ISD::FP_TO_SINT, MVT::i1, Promote);
2020 setOperationAction(ISD::FP_TO_SINT, MVT::i8, Promote);
2021 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
2022 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote);
2023 setOperationAction(ISD::UINT_TO_FP, MVT::i8, Promote);
2024 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote);
2025 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote);
2026 setOperationAction(ISD::SINT_TO_FP, MVT::i8, Promote);
2027 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
2028
2029 } else { // V4
2030 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
2031 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Expand);
2032 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
2033 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
2034 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Expand);
2035 setOperationAction(ISD::FP_TO_SINT, MVT::f32, Expand);
2036 setOperationAction(ISD::FP_EXTEND, MVT::f32, Expand);
2037 setOperationAction(ISD::FP_ROUND, MVT::f64, Expand);
2038 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
2039
2040 setOperationAction(ISD::CTPOP, MVT::i8, Expand);
2041 setOperationAction(ISD::CTPOP, MVT::i16, Expand);
2042 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
2043 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
2044
2045 // Expand these operations for both f32 and f64:
Benjamin Kramer62460692015-04-25 14:46:53 +00002046 for (unsigned FPExpOpV4 :
2047 {ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FABS, ISD::FNEG, ISD::FMA}) {
2048 setOperationAction(FPExpOpV4, MVT::f32, Expand);
2049 setOperationAction(FPExpOpV4, MVT::f64, Expand);
2050 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002051
Benjamin Kramer62460692015-04-25 14:46:53 +00002052 for (ISD::CondCode FPExpCCV4 :
2053 {ISD::SETOEQ, ISD::SETOGT, ISD::SETOLT, ISD::SETOGE, ISD::SETOLE,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002054 ISD::SETUO, ISD::SETO}) {
Benjamin Kramer62460692015-04-25 14:46:53 +00002055 setCondCodeAction(FPExpCCV4, MVT::f32, Expand);
2056 setCondCodeAction(FPExpCCV4, MVT::f64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002057 }
2058 }
2059
2060 // Handling of indexed loads/stores: default is "expand".
2061 //
Benjamin Kramer62460692015-04-25 14:46:53 +00002062 for (MVT LSXTy : {MVT::i8, MVT::i16, MVT::i32, MVT::i64}) {
2063 setIndexedLoadAction(ISD::POST_INC, LSXTy, Legal);
2064 setIndexedStoreAction(ISD::POST_INC, LSXTy, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002065 }
2066
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002067 if (UseHVXDbl) {
2068 for (MVT VT : {MVT::v128i8, MVT::v64i16, MVT::v32i32, MVT::v16i64}) {
2069 setIndexedLoadAction(ISD::POST_INC, VT, Legal);
2070 setIndexedStoreAction(ISD::POST_INC, VT, Legal);
2071 }
2072 }
2073
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002074 computeRegisterProperties(&HRI);
2075
2076 //
2077 // Library calls for unsupported operations
2078 //
2079 bool FastMath = EnableFastMath;
2080
Benjamin Kramera37c8092015-04-25 14:46:46 +00002081 setLibcallName(RTLIB::SDIV_I32, "__hexagon_divsi3");
2082 setLibcallName(RTLIB::SDIV_I64, "__hexagon_divdi3");
2083 setLibcallName(RTLIB::UDIV_I32, "__hexagon_udivsi3");
2084 setLibcallName(RTLIB::UDIV_I64, "__hexagon_udivdi3");
2085 setLibcallName(RTLIB::SREM_I32, "__hexagon_modsi3");
2086 setLibcallName(RTLIB::SREM_I64, "__hexagon_moddi3");
2087 setLibcallName(RTLIB::UREM_I32, "__hexagon_umodsi3");
2088 setLibcallName(RTLIB::UREM_I64, "__hexagon_umoddi3");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002089
Benjamin Kramera37c8092015-04-25 14:46:46 +00002090 setLibcallName(RTLIB::SINTTOFP_I128_F64, "__hexagon_floattidf");
2091 setLibcallName(RTLIB::SINTTOFP_I128_F32, "__hexagon_floattisf");
2092 setLibcallName(RTLIB::FPTOUINT_F32_I128, "__hexagon_fixunssfti");
2093 setLibcallName(RTLIB::FPTOUINT_F64_I128, "__hexagon_fixunsdfti");
2094 setLibcallName(RTLIB::FPTOSINT_F32_I128, "__hexagon_fixsfti");
2095 setLibcallName(RTLIB::FPTOSINT_F64_I128, "__hexagon_fixdfti");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002096
2097 if (IsV4) {
2098 // Handle single-precision floating point operations on V4.
Benjamin Kramera37c8092015-04-25 14:46:46 +00002099 if (FastMath) {
2100 setLibcallName(RTLIB::ADD_F32, "__hexagon_fast_addsf3");
2101 setLibcallName(RTLIB::SUB_F32, "__hexagon_fast_subsf3");
2102 setLibcallName(RTLIB::MUL_F32, "__hexagon_fast_mulsf3");
2103 setLibcallName(RTLIB::OGT_F32, "__hexagon_fast_gtsf2");
2104 setLibcallName(RTLIB::OLT_F32, "__hexagon_fast_ltsf2");
2105 // Double-precision compares.
2106 setLibcallName(RTLIB::OGT_F64, "__hexagon_fast_gtdf2");
2107 setLibcallName(RTLIB::OLT_F64, "__hexagon_fast_ltdf2");
2108 } else {
2109 setLibcallName(RTLIB::ADD_F32, "__hexagon_addsf3");
2110 setLibcallName(RTLIB::SUB_F32, "__hexagon_subsf3");
2111 setLibcallName(RTLIB::MUL_F32, "__hexagon_mulsf3");
2112 setLibcallName(RTLIB::OGT_F32, "__hexagon_gtsf2");
2113 setLibcallName(RTLIB::OLT_F32, "__hexagon_ltsf2");
2114 // Double-precision compares.
2115 setLibcallName(RTLIB::OGT_F64, "__hexagon_gtdf2");
2116 setLibcallName(RTLIB::OLT_F64, "__hexagon_ltdf2");
2117 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002118 }
2119
2120 // This is the only fast library function for sqrtd.
2121 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00002122 setLibcallName(RTLIB::SQRT_F64, "__hexagon_fast2_sqrtdf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002123
Benjamin Kramera37c8092015-04-25 14:46:46 +00002124 // Prefix is: nothing for "slow-math",
2125 // "fast2_" for V4 fast-math and V5+ fast-math double-precision
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002126 // (actually, keep fast-math and fast-math2 separate for now)
Benjamin Kramera37c8092015-04-25 14:46:46 +00002127 if (FastMath) {
2128 setLibcallName(RTLIB::ADD_F64, "__hexagon_fast_adddf3");
2129 setLibcallName(RTLIB::SUB_F64, "__hexagon_fast_subdf3");
2130 setLibcallName(RTLIB::MUL_F64, "__hexagon_fast_muldf3");
2131 setLibcallName(RTLIB::DIV_F64, "__hexagon_fast_divdf3");
2132 // Calling __hexagon_fast2_divsf3 with fast-math on V5 (ok).
2133 setLibcallName(RTLIB::DIV_F32, "__hexagon_fast_divsf3");
2134 } else {
2135 setLibcallName(RTLIB::ADD_F64, "__hexagon_adddf3");
2136 setLibcallName(RTLIB::SUB_F64, "__hexagon_subdf3");
2137 setLibcallName(RTLIB::MUL_F64, "__hexagon_muldf3");
2138 setLibcallName(RTLIB::DIV_F64, "__hexagon_divdf3");
2139 setLibcallName(RTLIB::DIV_F32, "__hexagon_divsf3");
2140 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002141
2142 if (Subtarget.hasV5TOps()) {
2143 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00002144 setLibcallName(RTLIB::SQRT_F32, "__hexagon_fast2_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002145 else
Benjamin Kramera37c8092015-04-25 14:46:46 +00002146 setLibcallName(RTLIB::SQRT_F32, "__hexagon_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002147 } else {
2148 // V4
Benjamin Kramera37c8092015-04-25 14:46:46 +00002149 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__hexagon_floatsisf");
2150 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__hexagon_floatsidf");
2151 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__hexagon_floatdisf");
2152 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__hexagon_floatdidf");
2153 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__hexagon_floatunsisf");
2154 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__hexagon_floatunsidf");
2155 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__hexagon_floatundisf");
2156 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__hexagon_floatundidf");
2157 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__hexagon_fixunssfsi");
2158 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__hexagon_fixunssfdi");
2159 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__hexagon_fixunsdfsi");
2160 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__hexagon_fixunsdfdi");
2161 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__hexagon_fixsfsi");
2162 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__hexagon_fixsfdi");
2163 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__hexagon_fixdfsi");
2164 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__hexagon_fixdfdi");
2165 setLibcallName(RTLIB::FPEXT_F32_F64, "__hexagon_extendsfdf2");
2166 setLibcallName(RTLIB::FPROUND_F64_F32, "__hexagon_truncdfsf2");
2167 setLibcallName(RTLIB::OEQ_F32, "__hexagon_eqsf2");
2168 setLibcallName(RTLIB::OEQ_F64, "__hexagon_eqdf2");
2169 setLibcallName(RTLIB::OGE_F32, "__hexagon_gesf2");
2170 setLibcallName(RTLIB::OGE_F64, "__hexagon_gedf2");
2171 setLibcallName(RTLIB::OLE_F32, "__hexagon_lesf2");
2172 setLibcallName(RTLIB::OLE_F64, "__hexagon_ledf2");
2173 setLibcallName(RTLIB::UNE_F32, "__hexagon_nesf2");
2174 setLibcallName(RTLIB::UNE_F64, "__hexagon_nedf2");
2175 setLibcallName(RTLIB::UO_F32, "__hexagon_unordsf2");
2176 setLibcallName(RTLIB::UO_F64, "__hexagon_unorddf2");
2177 setLibcallName(RTLIB::O_F32, "__hexagon_unordsf2");
2178 setLibcallName(RTLIB::O_F64, "__hexagon_unorddf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002179 }
2180
2181 // These cause problems when the shift amount is non-constant.
2182 setLibcallName(RTLIB::SHL_I128, nullptr);
2183 setLibcallName(RTLIB::SRL_I128, nullptr);
2184 setLibcallName(RTLIB::SRA_I128, nullptr);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002185}
2186
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002187
2188const char* HexagonTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00002189 switch ((HexagonISD::NodeType)Opcode) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002190 case HexagonISD::ALLOCA: return "HexagonISD::ALLOCA";
2191 case HexagonISD::ARGEXTEND: return "HexagonISD::ARGEXTEND";
2192 case HexagonISD::AT_GOT: return "HexagonISD::AT_GOT";
2193 case HexagonISD::AT_PCREL: return "HexagonISD::AT_PCREL";
2194 case HexagonISD::BARRIER: return "HexagonISD::BARRIER";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002195 case HexagonISD::CALLR: return "HexagonISD::CALLR";
2196 case HexagonISD::CALLv3nr: return "HexagonISD::CALLv3nr";
2197 case HexagonISD::CALLv3: return "HexagonISD::CALLv3";
2198 case HexagonISD::COMBINE: return "HexagonISD::COMBINE";
2199 case HexagonISD::CONST32_GP: return "HexagonISD::CONST32_GP";
2200 case HexagonISD::CONST32: return "HexagonISD::CONST32";
2201 case HexagonISD::CP: return "HexagonISD::CP";
2202 case HexagonISD::DCFETCH: return "HexagonISD::DCFETCH";
2203 case HexagonISD::EH_RETURN: return "HexagonISD::EH_RETURN";
2204 case HexagonISD::EXTRACTU: return "HexagonISD::EXTRACTU";
2205 case HexagonISD::EXTRACTURP: return "HexagonISD::EXTRACTURP";
2206 case HexagonISD::FCONST32: return "HexagonISD::FCONST32";
2207 case HexagonISD::INSERT: return "HexagonISD::INSERT";
2208 case HexagonISD::INSERTRP: return "HexagonISD::INSERTRP";
2209 case HexagonISD::JT: return "HexagonISD::JT";
2210 case HexagonISD::PACKHL: return "HexagonISD::PACKHL";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002211 case HexagonISD::POPCOUNT: return "HexagonISD::POPCOUNT";
2212 case HexagonISD::RET_FLAG: return "HexagonISD::RET_FLAG";
2213 case HexagonISD::SHUFFEB: return "HexagonISD::SHUFFEB";
2214 case HexagonISD::SHUFFEH: return "HexagonISD::SHUFFEH";
2215 case HexagonISD::SHUFFOB: return "HexagonISD::SHUFFOB";
2216 case HexagonISD::SHUFFOH: return "HexagonISD::SHUFFOH";
2217 case HexagonISD::TC_RETURN: return "HexagonISD::TC_RETURN";
2218 case HexagonISD::VCMPBEQ: return "HexagonISD::VCMPBEQ";
2219 case HexagonISD::VCMPBGT: return "HexagonISD::VCMPBGT";
2220 case HexagonISD::VCMPBGTU: return "HexagonISD::VCMPBGTU";
2221 case HexagonISD::VCMPHEQ: return "HexagonISD::VCMPHEQ";
2222 case HexagonISD::VCMPHGT: return "HexagonISD::VCMPHGT";
2223 case HexagonISD::VCMPHGTU: return "HexagonISD::VCMPHGTU";
2224 case HexagonISD::VCMPWEQ: return "HexagonISD::VCMPWEQ";
2225 case HexagonISD::VCMPWGT: return "HexagonISD::VCMPWGT";
2226 case HexagonISD::VCMPWGTU: return "HexagonISD::VCMPWGTU";
Krzysztof Parzyszekc168c012015-12-03 16:47:20 +00002227 case HexagonISD::VCOMBINE: return "HexagonISD::VCOMBINE";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002228 case HexagonISD::VSHLH: return "HexagonISD::VSHLH";
2229 case HexagonISD::VSHLW: return "HexagonISD::VSHLW";
2230 case HexagonISD::VSPLATB: return "HexagonISD::VSPLTB";
2231 case HexagonISD::VSPLATH: return "HexagonISD::VSPLATH";
2232 case HexagonISD::VSRAH: return "HexagonISD::VSRAH";
2233 case HexagonISD::VSRAW: return "HexagonISD::VSRAW";
2234 case HexagonISD::VSRLH: return "HexagonISD::VSRLH";
2235 case HexagonISD::VSRLW: return "HexagonISD::VSRLW";
2236 case HexagonISD::VSXTBH: return "HexagonISD::VSXTBH";
2237 case HexagonISD::VSXTBW: return "HexagonISD::VSXTBW";
Matthias Braund04893f2015-05-07 21:33:59 +00002238 case HexagonISD::OP_END: break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002239 }
Matthias Braund04893f2015-05-07 21:33:59 +00002240 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002241}
2242
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002243bool HexagonTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002244 EVT MTy1 = EVT::getEVT(Ty1);
2245 EVT MTy2 = EVT::getEVT(Ty2);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002246 if (!MTy1.isSimple() || !MTy2.isSimple())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002247 return false;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002248 return (MTy1.getSimpleVT() == MVT::i64) && (MTy2.getSimpleVT() == MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002249}
2250
2251bool HexagonTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002252 if (!VT1.isSimple() || !VT2.isSimple())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002253 return false;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002254 return (VT1.getSimpleVT() == MVT::i64) && (VT2.getSimpleVT() == MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002255}
2256
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002257// shouldExpandBuildVectorWithShuffles
2258// Should we expand the build vector with shuffles?
2259bool
2260HexagonTargetLowering::shouldExpandBuildVectorWithShuffles(EVT VT,
2261 unsigned DefinedValues) const {
2262
2263 // Hexagon vector shuffle operates on element sizes of bytes or halfwords
2264 EVT EltVT = VT.getVectorElementType();
2265 int EltBits = EltVT.getSizeInBits();
2266 if ((EltBits != 8) && (EltBits != 16))
2267 return false;
2268
2269 return TargetLowering::shouldExpandBuildVectorWithShuffles(VT, DefinedValues);
2270}
2271
2272// LowerVECTOR_SHUFFLE - Lower a vector shuffle (V1, V2, V3). V1 and
2273// V2 are the two vectors to select data from, V3 is the permutation.
2274static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
2275 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
2276 SDValue V1 = Op.getOperand(0);
2277 SDValue V2 = Op.getOperand(1);
2278 SDLoc dl(Op);
2279 EVT VT = Op.getValueType();
2280
2281 if (V2.getOpcode() == ISD::UNDEF)
2282 V2 = V1;
2283
2284 if (SVN->isSplat()) {
2285 int Lane = SVN->getSplatIndex();
2286 if (Lane == -1) Lane = 0;
2287
2288 // Test if V1 is a SCALAR_TO_VECTOR.
2289 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR)
2290 return createSplat(DAG, dl, VT, V1.getOperand(0));
2291
2292 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
2293 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
2294 // reaches it).
2295 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
2296 !isa<ConstantSDNode>(V1.getOperand(0))) {
2297 bool IsScalarToVector = true;
2298 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
2299 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
2300 IsScalarToVector = false;
2301 break;
2302 }
2303 if (IsScalarToVector)
2304 return createSplat(DAG, dl, VT, V1.getOperand(0));
2305 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002306 return createSplat(DAG, dl, VT, DAG.getConstant(Lane, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002307 }
2308
2309 // FIXME: We need to support more general vector shuffles. See
2310 // below the comment from the ARM backend that deals in the general
2311 // case with the vector shuffles. For now, let expand handle these.
2312 return SDValue();
2313
2314 // If the shuffle is not directly supported and it has 4 elements, use
2315 // the PerfectShuffle-generated table to synthesize it from other shuffles.
2316}
2317
2318// If BUILD_VECTOR has same base element repeated several times,
2319// report true.
2320static bool isCommonSplatElement(BuildVectorSDNode *BVN) {
2321 unsigned NElts = BVN->getNumOperands();
2322 SDValue V0 = BVN->getOperand(0);
2323
2324 for (unsigned i = 1, e = NElts; i != e; ++i) {
2325 if (BVN->getOperand(i) != V0)
2326 return false;
2327 }
2328 return true;
2329}
2330
2331// LowerVECTOR_SHIFT - Lower a vector shift. Try to convert
2332// <VT> = SHL/SRA/SRL <VT> by <VT> to Hexagon specific
2333// <VT> = SHL/SRA/SRL <VT> by <IT/i32>.
2334static SDValue LowerVECTOR_SHIFT(SDValue Op, SelectionDAG &DAG) {
2335 BuildVectorSDNode *BVN = 0;
2336 SDValue V1 = Op.getOperand(0);
2337 SDValue V2 = Op.getOperand(1);
2338 SDValue V3;
2339 SDLoc dl(Op);
2340 EVT VT = Op.getValueType();
2341
2342 if ((BVN = dyn_cast<BuildVectorSDNode>(V1.getNode())) &&
2343 isCommonSplatElement(BVN))
2344 V3 = V2;
2345 else if ((BVN = dyn_cast<BuildVectorSDNode>(V2.getNode())) &&
2346 isCommonSplatElement(BVN))
2347 V3 = V1;
2348 else
2349 return SDValue();
2350
2351 SDValue CommonSplat = BVN->getOperand(0);
2352 SDValue Result;
2353
2354 if (VT.getSimpleVT() == MVT::v4i16) {
2355 switch (Op.getOpcode()) {
2356 case ISD::SRA:
2357 Result = DAG.getNode(HexagonISD::VSRAH, dl, VT, V3, CommonSplat);
2358 break;
2359 case ISD::SHL:
2360 Result = DAG.getNode(HexagonISD::VSHLH, dl, VT, V3, CommonSplat);
2361 break;
2362 case ISD::SRL:
2363 Result = DAG.getNode(HexagonISD::VSRLH, dl, VT, V3, CommonSplat);
2364 break;
2365 default:
2366 return SDValue();
2367 }
2368 } else if (VT.getSimpleVT() == MVT::v2i32) {
2369 switch (Op.getOpcode()) {
2370 case ISD::SRA:
2371 Result = DAG.getNode(HexagonISD::VSRAW, dl, VT, V3, CommonSplat);
2372 break;
2373 case ISD::SHL:
2374 Result = DAG.getNode(HexagonISD::VSHLW, dl, VT, V3, CommonSplat);
2375 break;
2376 case ISD::SRL:
2377 Result = DAG.getNode(HexagonISD::VSRLW, dl, VT, V3, CommonSplat);
2378 break;
2379 default:
2380 return SDValue();
2381 }
2382 } else {
2383 return SDValue();
2384 }
2385
2386 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
2387}
2388
2389SDValue
2390HexagonTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
2391 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
2392 SDLoc dl(Op);
2393 EVT VT = Op.getValueType();
2394
2395 unsigned Size = VT.getSizeInBits();
2396
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002397 // Only handle vectors of 64 bits or shorter.
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002398 if (Size > 64)
2399 return SDValue();
2400
2401 APInt APSplatBits, APSplatUndef;
2402 unsigned SplatBitSize;
2403 bool HasAnyUndefs;
2404 unsigned NElts = BVN->getNumOperands();
2405
2406 // Try to generate a SPLAT instruction.
2407 if ((VT.getSimpleVT() == MVT::v4i8 || VT.getSimpleVT() == MVT::v4i16) &&
2408 (BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
2409 HasAnyUndefs, 0, true) && SplatBitSize <= 16)) {
2410 unsigned SplatBits = APSplatBits.getZExtValue();
2411 int32_t SextVal = ((int32_t) (SplatBits << (32 - SplatBitSize)) >>
2412 (32 - SplatBitSize));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002413 return createSplat(DAG, dl, VT, DAG.getConstant(SextVal, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002414 }
2415
2416 // Try to generate COMBINE to build v2i32 vectors.
2417 if (VT.getSimpleVT() == MVT::v2i32) {
2418 SDValue V0 = BVN->getOperand(0);
2419 SDValue V1 = BVN->getOperand(1);
2420
2421 if (V0.getOpcode() == ISD::UNDEF)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002422 V0 = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002423 if (V1.getOpcode() == ISD::UNDEF)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002424 V1 = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002425
2426 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(V0);
2427 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(V1);
2428 // If the element isn't a constant, it is in a register:
2429 // generate a COMBINE Register Register instruction.
2430 if (!C0 || !C1)
2431 return DAG.getNode(HexagonISD::COMBINE, dl, VT, V1, V0);
2432
2433 // If one of the operands is an 8 bit integer constant, generate
2434 // a COMBINE Immediate Immediate instruction.
2435 if (isInt<8>(C0->getSExtValue()) ||
2436 isInt<8>(C1->getSExtValue()))
2437 return DAG.getNode(HexagonISD::COMBINE, dl, VT, V1, V0);
2438 }
2439
2440 // Try to generate a S2_packhl to build v2i16 vectors.
2441 if (VT.getSimpleVT() == MVT::v2i16) {
2442 for (unsigned i = 0, e = NElts; i != e; ++i) {
2443 if (BVN->getOperand(i).getOpcode() == ISD::UNDEF)
2444 continue;
2445 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(BVN->getOperand(i));
2446 // If the element isn't a constant, it is in a register:
2447 // generate a S2_packhl instruction.
2448 if (!Cst) {
2449 SDValue pack = DAG.getNode(HexagonISD::PACKHL, dl, MVT::v4i16,
2450 BVN->getOperand(1), BVN->getOperand(0));
2451
2452 return DAG.getTargetExtractSubreg(Hexagon::subreg_loreg, dl, MVT::v2i16,
2453 pack);
2454 }
2455 }
2456 }
2457
2458 // In the general case, generate a CONST32 or a CONST64 for constant vectors,
2459 // and insert_vector_elt for all the other cases.
2460 uint64_t Res = 0;
2461 unsigned EltSize = Size / NElts;
2462 SDValue ConstVal;
2463 uint64_t Mask = ~uint64_t(0ULL) >> (64 - EltSize);
2464 bool HasNonConstantElements = false;
2465
2466 for (unsigned i = 0, e = NElts; i != e; ++i) {
2467 // LLVM's BUILD_VECTOR operands are in Little Endian mode, whereas Hexagon's
2468 // combine, const64, etc. are Big Endian.
2469 unsigned OpIdx = NElts - i - 1;
2470 SDValue Operand = BVN->getOperand(OpIdx);
2471 if (Operand.getOpcode() == ISD::UNDEF)
2472 continue;
2473
2474 int64_t Val = 0;
2475 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Operand))
2476 Val = Cst->getSExtValue();
2477 else
2478 HasNonConstantElements = true;
2479
2480 Val &= Mask;
2481 Res = (Res << EltSize) | Val;
2482 }
2483
2484 if (Size == 64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002485 ConstVal = DAG.getConstant(Res, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002486 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002487 ConstVal = DAG.getConstant(Res, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002488
2489 // When there are non constant operands, add them with INSERT_VECTOR_ELT to
2490 // ConstVal, the constant part of the vector.
2491 if (HasNonConstantElements) {
2492 EVT EltVT = VT.getVectorElementType();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002493 SDValue Width = DAG.getConstant(EltVT.getSizeInBits(), dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002494 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002495 DAG.getConstant(32, dl, MVT::i64));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002496
2497 for (unsigned i = 0, e = NElts; i != e; ++i) {
2498 // LLVM's BUILD_VECTOR operands are in Little Endian mode, whereas Hexagon
2499 // is Big Endian.
2500 unsigned OpIdx = NElts - i - 1;
2501 SDValue Operand = BVN->getOperand(OpIdx);
Benjamin Kramer619c4e52015-04-10 11:24:51 +00002502 if (isa<ConstantSDNode>(Operand))
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002503 // This operand is already in ConstVal.
2504 continue;
2505
2506 if (VT.getSizeInBits() == 64 &&
2507 Operand.getValueType().getSizeInBits() == 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002508 SDValue C = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002509 Operand = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, Operand);
2510 }
2511
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002512 SDValue Idx = DAG.getConstant(OpIdx, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002513 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i64, Idx, Width);
2514 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2515 const SDValue Ops[] = {ConstVal, Operand, Combined};
2516
2517 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002518 ConstVal = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002519 else
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002520 ConstVal = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002521 }
2522 }
2523
2524 return DAG.getNode(ISD::BITCAST, dl, VT, ConstVal);
2525}
2526
2527SDValue
2528HexagonTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
2529 SelectionDAG &DAG) const {
2530 SDLoc dl(Op);
Krzysztof Parzyszekc168c012015-12-03 16:47:20 +00002531 bool UseHVX = Subtarget.useHVXOps();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002532 EVT VT = Op.getValueType();
2533 unsigned NElts = Op.getNumOperands();
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002534 SDValue Vec0 = Op.getOperand(0);
2535 EVT VecVT = Vec0.getValueType();
2536 unsigned Width = VecVT.getSizeInBits();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002537
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002538 if (NElts == 2) {
2539 MVT ST = VecVT.getSimpleVT();
2540 // We are trying to concat two v2i16 to a single v4i16, or two v4i8
2541 // into a single v8i8.
2542 if (ST == MVT::v2i16 || ST == MVT::v4i8)
2543 return DAG.getNode(HexagonISD::COMBINE, dl, VT, Op.getOperand(1), Vec0);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002544
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002545 if (UseHVX) {
2546 assert((Width == 64*8 && Subtarget.useHVXSglOps()) ||
2547 (Width == 128*8 && Subtarget.useHVXDblOps()));
2548 SDValue Vec1 = Op.getOperand(1);
2549 MVT OpTy = Subtarget.useHVXSglOps() ? MVT::v16i32 : MVT::v32i32;
2550 MVT ReTy = Subtarget.useHVXSglOps() ? MVT::v32i32 : MVT::v64i32;
2551 SDValue B0 = DAG.getNode(ISD::BITCAST, dl, OpTy, Vec0);
2552 SDValue B1 = DAG.getNode(ISD::BITCAST, dl, OpTy, Vec1);
2553 SDValue VC = DAG.getNode(HexagonISD::VCOMBINE, dl, ReTy, B1, B0);
2554 return DAG.getNode(ISD::BITCAST, dl, VT, VC);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002555 }
2556 }
2557
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002558 if (VT.getSizeInBits() != 32 && VT.getSizeInBits() != 64)
2559 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002560
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002561 SDValue C0 = DAG.getConstant(0, dl, MVT::i64);
2562 SDValue C32 = DAG.getConstant(32, dl, MVT::i64);
2563 SDValue W = DAG.getConstant(Width, dl, MVT::i64);
2564 // Create the "width" part of the argument to insert_rp/insertp_rp.
2565 SDValue S = DAG.getNode(ISD::SHL, dl, MVT::i64, W, C32);
2566 SDValue V = C0;
2567
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002568 for (unsigned i = 0, e = NElts; i != e; ++i) {
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002569 unsigned N = NElts-i-1;
2570 SDValue OpN = Op.getOperand(N);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002571
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002572 if (VT.getSizeInBits() == 64 && OpN.getValueType().getSizeInBits() == 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002573 SDValue C = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002574 OpN = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, OpN);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002575 }
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002576 SDValue Idx = DAG.getConstant(N, dl, MVT::i64);
2577 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i64, Idx, W);
2578 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, S, Offset);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002579 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002580 V = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, {V, OpN, Or});
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002581 else
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002582 V = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, {V, OpN, Or});
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002583 }
2584
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002585 return DAG.getNode(ISD::BITCAST, dl, VT, V);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002586}
2587
2588SDValue
2589HexagonTargetLowering::LowerEXTRACT_VECTOR(SDValue Op,
2590 SelectionDAG &DAG) const {
2591 EVT VT = Op.getValueType();
2592 int VTN = VT.isVector() ? VT.getVectorNumElements() : 1;
2593 SDLoc dl(Op);
2594 SDValue Idx = Op.getOperand(1);
2595 SDValue Vec = Op.getOperand(0);
2596 EVT VecVT = Vec.getValueType();
2597 EVT EltVT = VecVT.getVectorElementType();
2598 int EltSize = EltVT.getSizeInBits();
2599 SDValue Width = DAG.getConstant(Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT ?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002600 EltSize : VTN * EltSize, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002601
2602 // Constant element number.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002603 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Idx)) {
2604 uint64_t X = CI->getZExtValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002605 SDValue Offset = DAG.getConstant(X * EltSize, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002606 const SDValue Ops[] = {Vec, Width, Offset};
2607
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002608 ConstantSDNode *CW = dyn_cast<ConstantSDNode>(Width);
2609 assert(CW && "Non constant width in LowerEXTRACT_VECTOR");
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002610
2611 SDValue N;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002612 MVT SVT = VecVT.getSimpleVT();
2613 uint64_t W = CW->getZExtValue();
2614
2615 if (W == 32) {
2616 // Translate this node into EXTRACT_SUBREG.
2617 unsigned Subreg = (X == 0) ? Hexagon::subreg_loreg : 0;
2618
2619 if (X == 0)
2620 Subreg = Hexagon::subreg_loreg;
2621 else if (SVT == MVT::v2i32 && X == 1)
2622 Subreg = Hexagon::subreg_hireg;
2623 else if (SVT == MVT::v4i16 && X == 2)
2624 Subreg = Hexagon::subreg_hireg;
2625 else if (SVT == MVT::v8i8 && X == 4)
2626 Subreg = Hexagon::subreg_hireg;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002627 else
2628 llvm_unreachable("Bad offset");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002629 N = DAG.getTargetExtractSubreg(Subreg, dl, MVT::i32, Vec);
2630
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002631 } else if (VecVT.getSizeInBits() == 32) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002632 N = DAG.getNode(HexagonISD::EXTRACTU, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002633 } else {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002634 N = DAG.getNode(HexagonISD::EXTRACTU, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002635 if (VT.getSizeInBits() == 32)
2636 N = DAG.getTargetExtractSubreg(Hexagon::subreg_loreg, dl, MVT::i32, N);
2637 }
2638
2639 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2640 }
2641
2642 // Variable element number.
2643 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i32, Idx,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002644 DAG.getConstant(EltSize, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002645 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002646 DAG.getConstant(32, dl, MVT::i64));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002647 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2648
2649 const SDValue Ops[] = {Vec, Combined};
2650
2651 SDValue N;
2652 if (VecVT.getSizeInBits() == 32) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002653 N = DAG.getNode(HexagonISD::EXTRACTURP, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002654 } else {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002655 N = DAG.getNode(HexagonISD::EXTRACTURP, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002656 if (VT.getSizeInBits() == 32)
2657 N = DAG.getTargetExtractSubreg(Hexagon::subreg_loreg, dl, MVT::i32, N);
2658 }
2659 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2660}
2661
2662SDValue
2663HexagonTargetLowering::LowerINSERT_VECTOR(SDValue Op,
2664 SelectionDAG &DAG) const {
2665 EVT VT = Op.getValueType();
2666 int VTN = VT.isVector() ? VT.getVectorNumElements() : 1;
2667 SDLoc dl(Op);
2668 SDValue Vec = Op.getOperand(0);
2669 SDValue Val = Op.getOperand(1);
2670 SDValue Idx = Op.getOperand(2);
2671 EVT VecVT = Vec.getValueType();
2672 EVT EltVT = VecVT.getVectorElementType();
2673 int EltSize = EltVT.getSizeInBits();
2674 SDValue Width = DAG.getConstant(Op.getOpcode() == ISD::INSERT_VECTOR_ELT ?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002675 EltSize : VTN * EltSize, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002676
2677 if (ConstantSDNode *C = cast<ConstantSDNode>(Idx)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002678 SDValue Offset = DAG.getConstant(C->getSExtValue() * EltSize, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002679 const SDValue Ops[] = {Vec, Val, Width, Offset};
2680
2681 SDValue N;
2682 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002683 N = DAG.getNode(HexagonISD::INSERT, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002684 else
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002685 N = DAG.getNode(HexagonISD::INSERT, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002686
2687 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2688 }
2689
2690 // Variable element number.
2691 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i32, Idx,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002692 DAG.getConstant(EltSize, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002693 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002694 DAG.getConstant(32, dl, MVT::i64));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002695 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2696
2697 if (VT.getSizeInBits() == 64 &&
2698 Val.getValueType().getSizeInBits() == 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002699 SDValue C = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002700 Val = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, Val);
2701 }
2702
2703 const SDValue Ops[] = {Vec, Val, Combined};
2704
2705 SDValue N;
2706 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002707 N = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002708 else
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002709 N = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002710
2711 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2712}
2713
Tim Northovera4415852013-08-06 09:12:35 +00002714bool
2715HexagonTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
2716 // Assuming the caller does not have either a signext or zeroext modifier, and
2717 // only one value is accepted, any reasonable truncation is allowed.
2718 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
2719 return false;
2720
2721 // FIXME: in principle up to 64-bit could be made safe, but it would be very
2722 // fragile at the moment: any support for multiple value returns would be
2723 // liable to disallow tail calls involving i64 -> iN truncation in many cases.
2724 return Ty1->getPrimitiveSizeInBits() <= 32;
2725}
2726
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002727SDValue
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002728HexagonTargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
2729 SDValue Chain = Op.getOperand(0);
2730 SDValue Offset = Op.getOperand(1);
2731 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002732 SDLoc dl(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002733 auto PtrVT = getPointerTy(DAG.getDataLayout());
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002734
2735 // Mark function as containing a call to EH_RETURN.
2736 HexagonMachineFunctionInfo *FuncInfo =
2737 DAG.getMachineFunction().getInfo<HexagonMachineFunctionInfo>();
2738 FuncInfo->setHasEHReturn();
2739
2740 unsigned OffsetReg = Hexagon::R28;
2741
Mehdi Amini44ede332015-07-09 02:09:04 +00002742 SDValue StoreAddr =
2743 DAG.getNode(ISD::ADD, dl, PtrVT, DAG.getRegister(Hexagon::R30, PtrVT),
2744 DAG.getIntPtrConstant(4, dl));
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002745 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
2746 false, false, 0);
2747 Chain = DAG.getCopyToReg(Chain, dl, OffsetReg, Offset);
2748
2749 // Not needed we already use it as explict input to EH_RETURN.
2750 // MF.getRegInfo().addLiveOut(OffsetReg);
2751
2752 return DAG.getNode(HexagonISD::EH_RETURN, dl, MVT::Other, Chain);
2753}
2754
2755SDValue
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002756HexagonTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002757 unsigned Opc = Op.getOpcode();
2758 switch (Opc) {
2759 default:
2760#ifndef NDEBUG
2761 Op.getNode()->dumpr(&DAG);
2762 if (Opc > HexagonISD::OP_BEGIN && Opc < HexagonISD::OP_END)
2763 errs() << "Check for a non-legal type in this operation\n";
2764#endif
2765 llvm_unreachable("Should not custom lower this!");
2766 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
2767 case ISD::INSERT_SUBVECTOR: return LowerINSERT_VECTOR(Op, DAG);
2768 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR(Op, DAG);
2769 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_VECTOR(Op, DAG);
2770 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR(Op, DAG);
2771 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2772 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002773 case ISD::SRA:
2774 case ISD::SHL:
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002775 case ISD::SRL: return LowerVECTOR_SHIFT(Op, DAG);
2776 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002777 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002778 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
2779 // Frame & Return address. Currently unimplemented.
2780 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
2781 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00002782 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002783 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
2784 case ISD::GlobalAddress: return LowerGLOBALADDRESS(Op, DAG);
2785 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002786 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002787 case ISD::VASTART: return LowerVASTART(Op, DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002788 // Custom lower some vector loads.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002789 case ISD::LOAD: return LowerLOAD(Op, DAG);
2790 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
2791 case ISD::SETCC: return LowerSETCC(Op, DAG);
2792 case ISD::VSELECT: return LowerVSELECT(Op, DAG);
2793 case ISD::CTPOP: return LowerCTPOP(Op, DAG);
2794 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00002795 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002796 case ISD::INLINEASM: return LowerINLINEASM(Op, DAG);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00002797 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002798 }
2799}
2800
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002801/// Returns relocation base for the given PIC jumptable.
2802SDValue
2803HexagonTargetLowering::getPICJumpTableRelocBase(SDValue Table,
2804 SelectionDAG &DAG) const {
2805 int Idx = cast<JumpTableSDNode>(Table)->getIndex();
2806 EVT VT = Table.getValueType();
2807 SDValue T = DAG.getTargetJumpTable(Idx, VT, HexagonII::MO_PCREL);
2808 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Table), VT, T);
2809}
2810
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002811MachineBasicBlock *
2812HexagonTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
2813 MachineBasicBlock *BB)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00002814 const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002815 switch (MI->getOpcode()) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00002816 case Hexagon::ALLOCA: {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002817 MachineFunction *MF = BB->getParent();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00002818 auto *FuncInfo = MF->getInfo<HexagonMachineFunctionInfo>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002819 FuncInfo->addAllocaAdjustInst(MI);
2820 return BB;
2821 }
Craig Toppere55c5562012-02-07 02:50:20 +00002822 default: llvm_unreachable("Unexpected instr type to insert");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002823 } // switch
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002824}
2825
2826//===----------------------------------------------------------------------===//
2827// Inline Assembly Support
2828//===----------------------------------------------------------------------===//
2829
Eric Christopher11e4df72015-02-26 22:38:43 +00002830std::pair<unsigned, const TargetRegisterClass *>
2831HexagonTargetLowering::getRegForInlineAsmConstraint(
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00002832 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002833 bool UseHVX = Subtarget.useHVXOps(), UseHVXDbl = Subtarget.useHVXDblOps();
2834
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002835 if (Constraint.size() == 1) {
2836 switch (Constraint[0]) {
2837 case 'r': // R0-R31
Chad Rosier295bd432013-06-22 18:37:38 +00002838 switch (VT.SimpleTy) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002839 default:
Craig Toppere55c5562012-02-07 02:50:20 +00002840 llvm_unreachable("getRegForInlineAsmConstraint Unhandled data type");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002841 case MVT::i32:
2842 case MVT::i16:
2843 case MVT::i8:
Sirish Pande69295b82012-05-10 20:20:25 +00002844 case MVT::f32:
Craig Topperc7242e02012-04-20 07:30:17 +00002845 return std::make_pair(0U, &Hexagon::IntRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002846 case MVT::i64:
Sirish Pande69295b82012-05-10 20:20:25 +00002847 case MVT::f64:
Craig Topperc7242e02012-04-20 07:30:17 +00002848 return std::make_pair(0U, &Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002849 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002850 case 'q': // q0-q3
2851 switch (VT.SimpleTy) {
2852 default:
2853 llvm_unreachable("getRegForInlineAsmConstraint Unhandled data type");
2854 case MVT::v1024i1:
2855 case MVT::v512i1:
2856 case MVT::v32i16:
2857 case MVT::v16i32:
2858 case MVT::v64i8:
2859 case MVT::v8i64:
2860 return std::make_pair(0U, &Hexagon::VecPredRegsRegClass);
2861 }
2862 case 'v': // V0-V31
2863 switch (VT.SimpleTy) {
2864 default:
2865 llvm_unreachable("getRegForInlineAsmConstraint Unhandled data type");
2866 case MVT::v16i32:
2867 case MVT::v32i16:
2868 case MVT::v64i8:
2869 case MVT::v8i64:
2870 return std::make_pair(0U, &Hexagon::VectorRegsRegClass);
2871 case MVT::v32i32:
2872 case MVT::v64i16:
2873 case MVT::v16i64:
2874 case MVT::v128i8:
2875 if (Subtarget.hasV60TOps() && UseHVX && UseHVXDbl)
2876 return std::make_pair(0U, &Hexagon::VectorRegs128BRegClass);
2877 else
2878 return std::make_pair(0U, &Hexagon::VecDblRegsRegClass);
2879 case MVT::v256i8:
2880 case MVT::v128i16:
2881 case MVT::v64i32:
2882 case MVT::v32i64:
2883 return std::make_pair(0U, &Hexagon::VecDblRegs128BRegClass);
2884 }
2885
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002886 default:
Craig Toppere55c5562012-02-07 02:50:20 +00002887 llvm_unreachable("Unknown asm register class");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002888 }
2889 }
2890
Eric Christopher11e4df72015-02-26 22:38:43 +00002891 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002892}
2893
Sirish Pande69295b82012-05-10 20:20:25 +00002894/// isFPImmLegal - Returns true if the target can instruction select the
2895/// specified FP immediate natively. If false, the legalizer will
2896/// materialize the FP immediate as a load from a constant pool.
2897bool HexagonTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002898 return Subtarget.hasV5TOps();
Sirish Pande69295b82012-05-10 20:20:25 +00002899}
2900
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002901/// isLegalAddressingMode - Return true if the addressing mode represented by
2902/// AM is legal for this target, for a load/store of the specified type.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +00002903bool HexagonTargetLowering::isLegalAddressingMode(const DataLayout &DL,
2904 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +00002905 unsigned AS) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002906 // Allows a signed-extended 11-bit immediate field.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002907 if (AM.BaseOffs <= -(1LL << 13) || AM.BaseOffs >= (1LL << 13)-1)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002908 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002909
2910 // No global is ever allowed as a base.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002911 if (AM.BaseGV)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002912 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002913
2914 int Scale = AM.Scale;
2915 if (Scale < 0) Scale = -Scale;
2916 switch (Scale) {
2917 case 0: // No scale reg, "r+i", "r", or just "i".
2918 break;
2919 default: // No scaled addressing mode.
2920 return false;
2921 }
2922 return true;
2923}
2924
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002925/// Return true if folding a constant offset with the given GlobalAddress is
2926/// legal. It is frequently not legal in PIC relocation models.
2927bool HexagonTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA)
2928 const {
2929 return HTM.getRelocationModel() == Reloc::Static;
2930}
2931
2932
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002933/// isLegalICmpImmediate - Return true if the specified immediate is legal
2934/// icmp immediate, that is the target has icmp instructions which can compare
2935/// a register against the immediate without having to materialize the
2936/// immediate into a register.
2937bool HexagonTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
2938 return Imm >= -512 && Imm <= 511;
2939}
2940
2941/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2942/// for tail call optimization. Targets which want to do tail call
2943/// optimization should implement this function.
2944bool HexagonTargetLowering::IsEligibleForTailCallOptimization(
2945 SDValue Callee,
2946 CallingConv::ID CalleeCC,
2947 bool isVarArg,
2948 bool isCalleeStructRet,
2949 bool isCallerStructRet,
2950 const SmallVectorImpl<ISD::OutputArg> &Outs,
2951 const SmallVectorImpl<SDValue> &OutVals,
2952 const SmallVectorImpl<ISD::InputArg> &Ins,
2953 SelectionDAG& DAG) const {
2954 const Function *CallerF = DAG.getMachineFunction().getFunction();
2955 CallingConv::ID CallerCC = CallerF->getCallingConv();
2956 bool CCMatch = CallerCC == CalleeCC;
2957
2958 // ***************************************************************************
2959 // Look for obvious safe cases to perform tail call optimization that do not
2960 // require ABI changes.
2961 // ***************************************************************************
2962
2963 // If this is a tail call via a function pointer, then don't do it!
Craig Topper66059c92015-11-18 07:07:59 +00002964 if (!(isa<GlobalAddressSDNode>(Callee)) &&
2965 !(isa<ExternalSymbolSDNode>(Callee))) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002966 return false;
2967 }
2968
2969 // Do not optimize if the calling conventions do not match.
2970 if (!CCMatch)
2971 return false;
2972
2973 // Do not tail call optimize vararg calls.
2974 if (isVarArg)
2975 return false;
2976
2977 // Also avoid tail call optimization if either caller or callee uses struct
2978 // return semantics.
2979 if (isCalleeStructRet || isCallerStructRet)
2980 return false;
2981
2982 // In addition to the cases above, we also disable Tail Call Optimization if
2983 // the calling convention code that at least one outgoing argument needs to
2984 // go on the stack. We cannot check that here because at this point that
2985 // information is not available.
2986 return true;
2987}
Colin LeMahieu025f8602014-12-08 21:19:18 +00002988
2989// Return true when the given node fits in a positive half word.
2990bool llvm::isPositiveHalfWord(SDNode *N) {
2991 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N);
2992 if (CN && CN->getSExtValue() > 0 && isInt<16>(CN->getSExtValue()))
2993 return true;
2994
2995 switch (N->getOpcode()) {
2996 default:
2997 return false;
2998 case ISD::SIGN_EXTEND_INREG:
2999 return true;
3000 }
3001}
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003002
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003003std::pair<const TargetRegisterClass*, uint8_t>
3004HexagonTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
3005 MVT VT) const {
3006 const TargetRegisterClass *RRC = nullptr;
3007
3008 uint8_t Cost = 1;
3009 switch (VT.SimpleTy) {
3010 default:
3011 return TargetLowering::findRepresentativeClass(TRI, VT);
3012 case MVT::v64i8:
3013 case MVT::v32i16:
3014 case MVT::v16i32:
3015 case MVT::v8i64:
3016 RRC = &Hexagon::VectorRegsRegClass;
3017 break;
3018 case MVT::v128i8:
3019 case MVT::v64i16:
3020 case MVT::v32i32:
3021 case MVT::v16i64:
3022 if (Subtarget.hasV60TOps() && Subtarget.useHVXOps() &&
3023 Subtarget.useHVXDblOps())
3024 RRC = &Hexagon::VectorRegs128BRegClass;
3025 else
3026 RRC = &Hexagon::VecDblRegsRegClass;
3027 break;
3028 case MVT::v256i8:
3029 case MVT::v128i16:
3030 case MVT::v64i32:
3031 case MVT::v32i64:
3032 RRC = &Hexagon::VecDblRegs128BRegClass;
3033 break;
3034 }
3035 return std::make_pair(RRC, Cost);
3036}
3037
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003038Value *HexagonTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
3039 AtomicOrdering Ord) const {
3040 BasicBlock *BB = Builder.GetInsertBlock();
3041 Module *M = BB->getParent()->getParent();
3042 Type *Ty = cast<PointerType>(Addr->getType())->getElementType();
3043 unsigned SZ = Ty->getPrimitiveSizeInBits();
3044 assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic loads supported");
3045 Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_L2_loadw_locked
3046 : Intrinsic::hexagon_L4_loadd_locked;
3047 Value *Fn = Intrinsic::getDeclaration(M, IntID);
3048 return Builder.CreateCall(Fn, Addr, "larx");
3049}
3050
3051/// Perform a store-conditional operation to Addr. Return the status of the
3052/// store. This should be 0 if the store succeeded, non-zero otherwise.
3053Value *HexagonTargetLowering::emitStoreConditional(IRBuilder<> &Builder,
3054 Value *Val, Value *Addr, AtomicOrdering Ord) const {
3055 BasicBlock *BB = Builder.GetInsertBlock();
3056 Module *M = BB->getParent()->getParent();
3057 Type *Ty = Val->getType();
3058 unsigned SZ = Ty->getPrimitiveSizeInBits();
3059 assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic stores supported");
3060 Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_S2_storew_locked
3061 : Intrinsic::hexagon_S4_stored_locked;
3062 Value *Fn = Intrinsic::getDeclaration(M, IntID);
3063 Value *Call = Builder.CreateCall(Fn, {Addr, Val}, "stcx");
3064 Value *Cmp = Builder.CreateICmpEQ(Call, Builder.getInt32(0), "");
3065 Value *Ext = Builder.CreateZExt(Cmp, Type::getInt32Ty(M->getContext()));
3066 return Ext;
3067}
3068
Ahmed Bougacha52468672015-09-11 17:08:28 +00003069TargetLowering::AtomicExpansionKind
3070HexagonTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003071 // Do not expand loads and stores that don't exceed 64 bits.
Ahmed Bougacha52468672015-09-11 17:08:28 +00003072 return LI->getType()->getPrimitiveSizeInBits() > 64
Tim Northoverf520eff2015-12-02 18:12:57 +00003073 ? AtomicExpansionKind::LLOnly
Ahmed Bougacha52468672015-09-11 17:08:28 +00003074 : AtomicExpansionKind::None;
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003075}
3076
3077bool HexagonTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
3078 // Do not expand loads and stores that don't exceed 64 bits.
3079 return SI->getValueOperand()->getType()->getPrimitiveSizeInBits() > 64;
3080}