1//===- HexagonSubtarget.h - Define Subtarget for the Hexagon ----*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file declares the Hexagon specific subclass of TargetSubtarget.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_LIB_TARGET_HEXAGON_HEXAGONSUBTARGET_H
14#define LLVM_LIB_TARGET_HEXAGON_HEXAGONSUBTARGET_H
15
16#include "HexagonDepArch.h"
17#include "HexagonFrameLowering.h"
18#include "HexagonISelLowering.h"
19#include "HexagonInstrInfo.h"
20#include "HexagonRegisterInfo.h"
21#include "HexagonSelectionDAGInfo.h"
22#include "llvm/ADT/SmallSet.h"
23#include "llvm/ADT/StringRef.h"
24#include "llvm/CodeGen/ScheduleDAGMutation.h"
25#include "llvm/CodeGen/TargetSubtargetInfo.h"
26#include "llvm/MC/MCInstrItineraries.h"
27#include "llvm/Support/Alignment.h"
28#include <bitset>
29#include <memory>
30#include <string>
31#include <vector>
32
33#define GET_SUBTARGETINFO_HEADER
34#include "HexagonGenSubtargetInfo.inc"
35
36namespace llvm {
37
38class LibcallLoweringInfo;
39class MachineInstr;
40class SDep;
41class SUnit;
42class TargetMachine;
43class Triple;
44
45class HexagonSubtarget : public HexagonGenSubtargetInfo {
46 virtual void anchor();
47
48 bool UseHVX64BOps = false;
49 bool UseHVX128BOps = false;
50
51 bool UseAudioOps = false;
52 bool UseCompound = false;
53 bool UseLongCalls = false;
54 bool UseMemops = false;
55 bool UsePackets = false;
56 bool UseNewValueJumps = false;
57 bool UseNewValueStores = false;
58 bool UseSmallData = false;
59 bool UseZRegOps = false;
60 bool UseHMXOps = false;
61 bool UseHLXOps = false;
62 bool UseHVXIEEEFPOps = false;
63 bool UseHVXQFloatOps = false;
64 bool UseHVXFloatingPoint = false;
65 bool UseCabac = false;
66
67 bool HasPreV65 = false;
68 bool HasMemNoShuf = false;
69 bool EnableDuplex = false;
70 std::bitset<Hexagon::NUM_TARGET_REGS> UserReservedRegister;
71 std::bitset<Hexagon::NUM_TARGET_REGS> SCSPointerRegister;
72 bool NoreturnStackElim = false;
73
74 /// Register holding the shadow call stack pointer, resolved from
75 /// SCSPointerRegister in initializeSubtargetDependencies().
76 Register SCSPReg;
77
78public:
79 Hexagon::ArchEnum HexagonArchVersion;
80 Hexagon::ArchEnum HexagonHVXVersion = Hexagon::ArchEnum::NoArch;
81 CodeGenOptLevel OptLevel;
82 /// True if the target should use Back-Skip-Back scheduling. This is the
83 /// default for V60.
84 bool UseBSBScheduling;
85
86 struct UsrOverflowMutation : public ScheduleDAGMutation {
87 void apply(ScheduleDAGInstrs *DAG) override;
88 };
89 struct HVXMemLatencyMutation : public ScheduleDAGMutation {
90 void apply(ScheduleDAGInstrs *DAG) override;
91 };
92 struct HMXActivateWeightMutation : public ScheduleDAGMutation {
93 void apply(ScheduleDAGInstrs *DAG) override;
94 };
95 struct CallMutation : public ScheduleDAGMutation {
96 void apply(ScheduleDAGInstrs *DAG) override;
97 private:
98 bool shouldTFRICallBind(const HexagonInstrInfo &HII,
99 const SUnit &Inst1, const SUnit &Inst2) const;
100 };
101 struct BankConflictMutation : public ScheduleDAGMutation {
102 void apply(ScheduleDAGInstrs *DAG) override;
103 };
104 struct HLXPseudoMutation : public ScheduleDAGMutation {
105 void apply(ScheduleDAGInstrs *DAG) override;
106 };
107
108private:
109 enum HexagonProcFamilyEnum { Others, TinyCore, HLX2048B };
110
111 std::string CPUString;
112 HexagonProcFamilyEnum HexagonProcFamily = Others;
113 Triple TargetTriple;
114
115 // The following objects can use the TargetTriple, so they must be
116 // declared after it.
117 HexagonInstrInfo InstrInfo;
118 HexagonTargetLowering TLInfo;
119 HexagonSelectionDAGInfo TSInfo;
120 HexagonFrameLowering FrameLowering;
121 InstrItineraryData InstrItins;
122
123public:
124 HexagonSubtarget(const Triple &TT, StringRef CPU, StringRef FS,
125 const TargetMachine &TM);
126
127 const Triple &getTargetTriple() const { return TargetTriple; }
128 bool isEnvironmentMusl() const {
129 return TargetTriple.getEnvironment() == Triple::Musl;
130 }
131
132 /// getInstrItins - Return the instruction itineraries based on subtarget
133 /// selection.
134 const InstrItineraryData *getInstrItineraryData() const override {
135 return &InstrItins;
136 }
137 const HexagonInstrInfo *getInstrInfo() const override { return &InstrInfo; }
138 const HexagonRegisterInfo *getRegisterInfo() const override {
139 return &InstrInfo.getRegisterInfo();
140 }
141 const HexagonTargetLowering *getTargetLowering() const override {
142 return &TLInfo;
143 }
144 const HexagonFrameLowering *getFrameLowering() const override {
145 return &FrameLowering;
146 }
147 const HexagonSelectionDAGInfo *getSelectionDAGInfo() const override {
148 return &TSInfo;
149 }
150
151 HexagonSubtarget &initializeSubtargetDependencies(StringRef CPU,
152 StringRef FS);
153
154 void initLibcallLoweringInfo(LibcallLoweringInfo &Info) const override;
155
156 /// ParseSubtargetFeatures - Parses features string setting specified
157 /// subtarget options. Definition of function is auto generated by tblgen.
158 void ParseSubtargetFeatures(StringRef CPU, StringRef TuneCPU, StringRef FS);
159
160 bool isXRaySupported() const override { return true; }
161
162 bool hasV5Ops() const {
163 return getHexagonArchVersion() >= Hexagon::ArchEnum::V5;
164 }
165 bool hasV5OpsOnly() const {
166 return getHexagonArchVersion() == Hexagon::ArchEnum::V5;
167 }
168 bool hasV55Ops() const {
169 return getHexagonArchVersion() >= Hexagon::ArchEnum::V55;
170 }
171 bool hasV55OpsOnly() const {
172 return getHexagonArchVersion() == Hexagon::ArchEnum::V55;
173 }
174 bool hasV60Ops() const {
175 return getHexagonArchVersion() >= Hexagon::ArchEnum::V60;
176 }
177 bool hasV60OpsOnly() const {
178 return getHexagonArchVersion() == Hexagon::ArchEnum::V60;
179 }
180 bool hasV62Ops() const {
181 return getHexagonArchVersion() >= Hexagon::ArchEnum::V62;
182 }
183 bool hasV62OpsOnly() const {
184 return getHexagonArchVersion() == Hexagon::ArchEnum::V62;
185 }
186 bool hasV65Ops() const {
187 return getHexagonArchVersion() >= Hexagon::ArchEnum::V65;
188 }
189 bool hasV65OpsOnly() const {
190 return getHexagonArchVersion() == Hexagon::ArchEnum::V65;
191 }
192 bool hasV66Ops() const {
193 return getHexagonArchVersion() >= Hexagon::ArchEnum::V66;
194 }
195 bool hasV66OpsOnly() const {
196 return getHexagonArchVersion() == Hexagon::ArchEnum::V66;
197 }
198 bool hasV67Ops() const {
199 return getHexagonArchVersion() >= Hexagon::ArchEnum::V67;
200 }
201 bool hasV67OpsOnly() const {
202 return getHexagonArchVersion() == Hexagon::ArchEnum::V67;
203 }
204 bool hasV68Ops() const {
205 return getHexagonArchVersion() >= Hexagon::ArchEnum::V68;
206 }
207 bool hasV68OpsOnly() const {
208 return getHexagonArchVersion() == Hexagon::ArchEnum::V68;
209 }
210 bool hasV69Ops() const {
211 return getHexagonArchVersion() >= Hexagon::ArchEnum::V69;
212 }
213 bool hasV69OpsOnly() const {
214 return getHexagonArchVersion() == Hexagon::ArchEnum::V69;
215 }
216 bool hasV71Ops() const {
217 return getHexagonArchVersion() >= Hexagon::ArchEnum::V71;
218 }
219 bool hasV71OpsOnly() const {
220 return getHexagonArchVersion() == Hexagon::ArchEnum::V71;
221 }
222 bool hasV73Ops() const {
223 return getHexagonArchVersion() >= Hexagon::ArchEnum::V73;
224 }
225 bool hasV73OpsOnly() const {
226 return getHexagonArchVersion() == Hexagon::ArchEnum::V73;
227 }
228 bool hasV75Ops() const {
229 return getHexagonArchVersion() >= Hexagon::ArchEnum::V75;
230 }
231 bool hasV75OpsOnly() const {
232 return getHexagonArchVersion() == Hexagon::ArchEnum::V75;
233 }
234 bool hasV79Ops() const {
235 return getHexagonArchVersion() >= Hexagon::ArchEnum::V79;
236 }
237 bool hasV79OpsOnly() const {
238 return getHexagonArchVersion() == Hexagon::ArchEnum::V79;
239 }
240 bool useHVXV79Ops() const {
241 return HexagonHVXVersion >= Hexagon::ArchEnum::V79;
242 }
243 bool useHVXV79OpsOnly() const {
244 return HexagonHVXVersion == Hexagon::ArchEnum::V79;
245 }
246 bool hasV81Ops() const {
247 return getHexagonArchVersion() >= Hexagon::ArchEnum::V81;
248 }
249 bool hasV81OpsOnly() const {
250 return getHexagonArchVersion() == Hexagon::ArchEnum::V81;
251 }
252 bool useHVXV81Ops() const {
253 return HexagonHVXVersion >= Hexagon::ArchEnum::V81;
254 }
255 bool hasV83Ops() const {
256 return getHexagonArchVersion() >= Hexagon::ArchEnum::V83;
257 }
258 bool hasV83OpsOnly() const {
259 return getHexagonArchVersion() == Hexagon::ArchEnum::V83;
260 }
261 bool useHVXV83Ops() const {
262 return HexagonHVXVersion >= Hexagon::ArchEnum::V83;
263 }
264 bool hasV85Ops() const {
265 return getHexagonArchVersion() >= Hexagon::ArchEnum::V85;
266 }
267 bool hasV85OpsOnly() const {
268 return getHexagonArchVersion() == Hexagon::ArchEnum::V85;
269 }
270 bool useHVXV85Ops() const {
271 return HexagonHVXVersion >= Hexagon::ArchEnum::V85;
272 }
273
274 bool useAudioOps() const { return UseAudioOps; }
275 bool useCompound() const { return UseCompound; }
276 bool useLongCalls() const { return UseLongCalls; }
277 bool useMemops() const { return UseMemops; }
278 bool usePackets() const { return UsePackets; }
279 bool useNewValueJumps() const { return UseNewValueJumps; }
280 bool useNewValueStores() const { return UseNewValueStores; }
281 bool useSmallData() const { return UseSmallData; }
282 bool useZRegOps() const { return UseZRegOps; }
283 bool useHMXOps() const { return UseHMXOps; }
284 bool useHLXOps() const { return UseHLXOps; }
285 bool useHLX2048BOps() const {
286 return UseHLXOps && HexagonProcFamily == HLX2048B;
287 }
288 bool useCabac() const { return UseCabac; }
289
290 bool isTinyCore() const { return HexagonProcFamily == TinyCore; }
291 bool isTinyCoreWithDuplex() const { return isTinyCore() && EnableDuplex; }
292
293 bool useHVXIEEEFPOps() const { return UseHVXIEEEFPOps && useHVXOps(); }
294 bool useHVXQFloatOps() const {
295 return UseHVXQFloatOps && HexagonHVXVersion >= Hexagon::ArchEnum::V68;
296 }
297 bool useHVXFloatingPoint() const { return UseHVXFloatingPoint; }
298 bool useHVXOps() const {
299 return HexagonHVXVersion > Hexagon::ArchEnum::NoArch;
300 }
301 bool useHVXV60Ops() const {
302 return HexagonHVXVersion >= Hexagon::ArchEnum::V60;
303 }
304 bool useHVXV62Ops() const {
305 return HexagonHVXVersion >= Hexagon::ArchEnum::V62;
306 }
307 bool useHVXV65Ops() const {
308 return HexagonHVXVersion >= Hexagon::ArchEnum::V65;
309 }
310 bool useHVXV66Ops() const {
311 return HexagonHVXVersion >= Hexagon::ArchEnum::V66;
312 }
313 bool useHVXV67Ops() const {
314 return HexagonHVXVersion >= Hexagon::ArchEnum::V67;
315 }
316 bool useHVXV68Ops() const {
317 return HexagonHVXVersion >= Hexagon::ArchEnum::V68;
318 }
319 bool useHVXV69Ops() const {
320 return HexagonHVXVersion >= Hexagon::ArchEnum::V69;
321 }
322 bool useHVXV71Ops() const {
323 return HexagonHVXVersion >= Hexagon::ArchEnum::V71;
324 }
325 bool useHVXV73Ops() const {
326 return HexagonHVXVersion >= Hexagon::ArchEnum::V73;
327 }
328 bool useHVX128BOps() const { return useHVXOps() && UseHVX128BOps; }
329 bool useHVX64BOps() const { return useHVXOps() && UseHVX64BOps; }
330
331 bool hasMemNoShuf() const { return HasMemNoShuf; }
332 bool isRegisterReservedByUser(Register i) const override {
333 assert(i.id() < Hexagon::NUM_TARGET_REGS && "Register out of range");
334 return UserReservedRegister[i.id()];
335 }
336
337 /// Returns the register that holds the shadow call stack pointer. Defaults
338 /// to R18, overridable with the "scs-reg-rN" subtarget features.
339 Register getSCSPReg() const { return SCSPReg; }
340
341 bool usePredicatedCalls() const;
342
343 bool noreturnStackElim() const { return NoreturnStackElim; }
344
345 bool useBSBScheduling() const { return UseBSBScheduling; }
346 bool enableMachineScheduler() const override;
347
348 // Always use the TargetLowering default scheduler.
349 // FIXME: This will use the vliw scheduler which is probably just hurting
350 // compiler time and will be removed eventually anyway.
351 bool enableMachineSchedDefaultSched() const override { return false; }
352
353 // For use with PostRAScheduling: get the anti-dependence breaking that should
354 // be performed before post-RA scheduling.
355 AntiDepBreakMode getAntiDepBreakMode() const override { return ANTIDEP_ALL; }
356 /// True if the subtarget should run a scheduler after register
357 /// allocation.
358 bool enablePostRAScheduler() const override { return true; }
359
360 bool enableSubRegLiveness() const override;
361
362 const std::string &getCPUString () const { return CPUString; }
363
364 const Hexagon::ArchEnum &getHexagonArchVersion() const {
365 return HexagonArchVersion;
366 }
367
368 void getPostRAMutations(
369 std::vector<std::unique_ptr<ScheduleDAGMutation>> &Mutations)
370 const override;
371
372 void getSMSMutations(
373 std::vector<std::unique_ptr<ScheduleDAGMutation>> &Mutations)
374 const override;
375
376 /// Enable use of alias analysis during code generation (during MI
377 /// scheduling, DAGCombine, etc.).
378 bool useAA() const override;
379
380 /// Perform target specific adjustments to the latency of a schedule
381 /// dependency.
382 void adjustSchedDependency(SUnit *Def, int DefOpIdx, SUnit *Use, int UseOpIdx,
383 SDep &Dep,
384 const TargetSchedModel *SchedModel) const override;
385
386 unsigned getVectorLength() const {
387 assert(useHVXOps());
388 if (useHVX64BOps())
389 return 64;
390 if (useHVX128BOps())
391 return 128;
392 llvm_unreachable("Invalid HVX vector length settings");
393 }
394
395 ArrayRef<MVT> getHVXElementTypes() const {
396 static MVT Types[] = {MVT::i8, MVT::i16, MVT::i32};
397 static MVT TypesV68[] = {MVT::i8, MVT::i16, MVT::i32, MVT::f16, MVT::f32};
398 static MVT TypesV81[] = {MVT::i8, MVT::i16, MVT::i32,
399 MVT::f16, MVT::bf16, MVT::f32};
400
401 if (useHVXV81Ops() && useHVXFloatingPoint())
402 return ArrayRef(TypesV81);
403 if (useHVXV68Ops() && useHVXFloatingPoint())
404 return ArrayRef(TypesV68);
405 return ArrayRef(Types);
406 }
407
408 bool isHVXElementType(MVT Ty, bool IncludeBool = false) const;
409 bool isHVXVectorType(EVT VecTy, bool IncludeBool = false) const;
410 bool isTypeForHVX(Type *VecTy, bool IncludeBool = false) const;
411
412 unsigned getHLXVectorLength() const {
413 assert(useHLXOps());
414 if (useHLX2048BOps())
415 return 2048;
416 llvm_unreachable("Invalid HLX vector length settings");
417 }
418
419 ArrayRef<MVT> getHLXElementTypes() const {
420 static MVT Types[] = {MVT::i8, MVT::i16, MVT::i32};
421
422 return ArrayRef(Types);
423 }
424
425 bool isHLXElementType(MVT Ty) const;
426 bool isHLXVectorType(EVT VecTy) const;
427 bool isTypeForHLX(Type *VecTy) const;
428
429 Align getTypeAlignment(MVT Ty) const {
430 if (isHVXVectorType(VecTy: Ty, IncludeBool: true))
431 return Align(getVectorLength());
432 return Align(std::max<unsigned>(a: 1, b: Ty.getSizeInBits() / 8));
433 }
434
435 unsigned getL1CacheLineSize() const;
436 unsigned getL1PrefetchDistance() const;
437
438 Intrinsic::ID getIntrinsicId(unsigned Opc) const;
439
440private:
441 // Helper function responsible for increasing the latency only.
442 int updateLatency(MachineInstr &SrcInst, MachineInstr &DstInst,
443 bool IsArtificial, int Latency) const;
444 void restoreLatency(SUnit *Src, SUnit *Dst) const;
445 void changeLatency(SUnit *Src, SUnit *Dst, unsigned Lat) const;
446 bool isBestZeroLatency(SUnit *Src, SUnit *Dst, const HexagonInstrInfo *TII,
447 SmallPtrSet<SUnit *, 4> &ExclSrc,
448 SmallPtrSet<SUnit *, 4> &ExclDst) const;
449};
450
451} // end namespace llvm
452
453#endif // LLVM_LIB_TARGET_HEXAGON_HEXAGONSUBTARGET_H
454