1//===-- MipsSubtarget.cpp - Mips Subtarget Information --------------------===//
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 implements the Mips specific subclass of TargetSubtargetInfo.
10//
11//===----------------------------------------------------------------------===//
12
13#include "MipsSubtarget.h"
14#include "Mips.h"
15#include "MipsCallLowering.h"
16#include "MipsLegalizerInfo.h"
17#include "MipsRegisterBankInfo.h"
18#include "MipsRegisterInfo.h"
19#include "MipsSelectionDAGInfo.h"
20#include "MipsTargetMachine.h"
21#include "llvm/IR/Attributes.h"
22#include "llvm/IR/Function.h"
23#include "llvm/MC/TargetRegistry.h"
24#include "llvm/Option/LibraryOptions.h"
25#include "llvm/Support/Debug.h"
26#include "llvm/Support/raw_ostream.h"
27
28using namespace llvm;
29
30#define DEBUG_TYPE "mips-subtarget"
31
32#define GET_SUBTARGETINFO_TARGET_DESC
33#define GET_SUBTARGETINFO_CTOR
34#include "MipsGenSubtargetInfo.inc"
35
36#define OPTIONS_STRUCT_DEFS
37#include "MipsOptions.inc"
38
39bool MipsSubtarget::DspWarningPrinted = false;
40bool MipsSubtarget::MSAWarningPrinted = false;
41bool MipsSubtarget::VirtWarningPrinted = false;
42bool MipsSubtarget::CRCWarningPrinted = false;
43bool MipsSubtarget::GINVWarningPrinted = false;
44
45void MipsSubtarget::anchor() {}
46
47MipsSubtarget::MipsSubtarget(const Triple &TT, StringRef CPU, StringRef FS,
48 StringRef ABIName, bool little,
49 const MipsTargetMachine &TM,
50 MaybeAlign StackAlignOverride)
51 : MipsGenSubtargetInfo(TT, CPU, /*TuneCPU*/ CPU, FS),
52 CLOpts(TM.getCLOpts()), MCCLOpts(TM.getMCCLOpts()),
53 MipsArchVersion(MipsDefault), IsLittle(little), IsSoftFloat(false),
54 IsSingleFloat(false), IsFPXX(false), NoABICalls(false), Abs2008(false),
55 IsFP64bit(false), UseOddSPReg(true), IsNaN2008bit(false),
56 IsGP64bit(false), HasVFPU(false), HasCnMips(false), HasCnMipsP(false),
57 IsR5900(false), FixR5900(false), HasMips3_32(false), HasMips3_32r2(false),
58 HasMips4_32(false), HasMips4_32r2(false), HasMips5_32r2(false),
59 InMips16Mode(false), InMips16HardFloat(CLOpts.mips16_hard_float),
60 InMicroMipsMode(false), HasDSP(false), HasDSPR2(false), HasDSPR3(false),
61 HasMSA(false), UseTCCInDIV(false), HasSym32(false), HasEVA(false),
62 DisableMadd4(false), HasMT(false), HasCRC(false), HasVirt(false),
63 HasGINV(false), UseIndirectJumpsHazard(false), StrictAlign(false),
64 UseCompactBranches(CLOpts.compact_branches != CompactBranchPolicy::Never),
65 StackAlignOverride(StackAlignOverride), TM(TM),
66 ABI(MipsABIInfo::computeTargetABI(TT, ABIName)),
67 InstrInfo(
68 MipsInstrInfo::create(STI&: initializeSubtargetDependencies(CPU, FS, TM))),
69 FrameLowering(MipsFrameLowering::create(ST: *this)),
70 TLInfo(MipsTargetLowering::create(TM, STI: *this)) {
71
72 if (MipsArchVersion == MipsDefault)
73 MipsArchVersion = Mips32;
74
75 // Don't even attempt to generate code for MIPS-V. It has not
76 // been tested and currently exists for the integrated assembler only.
77 if (MipsArchVersion == Mips5)
78 report_fatal_error(reason: "Code generation for MIPS-V is not implemented", gen_crash_diag: false);
79
80 // nanoMIPS is 32-bit architecture and always uses the P32 ABI.
81 // Other MIPS 32-bit architectures supports only O32 ABI.
82 // 64-bit MIPS architectures supports all ABIs except P32.
83 const bool IsValidArchABI =
84 (hasNanoMips() && isABI_P32()) ||
85 (!hasNanoMips() && ((!isGP64bit() && isABI_O32()) || isGP64bit()));
86
87 if (!IsValidArchABI)
88 report_fatal_error(reason: "Invalid Arch & ABI pair.", gen_crash_diag: false);
89
90 if (hasMSA() && !isFP64bit())
91 report_fatal_error(reason: "MSA requires a 64-bit FPU register file (FR=1 mode). "
92 "See -mattr=+fp64.",
93 gen_crash_diag: false);
94
95 if (isFP64bit() && !hasMips64() && hasMips32() && !hasMips32r2())
96 report_fatal_error(
97 reason: "FPU with 64-bit registers is not available on MIPS32 pre revision 2. "
98 "Use -mcpu=mips32r2 or greater.", gen_crash_diag: false);
99
100 if (IsFPXX && (isABI_N32() || isABI_N64()))
101 report_fatal_error(reason: "FPXX is not permitted for the N32/N64 ABI's.", gen_crash_diag: false);
102
103 if (hasMips64r6() && InMicroMipsMode)
104 report_fatal_error(reason: "microMIPS64R6 is not supported", gen_crash_diag: false);
105
106 if (!hasNanoMips()) {
107 if (!isABI_O32() && !useOddSPReg())
108 report_fatal_error(reason: "-mattr=+nooddspreg requires the O32 ABI.", gen_crash_diag: false);
109
110 if (!isABI_O32() && InMicroMipsMode)
111 report_fatal_error(reason: "microMIPS64 is not supported.", gen_crash_diag: false);
112 }
113
114 if (UseIndirectJumpsHazard) {
115 if (InMicroMipsMode)
116 report_fatal_error(
117 reason: "cannot combine indirect jumps with hazard barriers and microMIPS");
118 if (!hasMips32r2())
119 report_fatal_error(
120 reason: "indirect jumps with hazard barriers requires MIPS32R2 or later");
121 }
122 if (inAbs2008Mode() && hasMips32() && !hasMips32r2()) {
123 report_fatal_error(reason: "IEEE 754-2008 abs.fmt is not supported for the given "
124 "architecture.",
125 gen_crash_diag: false);
126 }
127
128 if (hasMips32r6()) {
129 StringRef ISA = hasMips64r6() ? "MIPS64r6" : "MIPS32r6";
130
131 assert(isFP64bit());
132 assert(isNaN2008());
133 assert(inAbs2008Mode());
134 if (hasDSP())
135 report_fatal_error(reason: ISA + " is not compatible with the DSP ASE", gen_crash_diag: false);
136 }
137
138 if (!NoABICalls && hasNanoMips())
139 report_fatal_error(reason: "nanoMIPS requires '-mno-abicalls'");
140
141 if (NoABICalls && TM.isPositionIndependent())
142 report_fatal_error(reason: "position-independent code requires '-mabicalls'");
143
144 if (isABI_N64() && !TM.isPositionIndependent() && !hasSym32())
145 NoABICalls = true;
146
147 // Set UseSmallSection.
148 UseSmallSection = CLOpts.mgpopt;
149 if (!NoABICalls && CLOpts.mgpopt) {
150 errs() << "warning: cannot use small-data accesses for '-mabicalls'"
151 << "\n";
152 UseSmallSection = false;
153 }
154
155 if (hasDSPR2() && !DspWarningPrinted) {
156 if (hasMips64() && !hasMips64r2()) {
157 errs() << "warning: the 'dspr2' ASE requires MIPS64 revision 2 or "
158 << "greater\n";
159 DspWarningPrinted = true;
160 } else if (hasMips32() && !hasMips32r2()) {
161 errs() << "warning: the 'dspr2' ASE requires MIPS32 revision 2 or "
162 << "greater\n";
163 DspWarningPrinted = true;
164 }
165 } else if (hasDSP() && !DspWarningPrinted) {
166 if (hasMips64() && !hasMips64r2()) {
167 errs() << "warning: the 'dsp' ASE requires MIPS64 revision 2 or "
168 << "greater\n";
169 DspWarningPrinted = true;
170 } else if (hasMips32() && !hasMips32r2()) {
171 errs() << "warning: the 'dsp' ASE requires MIPS32 revision 2 or "
172 << "greater\n";
173 DspWarningPrinted = true;
174 }
175 }
176
177 StringRef ArchName = hasMips64() ? "MIPS64" : "MIPS32";
178
179 if (!hasNanoMips()) {
180 if (!hasMips32r5() && hasMSA() && !MSAWarningPrinted) {
181 errs() << "warning: the 'msa' ASE requires " << ArchName
182 << " revision 5 or greater\n";
183 MSAWarningPrinted = true;
184 }
185 if (!hasMips32r5() && hasVirt() && !VirtWarningPrinted) {
186 errs() << "warning: the 'virt' ASE requires " << ArchName
187 << " revision 5 or greater\n";
188 VirtWarningPrinted = true;
189 }
190 if (!hasMips32r6() && hasCRC() && !CRCWarningPrinted) {
191 errs() << "warning: the 'crc' ASE requires " << ArchName
192 << " revision 6 or greater\n";
193 CRCWarningPrinted = true;
194 }
195 if (!hasMips32r6() && hasGINV() && !GINVWarningPrinted) {
196 errs() << "warning: the 'ginv' ASE requires " << ArchName
197 << " revision 6 or greater\n";
198 GINVWarningPrinted = true;
199 }
200 }
201
202 TSInfo = std::make_unique<MipsSelectionDAGInfo>();
203
204 CallLoweringInfo.reset(p: new MipsCallLowering(*getTargetLowering()));
205 Legalizer.reset(p: new MipsLegalizerInfo(*this));
206
207 auto *RBI = new MipsRegisterBankInfo(*getRegisterInfo());
208 RegBankInfo.reset(p: RBI);
209 InstSelector.reset(p: createMipsInstructionSelector(TM, *this, *RBI));
210}
211
212MipsSubtarget::~MipsSubtarget() = default;
213
214bool MipsSubtarget::isPositionIndependent() const {
215 return TM.isPositionIndependent();
216}
217
218/// This overrides the PostRAScheduler bit in the SchedModel for any CPU.
219bool MipsSubtarget::enablePostRAScheduler() const { return true; }
220
221void MipsSubtarget::getCriticalPathRCs(RegClassVector &CriticalPathRCs) const {
222 CriticalPathRCs.clear();
223 CriticalPathRCs.push_back(Elt: isGP64bit() ? &Mips::GPR64RegClass
224 : &Mips::GPR32RegClass);
225}
226
227CodeGenOptLevel MipsSubtarget::getOptLevelToEnablePostRAScheduler() const {
228 return CodeGenOptLevel::Aggressive;
229}
230
231MipsSubtarget &
232MipsSubtarget::initializeSubtargetDependencies(StringRef CPU, StringRef FS,
233 const TargetMachine &TM) {
234 StringRef CPUName = MIPS_MC::selectMipsCPU(TT: TM.getTargetTriple(), CPU);
235
236 // Parse features string.
237 ParseSubtargetFeatures(CPU: CPUName, /*TuneCPU*/ CPUName, FS);
238
239 // O32 uses 32-bit GPRs even when the selected CPU supports a 64-bit ISA.
240 if (isABI_O32())
241 IsGP64bit = false;
242
243 if (InMips16Mode && !IsSoftFloat)
244 InMips16HardFloat = true;
245
246 if (StackAlignOverride)
247 stackAlignment = *StackAlignOverride;
248 else if (isABI_N32() || isABI_N64() || isABI_P32())
249 stackAlignment = Align(16);
250 else {
251 assert(isABI_O32() && "Unknown ABI for stack alignment!");
252 stackAlignment = Align(8);
253 }
254
255 if ((isABI_N32() || isABI_N64()) && !isGP64bit())
256 reportFatalUsageError(reason: "64-bit code requested on a subtarget that doesn't "
257 "support it!");
258
259 return *this;
260}
261
262bool MipsSubtarget::useConstantIslands() const {
263 LLVM_DEBUG(dbgs() << "use constant islands " << CLOpts.mips16_constant_islands
264 << "\n");
265 return CLOpts.mips16_constant_islands;
266}
267
268Reloc::Model MipsSubtarget::getRelocationModel() const {
269 return TM.getRelocationModel();
270}
271
272bool MipsSubtarget::isABI_N64() const { return getABI().IsN64(); }
273bool MipsSubtarget::isABI_N32() const { return getABI().IsN32(); }
274bool MipsSubtarget::isABI_O32() const { return getABI().IsO32(); }
275bool MipsSubtarget::isABI_P32() const { return getABI().IsP32(); }
276
277const SelectionDAGTargetInfo *MipsSubtarget::getSelectionDAGInfo() const {
278 return TSInfo.get();
279}
280
281const CallLowering *MipsSubtarget::getCallLowering() const {
282 return CallLoweringInfo.get();
283}
284
285const LegalizerInfo *MipsSubtarget::getLegalizerInfo() const {
286 return Legalizer.get();
287}
288
289const RegisterBankInfo *MipsSubtarget::getRegBankInfo() const {
290 return RegBankInfo.get();
291}
292
293InstructionSelector *MipsSubtarget::getInstructionSelector() const {
294 return InstSelector.get();
295}
296
297// Libcalls for which no helper is generated. Sorted by name for binary search.
298const RTLIB::LibcallImpl MipsSubtarget::HardFloatLibCalls[34] = {
299 RTLIB::impl___mips16_adddf3, RTLIB::impl___mips16_addsf3,
300 RTLIB::impl___mips16_divdf3, RTLIB::impl___mips16_divsf3,
301 RTLIB::impl___mips16_eqdf2, RTLIB::impl___mips16_eqsf2,
302 RTLIB::impl___mips16_extendsfdf2, RTLIB::impl___mips16_fix_truncdfsi,
303 RTLIB::impl___mips16_fix_truncsfsi, RTLIB::impl___mips16_floatsidf,
304 RTLIB::impl___mips16_floatsisf, RTLIB::impl___mips16_floatunsidf,
305 RTLIB::impl___mips16_floatunsisf, RTLIB::impl___mips16_gedf2,
306 RTLIB::impl___mips16_gesf2, RTLIB::impl___mips16_gtdf2,
307 RTLIB::impl___mips16_gtsf2, RTLIB::impl___mips16_ledf2,
308 RTLIB::impl___mips16_lesf2, RTLIB::impl___mips16_ltdf2,
309 RTLIB::impl___mips16_ltsf2, RTLIB::impl___mips16_muldf3,
310 RTLIB::impl___mips16_mulsf3, RTLIB::impl___mips16_nedf2,
311 RTLIB::impl___mips16_nesf2, RTLIB::impl___mips16_ret_dc,
312 RTLIB::impl___mips16_ret_df, RTLIB::impl___mips16_ret_sc,
313 RTLIB::impl___mips16_ret_sf, RTLIB::impl___mips16_subdf3,
314 RTLIB::impl___mips16_subsf3, RTLIB::impl___mips16_truncdfsf2,
315 RTLIB::impl___mips16_unorddf2, RTLIB::impl___mips16_unordsf2};
316
317void MipsSubtarget::initLibcallLoweringInfo(LibcallLoweringInfo &Info) const {
318 assert(is_sorted(HardFloatLibCalls) && "Array not sorted!");
319
320 static const struct {
321 RTLIB::Libcall LC;
322 RTLIB::LibcallImpl Mips16Impl;
323 RTLIB::LibcallImpl GenericImpl;
324 } HardFloatCalls[] = {
325 {.LC: RTLIB::ADD_F64, .Mips16Impl: RTLIB::impl___mips16_adddf3, .GenericImpl: RTLIB::impl___adddf3},
326 {.LC: RTLIB::ADD_F32, .Mips16Impl: RTLIB::impl___mips16_addsf3, .GenericImpl: RTLIB::impl___addsf3},
327 {.LC: RTLIB::DIV_F64, .Mips16Impl: RTLIB::impl___mips16_divdf3, .GenericImpl: RTLIB::impl___divdf3},
328 {.LC: RTLIB::DIV_F32, .Mips16Impl: RTLIB::impl___mips16_divsf3, .GenericImpl: RTLIB::impl___divsf3},
329 {.LC: RTLIB::FCMP3_PRED_OEQ_F64, .Mips16Impl: RTLIB::impl___mips16_eqdf2,
330 .GenericImpl: RTLIB::impl___eqdf2},
331 {.LC: RTLIB::FCMP3_PRED_OEQ_F32, .Mips16Impl: RTLIB::impl___mips16_eqsf2,
332 .GenericImpl: RTLIB::impl___eqsf2},
333 {.LC: RTLIB::FPEXT_F32_F64, .Mips16Impl: RTLIB::impl___mips16_extendsfdf2,
334 .GenericImpl: RTLIB::impl___extendsfdf2},
335 {.LC: RTLIB::FPTOSINT_F64_I32, .Mips16Impl: RTLIB::impl___mips16_fix_truncdfsi,
336 .GenericImpl: RTLIB::impl___fixdfsi},
337 {.LC: RTLIB::FPTOSINT_F32_I32, .Mips16Impl: RTLIB::impl___mips16_fix_truncsfsi,
338 .GenericImpl: RTLIB::impl___fixsfsi},
339 {.LC: RTLIB::SINTTOFP_I32_F64, .Mips16Impl: RTLIB::impl___mips16_floatsidf,
340 .GenericImpl: RTLIB::impl___floatsidf},
341 {.LC: RTLIB::SINTTOFP_I32_F32, .Mips16Impl: RTLIB::impl___mips16_floatsisf,
342 .GenericImpl: RTLIB::impl___floatsisf},
343 {.LC: RTLIB::UINTTOFP_I32_F64, .Mips16Impl: RTLIB::impl___mips16_floatunsidf,
344 .GenericImpl: RTLIB::impl___floatunsidf},
345 {.LC: RTLIB::UINTTOFP_I32_F32, .Mips16Impl: RTLIB::impl___mips16_floatunsisf,
346 .GenericImpl: RTLIB::impl___floatunsisf},
347 {.LC: RTLIB::FCMP3_PRED_OGE_F64, .Mips16Impl: RTLIB::impl___mips16_gedf2,
348 .GenericImpl: RTLIB::impl___gedf2},
349 {.LC: RTLIB::FCMP3_PRED_OGE_F32, .Mips16Impl: RTLIB::impl___mips16_gesf2,
350 .GenericImpl: RTLIB::impl___gesf2},
351 {.LC: RTLIB::FCMP3_PRED_OGT_F64, .Mips16Impl: RTLIB::impl___mips16_gtdf2,
352 .GenericImpl: RTLIB::impl___gtdf2},
353 {.LC: RTLIB::FCMP3_PRED_OGT_F32, .Mips16Impl: RTLIB::impl___mips16_gtsf2,
354 .GenericImpl: RTLIB::impl___gtsf2},
355 {.LC: RTLIB::FCMP3_PRED_OLE_F64, .Mips16Impl: RTLIB::impl___mips16_ledf2,
356 .GenericImpl: RTLIB::impl___ledf2},
357 {.LC: RTLIB::FCMP3_PRED_OLE_F32, .Mips16Impl: RTLIB::impl___mips16_lesf2,
358 .GenericImpl: RTLIB::impl___lesf2},
359 {.LC: RTLIB::FCMP3_PRED_OLT_F64, .Mips16Impl: RTLIB::impl___mips16_ltdf2,
360 .GenericImpl: RTLIB::impl___ltdf2},
361 {.LC: RTLIB::FCMP3_PRED_OLT_F32, .Mips16Impl: RTLIB::impl___mips16_ltsf2,
362 .GenericImpl: RTLIB::impl___ltsf2},
363 {.LC: RTLIB::MUL_F64, .Mips16Impl: RTLIB::impl___mips16_muldf3, .GenericImpl: RTLIB::impl___muldf3},
364 {.LC: RTLIB::MUL_F32, .Mips16Impl: RTLIB::impl___mips16_mulsf3, .GenericImpl: RTLIB::impl___mulsf3},
365 {.LC: RTLIB::FCMP3_PRED_UNE_F64, .Mips16Impl: RTLIB::impl___mips16_nedf2,
366 .GenericImpl: RTLIB::impl___nedf2},
367 {.LC: RTLIB::FCMP3_PRED_UNE_F32, .Mips16Impl: RTLIB::impl___mips16_nesf2,
368 .GenericImpl: RTLIB::impl___nesf2},
369 {.LC: RTLIB::MIPS16_RET_DC, .Mips16Impl: RTLIB::impl___mips16_ret_dc, .GenericImpl: RTLIB::Unsupported},
370 {.LC: RTLIB::MIPS16_RET_DF, .Mips16Impl: RTLIB::impl___mips16_ret_df, .GenericImpl: RTLIB::Unsupported},
371 {.LC: RTLIB::MIPS16_RET_SC, .Mips16Impl: RTLIB::impl___mips16_ret_sc, .GenericImpl: RTLIB::Unsupported},
372 {.LC: RTLIB::MIPS16_RET_SF, .Mips16Impl: RTLIB::impl___mips16_ret_sf, .GenericImpl: RTLIB::Unsupported},
373 {.LC: RTLIB::SUB_F64, .Mips16Impl: RTLIB::impl___mips16_subdf3, .GenericImpl: RTLIB::impl___subdf3},
374 {.LC: RTLIB::SUB_F32, .Mips16Impl: RTLIB::impl___mips16_subsf3, .GenericImpl: RTLIB::impl___subsf3},
375 {.LC: RTLIB::FPROUND_F64_F32, .Mips16Impl: RTLIB::impl___mips16_truncdfsf2,
376 .GenericImpl: RTLIB::impl___truncdfsf2},
377 {.LC: RTLIB::UO_F64, .Mips16Impl: RTLIB::impl___mips16_unorddf2, .GenericImpl: RTLIB::impl___unorddf2},
378 {.LC: RTLIB::UO_F32, .Mips16Impl: RTLIB::impl___mips16_unordsf2, .GenericImpl: RTLIB::impl___unordsf2},
379 };
380
381 bool UseMips16HardFloat = inMips16Mode() && !useSoftFloat();
382 for (const auto &E : HardFloatCalls) {
383 Info.setLibcallImpl(Call: E.LC,
384 Impl: UseMips16HardFloat ? E.Mips16Impl : E.GenericImpl);
385 }
386}
387