1//===-- MipsSubtarget.h - Define Subtarget for the Mips ---------*- 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 Mips specific subclass of TargetSubtargetInfo.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_LIB_TARGET_MIPS_MIPSSUBTARGET_H
14#define LLVM_LIB_TARGET_MIPS_MIPSSUBTARGET_H
15
16#include "MCTargetDesc/MipsABIInfo.h"
17#include "MipsFrameLowering.h"
18#include "MipsISelLowering.h"
19#include "MipsInstrInfo.h"
20#include "llvm/CodeGen/GlobalISel/CallLowering.h"
21#include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
22#include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
23#include "llvm/CodeGen/RegisterBankInfo.h"
24#include "llvm/CodeGen/TargetSubtargetInfo.h"
25#include "llvm/IR/DataLayout.h"
26#include "llvm/MC/MCInstrItineraries.h"
27#include "llvm/Support/ErrorHandling.h"
28
29#define GET_SUBTARGETINFO_HEADER
30#include "MipsGenSubtargetInfo.inc"
31
32namespace llvm {
33class StringRef;
34
35enum CompactBranchPolicy {
36 CB_Never, ///< The policy 'never' may in some circumstances or for some
37 ///< ISAs not be absolutely adhered to.
38 CB_Optimal, ///< Optimal is the default and will produce compact branches
39 ///< when appropriate.
40 CB_Always ///< 'always' may in some circumstances may not be
41 ///< absolutely adhered to, there may not be a corresponding
42 ///< compact form of a branch.
43};
44
45class MipsTargetMachine;
46
47class MipsSubtarget : public MipsGenSubtargetInfo {
48 virtual void anchor();
49
50 enum MipsArchEnum {
51 MipsDefault,
52 Mips1, Mips2, Mips32, Mips32r2, Mips32r3, Mips32r5, Mips32r6, Mips32Max,
53 Mips3, Mips4, Mips5, Mips64, Mips64r2, Mips64r3, Mips64r5, Mips64r6
54 };
55
56 enum class CPU { Others, P5600, I6400, I6500 };
57
58 // Used to avoid printing dsp warnings multiple times.
59 static bool DspWarningPrinted;
60
61 // Used to avoid printing msa warnings multiple times.
62 static bool MSAWarningPrinted;
63
64 // Used to avoid printing crc warnings multiple times.
65 static bool CRCWarningPrinted;
66
67 // Used to avoid printing ginv warnings multiple times.
68 static bool GINVWarningPrinted;
69
70 // Used to avoid printing Mips1 warnings multiple times.
71 static bool MIPS1WarningPrinted;
72
73 // Used to avoid printing virt warnings multiple times.
74 static bool VirtWarningPrinted;
75
76 // Mips architecture version
77 MipsArchEnum MipsArchVersion;
78
79 // Processor implementation
80 CPU ProcImpl = CPU::Others;
81
82 // IsLittle - The target is Little Endian
83 bool IsLittle;
84
85 // IsSoftFloat - The target does not support any floating point instructions.
86 bool IsSoftFloat;
87
88 // IsSingleFloat - The target only supports single precision float
89 // point operations. This enable the target to use all 32 32-bit
90 // floating point registers instead of only using even ones.
91 bool IsSingleFloat;
92
93 // IsFPXX - MIPS O32 modeless ABI.
94 bool IsFPXX;
95
96 // NoABICalls - Disable SVR4-style position-independent code.
97 bool NoABICalls;
98
99 // Abs2008 - Use IEEE 754-2008 abs.fmt instruction.
100 bool Abs2008;
101
102 // IsFP64bit - The target processor has 64-bit floating point registers.
103 bool IsFP64bit;
104
105 /// Are odd single-precision registers permitted?
106 /// This corresponds to -modd-spreg and -mno-odd-spreg
107 bool UseOddSPReg;
108
109 // IsNan2008 - IEEE 754-2008 NaN encoding.
110 bool IsNaN2008bit;
111
112 // IsGP64bit - General-purpose registers are 64 bits wide
113 bool IsGP64bit;
114
115 // IsPTR64bit - Pointers are 64 bit wide
116 bool IsPTR64bit;
117
118 // HasVFPU - Processor has a vector floating point unit.
119 bool HasVFPU;
120
121 // CPU supports cnMIPS (Cavium Networks Octeon CPU).
122 bool HasCnMips;
123
124 // CPU supports cnMIPSP (Cavium Networks Octeon+ CPU).
125 bool HasCnMipsP;
126
127 // IsR5900 - CPU is R5900 (PlayStation 2 Emotion Engine).
128 bool IsR5900;
129
130 // FixR5900 - Enable R5900 short loop erratum fix.
131 bool FixR5900;
132
133 // isLinux - Target system is Linux. Is false we consider ELFOS for now.
134 bool IsLinux;
135
136 // UseSmallSection - Small section is used.
137 bool UseSmallSection;
138
139 /// Features related to the presence of specific instructions.
140
141 // HasMips3_32 - The subset of MIPS-III instructions added to MIPS32
142 bool HasMips3_32;
143
144 // HasMips3_32r2 - The subset of MIPS-III instructions added to MIPS32r2
145 bool HasMips3_32r2;
146
147 // HasMips4_32 - Has the subset of MIPS-IV present in MIPS32
148 bool HasMips4_32;
149
150 // HasMips4_32r2 - Has the subset of MIPS-IV present in MIPS32r2
151 bool HasMips4_32r2;
152
153 // HasMips5_32r2 - Has the subset of MIPS-V present in MIPS32r2
154 bool HasMips5_32r2;
155
156 // InMips16 -- can process Mips16 instructions
157 bool InMips16Mode;
158
159 // Mips16 hard float
160 bool InMips16HardFloat;
161
162 // InMicroMips -- can process MicroMips instructions
163 bool InMicroMipsMode;
164
165 // HasDSP, HasDSPR2, HasDSPR3 -- supports DSP ASE.
166 bool HasDSP, HasDSPR2, HasDSPR3;
167
168 // Has3D -- Supports Mips3D ASE.
169 bool Has3D;
170
171 // Allow mixed Mips16 and Mips32 in one source file
172 bool AllowMixed16_32;
173
174 // Optimize for space by compiling all functions as Mips 16 unless
175 // it needs floating point. Functions needing floating point are
176 // compiled as Mips32
177 bool Os16;
178
179 // HasMSA -- supports MSA ASE.
180 bool HasMSA;
181
182 // UseTCCInDIV -- Enables the use of trapping in the assembler.
183 bool UseTCCInDIV;
184
185 // Sym32 -- On Mips64 symbols are 32 bits.
186 bool HasSym32;
187
188 // HasEVA -- supports EVA ASE.
189 bool HasEVA;
190
191 // nomadd4 - disables generation of 4-operand madd.s, madd.d and
192 // related instructions.
193 bool DisableMadd4;
194
195 // HasMT -- support MT ASE.
196 bool HasMT;
197
198 // HasCRC -- supports R6 CRC ASE
199 bool HasCRC;
200
201 // HasVirt -- supports Virtualization ASE
202 bool HasVirt;
203
204 // HasGINV -- supports R6 Global INValidate ASE
205 bool HasGINV;
206
207 // Use hazard variants of the jump register instructions for indirect
208 // function calls and jump tables.
209 bool UseIndirectJumpsHazard;
210
211 // Disable use of the `jal` instruction.
212 bool UseLongCalls = false;
213
214 // Assume 32-bit GOT.
215 bool UseXGOT = false;
216
217 // Disable unaligned load store for r6.
218 bool StrictAlign;
219
220 // Use compact branch instructions for R6.
221 bool UseCompactBranches = true;
222
223 /// The minimum alignment known to hold of the stack frame on
224 /// entry to the function and which must be maintained by every function.
225 Align stackAlignment;
226
227 /// The overridden stack alignment.
228 MaybeAlign StackAlignOverride;
229
230 InstrItineraryData InstrItins;
231
232 // We can override the determination of whether we are in mips16 mode
233 // as from the command line
234 enum {NoOverride, Mips16Override, NoMips16Override} OverrideMode;
235
236 const MipsTargetMachine &TM;
237
238 Triple TargetTriple;
239
240 std::unique_ptr<const SelectionDAGTargetInfo> TSInfo;
241 std::unique_ptr<const MipsInstrInfo> InstrInfo;
242 std::unique_ptr<const MipsFrameLowering> FrameLowering;
243 std::unique_ptr<const MipsTargetLowering> TLInfo;
244
245public:
246 bool isPositionIndependent() const;
247 /// This overrides the PostRAScheduler bit in the SchedModel for each CPU.
248 bool enablePostRAScheduler() const override;
249 void getCriticalPathRCs(RegClassVector &CriticalPathRCs) const override;
250 CodeGenOptLevel getOptLevelToEnablePostRAScheduler() const override;
251
252 bool isABI_N64() const;
253 bool isABI_N32() const;
254 bool isABI_O32() const;
255 const MipsABIInfo &getABI() const;
256 bool isABI_FPXX() const { return isABI_O32() && IsFPXX; }
257
258 /// This constructor initializes the data members to match that
259 /// of the specified triple.
260 MipsSubtarget(const Triple &TT, StringRef CPU, StringRef FS, bool little,
261 const MipsTargetMachine &TM, MaybeAlign StackAlignOverride);
262
263 ~MipsSubtarget() override;
264
265 /// ParseSubtargetFeatures - Parses features string setting specified
266 /// subtarget options. Definition of function is auto generated by tblgen.
267 void ParseSubtargetFeatures(StringRef CPU, StringRef TuneCPU, StringRef FS);
268
269 static const RTLIB::LibcallImpl HardFloatLibCalls[34];
270
271 bool hasMips1() const { return MipsArchVersion >= Mips1; }
272 bool hasMips2() const { return MipsArchVersion >= Mips2; }
273 bool hasMips3() const { return MipsArchVersion >= Mips3; }
274 bool hasMips4() const { return MipsArchVersion >= Mips4; }
275 bool hasMips5() const { return MipsArchVersion >= Mips5; }
276 bool hasMips4_32() const { return HasMips4_32; }
277 bool hasMips4_32r2() const { return HasMips4_32r2; }
278 bool hasMips32() const {
279 return (MipsArchVersion >= Mips32 && MipsArchVersion < Mips32Max) ||
280 hasMips64();
281 }
282 bool hasMips32r2() const {
283 return (MipsArchVersion >= Mips32r2 && MipsArchVersion < Mips32Max) ||
284 hasMips64r2();
285 }
286 bool hasMips32r3() const {
287 return (MipsArchVersion >= Mips32r3 && MipsArchVersion < Mips32Max) ||
288 hasMips64r2();
289 }
290 bool hasMips32r5() const {
291 return (MipsArchVersion >= Mips32r5 && MipsArchVersion < Mips32Max) ||
292 hasMips64r5();
293 }
294 bool hasMips32r6() const {
295 return (MipsArchVersion >= Mips32r6 && MipsArchVersion < Mips32Max) ||
296 hasMips64r6();
297 }
298 bool hasMips64() const { return MipsArchVersion >= Mips64; }
299 bool hasMips64r2() const { return MipsArchVersion >= Mips64r2; }
300 bool hasMips64r3() const { return MipsArchVersion >= Mips64r3; }
301 bool hasMips64r5() const { return MipsArchVersion >= Mips64r5; }
302 bool hasMips64r6() const { return MipsArchVersion >= Mips64r6; }
303
304 bool hasCnMips() const { return HasCnMips; }
305 bool hasCnMipsP() const { return HasCnMipsP; }
306 bool isR5900() const { return IsR5900; }
307 bool fixR5900() const { return FixR5900; }
308
309 bool isLittle() const { return IsLittle; }
310 bool isABICalls() const { return !NoABICalls; }
311 bool isFPXX() const { return IsFPXX; }
312 bool isFP64bit() const { return IsFP64bit; }
313 bool useOddSPReg() const { return UseOddSPReg; }
314 bool noOddSPReg() const { return !UseOddSPReg; }
315 bool isNaN2008() const { return IsNaN2008bit; }
316 bool inAbs2008Mode() const { return Abs2008; }
317 bool isGP64bit() const { return IsGP64bit; }
318 bool isGP32bit() const { return !IsGP64bit; }
319 unsigned getGPRSizeInBytes() const { return isGP64bit() ? 8 : 4; }
320 bool isPTR64bit() const { return IsPTR64bit; }
321 bool isPTR32bit() const { return !IsPTR64bit; }
322 bool hasSym32() const {
323 return (HasSym32 && isABI_N64()) || isABI_N32() || isABI_O32();
324 }
325 bool isSingleFloat() const { return IsSingleFloat; }
326 bool isTargetCOFF() const { return TargetTriple.isOSBinFormatCOFF(); }
327 bool isTargetELF() const { return TargetTriple.isOSBinFormatELF(); }
328 bool hasVFPU() const { return HasVFPU; }
329 bool inMips16Mode() const { return InMips16Mode; }
330 bool inMips16ModeDefault() const {
331 return InMips16Mode;
332 }
333 // Hard float for mips16 means essentially to compile as soft float
334 // but to use a runtime library for soft float that is written with
335 // native mips32 floating point instructions (those runtime routines
336 // run in mips32 hard float mode).
337 bool inMips16HardFloat() const {
338 return inMips16Mode() && InMips16HardFloat;
339 }
340 bool inMicroMipsMode() const { return InMicroMipsMode && !InMips16Mode; }
341 bool inMicroMips32r6Mode() const {
342 return inMicroMipsMode() && hasMips32r6();
343 }
344 bool hasDSP() const { return HasDSP; }
345 bool hasDSPR2() const { return HasDSPR2; }
346 bool hasDSPR3() const { return HasDSPR3; }
347 bool has3D() const { return Has3D; }
348 bool hasMSA() const { return HasMSA; }
349 bool disableMadd4() const { return DisableMadd4; }
350 bool hasEVA() const { return HasEVA; }
351 bool hasMT() const { return HasMT; }
352 bool hasCRC() const { return HasCRC; }
353 bool hasVirt() const { return HasVirt; }
354 bool hasGINV() const { return HasGINV; }
355 bool useIndirectJumpsHazard() const {
356 return UseIndirectJumpsHazard && hasMips32r2();
357 }
358 bool useSmallSection() const { return UseSmallSection; }
359
360 bool useCompactBranches() const {
361 return UseCompactBranches && hasMips32r6();
362 }
363
364 bool hasStandardEncoding() const { return !InMips16Mode && !InMicroMipsMode; }
365
366 bool useSoftFloat() const { return IsSoftFloat; }
367
368 bool useLongCalls() const { return UseLongCalls; }
369
370 bool useXGOT() const { return UseXGOT; }
371
372 bool enableLongBranchPass() const {
373 return hasStandardEncoding() || inMicroMipsMode() || allowMixed16_32();
374 }
375
376 /// Features related to the presence of specific instructions.
377 bool hasExtractInsert() const { return !inMips16Mode() && hasMips32r2(); }
378 bool hasMTHC1() const { return hasMips32r2(); }
379
380 bool allowMixed16_32() const { return inMips16ModeDefault() |
381 AllowMixed16_32; }
382
383 bool os16() const { return Os16; }
384
385 bool isTargetWindows() const { return TargetTriple.isOSWindows(); }
386
387 bool isXRaySupported() const override { return true; }
388
389 // for now constant islands are on for the whole compilation unit but we only
390 // really use them if in addition we are in mips16 mode
391 static bool useConstantIslands();
392
393 Align getStackAlignment() const { return stackAlignment; }
394
395 // Grab relocation model
396 Reloc::Model getRelocationModel() const;
397
398 MipsSubtarget &initializeSubtargetDependencies(StringRef CPU, StringRef FS,
399 const TargetMachine &TM);
400
401 /// Does the system support unaligned memory access.
402 ///
403 /// MIPS32r6/MIPS64r6 require full unaligned access support but does not
404 /// specify which component of the system provides it. Hardware, software, and
405 /// hybrid implementations are all valid.
406 bool systemSupportsUnalignedAccess() const {
407 return hasMips32r6() && !StrictAlign;
408 }
409
410 // Set helper classes
411 void setHelperClassesMips16();
412 void setHelperClassesMipsSE();
413
414 const SelectionDAGTargetInfo *getSelectionDAGInfo() const override;
415
416 const MipsInstrInfo *getInstrInfo() const override { return InstrInfo.get(); }
417 const TargetFrameLowering *getFrameLowering() const override {
418 return FrameLowering.get();
419 }
420 const MipsRegisterInfo *getRegisterInfo() const override {
421 return &InstrInfo->getRegisterInfo();
422 }
423 const MipsTargetLowering *getTargetLowering() const override {
424 return TLInfo.get();
425 }
426 const InstrItineraryData *getInstrItineraryData() const override {
427 return &InstrItins;
428 }
429
430 void initLibcallLoweringInfo(LibcallLoweringInfo &Info) const override;
431
432protected:
433 // GlobalISel related APIs.
434 std::unique_ptr<CallLowering> CallLoweringInfo;
435 std::unique_ptr<LegalizerInfo> Legalizer;
436 std::unique_ptr<RegisterBankInfo> RegBankInfo;
437 std::unique_ptr<InstructionSelector> InstSelector;
438
439public:
440 const CallLowering *getCallLowering() const override;
441 const LegalizerInfo *getLegalizerInfo() const override;
442 const RegisterBankInfo *getRegBankInfo() const override;
443 InstructionSelector *getInstructionSelector() const override;
444};
445} // End llvm namespace
446
447#endif
448