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