Path: blob/main/contrib/llvm-project/llvm/utils/TableGen/Common/SubtargetFeatureInfo.cpp
35290 views
//===- SubtargetFeatureInfo.cpp - Helpers for subtarget features ----------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "SubtargetFeatureInfo.h"9#include "Types.h"10#include "llvm/Config/llvm-config.h"11#include "llvm/TableGen/Error.h"12#include "llvm/TableGen/Record.h"1314using namespace llvm;1516#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)17LLVM_DUMP_METHOD void SubtargetFeatureInfo::dump() const {18errs() << getEnumName() << " " << Index << "\n" << *TheDef;19}20#endif2122std::vector<std::pair<Record *, SubtargetFeatureInfo>>23SubtargetFeatureInfo::getAll(const RecordKeeper &Records) {24std::vector<std::pair<Record *, SubtargetFeatureInfo>> SubtargetFeatures;25std::vector<Record *> AllPredicates =26Records.getAllDerivedDefinitions("Predicate");27for (Record *Pred : AllPredicates) {28// Ignore predicates that are not intended for the assembler.29//30// The "AssemblerMatcherPredicate" string should be promoted to an argument31// if we re-use the machinery for non-assembler purposes in future.32if (!Pred->getValueAsBit("AssemblerMatcherPredicate"))33continue;3435if (Pred->getName().empty())36PrintFatalError(Pred->getLoc(), "Predicate has no name!");3738// Ignore always true predicates.39if (Pred->getValueAsString("CondString").empty())40continue;4142SubtargetFeatures.emplace_back(43Pred, SubtargetFeatureInfo(Pred, SubtargetFeatures.size()));44}45return SubtargetFeatures;46}4748void SubtargetFeatureInfo::emitSubtargetFeatureBitEnumeration(49const SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS,50const std::map<std::string, unsigned> *HwModes) {51OS << "// Bits for subtarget features that participate in "52<< "instruction matching.\n";53unsigned Size = SubtargetFeatures.size();54if (HwModes)55Size += HwModes->size();5657OS << "enum SubtargetFeatureBits : " << getMinimalTypeForRange(Size)58<< " {\n";59for (const auto &SF : SubtargetFeatures) {60const SubtargetFeatureInfo &SFI = SF.second;61OS << " " << SFI.getEnumBitName() << " = " << SFI.Index << ",\n";62}6364if (HwModes) {65unsigned Offset = SubtargetFeatures.size();66for (const auto &M : *HwModes) {67OS << " Feature_HwMode" << M.second << "Bit = " << (M.second + Offset)68<< ",\n";69}70}7172OS << "};\n\n";73}7475void SubtargetFeatureInfo::emitNameTable(76SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS) {77// Need to sort the name table so that lookup by the log of the enum value78// gives the proper name. More specifically, for a feature of value 1<<n,79// SubtargetFeatureNames[n] should be the name of the feature.80uint64_t IndexUB = 0;81for (const auto &SF : SubtargetFeatures)82if (IndexUB <= SF.second.Index)83IndexUB = SF.second.Index + 1;8485std::vector<std::string> Names;86if (IndexUB > 0)87Names.resize(IndexUB);88for (const auto &SF : SubtargetFeatures)89Names[SF.second.Index] = SF.second.getEnumName();9091OS << "static const char *SubtargetFeatureNames[] = {\n";92for (uint64_t I = 0; I < IndexUB; ++I)93OS << " \"" << Names[I] << "\",\n";9495// A small number of targets have no predicates. Null terminate the array to96// avoid a zero-length array.97OS << " nullptr\n"98<< "};\n\n";99}100101void SubtargetFeatureInfo::emitComputeAvailableFeatures(102StringRef TargetName, StringRef ClassName, StringRef FuncName,103const SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS,104StringRef ExtraParams, const std::map<std::string, unsigned> *HwModes) {105OS << "PredicateBitset " << ClassName << "::\n"106<< FuncName << "(const " << TargetName << "Subtarget *Subtarget";107if (!ExtraParams.empty())108OS << ", " << ExtraParams;109OS << ") const {\n";110OS << " PredicateBitset Features{};\n";111for (const auto &SF : SubtargetFeatures) {112const SubtargetFeatureInfo &SFI = SF.second;113StringRef CondStr = SFI.TheDef->getValueAsString("CondString");114assert(!CondStr.empty() && "true predicate should have been filtered");115116OS << " if (" << CondStr << ")\n";117OS << " Features.set(" << SFI.getEnumBitName() << ");\n";118}119120if (HwModes) {121for (const auto &M : *HwModes) {122OS << " if (" << M.first << ")\n";123OS << " Features.set(Feature_HwMode" << M.second << "Bit);\n";124}125}126127OS << " return Features;\n";128OS << "}\n\n";129}130131// If ParenIfBinOp is true, print a surrounding () if Val uses && or ||.132static bool emitFeaturesAux(StringRef TargetName, const Init &Val,133bool ParenIfBinOp, raw_ostream &OS) {134if (auto *D = dyn_cast<DefInit>(&Val)) {135if (!D->getDef()->isSubClassOf("SubtargetFeature"))136return true;137OS << "FB[" << TargetName << "::" << D->getAsString() << "]";138return false;139}140if (auto *D = dyn_cast<DagInit>(&Val)) {141auto *Op = dyn_cast<DefInit>(D->getOperator());142if (!Op)143return true;144StringRef OpName = Op->getDef()->getName();145if (OpName == "not" && D->getNumArgs() == 1) {146OS << '!';147return emitFeaturesAux(TargetName, *D->getArg(0), true, OS);148}149if ((OpName == "any_of" || OpName == "all_of") && D->getNumArgs() > 0) {150bool Paren = D->getNumArgs() > 1 && std::exchange(ParenIfBinOp, true);151if (Paren)152OS << '(';153ListSeparator LS(OpName == "any_of" ? " || " : " && ");154for (auto *Arg : D->getArgs()) {155OS << LS;156if (emitFeaturesAux(TargetName, *Arg, ParenIfBinOp, OS))157return true;158}159if (Paren)160OS << ')';161return false;162}163}164return true;165}166167void SubtargetFeatureInfo::emitComputeAssemblerAvailableFeatures(168StringRef TargetName, StringRef ClassName, StringRef FuncName,169SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS) {170OS << "FeatureBitset ";171if (!ClassName.empty())172OS << TargetName << ClassName << "::\n";173OS << FuncName << "(const FeatureBitset &FB) ";174if (!ClassName.empty())175OS << "const ";176OS << "{\n";177OS << " FeatureBitset Features;\n";178for (const auto &SF : SubtargetFeatures) {179const SubtargetFeatureInfo &SFI = SF.second;180181OS << " if (";182emitFeaturesAux(TargetName, *SFI.TheDef->getValueAsDag("AssemblerCondDag"),183/*ParenIfBinOp=*/false, OS);184OS << ")\n";185OS << " Features.set(" << SFI.getEnumBitName() << ");\n";186}187OS << " return Features;\n";188OS << "}\n\n";189}190191192