Path: blob/main/contrib/llvm-project/llvm/utils/TableGen/Common/InfoByHwMode.h
35290 views
//===--- InfoByHwMode.h -----------------------------------------*- C++ -*-===//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#ifndef LLVM_UTILS_TABLEGEN_INFOBYHWMODE_H14#define LLVM_UTILS_TABLEGEN_INFOBYHWMODE_H1516#include "CodeGenHwModes.h"17#include "llvm/ADT/SmallVector.h"18#include "llvm/ADT/StringRef.h"19#include "llvm/CodeGenTypes/MachineValueType.h"20#include "llvm/Support/Compiler.h"21#include <cassert>22#include <limits>23#include <map>24#include <string>25#include <tuple>26#include <utility>2728namespace llvm {2930class Record;31class raw_ostream;3233template <typename InfoT> struct InfoByHwMode;3435std::string getModeName(unsigned Mode);3637enum : unsigned {38DefaultMode = CodeGenHwModes::DefaultMode,39};4041template <typename InfoT>42void union_modes(const InfoByHwMode<InfoT> &A, const InfoByHwMode<InfoT> &B,43SmallVectorImpl<unsigned> &Modes) {44auto AI = A.begin();45auto BI = B.begin();4647// Skip default mode, but remember if we had one.48bool HasDefault = false;49if (AI != A.end() && AI->first == DefaultMode) {50HasDefault = true;51++AI;52}53if (BI != B.end() && BI->first == DefaultMode) {54HasDefault = true;55++BI;56}5758while (AI != A.end()) {59// If we're done with B, finish A.60if (BI == B.end()) {61for (; AI != A.end(); ++AI)62Modes.push_back(AI->first);63break;64}6566if (BI->first < AI->first) {67Modes.push_back(BI->first);68++BI;69} else {70Modes.push_back(AI->first);71if (AI->first == BI->first)72++BI;73++AI;74}75}7677// Finish B.78for (; BI != B.end(); ++BI)79Modes.push_back(BI->first);8081// Make sure that the default mode is last on the list.82if (HasDefault)83Modes.push_back(DefaultMode);84}8586template <typename InfoT> struct InfoByHwMode {87typedef std::map<unsigned, InfoT> MapType;88typedef typename MapType::value_type PairType;89typedef typename MapType::iterator iterator;90typedef typename MapType::const_iterator const_iterator;9192InfoByHwMode() = default;93InfoByHwMode(const MapType &M) : Map(M) {}9495LLVM_ATTRIBUTE_ALWAYS_INLINE96iterator begin() { return Map.begin(); }97LLVM_ATTRIBUTE_ALWAYS_INLINE98iterator end() { return Map.end(); }99LLVM_ATTRIBUTE_ALWAYS_INLINE100const_iterator begin() const { return Map.begin(); }101LLVM_ATTRIBUTE_ALWAYS_INLINE102const_iterator end() const { return Map.end(); }103LLVM_ATTRIBUTE_ALWAYS_INLINE104bool empty() const { return Map.empty(); }105106LLVM_ATTRIBUTE_ALWAYS_INLINE107bool hasMode(unsigned M) const { return Map.find(M) != Map.end(); }108LLVM_ATTRIBUTE_ALWAYS_INLINE109bool hasDefault() const {110return !Map.empty() && Map.begin()->first == DefaultMode;111}112113InfoT &get(unsigned Mode) {114auto F = Map.find(Mode);115if (F != Map.end())116return F->second;117118// Copy and insert the default mode which should be first.119assert(hasDefault());120auto P = Map.insert({Mode, Map.begin()->second});121return P.first->second;122}123const InfoT &get(unsigned Mode) const {124auto F = Map.find(Mode);125if (F != Map.end())126return F->second;127// Get the default mode which should be first.128F = Map.begin();129assert(F != Map.end() && F->first == DefaultMode);130return F->second;131}132133LLVM_ATTRIBUTE_ALWAYS_INLINE134bool isSimple() const {135return Map.size() == 1 && Map.begin()->first == DefaultMode;136}137LLVM_ATTRIBUTE_ALWAYS_INLINE138const InfoT &getSimple() const {139assert(isSimple());140return Map.begin()->second;141}142void makeSimple(unsigned Mode) {143assert(hasMode(Mode) || hasDefault());144InfoT I = get(Mode);145Map.clear();146Map.insert(std::pair(DefaultMode, I));147}148149protected:150MapType Map;151};152153struct ValueTypeByHwMode : public InfoByHwMode<MVT> {154ValueTypeByHwMode(Record *R, const CodeGenHwModes &CGH);155ValueTypeByHwMode(Record *R, MVT T);156ValueTypeByHwMode(MVT T) { Map.insert({DefaultMode, T}); }157ValueTypeByHwMode() = default;158159bool operator==(const ValueTypeByHwMode &T) const;160bool operator<(const ValueTypeByHwMode &T) const;161162bool isValid() const { return !Map.empty(); }163MVT getType(unsigned Mode) const { return get(Mode); }164MVT &getOrCreateTypeForMode(unsigned Mode, MVT Type);165166static StringRef getMVTName(MVT T);167void writeToStream(raw_ostream &OS) const;168void dump() const;169170unsigned PtrAddrSpace = std::numeric_limits<unsigned>::max();171bool isPointer() const {172return PtrAddrSpace != std::numeric_limits<unsigned>::max();173}174};175176ValueTypeByHwMode getValueTypeByHwMode(Record *Rec, const CodeGenHwModes &CGH);177178raw_ostream &operator<<(raw_ostream &OS, const ValueTypeByHwMode &T);179180struct RegSizeInfo {181unsigned RegSize;182unsigned SpillSize;183unsigned SpillAlignment;184185RegSizeInfo(Record *R);186RegSizeInfo() = default;187bool operator<(const RegSizeInfo &I) const;188bool operator==(const RegSizeInfo &I) const {189return std::tie(RegSize, SpillSize, SpillAlignment) ==190std::tie(I.RegSize, I.SpillSize, I.SpillAlignment);191}192bool operator!=(const RegSizeInfo &I) const { return !(*this == I); }193194bool isSubClassOf(const RegSizeInfo &I) const;195void writeToStream(raw_ostream &OS) const;196};197198struct RegSizeInfoByHwMode : public InfoByHwMode<RegSizeInfo> {199RegSizeInfoByHwMode(Record *R, const CodeGenHwModes &CGH);200RegSizeInfoByHwMode() = default;201bool operator<(const RegSizeInfoByHwMode &VI) const;202bool operator==(const RegSizeInfoByHwMode &VI) const;203bool operator!=(const RegSizeInfoByHwMode &VI) const {204return !(*this == VI);205}206207bool isSubClassOf(const RegSizeInfoByHwMode &I) const;208bool hasStricterSpillThan(const RegSizeInfoByHwMode &I) const;209210void writeToStream(raw_ostream &OS) const;211212void insertRegSizeForMode(unsigned Mode, RegSizeInfo Info) {213Map.insert(std::pair(Mode, Info));214}215};216217raw_ostream &operator<<(raw_ostream &OS, const RegSizeInfo &T);218raw_ostream &operator<<(raw_ostream &OS, const RegSizeInfoByHwMode &T);219220struct SubRegRange {221uint16_t Size;222uint16_t Offset;223224SubRegRange(Record *R);225SubRegRange(uint16_t Size, uint16_t Offset) : Size(Size), Offset(Offset) {}226};227228struct SubRegRangeByHwMode : public InfoByHwMode<SubRegRange> {229SubRegRangeByHwMode(Record *R, const CodeGenHwModes &CGH);230SubRegRangeByHwMode(SubRegRange Range) { Map.insert({DefaultMode, Range}); }231SubRegRangeByHwMode() = default;232233void insertSubRegRangeForMode(unsigned Mode, SubRegRange Info) {234Map.insert(std::pair(Mode, Info));235}236};237238struct EncodingInfoByHwMode : public InfoByHwMode<Record *> {239EncodingInfoByHwMode(Record *R, const CodeGenHwModes &CGH);240EncodingInfoByHwMode() = default;241};242243} // namespace llvm244245#endif // LLVM_UTILS_TABLEGEN_INFOBYHWMODE_H246247248