xref: /freebsd-src/contrib/llvm-project/llvm/lib/Target/Mips/MipsSubtarget.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===-- MipsSubtarget.cpp - Mips Subtarget Information --------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements the Mips specific subclass of TargetSubtargetInfo.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "MipsSubtarget.h"
140b57cec5SDimitry Andric #include "Mips.h"
150b57cec5SDimitry Andric #include "MipsCallLowering.h"
160b57cec5SDimitry Andric #include "MipsLegalizerInfo.h"
17349cc55cSDimitry Andric #include "MipsMachineFunction.h"
180b57cec5SDimitry Andric #include "MipsRegisterBankInfo.h"
19349cc55cSDimitry Andric #include "MipsRegisterInfo.h"
20349cc55cSDimitry Andric #include "MipsTargetMachine.h"
210b57cec5SDimitry Andric #include "llvm/IR/Attributes.h"
220b57cec5SDimitry Andric #include "llvm/IR/Function.h"
23349cc55cSDimitry Andric #include "llvm/MC/TargetRegistry.h"
240b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
250b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
260b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric using namespace llvm;
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric #define DEBUG_TYPE "mips-subtarget"
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric #define GET_SUBTARGETINFO_TARGET_DESC
330b57cec5SDimitry Andric #define GET_SUBTARGETINFO_CTOR
340b57cec5SDimitry Andric #include "MipsGenSubtargetInfo.inc"
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric // FIXME: Maybe this should be on by default when Mips16 is specified
370b57cec5SDimitry Andric //
380b57cec5SDimitry Andric static cl::opt<bool>
390b57cec5SDimitry Andric     Mixed16_32("mips-mixed-16-32", cl::init(false),
400b57cec5SDimitry Andric                cl::desc("Allow for a mixture of Mips16 "
410b57cec5SDimitry Andric                         "and Mips32 code in a single output file"),
420b57cec5SDimitry Andric                cl::Hidden);
430b57cec5SDimitry Andric 
440b57cec5SDimitry Andric static cl::opt<bool> Mips_Os16("mips-os16", cl::init(false),
450b57cec5SDimitry Andric                                cl::desc("Compile all functions that don't use "
460b57cec5SDimitry Andric                                         "floating point as Mips 16"),
470b57cec5SDimitry Andric                                cl::Hidden);
480b57cec5SDimitry Andric 
490b57cec5SDimitry Andric static cl::opt<bool> Mips16HardFloat("mips16-hard-float", cl::NotHidden,
500b57cec5SDimitry Andric                                      cl::desc("Enable mips16 hard float."),
510b57cec5SDimitry Andric                                      cl::init(false));
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric static cl::opt<bool>
540b57cec5SDimitry Andric     Mips16ConstantIslands("mips16-constant-islands", cl::NotHidden,
550b57cec5SDimitry Andric                           cl::desc("Enable mips16 constant islands."),
560b57cec5SDimitry Andric                           cl::init(true));
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric static cl::opt<bool>
590b57cec5SDimitry Andric     GPOpt("mgpopt", cl::Hidden,
600b57cec5SDimitry Andric           cl::desc("Enable gp-relative addressing of mips small data items"));
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric bool MipsSubtarget::DspWarningPrinted = false;
630b57cec5SDimitry Andric bool MipsSubtarget::MSAWarningPrinted = false;
640b57cec5SDimitry Andric bool MipsSubtarget::VirtWarningPrinted = false;
650b57cec5SDimitry Andric bool MipsSubtarget::CRCWarningPrinted = false;
660b57cec5SDimitry Andric bool MipsSubtarget::GINVWarningPrinted = false;
6781ad6265SDimitry Andric bool MipsSubtarget::MIPS1WarningPrinted = false;
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric void MipsSubtarget::anchor() {}
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric MipsSubtarget::MipsSubtarget(const Triple &TT, StringRef CPU, StringRef FS,
720b57cec5SDimitry Andric                              bool little, const MipsTargetMachine &TM,
738bcb0991SDimitry Andric                              MaybeAlign StackAlignOverride)
74e8d8bef9SDimitry Andric     : MipsGenSubtargetInfo(TT, CPU, /*TuneCPU*/ CPU, FS),
75e8d8bef9SDimitry Andric       MipsArchVersion(MipsDefault), IsLittle(little), IsSoftFloat(false),
76e8d8bef9SDimitry Andric       IsSingleFloat(false), IsFPXX(false), NoABICalls(false), Abs2008(false),
77e8d8bef9SDimitry Andric       IsFP64bit(false), UseOddSPReg(true), IsNaN2008bit(false),
78e8d8bef9SDimitry Andric       IsGP64bit(false), HasVFPU(false), HasCnMips(false), HasCnMipsP(false),
79e8d8bef9SDimitry Andric       HasMips3_32(false), HasMips3_32r2(false), HasMips4_32(false),
80e8d8bef9SDimitry Andric       HasMips4_32r2(false), HasMips5_32r2(false), InMips16Mode(false),
81e8d8bef9SDimitry Andric       InMips16HardFloat(Mips16HardFloat), InMicroMipsMode(false), HasDSP(false),
82349cc55cSDimitry Andric       HasDSPR2(false), HasDSPR3(false), AllowMixed16_32(Mixed16_32 || Mips_Os16),
83e8d8bef9SDimitry Andric       Os16(Mips_Os16), HasMSA(false), UseTCCInDIV(false), HasSym32(false),
84e8d8bef9SDimitry Andric       HasEVA(false), DisableMadd4(false), HasMT(false), HasCRC(false),
85*0fca6ea1SDimitry Andric       HasVirt(false), HasGINV(false), UseIndirectJumpsHazard(false), StrictAlign(false),
86e8d8bef9SDimitry Andric       StackAlignOverride(StackAlignOverride), TM(TM), TargetTriple(TT),
87e8d8bef9SDimitry Andric       TSInfo(), InstrInfo(MipsInstrInfo::create(
88e8d8bef9SDimitry Andric                     initializeSubtargetDependencies(CPU, FS, TM))),
890b57cec5SDimitry Andric       FrameLowering(MipsFrameLowering::create(*this)),
900b57cec5SDimitry Andric       TLInfo(MipsTargetLowering::create(TM, *this)) {
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric   if (MipsArchVersion == MipsDefault)
930b57cec5SDimitry Andric     MipsArchVersion = Mips32;
940b57cec5SDimitry Andric 
9581ad6265SDimitry Andric   // MIPS-I has not been tested.
9681ad6265SDimitry Andric   if (MipsArchVersion == Mips1 && !MIPS1WarningPrinted) {
9781ad6265SDimitry Andric     errs() << "warning: MIPS-I support is experimental\n";
9881ad6265SDimitry Andric     MIPS1WarningPrinted = true;
9981ad6265SDimitry Andric   }
10081ad6265SDimitry Andric 
10181ad6265SDimitry Andric   // Don't even attempt to generate code for MIPS-V. It has not
10281ad6265SDimitry Andric   // been tested and currently exists for the integrated assembler only.
1030b57cec5SDimitry Andric   if (MipsArchVersion == Mips5)
1040b57cec5SDimitry Andric     report_fatal_error("Code generation for MIPS-V is not implemented", false);
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric   // Check if Architecture and ABI are compatible.
10706c3fb27SDimitry Andric   assert(((!isGP64bit() && isABI_O32()) || isGP64bit()) &&
1080b57cec5SDimitry Andric          "Invalid  Arch & ABI pair.");
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric   if (hasMSA() && !isFP64bit())
1110b57cec5SDimitry Andric     report_fatal_error("MSA requires a 64-bit FPU register file (FR=1 mode). "
1120b57cec5SDimitry Andric                        "See -mattr=+fp64.",
1130b57cec5SDimitry Andric                        false);
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric   if (isFP64bit() && !hasMips64() && hasMips32() && !hasMips32r2())
1160b57cec5SDimitry Andric     report_fatal_error(
1170b57cec5SDimitry Andric         "FPU with 64-bit registers is not available on MIPS32 pre revision 2. "
11881ad6265SDimitry Andric         "Use -mcpu=mips32r2 or greater.", false);
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric   if (!isABI_O32() && !useOddSPReg())
1210b57cec5SDimitry Andric     report_fatal_error("-mattr=+nooddspreg requires the O32 ABI.", false);
1220b57cec5SDimitry Andric 
1230b57cec5SDimitry Andric   if (IsFPXX && (isABI_N32() || isABI_N64()))
1240b57cec5SDimitry Andric     report_fatal_error("FPXX is not permitted for the N32/N64 ABI's.", false);
1250b57cec5SDimitry Andric 
1260b57cec5SDimitry Andric   if (hasMips64r6() && InMicroMipsMode)
1270b57cec5SDimitry Andric     report_fatal_error("microMIPS64R6 is not supported", false);
1280b57cec5SDimitry Andric 
1290b57cec5SDimitry Andric   if (!isABI_O32() && InMicroMipsMode)
1300b57cec5SDimitry Andric     report_fatal_error("microMIPS64 is not supported.", false);
1310b57cec5SDimitry Andric 
1320b57cec5SDimitry Andric   if (UseIndirectJumpsHazard) {
1330b57cec5SDimitry Andric     if (InMicroMipsMode)
1340b57cec5SDimitry Andric       report_fatal_error(
1350b57cec5SDimitry Andric           "cannot combine indirect jumps with hazard barriers and microMIPS");
1360b57cec5SDimitry Andric     if (!hasMips32r2())
1370b57cec5SDimitry Andric       report_fatal_error(
1380b57cec5SDimitry Andric           "indirect jumps with hazard barriers requires MIPS32R2 or later");
1390b57cec5SDimitry Andric   }
1400b57cec5SDimitry Andric   if (inAbs2008Mode() && hasMips32() && !hasMips32r2()) {
1410b57cec5SDimitry Andric     report_fatal_error("IEEE 754-2008 abs.fmt is not supported for the given "
1420b57cec5SDimitry Andric                        "architecture.",
1430b57cec5SDimitry Andric                        false);
1440b57cec5SDimitry Andric   }
1450b57cec5SDimitry Andric 
1460b57cec5SDimitry Andric   if (hasMips32r6()) {
1470b57cec5SDimitry Andric     StringRef ISA = hasMips64r6() ? "MIPS64r6" : "MIPS32r6";
1480b57cec5SDimitry Andric 
1490b57cec5SDimitry Andric     assert(isFP64bit());
1500b57cec5SDimitry Andric     assert(isNaN2008());
1510b57cec5SDimitry Andric     assert(inAbs2008Mode());
1520b57cec5SDimitry Andric     if (hasDSP())
1530b57cec5SDimitry Andric       report_fatal_error(ISA + " is not compatible with the DSP ASE", false);
1540b57cec5SDimitry Andric   }
1550b57cec5SDimitry Andric 
1560b57cec5SDimitry Andric   if (NoABICalls && TM.isPositionIndependent())
1570b57cec5SDimitry Andric     report_fatal_error("position-independent code requires '-mabicalls'");
1580b57cec5SDimitry Andric 
1590b57cec5SDimitry Andric   if (isABI_N64() && !TM.isPositionIndependent() && !hasSym32())
1600b57cec5SDimitry Andric     NoABICalls = true;
1610b57cec5SDimitry Andric 
1620b57cec5SDimitry Andric   // Set UseSmallSection.
1630b57cec5SDimitry Andric   UseSmallSection = GPOpt;
1640b57cec5SDimitry Andric   if (!NoABICalls && GPOpt) {
1650b57cec5SDimitry Andric     errs() << "warning: cannot use small-data accesses for '-mabicalls'"
1660b57cec5SDimitry Andric            << "\n";
1670b57cec5SDimitry Andric     UseSmallSection = false;
1680b57cec5SDimitry Andric   }
1690b57cec5SDimitry Andric 
1700b57cec5SDimitry Andric   if (hasDSPR2() && !DspWarningPrinted) {
1710b57cec5SDimitry Andric     if (hasMips64() && !hasMips64r2()) {
1720b57cec5SDimitry Andric       errs() << "warning: the 'dspr2' ASE requires MIPS64 revision 2 or "
1730b57cec5SDimitry Andric              << "greater\n";
1740b57cec5SDimitry Andric       DspWarningPrinted = true;
1750b57cec5SDimitry Andric     } else if (hasMips32() && !hasMips32r2()) {
1760b57cec5SDimitry Andric       errs() << "warning: the 'dspr2' ASE requires MIPS32 revision 2 or "
1770b57cec5SDimitry Andric              << "greater\n";
1780b57cec5SDimitry Andric       DspWarningPrinted = true;
1790b57cec5SDimitry Andric     }
1800b57cec5SDimitry Andric   } else if (hasDSP() && !DspWarningPrinted) {
1810b57cec5SDimitry Andric     if (hasMips64() && !hasMips64r2()) {
1820b57cec5SDimitry Andric       errs() << "warning: the 'dsp' ASE requires MIPS64 revision 2 or "
1830b57cec5SDimitry Andric              << "greater\n";
1840b57cec5SDimitry Andric       DspWarningPrinted = true;
1850b57cec5SDimitry Andric     } else if (hasMips32() && !hasMips32r2()) {
1860b57cec5SDimitry Andric       errs() << "warning: the 'dsp' ASE requires MIPS32 revision 2 or "
1870b57cec5SDimitry Andric              << "greater\n";
1880b57cec5SDimitry Andric       DspWarningPrinted = true;
1890b57cec5SDimitry Andric     }
1900b57cec5SDimitry Andric   }
1910b57cec5SDimitry Andric 
1920b57cec5SDimitry Andric   StringRef ArchName = hasMips64() ? "MIPS64" : "MIPS32";
1930b57cec5SDimitry Andric 
1940b57cec5SDimitry Andric   if (!hasMips32r5() && hasMSA() && !MSAWarningPrinted) {
1950b57cec5SDimitry Andric     errs() << "warning: the 'msa' ASE requires " << ArchName
1960b57cec5SDimitry Andric            << " revision 5 or greater\n";
1970b57cec5SDimitry Andric     MSAWarningPrinted = true;
1980b57cec5SDimitry Andric   }
1990b57cec5SDimitry Andric   if (!hasMips32r5() && hasVirt() && !VirtWarningPrinted) {
2000b57cec5SDimitry Andric     errs() << "warning: the 'virt' ASE requires " << ArchName
2010b57cec5SDimitry Andric            << " revision 5 or greater\n";
2020b57cec5SDimitry Andric     VirtWarningPrinted = true;
2030b57cec5SDimitry Andric   }
2040b57cec5SDimitry Andric   if (!hasMips32r6() && hasCRC() && !CRCWarningPrinted) {
2050b57cec5SDimitry Andric     errs() << "warning: the 'crc' ASE requires " << ArchName
2060b57cec5SDimitry Andric            << " revision 6 or greater\n";
2070b57cec5SDimitry Andric     CRCWarningPrinted = true;
2080b57cec5SDimitry Andric   }
2090b57cec5SDimitry Andric   if (!hasMips32r6() && hasGINV() && !GINVWarningPrinted) {
2100b57cec5SDimitry Andric     errs() << "warning: the 'ginv' ASE requires " << ArchName
2110b57cec5SDimitry Andric            << " revision 6 or greater\n";
2120b57cec5SDimitry Andric     GINVWarningPrinted = true;
2130b57cec5SDimitry Andric   }
2140b57cec5SDimitry Andric 
2150b57cec5SDimitry Andric   CallLoweringInfo.reset(new MipsCallLowering(*getTargetLowering()));
2160b57cec5SDimitry Andric   Legalizer.reset(new MipsLegalizerInfo(*this));
2170b57cec5SDimitry Andric 
2180b57cec5SDimitry Andric   auto *RBI = new MipsRegisterBankInfo(*getRegisterInfo());
2190b57cec5SDimitry Andric   RegBankInfo.reset(RBI);
220*0fca6ea1SDimitry Andric   InstSelector.reset(createMipsInstructionSelector(TM, *this, *RBI));
2210b57cec5SDimitry Andric }
2220b57cec5SDimitry Andric 
2230b57cec5SDimitry Andric bool MipsSubtarget::isPositionIndependent() const {
2240b57cec5SDimitry Andric   return TM.isPositionIndependent();
2250b57cec5SDimitry Andric }
2260b57cec5SDimitry Andric 
2270b57cec5SDimitry Andric /// This overrides the PostRAScheduler bit in the SchedModel for any CPU.
2280b57cec5SDimitry Andric bool MipsSubtarget::enablePostRAScheduler() const { return true; }
2290b57cec5SDimitry Andric 
2300b57cec5SDimitry Andric void MipsSubtarget::getCriticalPathRCs(RegClassVector &CriticalPathRCs) const {
2310b57cec5SDimitry Andric   CriticalPathRCs.clear();
2320b57cec5SDimitry Andric   CriticalPathRCs.push_back(isGP64bit() ? &Mips::GPR64RegClass
2330b57cec5SDimitry Andric                                         : &Mips::GPR32RegClass);
2340b57cec5SDimitry Andric }
2350b57cec5SDimitry Andric 
2365f757f3fSDimitry Andric CodeGenOptLevel MipsSubtarget::getOptLevelToEnablePostRAScheduler() const {
2375f757f3fSDimitry Andric   return CodeGenOptLevel::Aggressive;
2380b57cec5SDimitry Andric }
2390b57cec5SDimitry Andric 
2400b57cec5SDimitry Andric MipsSubtarget &
2410b57cec5SDimitry Andric MipsSubtarget::initializeSubtargetDependencies(StringRef CPU, StringRef FS,
2420b57cec5SDimitry Andric                                                const TargetMachine &TM) {
2435ffd83dbSDimitry Andric   StringRef CPUName = MIPS_MC::selectMipsCPU(TM.getTargetTriple(), CPU);
2440b57cec5SDimitry Andric 
2450b57cec5SDimitry Andric   // Parse features string.
246e8d8bef9SDimitry Andric   ParseSubtargetFeatures(CPUName, /*TuneCPU*/ CPUName, FS);
2470b57cec5SDimitry Andric   // Initialize scheduling itinerary for the specified CPU.
2480b57cec5SDimitry Andric   InstrItins = getInstrItineraryForCPU(CPUName);
2490b57cec5SDimitry Andric 
2500b57cec5SDimitry Andric   if (InMips16Mode && !IsSoftFloat)
2510b57cec5SDimitry Andric     InMips16HardFloat = true;
2520b57cec5SDimitry Andric 
2530b57cec5SDimitry Andric   if (StackAlignOverride)
2548bcb0991SDimitry Andric     stackAlignment = *StackAlignOverride;
2550b57cec5SDimitry Andric   else if (isABI_N32() || isABI_N64())
2568bcb0991SDimitry Andric     stackAlignment = Align(16);
2570b57cec5SDimitry Andric   else {
2580b57cec5SDimitry Andric     assert(isABI_O32() && "Unknown ABI for stack alignment!");
2598bcb0991SDimitry Andric     stackAlignment = Align(8);
2600b57cec5SDimitry Andric   }
2610b57cec5SDimitry Andric 
262480093f4SDimitry Andric   if ((isABI_N32() || isABI_N64()) && !isGP64bit())
263480093f4SDimitry Andric     report_fatal_error("64-bit code requested on a subtarget that doesn't "
264480093f4SDimitry Andric                        "support it!");
265480093f4SDimitry Andric 
2660b57cec5SDimitry Andric   return *this;
2670b57cec5SDimitry Andric }
2680b57cec5SDimitry Andric 
2690b57cec5SDimitry Andric bool MipsSubtarget::useConstantIslands() {
2700b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "use constant islands " << Mips16ConstantIslands
2710b57cec5SDimitry Andric                     << "\n");
2720b57cec5SDimitry Andric   return Mips16ConstantIslands;
2730b57cec5SDimitry Andric }
2740b57cec5SDimitry Andric 
2750b57cec5SDimitry Andric Reloc::Model MipsSubtarget::getRelocationModel() const {
2760b57cec5SDimitry Andric   return TM.getRelocationModel();
2770b57cec5SDimitry Andric }
2780b57cec5SDimitry Andric 
2790b57cec5SDimitry Andric bool MipsSubtarget::isABI_N64() const { return getABI().IsN64(); }
2800b57cec5SDimitry Andric bool MipsSubtarget::isABI_N32() const { return getABI().IsN32(); }
2810b57cec5SDimitry Andric bool MipsSubtarget::isABI_O32() const { return getABI().IsO32(); }
2820b57cec5SDimitry Andric const MipsABIInfo &MipsSubtarget::getABI() const { return TM.getABI(); }
2830b57cec5SDimitry Andric 
2840b57cec5SDimitry Andric const CallLowering *MipsSubtarget::getCallLowering() const {
2850b57cec5SDimitry Andric   return CallLoweringInfo.get();
2860b57cec5SDimitry Andric }
2870b57cec5SDimitry Andric 
2880b57cec5SDimitry Andric const LegalizerInfo *MipsSubtarget::getLegalizerInfo() const {
2890b57cec5SDimitry Andric   return Legalizer.get();
2900b57cec5SDimitry Andric }
2910b57cec5SDimitry Andric 
2920b57cec5SDimitry Andric const RegisterBankInfo *MipsSubtarget::getRegBankInfo() const {
2930b57cec5SDimitry Andric   return RegBankInfo.get();
2940b57cec5SDimitry Andric }
2950b57cec5SDimitry Andric 
2968bcb0991SDimitry Andric InstructionSelector *MipsSubtarget::getInstructionSelector() const {
2970b57cec5SDimitry Andric   return InstSelector.get();
2980b57cec5SDimitry Andric }
299