Path: blob/main/contrib/llvm-project/llvm/utils/TableGen/Common/CodeGenHwModes.cpp
35290 views
//===--- CodeGenHwModes.cpp -----------------------------------------------===//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//===----------------------------------------------------------------------===//7// Classes to parse and store HW mode information for instruction selection8//===----------------------------------------------------------------------===//910#include "CodeGenHwModes.h"11#include "llvm/Support/Debug.h"12#include "llvm/Support/raw_ostream.h"13#include "llvm/TableGen/Error.h"14#include "llvm/TableGen/Record.h"1516using namespace llvm;1718StringRef CodeGenHwModes::DefaultModeName = "DefaultMode";1920HwMode::HwMode(Record *R) {21Name = R->getName();22Features = std::string(R->getValueAsString("Features"));2324std::vector<Record *> PredicateRecs = R->getValueAsListOfDefs("Predicates");25SmallString<128> PredicateCheck;26raw_svector_ostream OS(PredicateCheck);27ListSeparator LS(" && ");28for (Record *Pred : PredicateRecs) {29StringRef CondString = Pred->getValueAsString("CondString");30if (CondString.empty())31continue;32OS << LS << '(' << CondString << ')';33}3435Predicates = std::string(PredicateCheck);36}3738LLVM_DUMP_METHOD39void HwMode::dump() const { dbgs() << Name << ": " << Features << '\n'; }4041HwModeSelect::HwModeSelect(Record *R, CodeGenHwModes &CGH) {42std::vector<Record *> Modes = R->getValueAsListOfDefs("Modes");43std::vector<Record *> Objects = R->getValueAsListOfDefs("Objects");44if (Modes.size() != Objects.size()) {45PrintError(46R->getLoc(),47"in record " + R->getName() +48" derived from HwModeSelect: the lists Modes and Objects should "49"have the same size");50report_fatal_error("error in target description.");51}52for (unsigned i = 0, e = Modes.size(); i != e; ++i) {53unsigned ModeId = CGH.getHwModeId(Modes[i]);54Items.push_back(std::pair(ModeId, Objects[i]));55}56}5758LLVM_DUMP_METHOD59void HwModeSelect::dump() const {60dbgs() << '{';61for (const PairType &P : Items)62dbgs() << " (" << P.first << ',' << P.second->getName() << ')';63dbgs() << " }\n";64}6566CodeGenHwModes::CodeGenHwModes(RecordKeeper &RK) : Records(RK) {67for (Record *R : Records.getAllDerivedDefinitions("HwMode")) {68// The default mode needs a definition in the .td sources for TableGen69// to accept references to it. We need to ignore the definition here.70if (R->getName() == DefaultModeName)71continue;72Modes.emplace_back(R);73ModeIds.insert(std::pair(R, Modes.size()));74}7576assert(Modes.size() <= 32 && "number of HwModes exceeds maximum of 32");7778for (Record *R : Records.getAllDerivedDefinitions("HwModeSelect")) {79auto P = ModeSelects.emplace(std::pair(R, HwModeSelect(R, *this)));80assert(P.second);81(void)P;82}83}8485unsigned CodeGenHwModes::getHwModeId(Record *R) const {86if (R->getName() == DefaultModeName)87return DefaultMode;88auto F = ModeIds.find(R);89assert(F != ModeIds.end() && "Unknown mode name");90return F->second;91}9293const HwModeSelect &CodeGenHwModes::getHwModeSelect(Record *R) const {94auto F = ModeSelects.find(R);95assert(F != ModeSelects.end() && "Record is not a \"mode select\"");96return F->second;97}9899LLVM_DUMP_METHOD100void CodeGenHwModes::dump() const {101dbgs() << "Modes: {\n";102for (const HwMode &M : Modes) {103dbgs() << " ";104M.dump();105}106dbgs() << "}\n";107108dbgs() << "ModeIds: {\n";109for (const auto &P : ModeIds)110dbgs() << " " << P.first->getName() << " -> " << P.second << '\n';111dbgs() << "}\n";112113dbgs() << "ModeSelects: {\n";114for (const auto &P : ModeSelects) {115dbgs() << " " << P.first->getName() << " -> ";116P.second.dump();117}118dbgs() << "}\n";119}120121122