blob: 09e40d454bad0271f6da8eac5c9a2a7e66c6a18b [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
Tony Linthicum1213a7a2011-12-12 21:14:40 +000044static cl::opt<bool>
45EmitJumpTables("hexagon-emit-jump-tables", 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
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000508SDValue
509HexagonTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG)
510const {
511 return SDValue();
512}
513
514/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
515/// by "Src" to address "Dst" of size "Size". Alignment information is
516/// specified by the specific parameter attribute. The copy will be passed as
517/// a byval function parameter. Sometimes what we are copying is the end of a
518/// larger object, the part that does not fit in registers.
519static SDValue
520CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
521 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000522 SDLoc dl) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000523
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000524 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000525 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
526 /*isVolatile=*/false, /*AlwaysInline=*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +0000527 /*isTailCall=*/false,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000528 MachinePointerInfo(), MachinePointerInfo());
529}
530
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000531static bool IsHvxVectorType(MVT ty) {
532 return (ty == MVT::v8i64 || ty == MVT::v16i32 || ty == MVT::v32i16 ||
533 ty == MVT::v64i8 ||
534 ty == MVT::v16i64 || ty == MVT::v32i32 || ty == MVT::v64i16 ||
535 ty == MVT::v128i8 ||
536 ty == MVT::v32i64 || ty == MVT::v64i32 || ty == MVT::v128i16 ||
537 ty == MVT::v256i8 ||
538 ty == MVT::v512i1 || ty == MVT::v1024i1);
539}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000540
541// LowerReturn - Lower ISD::RET. If a struct is larger than 8 bytes and is
542// passed by value, the function prototype is modified to return void and
543// the value is stored in memory pointed by a pointer passed by caller.
544SDValue
545HexagonTargetLowering::LowerReturn(SDValue Chain,
546 CallingConv::ID CallConv, bool isVarArg,
547 const SmallVectorImpl<ISD::OutputArg> &Outs,
548 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000549 SDLoc dl, SelectionDAG &DAG) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000550
551 // CCValAssign - represent the assignment of the return value to locations.
552 SmallVector<CCValAssign, 16> RVLocs;
553
554 // CCState - Info about the registers and stack slot.
Eric Christopherb5217502014-08-06 18:45:26 +0000555 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
556 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000557
558 // Analyze return values of ISD::RET
559 CCInfo.AnalyzeReturn(Outs, RetCC_Hexagon);
560
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000561 SDValue Flag;
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000562 SmallVector<SDValue, 4> RetOps(1, Chain);
563
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000564 // Copy the result values into the output registers.
565 for (unsigned i = 0; i != RVLocs.size(); ++i) {
566 CCValAssign &VA = RVLocs[i];
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000567
568 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), OutVals[i], Flag);
569
570 // Guarantee that all emitted copies are stuck together with flags.
571 Flag = Chain.getValue(1);
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000572 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000573 }
574
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000575 RetOps[0] = Chain; // Update chain.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000576
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000577 // Add the flag if we have it.
578 if (Flag.getNode())
579 RetOps.push_back(Flag);
580
Craig Topper48d114b2014-04-26 18:35:24 +0000581 return DAG.getNode(HexagonISD::RET_FLAG, dl, MVT::Other, RetOps);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000582}
583
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000584bool HexagonTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
585 // If either no tail call or told not to tail call at all, don't.
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000586 auto Attr =
587 CI->getParent()->getParent()->getFnAttribute("disable-tail-calls");
588 if (!CI->isTailCall() || Attr.getValueAsString() == "true")
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000589 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000590
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000591 return true;
592}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000593
594/// LowerCallResult - Lower the result values of an ISD::CALL into the
595/// appropriate copies out of appropriate physical registers. This assumes that
596/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
597/// being lowered. Returns a SDNode with the same number of values as the
598/// ISD::CALL.
599SDValue
600HexagonTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
601 CallingConv::ID CallConv, bool isVarArg,
602 const
603 SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000604 SDLoc dl, SelectionDAG &DAG,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000605 SmallVectorImpl<SDValue> &InVals,
606 const SmallVectorImpl<SDValue> &OutVals,
607 SDValue Callee) const {
608
609 // Assign locations to each value returned by this call.
610 SmallVector<CCValAssign, 16> RVLocs;
611
Eric Christopherb5217502014-08-06 18:45:26 +0000612 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
613 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000614
615 CCInfo.AnalyzeCallResult(Ins, RetCC_Hexagon);
616
617 // Copy all of the result registers out of their specified physreg.
618 for (unsigned i = 0; i != RVLocs.size(); ++i) {
619 Chain = DAG.getCopyFromReg(Chain, dl,
620 RVLocs[i].getLocReg(),
621 RVLocs[i].getValVT(), InFlag).getValue(1);
622 InFlag = Chain.getValue(2);
623 InVals.push_back(Chain.getValue(0));
624 }
625
626 return Chain;
627}
628
629/// LowerCall - Functions arguments are copied from virtual regs to
630/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
631SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +0000632HexagonTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000633 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +0000634 SelectionDAG &DAG = CLI.DAG;
Craig Topperb94011f2013-07-14 04:42:23 +0000635 SDLoc &dl = CLI.DL;
636 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
637 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
638 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000639 SDValue Chain = CLI.Chain;
640 SDValue Callee = CLI.Callee;
641 bool &isTailCall = CLI.IsTailCall;
642 CallingConv::ID CallConv = CLI.CallConv;
643 bool isVarArg = CLI.IsVarArg;
Colin LeMahieu2e3a26d2015-01-16 17:05:27 +0000644 bool doesNotReturn = CLI.DoesNotReturn;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000645
646 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000647 MachineFunction &MF = DAG.getMachineFunction();
Mehdi Amini44ede332015-07-09 02:09:04 +0000648 auto PtrVT = getPointerTy(MF.getDataLayout());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000649
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000650 // Check for varargs.
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000651 int NumNamedVarArgParams = -1;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000652 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Callee))
653 {
Craig Topper062a2ba2014-04-25 05:30:21 +0000654 const Function* CalleeFn = nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000655 Callee = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, MVT::i32);
656 if ((CalleeFn = dyn_cast<Function>(GA->getGlobal())))
657 {
658 // If a function has zero args and is a vararg function, that's
659 // disallowed so it must be an undeclared function. Do not assume
660 // varargs if the callee is undefined.
661 if (CalleeFn->isVarArg() &&
662 CalleeFn->getFunctionType()->getNumParams() != 0) {
663 NumNamedVarArgParams = CalleeFn->getFunctionType()->getNumParams();
664 }
665 }
666 }
667
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000668 // Analyze operands of the call, assigning locations to each operand.
669 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000670 HexagonCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
671 *DAG.getContext(), NumNamedVarArgParams);
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000672
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000673 if (isVarArg)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000674 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon_VarArg);
675 else
676 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon);
677
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000678 auto Attr = MF.getFunction()->getFnAttribute("disable-tail-calls");
679 if (Attr.getValueAsString() == "true")
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000680 isTailCall = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000681
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000682 if (isTailCall) {
683 bool StructAttrFlag = MF.getFunction()->hasStructRetAttr();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000684 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
685 isVarArg, IsStructRet,
686 StructAttrFlag,
687 Outs, OutVals, Ins, DAG);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000688 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000689 CCValAssign &VA = ArgLocs[i];
690 if (VA.isMemLoc()) {
691 isTailCall = false;
692 break;
693 }
694 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000695 DEBUG(dbgs() << (isTailCall ? "Eligible for Tail Call\n"
696 : "Argument must be passed on stack. "
697 "Not eligible for Tail Call\n"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000698 }
699 // Get a count of how many bytes are to be pushed on the stack.
700 unsigned NumBytes = CCInfo.getNextStackOffset();
701 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
702 SmallVector<SDValue, 8> MemOpChains;
703
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000704 auto &HRI = *Subtarget.getRegisterInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +0000705 SDValue StackPtr =
706 DAG.getCopyFromReg(Chain, dl, HRI.getStackRegister(), PtrVT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000707
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000708 bool NeedsArgAlign = false;
709 unsigned LargestAlignSeen = 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000710 // Walk the register/memloc assignments, inserting copies/loads.
711 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
712 CCValAssign &VA = ArgLocs[i];
713 SDValue Arg = OutVals[i];
714 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000715 // Record if we need > 8 byte alignment on an argument.
716 bool ArgAlign = IsHvxVectorType(VA.getValVT());
717 NeedsArgAlign |= ArgAlign;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000718
719 // Promote the value if needed.
720 switch (VA.getLocInfo()) {
721 default:
722 // Loc info must be one of Full, SExt, ZExt, or AExt.
Craig Toppere55c5562012-02-07 02:50:20 +0000723 llvm_unreachable("Unknown loc info!");
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000724 case CCValAssign::BCvt:
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000725 case CCValAssign::Full:
726 break;
727 case CCValAssign::SExt:
728 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
729 break;
730 case CCValAssign::ZExt:
731 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
732 break;
733 case CCValAssign::AExt:
734 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
735 break;
736 }
737
738 if (VA.isMemLoc()) {
739 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000740 SDValue MemAddr = DAG.getConstant(LocMemOffset, dl,
741 StackPtr.getValueType());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000742 MemAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, MemAddr);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000743 if (ArgAlign)
744 LargestAlignSeen = std::max(LargestAlignSeen,
745 VA.getLocVT().getStoreSizeInBits() >> 3);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000746 if (Flags.isByVal()) {
747 // The argument is a struct passed by value. According to LLVM, "Arg"
748 // is is pointer.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000749 MemOpChains.push_back(CreateCopyOfByValArgument(Arg, MemAddr, Chain,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000750 Flags, DAG, dl));
751 } else {
Alex Lorenze40c8a22015-08-11 23:09:45 +0000752 MachinePointerInfo LocPI = MachinePointerInfo::getStack(
753 DAG.getMachineFunction(), LocMemOffset);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000754 SDValue S = DAG.getStore(Chain, dl, Arg, MemAddr, LocPI, false,
755 false, 0);
756 MemOpChains.push_back(S);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000757 }
758 continue;
759 }
760
761 // Arguments that can be passed on register must be kept at RegsToPass
762 // vector.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000763 if (VA.isRegLoc())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000764 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000765 }
766
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000767 if (NeedsArgAlign && Subtarget.hasV60TOps()) {
768 DEBUG(dbgs() << "Function needs byte stack align due to call args\n");
769 MachineFrameInfo* MFI = DAG.getMachineFunction().getFrameInfo();
770 // V6 vectors passed by value have 64 or 128 byte alignment depending
771 // on whether we are 64 byte vector mode or 128 byte.
772 bool UseHVXDbl = Subtarget.useHVXDblOps();
773 assert(Subtarget.useHVXOps());
774 const unsigned ObjAlign = UseHVXDbl ? 128 : 64;
775 LargestAlignSeen = std::max(LargestAlignSeen, ObjAlign);
776 MFI->ensureMaxAlignment(LargestAlignSeen);
777 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000778 // Transform all store nodes into one single node because all store
779 // nodes are independent of each other.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000780 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000781 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000782
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000783 if (!isTailCall) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000784 SDValue C = DAG.getConstant(NumBytes, dl, PtrVT, true);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000785 Chain = DAG.getCALLSEQ_START(Chain, C, dl);
786 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000787
788 // Build a sequence of copy-to-reg nodes chained together with token
789 // chain and flag operands which copy the outgoing args into registers.
Benjamin Kramerbde91762012-06-02 10:20:22 +0000790 // The InFlag in necessary since all emitted instructions must be
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000791 // stuck together.
792 SDValue InFlag;
793 if (!isTailCall) {
794 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
795 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
796 RegsToPass[i].second, InFlag);
797 InFlag = Chain.getValue(1);
798 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000799 } else {
800 // For tail calls lower the arguments to the 'real' stack slot.
801 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000802 // Force all the incoming stack arguments to be loaded from the stack
803 // before any new outgoing arguments are stored to the stack, because the
804 // outgoing stack slots may alias the incoming argument stack slots, and
805 // the alias isn't otherwise explicit. This is slightly more conservative
806 // than necessary, because it means that each store effectively depends
807 // on every argument instead of just those arguments it would clobber.
808 //
Benjamin Kramerbde91762012-06-02 10:20:22 +0000809 // Do not flag preceding copytoreg stuff together with the following stuff.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000810 InFlag = SDValue();
811 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
812 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
813 RegsToPass[i].second, InFlag);
814 InFlag = Chain.getValue(1);
815 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000816 InFlag = SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000817 }
818
819 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
820 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
821 // node so that legalize doesn't hack it.
Tobias Edler von Kochb51460c2015-12-16 17:29:37 +0000822 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000823 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, PtrVT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000824 } else if (ExternalSymbolSDNode *S =
825 dyn_cast<ExternalSymbolSDNode>(Callee)) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000826 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), PtrVT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000827 }
828
829 // Returns a chain & a flag for retval copy to use.
830 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
831 SmallVector<SDValue, 8> Ops;
832 Ops.push_back(Chain);
833 Ops.push_back(Callee);
834
835 // Add argument registers to the end of the list so that they are
836 // known live into the call.
837 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
838 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
839 RegsToPass[i].second.getValueType()));
840 }
841
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000842 if (InFlag.getNode())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000843 Ops.push_back(InFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000844
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +0000845 if (isTailCall) {
846 MF.getFrameInfo()->setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +0000847 return DAG.getNode(HexagonISD::TC_RETURN, dl, NodeTys, Ops);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +0000848 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000849
Colin LeMahieu2e3a26d2015-01-16 17:05:27 +0000850 int OpCode = doesNotReturn ? HexagonISD::CALLv3nr : HexagonISD::CALLv3;
851 Chain = DAG.getNode(OpCode, dl, NodeTys, Ops);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000852 InFlag = Chain.getValue(1);
853
854 // Create the CALLSEQ_END node.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000855 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
856 DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000857 InFlag = Chain.getValue(1);
858
859 // Handle result values, copying them out of physregs into vregs that we
860 // return.
861 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
862 InVals, OutVals, Callee);
863}
864
865static bool getIndexedAddressParts(SDNode *Ptr, EVT VT,
866 bool isSEXTLoad, SDValue &Base,
867 SDValue &Offset, bool &isInc,
868 SelectionDAG &DAG) {
869 if (Ptr->getOpcode() != ISD::ADD)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000870 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000871
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000872 auto &HST = static_cast<const HexagonSubtarget&>(DAG.getSubtarget());
873 bool UseHVX = HST.useHVXOps();
874 bool UseHVXDbl = HST.useHVXDblOps();
875
876 bool ValidHVXDblType =
877 (UseHVX && UseHVXDbl) && (VT == MVT::v32i32 || VT == MVT::v16i64 ||
878 VT == MVT::v64i16 || VT == MVT::v128i8);
879 bool ValidHVXType =
880 UseHVX && !UseHVXDbl && (VT == MVT::v16i32 || VT == MVT::v8i64 ||
881 VT == MVT::v32i16 || VT == MVT::v64i8);
882
883 if (ValidHVXDblType || ValidHVXType ||
884 VT == MVT::i64 || VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000885 isInc = (Ptr->getOpcode() == ISD::ADD);
886 Base = Ptr->getOperand(0);
887 Offset = Ptr->getOperand(1);
888 // Ensure that Offset is a constant.
889 return (isa<ConstantSDNode>(Offset));
890 }
891
892 return false;
893}
894
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000895/// getPostIndexedAddressParts - returns true by value, base pointer and
896/// offset pointer and addressing mode by reference if this node can be
897/// combined with a load / store to form a post-indexed load / store.
898bool HexagonTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
899 SDValue &Base,
900 SDValue &Offset,
901 ISD::MemIndexedMode &AM,
902 SelectionDAG &DAG) const
903{
904 EVT VT;
905 SDValue Ptr;
906 bool isSEXTLoad = false;
907
908 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
909 VT = LD->getMemoryVT();
910 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
911 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
912 VT = ST->getMemoryVT();
913 if (ST->getValue().getValueType() == MVT::i64 && ST->isTruncatingStore()) {
914 return false;
915 }
916 } else {
917 return false;
918 }
919
Chad Rosier64dc8aa2012-01-06 20:11:59 +0000920 bool isInc = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000921 bool isLegal = getIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
922 isInc, DAG);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000923 if (isLegal) {
924 auto &HII = *Subtarget.getInstrInfo();
925 int32_t OffsetVal = cast<ConstantSDNode>(Offset.getNode())->getSExtValue();
926 if (HII.isValidAutoIncImm(VT, OffsetVal)) {
927 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
928 return true;
929 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000930 }
931
932 return false;
933}
934
935SDValue HexagonTargetLowering::LowerINLINEASM(SDValue Op,
936 SelectionDAG &DAG) const {
937 SDNode *Node = Op.getNode();
938 MachineFunction &MF = DAG.getMachineFunction();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000939 auto &FuncInfo = *MF.getInfo<HexagonMachineFunctionInfo>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000940 switch (Node->getOpcode()) {
941 case ISD::INLINEASM: {
942 unsigned NumOps = Node->getNumOperands();
943 if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue)
944 --NumOps; // Ignore the flag operand.
945
946 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000947 if (FuncInfo.hasClobberLR())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000948 break;
949 unsigned Flags =
950 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
951 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
952 ++i; // Skip the ID value.
953
954 switch (InlineAsm::getKind(Flags)) {
955 default: llvm_unreachable("Bad flags!");
956 case InlineAsm::Kind_RegDef:
957 case InlineAsm::Kind_RegUse:
958 case InlineAsm::Kind_Imm:
959 case InlineAsm::Kind_Clobber:
960 case InlineAsm::Kind_Mem: {
961 for (; NumVals; --NumVals, ++i) {}
962 break;
963 }
964 case InlineAsm::Kind_RegDefEarlyClobber: {
965 for (; NumVals; --NumVals, ++i) {
966 unsigned Reg =
967 cast<RegisterSDNode>(Node->getOperand(i))->getReg();
968
969 // Check it to be lr
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000970 const HexagonRegisterInfo *QRI = Subtarget.getRegisterInfo();
Eric Christopherdbe1cb02014-06-27 00:13:52 +0000971 if (Reg == QRI->getRARegister()) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000972 FuncInfo.setHasClobberLR(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000973 break;
974 }
975 }
976 break;
977 }
978 }
979 }
980 }
981 } // Node->getOpcode
982 return Op;
983}
984
985
986//
987// Taken from the XCore backend.
988//
989SDValue HexagonTargetLowering::
990LowerBR_JT(SDValue Op, SelectionDAG &DAG) const
991{
992 SDValue Chain = Op.getOperand(0);
993 SDValue Table = Op.getOperand(1);
994 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000995 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000996 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
997 unsigned JTI = JT->getIndex();
998 MachineFunction &MF = DAG.getMachineFunction();
999 const MachineJumpTableInfo *MJTI = MF.getJumpTableInfo();
1000 SDValue TargetJT = DAG.getTargetJumpTable(JT->getIndex(), MVT::i32);
1001
1002 // Mark all jump table targets as address taken.
1003 const std::vector<MachineJumpTableEntry> &JTE = MJTI->getJumpTables();
1004 const std::vector<MachineBasicBlock*> &JTBBs = JTE[JTI].MBBs;
1005 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
1006 MachineBasicBlock *MBB = JTBBs[i];
1007 MBB->setHasAddressTaken();
1008 // This line is needed to set the hasAddressTaken flag on the BasicBlock
1009 // object.
1010 BlockAddress::get(const_cast<BasicBlock *>(MBB->getBasicBlock()));
1011 }
1012
Mehdi Amini44ede332015-07-09 02:09:04 +00001013 SDValue JumpTableBase = DAG.getNode(
1014 HexagonISD::JT, dl, getPointerTy(DAG.getDataLayout()), TargetJT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001015 SDValue ShiftIndex = DAG.getNode(ISD::SHL, dl, MVT::i32, Index,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001016 DAG.getConstant(2, dl, MVT::i32));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001017 SDValue JTAddress = DAG.getNode(ISD::ADD, dl, MVT::i32, JumpTableBase,
1018 ShiftIndex);
1019 SDValue LoadTarget = DAG.getLoad(MVT::i32, dl, Chain, JTAddress,
1020 MachinePointerInfo(), false, false, false,
1021 0);
1022 return DAG.getNode(HexagonISD::BR_JT, dl, MVT::Other, Chain, LoadTarget);
1023}
1024
1025
1026SDValue
1027HexagonTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
1028 SelectionDAG &DAG) const {
1029 SDValue Chain = Op.getOperand(0);
1030 SDValue Size = Op.getOperand(1);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001031 SDValue Align = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001032 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001033
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001034 ConstantSDNode *AlignConst = dyn_cast<ConstantSDNode>(Align);
1035 assert(AlignConst && "Non-constant Align in LowerDYNAMIC_STACKALLOC");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001036
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001037 unsigned A = AlignConst->getSExtValue();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001038 auto &HFI = *Subtarget.getFrameLowering();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001039 // "Zero" means natural stack alignment.
1040 if (A == 0)
1041 A = HFI.getStackAlignment();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001042
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001043 DEBUG({
Krzysztof Parzyszek9ee04e42015-04-22 17:19:44 +00001044 dbgs () << LLVM_FUNCTION_NAME << " Align: " << A << " Size: ";
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001045 Size.getNode()->dump(&DAG);
1046 dbgs() << "\n";
1047 });
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001048
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001049 SDValue AC = DAG.getConstant(A, dl, MVT::i32);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001050 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +00001051 SDValue AA = DAG.getNode(HexagonISD::ALLOCA, dl, VTs, Chain, Size, AC);
1052 if (Op.getNode()->getHasDebugValue())
1053 DAG.TransferDbgValues(Op, AA);
1054 return AA;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001055}
1056
1057SDValue
1058HexagonTargetLowering::LowerFormalArguments(SDValue Chain,
1059 CallingConv::ID CallConv,
1060 bool isVarArg,
1061 const
1062 SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001063 SDLoc dl, SelectionDAG &DAG,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001064 SmallVectorImpl<SDValue> &InVals)
1065const {
1066
1067 MachineFunction &MF = DAG.getMachineFunction();
1068 MachineFrameInfo *MFI = MF.getFrameInfo();
1069 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001070 auto &FuncInfo = *MF.getInfo<HexagonMachineFunctionInfo>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001071
1072 // Assign locations to all of the incoming arguments.
1073 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001074 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1075 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001076
1077 CCInfo.AnalyzeFormalArguments(Ins, CC_Hexagon);
1078
1079 // For LLVM, in the case when returning a struct by value (>8byte),
1080 // the first argument is a pointer that points to the location on caller's
1081 // stack where the return value will be stored. For Hexagon, the location on
1082 // caller's stack is passed only when the struct size is smaller than (and
1083 // equal to) 8 bytes. If not, no address will be passed into callee and
1084 // callee return the result direclty through R0/R1.
1085
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001086 SmallVector<SDValue, 8> MemOps;
1087 bool UseHVX = Subtarget.useHVXOps(), UseHVXDbl = Subtarget.useHVXDblOps();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001088
1089 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1090 CCValAssign &VA = ArgLocs[i];
1091 ISD::ArgFlagsTy Flags = Ins[i].Flags;
1092 unsigned ObjSize;
1093 unsigned StackLocation;
1094 int FI;
1095
1096 if ( (VA.isRegLoc() && !Flags.isByVal())
1097 || (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() > 8)) {
1098 // Arguments passed in registers
1099 // 1. int, long long, ptr args that get allocated in register.
1100 // 2. Large struct that gets an register to put its address in.
1101 EVT RegVT = VA.getLocVT();
Sirish Pande69295b82012-05-10 20:20:25 +00001102 if (RegVT == MVT::i8 || RegVT == MVT::i16 ||
1103 RegVT == MVT::i32 || RegVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001104 unsigned VReg =
Craig Topperc7242e02012-04-20 07:30:17 +00001105 RegInfo.createVirtualRegister(&Hexagon::IntRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001106 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1107 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Colin LeMahieu4379d102015-01-28 22:08:16 +00001108 } else if (RegVT == MVT::i64 || RegVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001109 unsigned VReg =
Craig Topperc7242e02012-04-20 07:30:17 +00001110 RegInfo.createVirtualRegister(&Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001111 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1112 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001113
1114 // Single Vector
1115 } else if ((RegVT == MVT::v8i64 || RegVT == MVT::v16i32 ||
1116 RegVT == MVT::v32i16 || RegVT == MVT::v64i8)) {
1117 unsigned VReg =
1118 RegInfo.createVirtualRegister(&Hexagon::VectorRegsRegClass);
1119 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1120 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1121 } else if (UseHVX && UseHVXDbl &&
1122 ((RegVT == MVT::v16i64 || RegVT == MVT::v32i32 ||
1123 RegVT == MVT::v64i16 || RegVT == MVT::v128i8))) {
1124 unsigned VReg =
1125 RegInfo.createVirtualRegister(&Hexagon::VectorRegs128BRegClass);
1126 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1127 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1128
1129 // Double Vector
1130 } else if ((RegVT == MVT::v16i64 || RegVT == MVT::v32i32 ||
1131 RegVT == MVT::v64i16 || RegVT == MVT::v128i8)) {
1132 unsigned VReg =
1133 RegInfo.createVirtualRegister(&Hexagon::VecDblRegsRegClass);
1134 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1135 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1136 } else if (UseHVX && UseHVXDbl &&
1137 ((RegVT == MVT::v32i64 || RegVT == MVT::v64i32 ||
1138 RegVT == MVT::v128i16 || RegVT == MVT::v256i8))) {
1139 unsigned VReg =
1140 RegInfo.createVirtualRegister(&Hexagon::VecDblRegs128BRegClass);
1141 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1142 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1143 } else if (RegVT == MVT::v512i1 || RegVT == MVT::v1024i1) {
1144 assert(0 && "need to support VecPred regs");
1145 unsigned VReg =
1146 RegInfo.createVirtualRegister(&Hexagon::VecPredRegsRegClass);
1147 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1148 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001149 } else {
1150 assert (0);
1151 }
1152 } else if (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() <= 8) {
1153 assert (0 && "ByValSize must be bigger than 8 bytes");
1154 } else {
1155 // Sanity check.
1156 assert(VA.isMemLoc());
1157
1158 if (Flags.isByVal()) {
1159 // If it's a byval parameter, then we need to compute the
1160 // "real" size, not the size of the pointer.
1161 ObjSize = Flags.getByValSize();
1162 } else {
1163 ObjSize = VA.getLocVT().getStoreSizeInBits() >> 3;
1164 }
1165
1166 StackLocation = HEXAGON_LRFP_SIZE + VA.getLocMemOffset();
1167 // Create the frame index object for this incoming parameter...
1168 FI = MFI->CreateFixedObject(ObjSize, StackLocation, true);
1169
1170 // Create the SelectionDAG nodes cordl, responding to a load
1171 // from this parameter.
1172 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
1173
1174 if (Flags.isByVal()) {
1175 // If it's a pass-by-value aggregate, then do not dereference the stack
1176 // location. Instead, we should generate a reference to the stack
1177 // location.
1178 InVals.push_back(FIN);
1179 } else {
1180 InVals.push_back(DAG.getLoad(VA.getLocVT(), dl, Chain, FIN,
1181 MachinePointerInfo(), false, false,
1182 false, 0));
1183 }
1184 }
1185 }
1186
1187 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001188 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001189
1190 if (isVarArg) {
1191 // This will point to the next argument passed via stack.
1192 int FrameIndex = MFI->CreateFixedObject(Hexagon_PointerSize,
1193 HEXAGON_LRFP_SIZE +
1194 CCInfo.getNextStackOffset(),
1195 true);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001196 FuncInfo.setVarArgsFrameIndex(FrameIndex);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001197 }
1198
1199 return Chain;
1200}
1201
1202SDValue
1203HexagonTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
1204 // VASTART stores the address of the VarArgsFrameIndex slot into the
1205 // memory location argument.
1206 MachineFunction &MF = DAG.getMachineFunction();
1207 HexagonMachineFunctionInfo *QFI = MF.getInfo<HexagonMachineFunctionInfo>();
1208 SDValue Addr = DAG.getFrameIndex(QFI->getVarArgsFrameIndex(), MVT::i32);
1209 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001210 return DAG.getStore(Op.getOperand(0), SDLoc(Op), Addr,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001211 Op.getOperand(1), MachinePointerInfo(SV), false,
1212 false, 0);
1213}
1214
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001215// Creates a SPLAT instruction for a constant value VAL.
1216static SDValue createSplat(SelectionDAG &DAG, SDLoc dl, EVT VT, SDValue Val) {
1217 if (VT.getSimpleVT() == MVT::v4i8)
1218 return DAG.getNode(HexagonISD::VSPLATB, dl, VT, Val);
1219
1220 if (VT.getSimpleVT() == MVT::v4i16)
1221 return DAG.getNode(HexagonISD::VSPLATH, dl, VT, Val);
1222
1223 return SDValue();
1224}
1225
1226static bool isSExtFree(SDValue N) {
1227 // A sign-extend of a truncate of a sign-extend is free.
1228 if (N.getOpcode() == ISD::TRUNCATE &&
1229 N.getOperand(0).getOpcode() == ISD::AssertSext)
1230 return true;
1231 // We have sign-extended loads.
1232 if (N.getOpcode() == ISD::LOAD)
1233 return true;
1234 return false;
1235}
1236
1237SDValue HexagonTargetLowering::LowerCTPOP(SDValue Op, SelectionDAG &DAG) const {
1238 SDLoc dl(Op);
1239 SDValue InpVal = Op.getOperand(0);
1240 if (isa<ConstantSDNode>(InpVal)) {
1241 uint64_t V = cast<ConstantSDNode>(InpVal)->getZExtValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001242 return DAG.getTargetConstant(countPopulation(V), dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001243 }
1244 SDValue PopOut = DAG.getNode(HexagonISD::POPCOUNT, dl, MVT::i32, InpVal);
1245 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, PopOut);
1246}
1247
1248SDValue HexagonTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1249 SDLoc dl(Op);
1250
1251 SDValue LHS = Op.getOperand(0);
1252 SDValue RHS = Op.getOperand(1);
1253 SDValue Cmp = Op.getOperand(2);
1254 ISD::CondCode CC = cast<CondCodeSDNode>(Cmp)->get();
1255
1256 EVT VT = Op.getValueType();
1257 EVT LHSVT = LHS.getValueType();
1258 EVT RHSVT = RHS.getValueType();
1259
1260 if (LHSVT == MVT::v2i16) {
1261 assert(ISD::isSignedIntSetCC(CC) || ISD::isUnsignedIntSetCC(CC));
1262 unsigned ExtOpc = ISD::isSignedIntSetCC(CC) ? ISD::SIGN_EXTEND
1263 : ISD::ZERO_EXTEND;
1264 SDValue LX = DAG.getNode(ExtOpc, dl, MVT::v2i32, LHS);
1265 SDValue RX = DAG.getNode(ExtOpc, dl, MVT::v2i32, RHS);
1266 SDValue SC = DAG.getNode(ISD::SETCC, dl, MVT::v2i1, LX, RX, Cmp);
1267 return SC;
1268 }
1269
1270 // Treat all other vector types as legal.
1271 if (VT.isVector())
1272 return Op;
1273
1274 // Equals and not equals should use sign-extend, not zero-extend, since
1275 // we can represent small negative values in the compare instructions.
1276 // The LLVM default is to use zero-extend arbitrarily in these cases.
1277 if ((CC == ISD::SETEQ || CC == ISD::SETNE) &&
1278 (RHSVT == MVT::i8 || RHSVT == MVT::i16) &&
1279 (LHSVT == MVT::i8 || LHSVT == MVT::i16)) {
1280 ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS);
1281 if (C && C->getAPIntValue().isNegative()) {
1282 LHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, LHS);
1283 RHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, RHS);
1284 return DAG.getNode(ISD::SETCC, dl, Op.getValueType(),
1285 LHS, RHS, Op.getOperand(2));
1286 }
1287 if (isSExtFree(LHS) || isSExtFree(RHS)) {
1288 LHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, LHS);
1289 RHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, RHS);
1290 return DAG.getNode(ISD::SETCC, dl, Op.getValueType(),
1291 LHS, RHS, Op.getOperand(2));
1292 }
1293 }
1294 return SDValue();
1295}
1296
1297SDValue HexagonTargetLowering::LowerVSELECT(SDValue Op, SelectionDAG &DAG)
1298 const {
1299 SDValue PredOp = Op.getOperand(0);
1300 SDValue Op1 = Op.getOperand(1), Op2 = Op.getOperand(2);
1301 EVT OpVT = Op1.getValueType();
1302 SDLoc DL(Op);
1303
1304 if (OpVT == MVT::v2i16) {
1305 SDValue X1 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op1);
1306 SDValue X2 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op2);
1307 SDValue SL = DAG.getNode(ISD::VSELECT, DL, MVT::v2i32, PredOp, X1, X2);
1308 SDValue TR = DAG.getNode(ISD::TRUNCATE, DL, MVT::v2i16, SL);
1309 return TR;
1310 }
1311
1312 return SDValue();
1313}
1314
1315// Handle only specific vector loads.
1316SDValue HexagonTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1317 EVT VT = Op.getValueType();
1318 SDLoc DL(Op);
1319 LoadSDNode *LoadNode = cast<LoadSDNode>(Op);
1320 SDValue Chain = LoadNode->getChain();
1321 SDValue Ptr = Op.getOperand(1);
1322 SDValue LoweredLoad;
1323 SDValue Result;
1324 SDValue Base = LoadNode->getBasePtr();
1325 ISD::LoadExtType Ext = LoadNode->getExtensionType();
1326 unsigned Alignment = LoadNode->getAlignment();
1327 SDValue LoadChain;
1328
1329 if(Ext == ISD::NON_EXTLOAD)
1330 Ext = ISD::ZEXTLOAD;
1331
1332 if (VT == MVT::v4i16) {
1333 if (Alignment == 2) {
1334 SDValue Loads[4];
1335 // Base load.
1336 Loads[0] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Base,
1337 LoadNode->getPointerInfo(), MVT::i16,
1338 LoadNode->isVolatile(),
1339 LoadNode->isNonTemporal(),
1340 LoadNode->isInvariant(),
1341 Alignment);
1342 // Base+2 load.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001343 SDValue Increment = DAG.getConstant(2, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001344 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1345 Loads[1] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
1346 LoadNode->getPointerInfo(), MVT::i16,
1347 LoadNode->isVolatile(),
1348 LoadNode->isNonTemporal(),
1349 LoadNode->isInvariant(),
1350 Alignment);
1351 // SHL 16, then OR base and base+2.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001352 SDValue ShiftAmount = DAG.getConstant(16, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001353 SDValue Tmp1 = DAG.getNode(ISD::SHL, DL, MVT::i32, Loads[1], ShiftAmount);
1354 SDValue Tmp2 = DAG.getNode(ISD::OR, DL, MVT::i32, Tmp1, Loads[0]);
1355 // Base + 4.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001356 Increment = DAG.getConstant(4, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001357 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1358 Loads[2] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
1359 LoadNode->getPointerInfo(), MVT::i16,
1360 LoadNode->isVolatile(),
1361 LoadNode->isNonTemporal(),
1362 LoadNode->isInvariant(),
1363 Alignment);
1364 // Base + 6.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001365 Increment = DAG.getConstant(6, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001366 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1367 Loads[3] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
1368 LoadNode->getPointerInfo(), MVT::i16,
1369 LoadNode->isVolatile(),
1370 LoadNode->isNonTemporal(),
1371 LoadNode->isInvariant(),
1372 Alignment);
1373 // SHL 16, then OR base+4 and base+6.
1374 Tmp1 = DAG.getNode(ISD::SHL, DL, MVT::i32, Loads[3], ShiftAmount);
1375 SDValue Tmp4 = DAG.getNode(ISD::OR, DL, MVT::i32, Tmp1, Loads[2]);
1376 // Combine to i64. This could be optimised out later if we can
1377 // affect reg allocation of this code.
1378 Result = DAG.getNode(HexagonISD::COMBINE, DL, MVT::i64, Tmp4, Tmp2);
1379 LoadChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
1380 Loads[0].getValue(1), Loads[1].getValue(1),
1381 Loads[2].getValue(1), Loads[3].getValue(1));
1382 } else {
1383 // Perform default type expansion.
1384 Result = DAG.getLoad(MVT::i64, DL, Chain, Ptr, LoadNode->getPointerInfo(),
1385 LoadNode->isVolatile(), LoadNode->isNonTemporal(),
1386 LoadNode->isInvariant(), LoadNode->getAlignment());
1387 LoadChain = Result.getValue(1);
1388 }
1389 } else
1390 llvm_unreachable("Custom lowering unsupported load");
1391
1392 Result = DAG.getNode(ISD::BITCAST, DL, VT, Result);
1393 // Since we pretend to lower a load, we need the original chain
1394 // info attached to the result.
1395 SDValue Ops[] = { Result, LoadChain };
1396
1397 return DAG.getMergeValues(Ops, DL);
1398}
1399
1400
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001401SDValue
Sirish Pande69295b82012-05-10 20:20:25 +00001402HexagonTargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
1403 EVT ValTy = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001404 SDLoc dl(Op);
Sirish Pande69295b82012-05-10 20:20:25 +00001405 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
1406 SDValue Res;
1407 if (CP->isMachineConstantPoolEntry())
1408 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), ValTy,
1409 CP->getAlignment());
1410 else
1411 Res = DAG.getTargetConstantPool(CP->getConstVal(), ValTy,
1412 CP->getAlignment());
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001413 return DAG.getNode(HexagonISD::CP, dl, ValTy, Res);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001414}
1415
1416SDValue
1417HexagonTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001418 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001419 MachineFunction &MF = DAG.getMachineFunction();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001420 MachineFrameInfo &MFI = *MF.getFrameInfo();
1421 MFI.setReturnAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001422
Bill Wendling908bf812014-01-06 00:43:20 +00001423 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001424 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001425
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001426 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001427 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001428 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1429 if (Depth) {
1430 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001431 SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001432 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
1433 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
1434 MachinePointerInfo(), false, false, false, 0);
1435 }
1436
1437 // Return LR, which contains the return address. Mark it an implicit live-in.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001438 unsigned Reg = MF.addLiveIn(HRI.getRARegister(), getRegClassFor(MVT::i32));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001439 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
1440}
1441
1442SDValue
1443HexagonTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001444 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
1445 MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
1446 MFI.setFrameAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001447
1448 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001449 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001450 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1451 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001452 HRI.getFrameRegister(), VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001453 while (Depth--)
1454 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
1455 MachinePointerInfo(),
1456 false, false, false, 0);
1457 return FrameAddr;
1458}
1459
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001460SDValue HexagonTargetLowering::LowerATOMIC_FENCE(SDValue Op,
1461 SelectionDAG& DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001462 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001463 return DAG.getNode(HexagonISD::BARRIER, dl, MVT::Other, Op.getOperand(0));
1464}
1465
1466
1467SDValue HexagonTargetLowering::LowerGLOBALADDRESS(SDValue Op,
1468 SelectionDAG &DAG) const {
1469 SDValue Result;
1470 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1471 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001472 SDLoc dl(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00001473 auto PtrVT = getPointerTy(DAG.getDataLayout());
1474 Result = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001475
Eric Christopher36fe0282015-02-03 07:22:52 +00001476 const HexagonTargetObjectFile *TLOF =
1477 static_cast<const HexagonTargetObjectFile *>(
1478 getTargetMachine().getObjFileLowering());
1479 if (TLOF->IsGlobalInSmallSection(GV, getTargetMachine())) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001480 return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, Result);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001481 }
1482
Mehdi Amini44ede332015-07-09 02:09:04 +00001483 return DAG.getNode(HexagonISD::CONST32, dl, PtrVT, Result);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001484}
1485
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001486// Specifies that for loads and stores VT can be promoted to PromotedLdStVT.
1487void HexagonTargetLowering::promoteLdStType(EVT VT, EVT PromotedLdStVT) {
1488 if (VT != PromotedLdStVT) {
1489 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
1490 AddPromotedToType(ISD::LOAD, VT.getSimpleVT(),
1491 PromotedLdStVT.getSimpleVT());
1492
1493 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
1494 AddPromotedToType(ISD::STORE, VT.getSimpleVT(),
1495 PromotedLdStVT.getSimpleVT());
1496 }
1497}
1498
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001499SDValue
1500HexagonTargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
1501 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1502 SDValue BA_SD = DAG.getTargetBlockAddress(BA, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001503 SDLoc dl(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00001504 return DAG.getNode(HexagonISD::CONST32_GP, dl,
1505 getPointerTy(DAG.getDataLayout()), BA_SD);
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001506}
1507
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001508//===----------------------------------------------------------------------===//
1509// TargetLowering Implementation
1510//===----------------------------------------------------------------------===//
1511
Eric Christopherd737b762015-02-02 22:11:36 +00001512HexagonTargetLowering::HexagonTargetLowering(const TargetMachine &TM,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001513 const HexagonSubtarget &ST)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001514 : TargetLowering(TM), HTM(static_cast<const HexagonTargetMachine&>(TM)),
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001515 Subtarget(ST) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001516 bool IsV4 = !Subtarget.hasV5TOps();
1517 auto &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00001518 bool UseHVX = Subtarget.useHVXOps();
1519 bool UseHVXSgl = Subtarget.useHVXSglOps();
1520 bool UseHVXDbl = Subtarget.useHVXDblOps();
Sirish Pande69295b82012-05-10 20:20:25 +00001521
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001522 setPrefLoopAlignment(4);
1523 setPrefFunctionAlignment(4);
1524 setMinFunctionAlignment(2);
1525 setInsertFencesForAtomic(false);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001526 setStackPointerRegisterToSaveRestore(HRI.getStackRegister());
1527
1528 if (EnableHexSDNodeSched)
1529 setSchedulingPreference(Sched::VLIW);
1530 else
1531 setSchedulingPreference(Sched::Source);
1532
1533 // Limits for inline expansion of memcpy/memmove
1534 MaxStoresPerMemcpy = MaxStoresPerMemcpyCL;
1535 MaxStoresPerMemcpyOptSize = MaxStoresPerMemcpyOptSizeCL;
1536 MaxStoresPerMemmove = MaxStoresPerMemmoveCL;
1537 MaxStoresPerMemmoveOptSize = MaxStoresPerMemmoveOptSizeCL;
1538 MaxStoresPerMemset = MaxStoresPerMemsetCL;
1539 MaxStoresPerMemsetOptSize = MaxStoresPerMemsetOptSizeCL;
1540
1541 //
1542 // Set up register classes.
1543 //
1544
1545 addRegisterClass(MVT::i1, &Hexagon::PredRegsRegClass);
1546 addRegisterClass(MVT::v2i1, &Hexagon::PredRegsRegClass); // bbbbaaaa
1547 addRegisterClass(MVT::v4i1, &Hexagon::PredRegsRegClass); // ddccbbaa
1548 addRegisterClass(MVT::v8i1, &Hexagon::PredRegsRegClass); // hgfedcba
1549 addRegisterClass(MVT::i32, &Hexagon::IntRegsRegClass);
1550 addRegisterClass(MVT::v4i8, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001551 addRegisterClass(MVT::v2i16, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001552 addRegisterClass(MVT::i64, &Hexagon::DoubleRegsRegClass);
1553 addRegisterClass(MVT::v8i8, &Hexagon::DoubleRegsRegClass);
1554 addRegisterClass(MVT::v4i16, &Hexagon::DoubleRegsRegClass);
1555 addRegisterClass(MVT::v2i32, &Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001556
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001557 if (Subtarget.hasV5TOps()) {
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001558 addRegisterClass(MVT::f32, &Hexagon::IntRegsRegClass);
1559 addRegisterClass(MVT::f64, &Hexagon::DoubleRegsRegClass);
1560 }
Sirish Pande69295b82012-05-10 20:20:25 +00001561
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001562 if (Subtarget.hasV60TOps()) {
1563 if (Subtarget.useHVXSglOps()) {
1564 addRegisterClass(MVT::v64i8, &Hexagon::VectorRegsRegClass);
1565 addRegisterClass(MVT::v32i16, &Hexagon::VectorRegsRegClass);
1566 addRegisterClass(MVT::v16i32, &Hexagon::VectorRegsRegClass);
1567 addRegisterClass(MVT::v8i64, &Hexagon::VectorRegsRegClass);
1568 addRegisterClass(MVT::v128i8, &Hexagon::VecDblRegsRegClass);
1569 addRegisterClass(MVT::v64i16, &Hexagon::VecDblRegsRegClass);
1570 addRegisterClass(MVT::v32i32, &Hexagon::VecDblRegsRegClass);
1571 addRegisterClass(MVT::v16i64, &Hexagon::VecDblRegsRegClass);
1572 addRegisterClass(MVT::v512i1, &Hexagon::VecPredRegsRegClass);
1573 } else if (Subtarget.useHVXDblOps()) {
1574 addRegisterClass(MVT::v128i8, &Hexagon::VectorRegs128BRegClass);
1575 addRegisterClass(MVT::v64i16, &Hexagon::VectorRegs128BRegClass);
1576 addRegisterClass(MVT::v32i32, &Hexagon::VectorRegs128BRegClass);
1577 addRegisterClass(MVT::v16i64, &Hexagon::VectorRegs128BRegClass);
1578 addRegisterClass(MVT::v256i8, &Hexagon::VecDblRegs128BRegClass);
1579 addRegisterClass(MVT::v128i16, &Hexagon::VecDblRegs128BRegClass);
1580 addRegisterClass(MVT::v64i32, &Hexagon::VecDblRegs128BRegClass);
1581 addRegisterClass(MVT::v32i64, &Hexagon::VecDblRegs128BRegClass);
1582 addRegisterClass(MVT::v1024i1, &Hexagon::VecPredRegs128BRegClass);
1583 }
1584
1585 }
1586
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001587 //
1588 // Handling of scalar operations.
1589 //
1590 // All operations default to "legal", except:
1591 // - indexed loads and stores (pre-/post-incremented),
1592 // - ANY_EXTEND_VECTOR_INREG, ATOMIC_CMP_SWAP_WITH_SUCCESS, CONCAT_VECTORS,
1593 // ConstantFP, DEBUGTRAP, FCEIL, FCOPYSIGN, FEXP, FEXP2, FFLOOR, FGETSIGN,
1594 // FLOG, FLOG2, FLOG10, FMAXNUM, FMINNUM, FNEARBYINT, FRINT, FROUND, TRAP,
1595 // FTRUNC, PREFETCH, SIGN_EXTEND_VECTOR_INREG, ZERO_EXTEND_VECTOR_INREG,
1596 // which default to "expand" for at least one type.
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001597
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001598 // Misc operations.
1599 setOperationAction(ISD::ConstantFP, MVT::f32, Legal); // Default: expand
1600 setOperationAction(ISD::ConstantFP, MVT::f64, Legal); // Default: expand
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001601
1602 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001603 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001604 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
1605 setOperationAction(ISD::INLINEASM, MVT::Other, Custom);
1606 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
1607 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001608
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001609 // Custom legalize GlobalAddress nodes into CONST32.
1610 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001611 setOperationAction(ISD::GlobalAddress, MVT::i8, Custom);
1612 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001613
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001614 // Hexagon needs to optimize cases with negative constants.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001615 setOperationAction(ISD::SETCC, MVT::i8, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001616 setOperationAction(ISD::SETCC, MVT::i16, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001617
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001618 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
1619 setOperationAction(ISD::VASTART, MVT::Other, Custom);
1620 setOperationAction(ISD::VAEND, MVT::Other, Expand);
1621 setOperationAction(ISD::VAARG, MVT::Other, Expand);
1622
1623 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
1624 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
1625 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
1626
1627 if (EmitJumpTables)
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001628 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001629 else
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001630 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001631 // Increase jump tables cutover to 5, was 4.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001632 setMinimumJumpTableEntries(MinimumJumpTables);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001633
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001634 // Hexagon has instructions for add/sub with carry. The problem with
1635 // modeling these instructions is that they produce 2 results: Rdd and Px.
1636 // To model the update of Px, we will have to use Defs[p0..p3] which will
1637 // cause any predicate live range to spill. So, we pretend we dont't have
1638 // these instructions.
1639 setOperationAction(ISD::ADDE, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001640 setOperationAction(ISD::ADDE, MVT::i16, Expand);
1641 setOperationAction(ISD::ADDE, MVT::i32, Expand);
1642 setOperationAction(ISD::ADDE, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001643 setOperationAction(ISD::SUBE, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001644 setOperationAction(ISD::SUBE, MVT::i16, Expand);
1645 setOperationAction(ISD::SUBE, MVT::i32, Expand);
1646 setOperationAction(ISD::SUBE, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001647 setOperationAction(ISD::ADDC, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001648 setOperationAction(ISD::ADDC, MVT::i16, Expand);
1649 setOperationAction(ISD::ADDC, MVT::i32, Expand);
1650 setOperationAction(ISD::ADDC, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001651 setOperationAction(ISD::SUBC, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001652 setOperationAction(ISD::SUBC, MVT::i16, Expand);
1653 setOperationAction(ISD::SUBC, MVT::i32, Expand);
1654 setOperationAction(ISD::SUBC, MVT::i64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001655
Krzysztof Parzyszek2c4487d2015-04-13 20:37:01 +00001656 // Only add and sub that detect overflow are the saturating ones.
1657 for (MVT VT : MVT::integer_valuetypes()) {
1658 setOperationAction(ISD::UADDO, VT, Expand);
1659 setOperationAction(ISD::SADDO, VT, Expand);
1660 setOperationAction(ISD::USUBO, VT, Expand);
1661 setOperationAction(ISD::SSUBO, VT, Expand);
1662 }
1663
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001664 setOperationAction(ISD::CTLZ, MVT::i8, Promote);
1665 setOperationAction(ISD::CTLZ, MVT::i16, Promote);
1666 setOperationAction(ISD::CTTZ, MVT::i8, Promote);
1667 setOperationAction(ISD::CTTZ, MVT::i16, Promote);
1668 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8, Promote);
1669 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16, Promote);
1670 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i8, Promote);
1671 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16, Promote);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001672
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001673 // In V5, popcount can count # of 1s in i64 but returns i32.
1674 // On V4 it will be expanded (set later).
1675 setOperationAction(ISD::CTPOP, MVT::i8, Promote);
1676 setOperationAction(ISD::CTPOP, MVT::i16, Promote);
1677 setOperationAction(ISD::CTPOP, MVT::i32, Promote);
1678 setOperationAction(ISD::CTPOP, MVT::i64, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001679
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001680 // We custom lower i64 to i64 mul, so that it is not considered as a legal
1681 // operation. There is a pattern that will match i64 mul and transform it
1682 // to a series of instructions.
1683 setOperationAction(ISD::MUL, MVT::i64, Expand);
Colin LeMahieude68b662015-02-05 21:13:25 +00001684 setOperationAction(ISD::MULHS, MVT::i64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001685
Benjamin Kramer62460692015-04-25 14:46:53 +00001686 for (unsigned IntExpOp :
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001687 { ISD::SDIV, ISD::UDIV, ISD::SREM, ISD::UREM,
1688 ISD::SDIVREM, ISD::UDIVREM, ISD::ROTL, ISD::ROTR,
1689 ISD::BSWAP, ISD::SHL_PARTS, ISD::SRA_PARTS, ISD::SRL_PARTS,
1690 ISD::SMUL_LOHI, ISD::UMUL_LOHI }) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001691 setOperationAction(IntExpOp, MVT::i32, Expand);
1692 setOperationAction(IntExpOp, MVT::i64, Expand);
1693 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001694
Benjamin Kramer62460692015-04-25 14:46:53 +00001695 for (unsigned FPExpOp :
1696 {ISD::FDIV, ISD::FREM, ISD::FSQRT, ISD::FSIN, ISD::FCOS, ISD::FSINCOS,
1697 ISD::FPOW, ISD::FCOPYSIGN}) {
1698 setOperationAction(FPExpOp, MVT::f32, Expand);
1699 setOperationAction(FPExpOp, MVT::f64, Expand);
1700 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001701
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001702 // No extending loads from i32.
1703 for (MVT VT : MVT::integer_valuetypes()) {
1704 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
1705 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
1706 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
1707 }
1708 // Turn FP truncstore into trunc + store.
1709 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
1710 // Turn FP extload into load/fextend.
1711 for (MVT VT : MVT::fp_valuetypes())
1712 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001713
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001714 // Expand BR_CC and SELECT_CC for all integer and fp types.
1715 for (MVT VT : MVT::integer_valuetypes()) {
1716 setOperationAction(ISD::BR_CC, VT, Expand);
1717 setOperationAction(ISD::SELECT_CC, VT, Expand);
1718 }
1719 for (MVT VT : MVT::fp_valuetypes()) {
1720 setOperationAction(ISD::BR_CC, VT, Expand);
1721 setOperationAction(ISD::SELECT_CC, VT, Expand);
1722 }
1723 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001724
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001725 //
1726 // Handling of vector operations.
1727 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001728
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001729 // Custom lower v4i16 load only. Let v4i16 store to be
1730 // promoted for now.
1731 promoteLdStType(MVT::v4i8, MVT::i32);
1732 promoteLdStType(MVT::v2i16, MVT::i32);
1733 promoteLdStType(MVT::v8i8, MVT::i64);
1734 promoteLdStType(MVT::v2i32, MVT::i64);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001735
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001736 setOperationAction(ISD::LOAD, MVT::v4i16, Custom);
1737 setOperationAction(ISD::STORE, MVT::v4i16, Promote);
1738 AddPromotedToType(ISD::LOAD, MVT::v4i16, MVT::i64);
1739 AddPromotedToType(ISD::STORE, MVT::v4i16, MVT::i64);
1740
1741 // Set the action for vector operations to "expand", then override it with
1742 // either "custom" or "legal" for specific cases.
Craig Topper26260942015-10-18 05:15:34 +00001743 static const unsigned VectExpOps[] = {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001744 // Integer arithmetic:
1745 ISD::ADD, ISD::SUB, ISD::MUL, ISD::SDIV, ISD::UDIV,
1746 ISD::SREM, ISD::UREM, ISD::SDIVREM, ISD::UDIVREM, ISD::ADDC,
1747 ISD::SUBC, ISD::SADDO, ISD::UADDO, ISD::SSUBO, ISD::USUBO,
1748 ISD::SMUL_LOHI, ISD::UMUL_LOHI,
1749 // Logical/bit:
1750 ISD::AND, ISD::OR, ISD::XOR, ISD::ROTL, ISD::ROTR,
1751 ISD::CTPOP, ISD::CTLZ, ISD::CTTZ, ISD::CTLZ_ZERO_UNDEF,
1752 ISD::CTTZ_ZERO_UNDEF,
1753 // Floating point arithmetic/math functions:
1754 ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FMA, ISD::FDIV,
1755 ISD::FREM, ISD::FNEG, ISD::FABS, ISD::FSQRT, ISD::FSIN,
1756 ISD::FCOS, ISD::FPOWI, ISD::FPOW, ISD::FLOG, ISD::FLOG2,
1757 ISD::FLOG10, ISD::FEXP, ISD::FEXP2, ISD::FCEIL, ISD::FTRUNC,
1758 ISD::FRINT, ISD::FNEARBYINT, ISD::FROUND, ISD::FFLOOR,
1759 ISD::FMINNUM, ISD::FMAXNUM, ISD::FSINCOS,
1760 // Misc:
1761 ISD::SELECT, ISD::ConstantPool,
1762 // Vector:
1763 ISD::BUILD_VECTOR, ISD::SCALAR_TO_VECTOR,
1764 ISD::EXTRACT_VECTOR_ELT, ISD::INSERT_VECTOR_ELT,
1765 ISD::EXTRACT_SUBVECTOR, ISD::INSERT_SUBVECTOR,
1766 ISD::CONCAT_VECTORS, ISD::VECTOR_SHUFFLE
1767 };
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001768
1769 for (MVT VT : MVT::vector_valuetypes()) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001770 for (unsigned VectExpOp : VectExpOps)
1771 setOperationAction(VectExpOp, VT, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001772
1773 // Expand all extended loads and truncating stores:
1774 for (MVT TargetVT : MVT::vector_valuetypes()) {
1775 setLoadExtAction(ISD::EXTLOAD, TargetVT, VT, Expand);
1776 setTruncStoreAction(VT, TargetVT, Expand);
1777 }
1778
1779 setOperationAction(ISD::SRA, VT, Custom);
1780 setOperationAction(ISD::SHL, VT, Custom);
1781 setOperationAction(ISD::SRL, VT, Custom);
1782 }
1783
1784 // Types natively supported:
Benjamin Kramer62460692015-04-25 14:46:53 +00001785 for (MVT NativeVT : {MVT::v2i1, MVT::v4i1, MVT::v8i1, MVT::v32i1, MVT::v64i1,
1786 MVT::v4i8, MVT::v8i8, MVT::v2i16, MVT::v4i16, MVT::v1i32,
1787 MVT::v2i32, MVT::v1i64}) {
1788 setOperationAction(ISD::BUILD_VECTOR, NativeVT, Custom);
1789 setOperationAction(ISD::EXTRACT_VECTOR_ELT, NativeVT, Custom);
1790 setOperationAction(ISD::INSERT_VECTOR_ELT, NativeVT, Custom);
1791 setOperationAction(ISD::EXTRACT_SUBVECTOR, NativeVT, Custom);
1792 setOperationAction(ISD::INSERT_SUBVECTOR, NativeVT, Custom);
1793 setOperationAction(ISD::CONCAT_VECTORS, NativeVT, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001794
Benjamin Kramer62460692015-04-25 14:46:53 +00001795 setOperationAction(ISD::ADD, NativeVT, Legal);
1796 setOperationAction(ISD::SUB, NativeVT, Legal);
1797 setOperationAction(ISD::MUL, NativeVT, Legal);
1798 setOperationAction(ISD::AND, NativeVT, Legal);
1799 setOperationAction(ISD::OR, NativeVT, Legal);
1800 setOperationAction(ISD::XOR, NativeVT, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001801 }
1802
1803 setOperationAction(ISD::SETCC, MVT::v2i16, Custom);
1804 setOperationAction(ISD::VSELECT, MVT::v2i16, Custom);
1805 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
1806 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001807 if (UseHVX) {
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00001808 if (UseHVXSgl) {
1809 setOperationAction(ISD::CONCAT_VECTORS, MVT::v128i8, Custom);
1810 setOperationAction(ISD::CONCAT_VECTORS, MVT::v64i16, Custom);
1811 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i32, Custom);
1812 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i64, Custom);
1813 } else if (UseHVXDbl) {
1814 setOperationAction(ISD::CONCAT_VECTORS, MVT::v256i8, Custom);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001815 setOperationAction(ISD::CONCAT_VECTORS, MVT::v128i16, Custom);
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00001816 setOperationAction(ISD::CONCAT_VECTORS, MVT::v64i32, Custom);
1817 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i64, Custom);
1818 } else {
1819 llvm_unreachable("Unrecognized HVX mode");
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001820 }
1821 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001822 // Subtarget-specific operation actions.
1823 //
1824 if (Subtarget.hasV5TOps()) {
1825 setOperationAction(ISD::FMA, MVT::f64, Expand);
1826 setOperationAction(ISD::FADD, MVT::f64, Expand);
1827 setOperationAction(ISD::FSUB, MVT::f64, Expand);
1828 setOperationAction(ISD::FMUL, MVT::f64, Expand);
1829
1830 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Promote);
1831 setOperationAction(ISD::FP_TO_UINT, MVT::i8, Promote);
1832 setOperationAction(ISD::FP_TO_UINT, MVT::i16, Promote);
1833 setOperationAction(ISD::FP_TO_SINT, MVT::i1, Promote);
1834 setOperationAction(ISD::FP_TO_SINT, MVT::i8, Promote);
1835 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
1836 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote);
1837 setOperationAction(ISD::UINT_TO_FP, MVT::i8, Promote);
1838 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote);
1839 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote);
1840 setOperationAction(ISD::SINT_TO_FP, MVT::i8, Promote);
1841 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
1842
1843 } else { // V4
1844 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
1845 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Expand);
1846 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
1847 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
1848 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Expand);
1849 setOperationAction(ISD::FP_TO_SINT, MVT::f32, Expand);
1850 setOperationAction(ISD::FP_EXTEND, MVT::f32, Expand);
1851 setOperationAction(ISD::FP_ROUND, MVT::f64, Expand);
1852 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
1853
1854 setOperationAction(ISD::CTPOP, MVT::i8, Expand);
1855 setOperationAction(ISD::CTPOP, MVT::i16, Expand);
1856 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
1857 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
1858
1859 // Expand these operations for both f32 and f64:
Benjamin Kramer62460692015-04-25 14:46:53 +00001860 for (unsigned FPExpOpV4 :
1861 {ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FABS, ISD::FNEG, ISD::FMA}) {
1862 setOperationAction(FPExpOpV4, MVT::f32, Expand);
1863 setOperationAction(FPExpOpV4, MVT::f64, Expand);
1864 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001865
Benjamin Kramer62460692015-04-25 14:46:53 +00001866 for (ISD::CondCode FPExpCCV4 :
1867 {ISD::SETOEQ, ISD::SETOGT, ISD::SETOLT, ISD::SETOGE, ISD::SETOLE,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001868 ISD::SETUO, ISD::SETO}) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001869 setCondCodeAction(FPExpCCV4, MVT::f32, Expand);
1870 setCondCodeAction(FPExpCCV4, MVT::f64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001871 }
1872 }
1873
1874 // Handling of indexed loads/stores: default is "expand".
1875 //
Benjamin Kramer62460692015-04-25 14:46:53 +00001876 for (MVT LSXTy : {MVT::i8, MVT::i16, MVT::i32, MVT::i64}) {
1877 setIndexedLoadAction(ISD::POST_INC, LSXTy, Legal);
1878 setIndexedStoreAction(ISD::POST_INC, LSXTy, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001879 }
1880
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001881 if (UseHVXDbl) {
1882 for (MVT VT : {MVT::v128i8, MVT::v64i16, MVT::v32i32, MVT::v16i64}) {
1883 setIndexedLoadAction(ISD::POST_INC, VT, Legal);
1884 setIndexedStoreAction(ISD::POST_INC, VT, Legal);
1885 }
1886 }
1887
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001888 computeRegisterProperties(&HRI);
1889
1890 //
1891 // Library calls for unsupported operations
1892 //
1893 bool FastMath = EnableFastMath;
1894
Benjamin Kramera37c8092015-04-25 14:46:46 +00001895 setLibcallName(RTLIB::SDIV_I32, "__hexagon_divsi3");
1896 setLibcallName(RTLIB::SDIV_I64, "__hexagon_divdi3");
1897 setLibcallName(RTLIB::UDIV_I32, "__hexagon_udivsi3");
1898 setLibcallName(RTLIB::UDIV_I64, "__hexagon_udivdi3");
1899 setLibcallName(RTLIB::SREM_I32, "__hexagon_modsi3");
1900 setLibcallName(RTLIB::SREM_I64, "__hexagon_moddi3");
1901 setLibcallName(RTLIB::UREM_I32, "__hexagon_umodsi3");
1902 setLibcallName(RTLIB::UREM_I64, "__hexagon_umoddi3");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001903
Benjamin Kramera37c8092015-04-25 14:46:46 +00001904 setLibcallName(RTLIB::SINTTOFP_I128_F64, "__hexagon_floattidf");
1905 setLibcallName(RTLIB::SINTTOFP_I128_F32, "__hexagon_floattisf");
1906 setLibcallName(RTLIB::FPTOUINT_F32_I128, "__hexagon_fixunssfti");
1907 setLibcallName(RTLIB::FPTOUINT_F64_I128, "__hexagon_fixunsdfti");
1908 setLibcallName(RTLIB::FPTOSINT_F32_I128, "__hexagon_fixsfti");
1909 setLibcallName(RTLIB::FPTOSINT_F64_I128, "__hexagon_fixdfti");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001910
1911 if (IsV4) {
1912 // Handle single-precision floating point operations on V4.
Benjamin Kramera37c8092015-04-25 14:46:46 +00001913 if (FastMath) {
1914 setLibcallName(RTLIB::ADD_F32, "__hexagon_fast_addsf3");
1915 setLibcallName(RTLIB::SUB_F32, "__hexagon_fast_subsf3");
1916 setLibcallName(RTLIB::MUL_F32, "__hexagon_fast_mulsf3");
1917 setLibcallName(RTLIB::OGT_F32, "__hexagon_fast_gtsf2");
1918 setLibcallName(RTLIB::OLT_F32, "__hexagon_fast_ltsf2");
1919 // Double-precision compares.
1920 setLibcallName(RTLIB::OGT_F64, "__hexagon_fast_gtdf2");
1921 setLibcallName(RTLIB::OLT_F64, "__hexagon_fast_ltdf2");
1922 } else {
1923 setLibcallName(RTLIB::ADD_F32, "__hexagon_addsf3");
1924 setLibcallName(RTLIB::SUB_F32, "__hexagon_subsf3");
1925 setLibcallName(RTLIB::MUL_F32, "__hexagon_mulsf3");
1926 setLibcallName(RTLIB::OGT_F32, "__hexagon_gtsf2");
1927 setLibcallName(RTLIB::OLT_F32, "__hexagon_ltsf2");
1928 // Double-precision compares.
1929 setLibcallName(RTLIB::OGT_F64, "__hexagon_gtdf2");
1930 setLibcallName(RTLIB::OLT_F64, "__hexagon_ltdf2");
1931 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001932 }
1933
1934 // This is the only fast library function for sqrtd.
1935 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00001936 setLibcallName(RTLIB::SQRT_F64, "__hexagon_fast2_sqrtdf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001937
Benjamin Kramera37c8092015-04-25 14:46:46 +00001938 // Prefix is: nothing for "slow-math",
1939 // "fast2_" for V4 fast-math and V5+ fast-math double-precision
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001940 // (actually, keep fast-math and fast-math2 separate for now)
Benjamin Kramera37c8092015-04-25 14:46:46 +00001941 if (FastMath) {
1942 setLibcallName(RTLIB::ADD_F64, "__hexagon_fast_adddf3");
1943 setLibcallName(RTLIB::SUB_F64, "__hexagon_fast_subdf3");
1944 setLibcallName(RTLIB::MUL_F64, "__hexagon_fast_muldf3");
1945 setLibcallName(RTLIB::DIV_F64, "__hexagon_fast_divdf3");
1946 // Calling __hexagon_fast2_divsf3 with fast-math on V5 (ok).
1947 setLibcallName(RTLIB::DIV_F32, "__hexagon_fast_divsf3");
1948 } else {
1949 setLibcallName(RTLIB::ADD_F64, "__hexagon_adddf3");
1950 setLibcallName(RTLIB::SUB_F64, "__hexagon_subdf3");
1951 setLibcallName(RTLIB::MUL_F64, "__hexagon_muldf3");
1952 setLibcallName(RTLIB::DIV_F64, "__hexagon_divdf3");
1953 setLibcallName(RTLIB::DIV_F32, "__hexagon_divsf3");
1954 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001955
1956 if (Subtarget.hasV5TOps()) {
1957 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00001958 setLibcallName(RTLIB::SQRT_F32, "__hexagon_fast2_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001959 else
Benjamin Kramera37c8092015-04-25 14:46:46 +00001960 setLibcallName(RTLIB::SQRT_F32, "__hexagon_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001961 } else {
1962 // V4
Benjamin Kramera37c8092015-04-25 14:46:46 +00001963 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__hexagon_floatsisf");
1964 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__hexagon_floatsidf");
1965 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__hexagon_floatdisf");
1966 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__hexagon_floatdidf");
1967 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__hexagon_floatunsisf");
1968 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__hexagon_floatunsidf");
1969 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__hexagon_floatundisf");
1970 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__hexagon_floatundidf");
1971 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__hexagon_fixunssfsi");
1972 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__hexagon_fixunssfdi");
1973 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__hexagon_fixunsdfsi");
1974 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__hexagon_fixunsdfdi");
1975 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__hexagon_fixsfsi");
1976 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__hexagon_fixsfdi");
1977 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__hexagon_fixdfsi");
1978 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__hexagon_fixdfdi");
1979 setLibcallName(RTLIB::FPEXT_F32_F64, "__hexagon_extendsfdf2");
1980 setLibcallName(RTLIB::FPROUND_F64_F32, "__hexagon_truncdfsf2");
1981 setLibcallName(RTLIB::OEQ_F32, "__hexagon_eqsf2");
1982 setLibcallName(RTLIB::OEQ_F64, "__hexagon_eqdf2");
1983 setLibcallName(RTLIB::OGE_F32, "__hexagon_gesf2");
1984 setLibcallName(RTLIB::OGE_F64, "__hexagon_gedf2");
1985 setLibcallName(RTLIB::OLE_F32, "__hexagon_lesf2");
1986 setLibcallName(RTLIB::OLE_F64, "__hexagon_ledf2");
1987 setLibcallName(RTLIB::UNE_F32, "__hexagon_nesf2");
1988 setLibcallName(RTLIB::UNE_F64, "__hexagon_nedf2");
1989 setLibcallName(RTLIB::UO_F32, "__hexagon_unordsf2");
1990 setLibcallName(RTLIB::UO_F64, "__hexagon_unorddf2");
1991 setLibcallName(RTLIB::O_F32, "__hexagon_unordsf2");
1992 setLibcallName(RTLIB::O_F64, "__hexagon_unorddf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001993 }
1994
1995 // These cause problems when the shift amount is non-constant.
1996 setLibcallName(RTLIB::SHL_I128, nullptr);
1997 setLibcallName(RTLIB::SRL_I128, nullptr);
1998 setLibcallName(RTLIB::SRA_I128, nullptr);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001999}
2000
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002001
2002const char* HexagonTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00002003 switch ((HexagonISD::NodeType)Opcode) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002004 case HexagonISD::ALLOCA: return "HexagonISD::ALLOCA";
2005 case HexagonISD::ARGEXTEND: return "HexagonISD::ARGEXTEND";
2006 case HexagonISD::AT_GOT: return "HexagonISD::AT_GOT";
2007 case HexagonISD::AT_PCREL: return "HexagonISD::AT_PCREL";
2008 case HexagonISD::BARRIER: return "HexagonISD::BARRIER";
2009 case HexagonISD::BR_JT: return "HexagonISD::BR_JT";
2010 case HexagonISD::CALLR: return "HexagonISD::CALLR";
2011 case HexagonISD::CALLv3nr: return "HexagonISD::CALLv3nr";
2012 case HexagonISD::CALLv3: return "HexagonISD::CALLv3";
2013 case HexagonISD::COMBINE: return "HexagonISD::COMBINE";
2014 case HexagonISD::CONST32_GP: return "HexagonISD::CONST32_GP";
2015 case HexagonISD::CONST32: return "HexagonISD::CONST32";
2016 case HexagonISD::CP: return "HexagonISD::CP";
2017 case HexagonISD::DCFETCH: return "HexagonISD::DCFETCH";
2018 case HexagonISD::EH_RETURN: return "HexagonISD::EH_RETURN";
2019 case HexagonISD::EXTRACTU: return "HexagonISD::EXTRACTU";
2020 case HexagonISD::EXTRACTURP: return "HexagonISD::EXTRACTURP";
2021 case HexagonISD::FCONST32: return "HexagonISD::FCONST32";
2022 case HexagonISD::INSERT: return "HexagonISD::INSERT";
2023 case HexagonISD::INSERTRP: return "HexagonISD::INSERTRP";
2024 case HexagonISD::JT: return "HexagonISD::JT";
2025 case HexagonISD::PACKHL: return "HexagonISD::PACKHL";
2026 case HexagonISD::PIC_ADD: return "HexagonISD::PIC_ADD";
2027 case HexagonISD::POPCOUNT: return "HexagonISD::POPCOUNT";
2028 case HexagonISD::RET_FLAG: return "HexagonISD::RET_FLAG";
2029 case HexagonISD::SHUFFEB: return "HexagonISD::SHUFFEB";
2030 case HexagonISD::SHUFFEH: return "HexagonISD::SHUFFEH";
2031 case HexagonISD::SHUFFOB: return "HexagonISD::SHUFFOB";
2032 case HexagonISD::SHUFFOH: return "HexagonISD::SHUFFOH";
2033 case HexagonISD::TC_RETURN: return "HexagonISD::TC_RETURN";
2034 case HexagonISD::VCMPBEQ: return "HexagonISD::VCMPBEQ";
2035 case HexagonISD::VCMPBGT: return "HexagonISD::VCMPBGT";
2036 case HexagonISD::VCMPBGTU: return "HexagonISD::VCMPBGTU";
2037 case HexagonISD::VCMPHEQ: return "HexagonISD::VCMPHEQ";
2038 case HexagonISD::VCMPHGT: return "HexagonISD::VCMPHGT";
2039 case HexagonISD::VCMPHGTU: return "HexagonISD::VCMPHGTU";
2040 case HexagonISD::VCMPWEQ: return "HexagonISD::VCMPWEQ";
2041 case HexagonISD::VCMPWGT: return "HexagonISD::VCMPWGT";
2042 case HexagonISD::VCMPWGTU: return "HexagonISD::VCMPWGTU";
Krzysztof Parzyszekc168c012015-12-03 16:47:20 +00002043 case HexagonISD::VCOMBINE: return "HexagonISD::VCOMBINE";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002044 case HexagonISD::VSHLH: return "HexagonISD::VSHLH";
2045 case HexagonISD::VSHLW: return "HexagonISD::VSHLW";
2046 case HexagonISD::VSPLATB: return "HexagonISD::VSPLTB";
2047 case HexagonISD::VSPLATH: return "HexagonISD::VSPLATH";
2048 case HexagonISD::VSRAH: return "HexagonISD::VSRAH";
2049 case HexagonISD::VSRAW: return "HexagonISD::VSRAW";
2050 case HexagonISD::VSRLH: return "HexagonISD::VSRLH";
2051 case HexagonISD::VSRLW: return "HexagonISD::VSRLW";
2052 case HexagonISD::VSXTBH: return "HexagonISD::VSXTBH";
2053 case HexagonISD::VSXTBW: return "HexagonISD::VSXTBW";
Matthias Braund04893f2015-05-07 21:33:59 +00002054 case HexagonISD::OP_END: break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002055 }
Matthias Braund04893f2015-05-07 21:33:59 +00002056 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002057}
2058
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002059bool HexagonTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002060 EVT MTy1 = EVT::getEVT(Ty1);
2061 EVT MTy2 = EVT::getEVT(Ty2);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002062 if (!MTy1.isSimple() || !MTy2.isSimple())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002063 return false;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002064 return (MTy1.getSimpleVT() == MVT::i64) && (MTy2.getSimpleVT() == MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002065}
2066
2067bool HexagonTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002068 if (!VT1.isSimple() || !VT2.isSimple())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002069 return false;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002070 return (VT1.getSimpleVT() == MVT::i64) && (VT2.getSimpleVT() == MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002071}
2072
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002073// shouldExpandBuildVectorWithShuffles
2074// Should we expand the build vector with shuffles?
2075bool
2076HexagonTargetLowering::shouldExpandBuildVectorWithShuffles(EVT VT,
2077 unsigned DefinedValues) const {
2078
2079 // Hexagon vector shuffle operates on element sizes of bytes or halfwords
2080 EVT EltVT = VT.getVectorElementType();
2081 int EltBits = EltVT.getSizeInBits();
2082 if ((EltBits != 8) && (EltBits != 16))
2083 return false;
2084
2085 return TargetLowering::shouldExpandBuildVectorWithShuffles(VT, DefinedValues);
2086}
2087
2088// LowerVECTOR_SHUFFLE - Lower a vector shuffle (V1, V2, V3). V1 and
2089// V2 are the two vectors to select data from, V3 is the permutation.
2090static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
2091 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
2092 SDValue V1 = Op.getOperand(0);
2093 SDValue V2 = Op.getOperand(1);
2094 SDLoc dl(Op);
2095 EVT VT = Op.getValueType();
2096
2097 if (V2.getOpcode() == ISD::UNDEF)
2098 V2 = V1;
2099
2100 if (SVN->isSplat()) {
2101 int Lane = SVN->getSplatIndex();
2102 if (Lane == -1) Lane = 0;
2103
2104 // Test if V1 is a SCALAR_TO_VECTOR.
2105 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR)
2106 return createSplat(DAG, dl, VT, V1.getOperand(0));
2107
2108 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
2109 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
2110 // reaches it).
2111 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
2112 !isa<ConstantSDNode>(V1.getOperand(0))) {
2113 bool IsScalarToVector = true;
2114 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
2115 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
2116 IsScalarToVector = false;
2117 break;
2118 }
2119 if (IsScalarToVector)
2120 return createSplat(DAG, dl, VT, V1.getOperand(0));
2121 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002122 return createSplat(DAG, dl, VT, DAG.getConstant(Lane, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002123 }
2124
2125 // FIXME: We need to support more general vector shuffles. See
2126 // below the comment from the ARM backend that deals in the general
2127 // case with the vector shuffles. For now, let expand handle these.
2128 return SDValue();
2129
2130 // If the shuffle is not directly supported and it has 4 elements, use
2131 // the PerfectShuffle-generated table to synthesize it from other shuffles.
2132}
2133
2134// If BUILD_VECTOR has same base element repeated several times,
2135// report true.
2136static bool isCommonSplatElement(BuildVectorSDNode *BVN) {
2137 unsigned NElts = BVN->getNumOperands();
2138 SDValue V0 = BVN->getOperand(0);
2139
2140 for (unsigned i = 1, e = NElts; i != e; ++i) {
2141 if (BVN->getOperand(i) != V0)
2142 return false;
2143 }
2144 return true;
2145}
2146
2147// LowerVECTOR_SHIFT - Lower a vector shift. Try to convert
2148// <VT> = SHL/SRA/SRL <VT> by <VT> to Hexagon specific
2149// <VT> = SHL/SRA/SRL <VT> by <IT/i32>.
2150static SDValue LowerVECTOR_SHIFT(SDValue Op, SelectionDAG &DAG) {
2151 BuildVectorSDNode *BVN = 0;
2152 SDValue V1 = Op.getOperand(0);
2153 SDValue V2 = Op.getOperand(1);
2154 SDValue V3;
2155 SDLoc dl(Op);
2156 EVT VT = Op.getValueType();
2157
2158 if ((BVN = dyn_cast<BuildVectorSDNode>(V1.getNode())) &&
2159 isCommonSplatElement(BVN))
2160 V3 = V2;
2161 else if ((BVN = dyn_cast<BuildVectorSDNode>(V2.getNode())) &&
2162 isCommonSplatElement(BVN))
2163 V3 = V1;
2164 else
2165 return SDValue();
2166
2167 SDValue CommonSplat = BVN->getOperand(0);
2168 SDValue Result;
2169
2170 if (VT.getSimpleVT() == MVT::v4i16) {
2171 switch (Op.getOpcode()) {
2172 case ISD::SRA:
2173 Result = DAG.getNode(HexagonISD::VSRAH, dl, VT, V3, CommonSplat);
2174 break;
2175 case ISD::SHL:
2176 Result = DAG.getNode(HexagonISD::VSHLH, dl, VT, V3, CommonSplat);
2177 break;
2178 case ISD::SRL:
2179 Result = DAG.getNode(HexagonISD::VSRLH, dl, VT, V3, CommonSplat);
2180 break;
2181 default:
2182 return SDValue();
2183 }
2184 } else if (VT.getSimpleVT() == MVT::v2i32) {
2185 switch (Op.getOpcode()) {
2186 case ISD::SRA:
2187 Result = DAG.getNode(HexagonISD::VSRAW, dl, VT, V3, CommonSplat);
2188 break;
2189 case ISD::SHL:
2190 Result = DAG.getNode(HexagonISD::VSHLW, dl, VT, V3, CommonSplat);
2191 break;
2192 case ISD::SRL:
2193 Result = DAG.getNode(HexagonISD::VSRLW, dl, VT, V3, CommonSplat);
2194 break;
2195 default:
2196 return SDValue();
2197 }
2198 } else {
2199 return SDValue();
2200 }
2201
2202 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
2203}
2204
2205SDValue
2206HexagonTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
2207 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
2208 SDLoc dl(Op);
2209 EVT VT = Op.getValueType();
2210
2211 unsigned Size = VT.getSizeInBits();
2212
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002213 // Only handle vectors of 64 bits or shorter.
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002214 if (Size > 64)
2215 return SDValue();
2216
2217 APInt APSplatBits, APSplatUndef;
2218 unsigned SplatBitSize;
2219 bool HasAnyUndefs;
2220 unsigned NElts = BVN->getNumOperands();
2221
2222 // Try to generate a SPLAT instruction.
2223 if ((VT.getSimpleVT() == MVT::v4i8 || VT.getSimpleVT() == MVT::v4i16) &&
2224 (BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
2225 HasAnyUndefs, 0, true) && SplatBitSize <= 16)) {
2226 unsigned SplatBits = APSplatBits.getZExtValue();
2227 int32_t SextVal = ((int32_t) (SplatBits << (32 - SplatBitSize)) >>
2228 (32 - SplatBitSize));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002229 return createSplat(DAG, dl, VT, DAG.getConstant(SextVal, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002230 }
2231
2232 // Try to generate COMBINE to build v2i32 vectors.
2233 if (VT.getSimpleVT() == MVT::v2i32) {
2234 SDValue V0 = BVN->getOperand(0);
2235 SDValue V1 = BVN->getOperand(1);
2236
2237 if (V0.getOpcode() == ISD::UNDEF)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002238 V0 = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002239 if (V1.getOpcode() == ISD::UNDEF)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002240 V1 = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002241
2242 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(V0);
2243 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(V1);
2244 // If the element isn't a constant, it is in a register:
2245 // generate a COMBINE Register Register instruction.
2246 if (!C0 || !C1)
2247 return DAG.getNode(HexagonISD::COMBINE, dl, VT, V1, V0);
2248
2249 // If one of the operands is an 8 bit integer constant, generate
2250 // a COMBINE Immediate Immediate instruction.
2251 if (isInt<8>(C0->getSExtValue()) ||
2252 isInt<8>(C1->getSExtValue()))
2253 return DAG.getNode(HexagonISD::COMBINE, dl, VT, V1, V0);
2254 }
2255
2256 // Try to generate a S2_packhl to build v2i16 vectors.
2257 if (VT.getSimpleVT() == MVT::v2i16) {
2258 for (unsigned i = 0, e = NElts; i != e; ++i) {
2259 if (BVN->getOperand(i).getOpcode() == ISD::UNDEF)
2260 continue;
2261 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(BVN->getOperand(i));
2262 // If the element isn't a constant, it is in a register:
2263 // generate a S2_packhl instruction.
2264 if (!Cst) {
2265 SDValue pack = DAG.getNode(HexagonISD::PACKHL, dl, MVT::v4i16,
2266 BVN->getOperand(1), BVN->getOperand(0));
2267
2268 return DAG.getTargetExtractSubreg(Hexagon::subreg_loreg, dl, MVT::v2i16,
2269 pack);
2270 }
2271 }
2272 }
2273
2274 // In the general case, generate a CONST32 or a CONST64 for constant vectors,
2275 // and insert_vector_elt for all the other cases.
2276 uint64_t Res = 0;
2277 unsigned EltSize = Size / NElts;
2278 SDValue ConstVal;
2279 uint64_t Mask = ~uint64_t(0ULL) >> (64 - EltSize);
2280 bool HasNonConstantElements = false;
2281
2282 for (unsigned i = 0, e = NElts; i != e; ++i) {
2283 // LLVM's BUILD_VECTOR operands are in Little Endian mode, whereas Hexagon's
2284 // combine, const64, etc. are Big Endian.
2285 unsigned OpIdx = NElts - i - 1;
2286 SDValue Operand = BVN->getOperand(OpIdx);
2287 if (Operand.getOpcode() == ISD::UNDEF)
2288 continue;
2289
2290 int64_t Val = 0;
2291 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Operand))
2292 Val = Cst->getSExtValue();
2293 else
2294 HasNonConstantElements = true;
2295
2296 Val &= Mask;
2297 Res = (Res << EltSize) | Val;
2298 }
2299
2300 if (Size == 64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002301 ConstVal = DAG.getConstant(Res, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002302 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002303 ConstVal = DAG.getConstant(Res, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002304
2305 // When there are non constant operands, add them with INSERT_VECTOR_ELT to
2306 // ConstVal, the constant part of the vector.
2307 if (HasNonConstantElements) {
2308 EVT EltVT = VT.getVectorElementType();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002309 SDValue Width = DAG.getConstant(EltVT.getSizeInBits(), dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002310 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002311 DAG.getConstant(32, dl, MVT::i64));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002312
2313 for (unsigned i = 0, e = NElts; i != e; ++i) {
2314 // LLVM's BUILD_VECTOR operands are in Little Endian mode, whereas Hexagon
2315 // is Big Endian.
2316 unsigned OpIdx = NElts - i - 1;
2317 SDValue Operand = BVN->getOperand(OpIdx);
Benjamin Kramer619c4e52015-04-10 11:24:51 +00002318 if (isa<ConstantSDNode>(Operand))
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002319 // This operand is already in ConstVal.
2320 continue;
2321
2322 if (VT.getSizeInBits() == 64 &&
2323 Operand.getValueType().getSizeInBits() == 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002324 SDValue C = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002325 Operand = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, Operand);
2326 }
2327
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002328 SDValue Idx = DAG.getConstant(OpIdx, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002329 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i64, Idx, Width);
2330 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2331 const SDValue Ops[] = {ConstVal, Operand, Combined};
2332
2333 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002334 ConstVal = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002335 else
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002336 ConstVal = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002337 }
2338 }
2339
2340 return DAG.getNode(ISD::BITCAST, dl, VT, ConstVal);
2341}
2342
2343SDValue
2344HexagonTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
2345 SelectionDAG &DAG) const {
2346 SDLoc dl(Op);
Krzysztof Parzyszekc168c012015-12-03 16:47:20 +00002347 bool UseHVX = Subtarget.useHVXOps();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002348 EVT VT = Op.getValueType();
2349 unsigned NElts = Op.getNumOperands();
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002350 SDValue Vec0 = Op.getOperand(0);
2351 EVT VecVT = Vec0.getValueType();
2352 unsigned Width = VecVT.getSizeInBits();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002353
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002354 if (NElts == 2) {
2355 MVT ST = VecVT.getSimpleVT();
2356 // We are trying to concat two v2i16 to a single v4i16, or two v4i8
2357 // into a single v8i8.
2358 if (ST == MVT::v2i16 || ST == MVT::v4i8)
2359 return DAG.getNode(HexagonISD::COMBINE, dl, VT, Op.getOperand(1), Vec0);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002360
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002361 if (UseHVX) {
2362 assert((Width == 64*8 && Subtarget.useHVXSglOps()) ||
2363 (Width == 128*8 && Subtarget.useHVXDblOps()));
2364 SDValue Vec1 = Op.getOperand(1);
2365 MVT OpTy = Subtarget.useHVXSglOps() ? MVT::v16i32 : MVT::v32i32;
2366 MVT ReTy = Subtarget.useHVXSglOps() ? MVT::v32i32 : MVT::v64i32;
2367 SDValue B0 = DAG.getNode(ISD::BITCAST, dl, OpTy, Vec0);
2368 SDValue B1 = DAG.getNode(ISD::BITCAST, dl, OpTy, Vec1);
2369 SDValue VC = DAG.getNode(HexagonISD::VCOMBINE, dl, ReTy, B1, B0);
2370 return DAG.getNode(ISD::BITCAST, dl, VT, VC);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002371 }
2372 }
2373
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002374 if (VT.getSizeInBits() != 32 && VT.getSizeInBits() != 64)
2375 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002376
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002377 SDValue C0 = DAG.getConstant(0, dl, MVT::i64);
2378 SDValue C32 = DAG.getConstant(32, dl, MVT::i64);
2379 SDValue W = DAG.getConstant(Width, dl, MVT::i64);
2380 // Create the "width" part of the argument to insert_rp/insertp_rp.
2381 SDValue S = DAG.getNode(ISD::SHL, dl, MVT::i64, W, C32);
2382 SDValue V = C0;
2383
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002384 for (unsigned i = 0, e = NElts; i != e; ++i) {
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002385 unsigned N = NElts-i-1;
2386 SDValue OpN = Op.getOperand(N);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002387
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002388 if (VT.getSizeInBits() == 64 && OpN.getValueType().getSizeInBits() == 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002389 SDValue C = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002390 OpN = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, OpN);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002391 }
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002392 SDValue Idx = DAG.getConstant(N, dl, MVT::i64);
2393 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i64, Idx, W);
2394 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, S, Offset);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002395 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002396 V = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, {V, OpN, Or});
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002397 else
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002398 V = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, {V, OpN, Or});
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002399 }
2400
Krzysztof Parzyszekf1b3e5e2015-12-04 16:18:15 +00002401 return DAG.getNode(ISD::BITCAST, dl, VT, V);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002402}
2403
2404SDValue
2405HexagonTargetLowering::LowerEXTRACT_VECTOR(SDValue Op,
2406 SelectionDAG &DAG) const {
2407 EVT VT = Op.getValueType();
2408 int VTN = VT.isVector() ? VT.getVectorNumElements() : 1;
2409 SDLoc dl(Op);
2410 SDValue Idx = Op.getOperand(1);
2411 SDValue Vec = Op.getOperand(0);
2412 EVT VecVT = Vec.getValueType();
2413 EVT EltVT = VecVT.getVectorElementType();
2414 int EltSize = EltVT.getSizeInBits();
2415 SDValue Width = DAG.getConstant(Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT ?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002416 EltSize : VTN * EltSize, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002417
2418 // Constant element number.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002419 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Idx)) {
2420 uint64_t X = CI->getZExtValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002421 SDValue Offset = DAG.getConstant(X * EltSize, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002422 const SDValue Ops[] = {Vec, Width, Offset};
2423
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002424 ConstantSDNode *CW = dyn_cast<ConstantSDNode>(Width);
2425 assert(CW && "Non constant width in LowerEXTRACT_VECTOR");
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002426
2427 SDValue N;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002428 MVT SVT = VecVT.getSimpleVT();
2429 uint64_t W = CW->getZExtValue();
2430
2431 if (W == 32) {
2432 // Translate this node into EXTRACT_SUBREG.
2433 unsigned Subreg = (X == 0) ? Hexagon::subreg_loreg : 0;
2434
2435 if (X == 0)
2436 Subreg = Hexagon::subreg_loreg;
2437 else if (SVT == MVT::v2i32 && X == 1)
2438 Subreg = Hexagon::subreg_hireg;
2439 else if (SVT == MVT::v4i16 && X == 2)
2440 Subreg = Hexagon::subreg_hireg;
2441 else if (SVT == MVT::v8i8 && X == 4)
2442 Subreg = Hexagon::subreg_hireg;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002443 else
2444 llvm_unreachable("Bad offset");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002445 N = DAG.getTargetExtractSubreg(Subreg, dl, MVT::i32, Vec);
2446
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002447 } else if (VecVT.getSizeInBits() == 32) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002448 N = DAG.getNode(HexagonISD::EXTRACTU, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002449 } else {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002450 N = DAG.getNode(HexagonISD::EXTRACTU, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002451 if (VT.getSizeInBits() == 32)
2452 N = DAG.getTargetExtractSubreg(Hexagon::subreg_loreg, dl, MVT::i32, N);
2453 }
2454
2455 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2456 }
2457
2458 // Variable element number.
2459 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i32, Idx,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002460 DAG.getConstant(EltSize, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002461 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002462 DAG.getConstant(32, dl, MVT::i64));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002463 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2464
2465 const SDValue Ops[] = {Vec, Combined};
2466
2467 SDValue N;
2468 if (VecVT.getSizeInBits() == 32) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002469 N = DAG.getNode(HexagonISD::EXTRACTURP, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002470 } else {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002471 N = DAG.getNode(HexagonISD::EXTRACTURP, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002472 if (VT.getSizeInBits() == 32)
2473 N = DAG.getTargetExtractSubreg(Hexagon::subreg_loreg, dl, MVT::i32, N);
2474 }
2475 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2476}
2477
2478SDValue
2479HexagonTargetLowering::LowerINSERT_VECTOR(SDValue Op,
2480 SelectionDAG &DAG) const {
2481 EVT VT = Op.getValueType();
2482 int VTN = VT.isVector() ? VT.getVectorNumElements() : 1;
2483 SDLoc dl(Op);
2484 SDValue Vec = Op.getOperand(0);
2485 SDValue Val = Op.getOperand(1);
2486 SDValue Idx = Op.getOperand(2);
2487 EVT VecVT = Vec.getValueType();
2488 EVT EltVT = VecVT.getVectorElementType();
2489 int EltSize = EltVT.getSizeInBits();
2490 SDValue Width = DAG.getConstant(Op.getOpcode() == ISD::INSERT_VECTOR_ELT ?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002491 EltSize : VTN * EltSize, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002492
2493 if (ConstantSDNode *C = cast<ConstantSDNode>(Idx)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002494 SDValue Offset = DAG.getConstant(C->getSExtValue() * EltSize, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002495 const SDValue Ops[] = {Vec, Val, Width, Offset};
2496
2497 SDValue N;
2498 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002499 N = DAG.getNode(HexagonISD::INSERT, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002500 else
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002501 N = DAG.getNode(HexagonISD::INSERT, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002502
2503 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2504 }
2505
2506 // Variable element number.
2507 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i32, Idx,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002508 DAG.getConstant(EltSize, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002509 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002510 DAG.getConstant(32, dl, MVT::i64));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002511 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2512
2513 if (VT.getSizeInBits() == 64 &&
2514 Val.getValueType().getSizeInBits() == 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002515 SDValue C = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002516 Val = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, Val);
2517 }
2518
2519 const SDValue Ops[] = {Vec, Val, Combined};
2520
2521 SDValue N;
2522 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002523 N = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002524 else
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002525 N = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002526
2527 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2528}
2529
Tim Northovera4415852013-08-06 09:12:35 +00002530bool
2531HexagonTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
2532 // Assuming the caller does not have either a signext or zeroext modifier, and
2533 // only one value is accepted, any reasonable truncation is allowed.
2534 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
2535 return false;
2536
2537 // FIXME: in principle up to 64-bit could be made safe, but it would be very
2538 // fragile at the moment: any support for multiple value returns would be
2539 // liable to disallow tail calls involving i64 -> iN truncation in many cases.
2540 return Ty1->getPrimitiveSizeInBits() <= 32;
2541}
2542
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002543SDValue
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002544HexagonTargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
2545 SDValue Chain = Op.getOperand(0);
2546 SDValue Offset = Op.getOperand(1);
2547 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002548 SDLoc dl(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002549 auto PtrVT = getPointerTy(DAG.getDataLayout());
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002550
2551 // Mark function as containing a call to EH_RETURN.
2552 HexagonMachineFunctionInfo *FuncInfo =
2553 DAG.getMachineFunction().getInfo<HexagonMachineFunctionInfo>();
2554 FuncInfo->setHasEHReturn();
2555
2556 unsigned OffsetReg = Hexagon::R28;
2557
Mehdi Amini44ede332015-07-09 02:09:04 +00002558 SDValue StoreAddr =
2559 DAG.getNode(ISD::ADD, dl, PtrVT, DAG.getRegister(Hexagon::R30, PtrVT),
2560 DAG.getIntPtrConstant(4, dl));
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002561 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
2562 false, false, 0);
2563 Chain = DAG.getCopyToReg(Chain, dl, OffsetReg, Offset);
2564
2565 // Not needed we already use it as explict input to EH_RETURN.
2566 // MF.getRegInfo().addLiveOut(OffsetReg);
2567
2568 return DAG.getNode(HexagonISD::EH_RETURN, dl, MVT::Other, Chain);
2569}
2570
2571SDValue
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002572HexagonTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002573 unsigned Opc = Op.getOpcode();
2574 switch (Opc) {
2575 default:
2576#ifndef NDEBUG
2577 Op.getNode()->dumpr(&DAG);
2578 if (Opc > HexagonISD::OP_BEGIN && Opc < HexagonISD::OP_END)
2579 errs() << "Check for a non-legal type in this operation\n";
2580#endif
2581 llvm_unreachable("Should not custom lower this!");
2582 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
2583 case ISD::INSERT_SUBVECTOR: return LowerINSERT_VECTOR(Op, DAG);
2584 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR(Op, DAG);
2585 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_VECTOR(Op, DAG);
2586 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR(Op, DAG);
2587 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2588 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002589 case ISD::SRA:
2590 case ISD::SHL:
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002591 case ISD::SRL: return LowerVECTOR_SHIFT(Op, DAG);
2592 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
2593 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
2594 // Frame & Return address. Currently unimplemented.
2595 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
2596 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
2597 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
2598 case ISD::GlobalAddress: return LowerGLOBALADDRESS(Op, DAG);
2599 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
2600 case ISD::VASTART: return LowerVASTART(Op, DAG);
2601 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002602 // Custom lower some vector loads.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002603 case ISD::LOAD: return LowerLOAD(Op, DAG);
2604 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
2605 case ISD::SETCC: return LowerSETCC(Op, DAG);
2606 case ISD::VSELECT: return LowerVSELECT(Op, DAG);
2607 case ISD::CTPOP: return LowerCTPOP(Op, DAG);
2608 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
2609 case ISD::INLINEASM: return LowerINLINEASM(Op, DAG);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002610 }
2611}
2612
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002613MachineBasicBlock *
2614HexagonTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
2615 MachineBasicBlock *BB)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00002616 const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002617 switch (MI->getOpcode()) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00002618 case Hexagon::ALLOCA: {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002619 MachineFunction *MF = BB->getParent();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00002620 auto *FuncInfo = MF->getInfo<HexagonMachineFunctionInfo>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002621 FuncInfo->addAllocaAdjustInst(MI);
2622 return BB;
2623 }
Craig Toppere55c5562012-02-07 02:50:20 +00002624 default: llvm_unreachable("Unexpected instr type to insert");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002625 } // switch
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002626}
2627
2628//===----------------------------------------------------------------------===//
2629// Inline Assembly Support
2630//===----------------------------------------------------------------------===//
2631
Eric Christopher11e4df72015-02-26 22:38:43 +00002632std::pair<unsigned, const TargetRegisterClass *>
2633HexagonTargetLowering::getRegForInlineAsmConstraint(
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00002634 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002635 bool UseHVX = Subtarget.useHVXOps(), UseHVXDbl = Subtarget.useHVXDblOps();
2636
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002637 if (Constraint.size() == 1) {
2638 switch (Constraint[0]) {
2639 case 'r': // R0-R31
Chad Rosier295bd432013-06-22 18:37:38 +00002640 switch (VT.SimpleTy) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002641 default:
Craig Toppere55c5562012-02-07 02:50:20 +00002642 llvm_unreachable("getRegForInlineAsmConstraint Unhandled data type");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002643 case MVT::i32:
2644 case MVT::i16:
2645 case MVT::i8:
Sirish Pande69295b82012-05-10 20:20:25 +00002646 case MVT::f32:
Craig Topperc7242e02012-04-20 07:30:17 +00002647 return std::make_pair(0U, &Hexagon::IntRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002648 case MVT::i64:
Sirish Pande69295b82012-05-10 20:20:25 +00002649 case MVT::f64:
Craig Topperc7242e02012-04-20 07:30:17 +00002650 return std::make_pair(0U, &Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002651 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002652 case 'q': // q0-q3
2653 switch (VT.SimpleTy) {
2654 default:
2655 llvm_unreachable("getRegForInlineAsmConstraint Unhandled data type");
2656 case MVT::v1024i1:
2657 case MVT::v512i1:
2658 case MVT::v32i16:
2659 case MVT::v16i32:
2660 case MVT::v64i8:
2661 case MVT::v8i64:
2662 return std::make_pair(0U, &Hexagon::VecPredRegsRegClass);
2663 }
2664 case 'v': // V0-V31
2665 switch (VT.SimpleTy) {
2666 default:
2667 llvm_unreachable("getRegForInlineAsmConstraint Unhandled data type");
2668 case MVT::v16i32:
2669 case MVT::v32i16:
2670 case MVT::v64i8:
2671 case MVT::v8i64:
2672 return std::make_pair(0U, &Hexagon::VectorRegsRegClass);
2673 case MVT::v32i32:
2674 case MVT::v64i16:
2675 case MVT::v16i64:
2676 case MVT::v128i8:
2677 if (Subtarget.hasV60TOps() && UseHVX && UseHVXDbl)
2678 return std::make_pair(0U, &Hexagon::VectorRegs128BRegClass);
2679 else
2680 return std::make_pair(0U, &Hexagon::VecDblRegsRegClass);
2681 case MVT::v256i8:
2682 case MVT::v128i16:
2683 case MVT::v64i32:
2684 case MVT::v32i64:
2685 return std::make_pair(0U, &Hexagon::VecDblRegs128BRegClass);
2686 }
2687
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002688 default:
Craig Toppere55c5562012-02-07 02:50:20 +00002689 llvm_unreachable("Unknown asm register class");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002690 }
2691 }
2692
Eric Christopher11e4df72015-02-26 22:38:43 +00002693 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002694}
2695
Sirish Pande69295b82012-05-10 20:20:25 +00002696/// isFPImmLegal - Returns true if the target can instruction select the
2697/// specified FP immediate natively. If false, the legalizer will
2698/// materialize the FP immediate as a load from a constant pool.
2699bool HexagonTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002700 return Subtarget.hasV5TOps();
Sirish Pande69295b82012-05-10 20:20:25 +00002701}
2702
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002703/// isLegalAddressingMode - Return true if the addressing mode represented by
2704/// AM is legal for this target, for a load/store of the specified type.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +00002705bool HexagonTargetLowering::isLegalAddressingMode(const DataLayout &DL,
2706 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +00002707 unsigned AS) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002708 // Allows a signed-extended 11-bit immediate field.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002709 if (AM.BaseOffs <= -(1LL << 13) || AM.BaseOffs >= (1LL << 13)-1)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002710 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002711
2712 // No global is ever allowed as a base.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002713 if (AM.BaseGV)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002714 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002715
2716 int Scale = AM.Scale;
2717 if (Scale < 0) Scale = -Scale;
2718 switch (Scale) {
2719 case 0: // No scale reg, "r+i", "r", or just "i".
2720 break;
2721 default: // No scaled addressing mode.
2722 return false;
2723 }
2724 return true;
2725}
2726
2727/// isLegalICmpImmediate - Return true if the specified immediate is legal
2728/// icmp immediate, that is the target has icmp instructions which can compare
2729/// a register against the immediate without having to materialize the
2730/// immediate into a register.
2731bool HexagonTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
2732 return Imm >= -512 && Imm <= 511;
2733}
2734
2735/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2736/// for tail call optimization. Targets which want to do tail call
2737/// optimization should implement this function.
2738bool HexagonTargetLowering::IsEligibleForTailCallOptimization(
2739 SDValue Callee,
2740 CallingConv::ID CalleeCC,
2741 bool isVarArg,
2742 bool isCalleeStructRet,
2743 bool isCallerStructRet,
2744 const SmallVectorImpl<ISD::OutputArg> &Outs,
2745 const SmallVectorImpl<SDValue> &OutVals,
2746 const SmallVectorImpl<ISD::InputArg> &Ins,
2747 SelectionDAG& DAG) const {
2748 const Function *CallerF = DAG.getMachineFunction().getFunction();
2749 CallingConv::ID CallerCC = CallerF->getCallingConv();
2750 bool CCMatch = CallerCC == CalleeCC;
2751
2752 // ***************************************************************************
2753 // Look for obvious safe cases to perform tail call optimization that do not
2754 // require ABI changes.
2755 // ***************************************************************************
2756
2757 // If this is a tail call via a function pointer, then don't do it!
Craig Topper66059c92015-11-18 07:07:59 +00002758 if (!(isa<GlobalAddressSDNode>(Callee)) &&
2759 !(isa<ExternalSymbolSDNode>(Callee))) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002760 return false;
2761 }
2762
2763 // Do not optimize if the calling conventions do not match.
2764 if (!CCMatch)
2765 return false;
2766
2767 // Do not tail call optimize vararg calls.
2768 if (isVarArg)
2769 return false;
2770
2771 // Also avoid tail call optimization if either caller or callee uses struct
2772 // return semantics.
2773 if (isCalleeStructRet || isCallerStructRet)
2774 return false;
2775
2776 // In addition to the cases above, we also disable Tail Call Optimization if
2777 // the calling convention code that at least one outgoing argument needs to
2778 // go on the stack. We cannot check that here because at this point that
2779 // information is not available.
2780 return true;
2781}
Colin LeMahieu025f8602014-12-08 21:19:18 +00002782
2783// Return true when the given node fits in a positive half word.
2784bool llvm::isPositiveHalfWord(SDNode *N) {
2785 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N);
2786 if (CN && CN->getSExtValue() > 0 && isInt<16>(CN->getSExtValue()))
2787 return true;
2788
2789 switch (N->getOpcode()) {
2790 default:
2791 return false;
2792 case ISD::SIGN_EXTEND_INREG:
2793 return true;
2794 }
2795}
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00002796
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002797std::pair<const TargetRegisterClass*, uint8_t>
2798HexagonTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
2799 MVT VT) const {
2800 const TargetRegisterClass *RRC = nullptr;
2801
2802 uint8_t Cost = 1;
2803 switch (VT.SimpleTy) {
2804 default:
2805 return TargetLowering::findRepresentativeClass(TRI, VT);
2806 case MVT::v64i8:
2807 case MVT::v32i16:
2808 case MVT::v16i32:
2809 case MVT::v8i64:
2810 RRC = &Hexagon::VectorRegsRegClass;
2811 break;
2812 case MVT::v128i8:
2813 case MVT::v64i16:
2814 case MVT::v32i32:
2815 case MVT::v16i64:
2816 if (Subtarget.hasV60TOps() && Subtarget.useHVXOps() &&
2817 Subtarget.useHVXDblOps())
2818 RRC = &Hexagon::VectorRegs128BRegClass;
2819 else
2820 RRC = &Hexagon::VecDblRegsRegClass;
2821 break;
2822 case MVT::v256i8:
2823 case MVT::v128i16:
2824 case MVT::v64i32:
2825 case MVT::v32i64:
2826 RRC = &Hexagon::VecDblRegs128BRegClass;
2827 break;
2828 }
2829 return std::make_pair(RRC, Cost);
2830}
2831
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00002832Value *HexagonTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
2833 AtomicOrdering Ord) const {
2834 BasicBlock *BB = Builder.GetInsertBlock();
2835 Module *M = BB->getParent()->getParent();
2836 Type *Ty = cast<PointerType>(Addr->getType())->getElementType();
2837 unsigned SZ = Ty->getPrimitiveSizeInBits();
2838 assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic loads supported");
2839 Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_L2_loadw_locked
2840 : Intrinsic::hexagon_L4_loadd_locked;
2841 Value *Fn = Intrinsic::getDeclaration(M, IntID);
2842 return Builder.CreateCall(Fn, Addr, "larx");
2843}
2844
2845/// Perform a store-conditional operation to Addr. Return the status of the
2846/// store. This should be 0 if the store succeeded, non-zero otherwise.
2847Value *HexagonTargetLowering::emitStoreConditional(IRBuilder<> &Builder,
2848 Value *Val, Value *Addr, AtomicOrdering Ord) const {
2849 BasicBlock *BB = Builder.GetInsertBlock();
2850 Module *M = BB->getParent()->getParent();
2851 Type *Ty = Val->getType();
2852 unsigned SZ = Ty->getPrimitiveSizeInBits();
2853 assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic stores supported");
2854 Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_S2_storew_locked
2855 : Intrinsic::hexagon_S4_stored_locked;
2856 Value *Fn = Intrinsic::getDeclaration(M, IntID);
2857 Value *Call = Builder.CreateCall(Fn, {Addr, Val}, "stcx");
2858 Value *Cmp = Builder.CreateICmpEQ(Call, Builder.getInt32(0), "");
2859 Value *Ext = Builder.CreateZExt(Cmp, Type::getInt32Ty(M->getContext()));
2860 return Ext;
2861}
2862
Ahmed Bougacha52468672015-09-11 17:08:28 +00002863TargetLowering::AtomicExpansionKind
2864HexagonTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00002865 // Do not expand loads and stores that don't exceed 64 bits.
Ahmed Bougacha52468672015-09-11 17:08:28 +00002866 return LI->getType()->getPrimitiveSizeInBits() > 64
Tim Northoverf520eff2015-12-02 18:12:57 +00002867 ? AtomicExpansionKind::LLOnly
Ahmed Bougacha52468672015-09-11 17:08:28 +00002868 : AtomicExpansionKind::None;
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00002869}
2870
2871bool HexagonTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
2872 // Do not expand loads and stores that don't exceed 64 bits.
2873 return SI->getValueOperand()->getType()->getPrimitiveSizeInBits() > 64;
2874}