Path: blob/main/contrib/llvm-project/llvm/utils/TableGen/Common/InfoByHwMode.cpp
35290 views
//===--- InfoByHwMode.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 that implement data parameterized by HW modes for instruction8// selection. Currently it is ValueTypeByHwMode (parameterized ValueType),9// and RegSizeInfoByHwMode (parameterized register/spill size and alignment10// data).11//===----------------------------------------------------------------------===//1213#include "InfoByHwMode.h"14#include "CodeGenTarget.h"15#include "llvm/ADT/STLExtras.h"16#include "llvm/ADT/Twine.h"17#include "llvm/Support/Debug.h"18#include "llvm/Support/raw_ostream.h"19#include "llvm/TableGen/Record.h"20#include <string>2122using namespace llvm;2324std::string llvm::getModeName(unsigned Mode) {25if (Mode == DefaultMode)26return "*";27return (Twine('m') + Twine(Mode)).str();28}2930ValueTypeByHwMode::ValueTypeByHwMode(Record *R, const CodeGenHwModes &CGH) {31const HwModeSelect &MS = CGH.getHwModeSelect(R);32for (const HwModeSelect::PairType &P : MS.Items) {33auto I = Map.insert({P.first, MVT(llvm::getValueType(P.second))});34assert(I.second && "Duplicate entry?");35(void)I;36}37if (R->isSubClassOf("PtrValueType"))38PtrAddrSpace = R->getValueAsInt("AddrSpace");39}4041ValueTypeByHwMode::ValueTypeByHwMode(Record *R, MVT T) : ValueTypeByHwMode(T) {42if (R->isSubClassOf("PtrValueType"))43PtrAddrSpace = R->getValueAsInt("AddrSpace");44}4546bool ValueTypeByHwMode::operator==(const ValueTypeByHwMode &T) const {47assert(isValid() && T.isValid() && "Invalid type in assignment");48bool Simple = isSimple();49if (Simple != T.isSimple())50return false;51if (Simple)52return getSimple() == T.getSimple();5354return Map == T.Map;55}5657bool ValueTypeByHwMode::operator<(const ValueTypeByHwMode &T) const {58assert(isValid() && T.isValid() && "Invalid type in comparison");59// Default order for maps.60return Map < T.Map;61}6263MVT &ValueTypeByHwMode::getOrCreateTypeForMode(unsigned Mode, MVT Type) {64auto F = Map.find(Mode);65if (F != Map.end())66return F->second;67// If Mode is not in the map, look up the default mode. If it exists,68// make a copy of it for Mode and return it.69auto D = Map.begin();70if (D != Map.end() && D->first == DefaultMode)71return Map.insert(std::pair(Mode, D->second)).first->second;72// If default mode is not present either, use provided Type.73return Map.insert(std::pair(Mode, Type)).first->second;74}7576StringRef ValueTypeByHwMode::getMVTName(MVT T) {77StringRef N = llvm::getEnumName(T.SimpleTy);78N.consume_front("MVT::");79return N;80}8182void ValueTypeByHwMode::writeToStream(raw_ostream &OS) const {83if (isSimple()) {84OS << getMVTName(getSimple());85return;86}8788std::vector<const PairType *> Pairs;89for (const auto &P : Map)90Pairs.push_back(&P);91llvm::sort(Pairs, deref<std::less<PairType>>());9293OS << '{';94ListSeparator LS(",");95for (const PairType *P : Pairs)96OS << LS << '(' << getModeName(P->first) << ':'97<< getMVTName(P->second).str() << ')';98OS << '}';99}100101LLVM_DUMP_METHOD102void ValueTypeByHwMode::dump() const { dbgs() << *this << '\n'; }103104ValueTypeByHwMode llvm::getValueTypeByHwMode(Record *Rec,105const CodeGenHwModes &CGH) {106#ifndef NDEBUG107if (!Rec->isSubClassOf("ValueType"))108Rec->dump();109#endif110assert(Rec->isSubClassOf("ValueType") &&111"Record must be derived from ValueType");112if (Rec->isSubClassOf("HwModeSelect"))113return ValueTypeByHwMode(Rec, CGH);114return ValueTypeByHwMode(Rec, llvm::getValueType(Rec));115}116117RegSizeInfo::RegSizeInfo(Record *R) {118RegSize = R->getValueAsInt("RegSize");119SpillSize = R->getValueAsInt("SpillSize");120SpillAlignment = R->getValueAsInt("SpillAlignment");121}122123bool RegSizeInfo::operator<(const RegSizeInfo &I) const {124return std::tie(RegSize, SpillSize, SpillAlignment) <125std::tie(I.RegSize, I.SpillSize, I.SpillAlignment);126}127128bool RegSizeInfo::isSubClassOf(const RegSizeInfo &I) const {129return RegSize <= I.RegSize && SpillAlignment &&130I.SpillAlignment % SpillAlignment == 0 && SpillSize <= I.SpillSize;131}132133void RegSizeInfo::writeToStream(raw_ostream &OS) const {134OS << "[R=" << RegSize << ",S=" << SpillSize << ",A=" << SpillAlignment135<< ']';136}137138RegSizeInfoByHwMode::RegSizeInfoByHwMode(Record *R, const CodeGenHwModes &CGH) {139const HwModeSelect &MS = CGH.getHwModeSelect(R);140for (const HwModeSelect::PairType &P : MS.Items) {141auto I = Map.insert({P.first, RegSizeInfo(P.second)});142assert(I.second && "Duplicate entry?");143(void)I;144}145}146147bool RegSizeInfoByHwMode::operator<(const RegSizeInfoByHwMode &I) const {148unsigned M0 = Map.begin()->first;149return get(M0) < I.get(M0);150}151152bool RegSizeInfoByHwMode::operator==(const RegSizeInfoByHwMode &I) const {153unsigned M0 = Map.begin()->first;154return get(M0) == I.get(M0);155}156157bool RegSizeInfoByHwMode::isSubClassOf(const RegSizeInfoByHwMode &I) const {158unsigned M0 = Map.begin()->first;159return get(M0).isSubClassOf(I.get(M0));160}161162bool RegSizeInfoByHwMode::hasStricterSpillThan(163const RegSizeInfoByHwMode &I) const {164unsigned M0 = Map.begin()->first;165const RegSizeInfo &A0 = get(M0);166const RegSizeInfo &B0 = I.get(M0);167return std::tie(A0.SpillSize, A0.SpillAlignment) >168std::tie(B0.SpillSize, B0.SpillAlignment);169}170171void RegSizeInfoByHwMode::writeToStream(raw_ostream &OS) const {172typedef typename decltype(Map)::value_type PairType;173std::vector<const PairType *> Pairs;174for (const auto &P : Map)175Pairs.push_back(&P);176llvm::sort(Pairs, deref<std::less<PairType>>());177178OS << '{';179ListSeparator LS(",");180for (const PairType *P : Pairs)181OS << LS << '(' << getModeName(P->first) << ':' << P->second << ')';182OS << '}';183}184185SubRegRange::SubRegRange(Record *R) {186Size = R->getValueAsInt("Size");187Offset = R->getValueAsInt("Offset");188}189190SubRegRangeByHwMode::SubRegRangeByHwMode(Record *R, const CodeGenHwModes &CGH) {191const HwModeSelect &MS = CGH.getHwModeSelect(R);192for (const HwModeSelect::PairType &P : MS.Items) {193auto I = Map.insert({P.first, SubRegRange(P.second)});194assert(I.second && "Duplicate entry?");195(void)I;196}197}198199EncodingInfoByHwMode::EncodingInfoByHwMode(Record *R,200const CodeGenHwModes &CGH) {201const HwModeSelect &MS = CGH.getHwModeSelect(R);202for (const HwModeSelect::PairType &P : MS.Items) {203assert(P.second && P.second->isSubClassOf("InstructionEncoding") &&204"Encoding must subclass InstructionEncoding");205auto I = Map.insert({P.first, P.second});206assert(I.second && "Duplicate entry?");207(void)I;208}209}210211namespace llvm {212raw_ostream &operator<<(raw_ostream &OS, const ValueTypeByHwMode &T) {213T.writeToStream(OS);214return OS;215}216217raw_ostream &operator<<(raw_ostream &OS, const RegSizeInfo &T) {218T.writeToStream(OS);219return OS;220}221222raw_ostream &operator<<(raw_ostream &OS, const RegSizeInfoByHwMode &T) {223T.writeToStream(OS);224return OS;225}226} // namespace llvm227228229