Path: blob/main/contrib/llvm-project/llvm/utils/TableGen/Common/GlobalISel/Patterns.h
35315 views
//===- Patterns.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//8/// \file Contains the Pattern hierarchy alongside helper classes such as9/// PatFrag, MIFlagsInfo, PatternType, etc.10///11/// These classes are used by the GlobalISel Combiner backend to help parse,12/// process and emit MIR patterns.13//14//===----------------------------------------------------------------------===//1516#ifndef LLVM_UTILS_GLOBALISEL_PATTERNS_H17#define LLVM_UTILS_GLOBALISEL_PATTERNS_H1819#include "llvm/ADT/ArrayRef.h"20#include "llvm/ADT/SetVector.h"21#include "llvm/ADT/SmallVector.h"22#include "llvm/ADT/StringMap.h"23#include "llvm/ADT/StringRef.h"24#include "llvm/ADT/Twine.h"25#include <memory>26#include <optional>27#include <string>2829namespace llvm {3031class Record;32class SMLoc;33class StringInit;34class CodeExpansions;35class CodeGenInstruction;36struct CodeGenIntrinsic;3738namespace gi {3940class CXXPredicateCode;41class LLTCodeGen;42class LLTCodeGenOrTempType;43class RuleMatcher;4445//===- PatternType --------------------------------------------------------===//4647/// Represent the type of a Pattern Operand.48///49/// Types have two form:50/// - LLTs, which are straightforward.51/// - Special types, e.g. GITypeOf52class PatternType {53public:54static constexpr StringLiteral SpecialTyClassName = "GISpecialType";55static constexpr StringLiteral TypeOfClassName = "GITypeOf";5657enum PTKind : uint8_t {58PT_None,5960PT_ValueType,61PT_TypeOf,62};6364PatternType() : Kind(PT_None), Data() {}6566static std::optional<PatternType> get(ArrayRef<SMLoc> DiagLoc,67const Record *R, Twine DiagCtx);68static PatternType getTypeOf(StringRef OpName);6970bool isNone() const { return Kind == PT_None; }71bool isLLT() const { return Kind == PT_ValueType; }72bool isSpecial() const { return isTypeOf(); }73bool isTypeOf() const { return Kind == PT_TypeOf; }7475StringRef getTypeOfOpName() const;76const Record *getLLTRecord() const;7778explicit operator bool() const { return !isNone(); }7980bool operator==(const PatternType &Other) const;81bool operator!=(const PatternType &Other) const { return !operator==(Other); }8283std::string str() const;8485private:86PatternType(PTKind Kind) : Kind(Kind), Data() {}8788PTKind Kind;89union DataT {90DataT() : Str() {}9192/// PT_ValueType -> ValueType Def.93const Record *Def;9495/// PT_TypeOf -> Operand name (without the '$')96StringRef Str;97} Data;98};99100//===- Pattern Base Class -------------------------------------------------===//101102/// Base class for all patterns that can be written in an `apply`, `match` or103/// `pattern` DAG operator.104///105/// For example:106///107/// (apply (G_ZEXT $x, $y), (G_ZEXT $y, $z), "return isFoo(${z})")108///109/// Creates 3 Pattern objects:110/// - Two CodeGenInstruction Patterns111/// - A CXXPattern112class Pattern {113public:114enum {115K_AnyOpcode,116K_CXX,117118K_CodeGenInstruction,119K_PatFrag,120K_Builtin,121};122123virtual ~Pattern() = default;124125unsigned getKind() const { return Kind; }126const char *getKindName() const;127128bool hasName() const { return !Name.empty(); }129StringRef getName() const { return Name; }130131virtual void print(raw_ostream &OS, bool PrintName = true) const = 0;132void dump() const;133134protected:135Pattern(unsigned Kind, StringRef Name) : Kind(Kind), Name(Name) {136assert(!Name.empty() && "unnamed pattern!");137}138139void printImpl(raw_ostream &OS, bool PrintName,140function_ref<void()> ContentPrinter) const;141142private:143unsigned Kind;144StringRef Name;145};146147//===- AnyOpcodePattern ---------------------------------------------------===//148149/// `wip_match_opcode` patterns.150/// This matches one or more opcodes, and does not check any operands151/// whatsoever.152///153/// TODO: Long-term, this needs to be removed. It's a hack around MIR154/// pattern matching limitations.155class AnyOpcodePattern : public Pattern {156public:157AnyOpcodePattern(StringRef Name) : Pattern(K_AnyOpcode, Name) {}158159static bool classof(const Pattern *P) { return P->getKind() == K_AnyOpcode; }160161void addOpcode(const CodeGenInstruction *I) { Insts.push_back(I); }162const auto &insts() const { return Insts; }163164void print(raw_ostream &OS, bool PrintName = true) const override;165166private:167SmallVector<const CodeGenInstruction *, 4> Insts;168};169170//===- CXXPattern ---------------------------------------------------------===//171172/// Represents raw C++ code which may need some expansions.173///174/// e.g. [{ return isFooBux(${src}.getReg()); }]175///176/// For the expanded code, \see CXXPredicateCode. CXXPredicateCode objects are177/// created through `expandCode`.178///179/// \see CodeExpander and \see CodeExpansions for more information on code180/// expansions.181///182/// This object has two purposes:183/// - Represent C++ code as a pattern entry.184/// - Be a factory for expanded C++ code.185/// - It's immutable and only holds the raw code so we can expand the same186/// CXX pattern multiple times if we need to.187///188/// Note that the code is always trimmed in the constructor, so leading and189/// trailing whitespaces are removed. This removes bloat in the output, avoids190/// formatting issues, but also allows us to check things like191/// `.startswith("return")` trivially without worrying about spaces.192class CXXPattern : public Pattern {193public:194CXXPattern(const StringInit &Code, StringRef Name);195196CXXPattern(StringRef Code, StringRef Name)197: Pattern(K_CXX, Name), RawCode(Code.trim().str()) {}198199static bool classof(const Pattern *P) { return P->getKind() == K_CXX; }200201void setIsApply(bool Value = true) { IsApply = Value; }202StringRef getRawCode() const { return RawCode; }203204/// Expands raw code, replacing things such as `${foo}` with their205/// substitution in \p CE.206///207/// Can only be used on 'match' CXX Patterns. 'apply' CXX pattern emission208/// is handled differently as we emit both the 'match' and 'apply' part209/// together in a single Custom CXX Action.210///211/// \param CE Map of Code Expansions212/// \param Locs SMLocs for the Code Expander, in case it needs to emit213/// diagnostics.214/// \param AddComment Optionally called to emit a comment before the expanded215/// code.216///217/// \return A CXXPredicateCode object that contains the expanded code. Note218/// that this may or may not insert a new object. All CXXPredicateCode objects219/// are held in a set to avoid emitting duplicate C++ code.220const CXXPredicateCode &221expandCode(const CodeExpansions &CE, ArrayRef<SMLoc> Locs,222function_ref<void(raw_ostream &)> AddComment = {}) const;223224void print(raw_ostream &OS, bool PrintName = true) const override;225226private:227bool IsApply = false;228std::string RawCode;229};230231//===- InstructionPattern ---------------------------------------------===//232233/// An operand for an InstructionPattern.234///235/// Operands are composed of three elements:236/// - (Optional) Value237/// - (Optional) Name238/// - (Optional) Type239///240/// Some examples:241/// (i32 0):$x -> V=int(0), Name='x', Type=i32242/// 0:$x -> V=int(0), Name='x'243/// $x -> Name='x'244/// i32:$x -> Name='x', Type = i32245class InstructionOperand {246public:247using IntImmTy = int64_t;248249InstructionOperand(IntImmTy Imm, StringRef Name, PatternType Type)250: Value(Imm), Name(Name), Type(Type) {}251252InstructionOperand(StringRef Name, PatternType Type)253: Name(Name), Type(Type) {}254255bool isNamedImmediate() const { return hasImmValue() && isNamedOperand(); }256257bool hasImmValue() const { return Value.has_value(); }258IntImmTy getImmValue() const { return *Value; }259260bool isNamedOperand() const { return !Name.empty(); }261StringRef getOperandName() const {262assert(isNamedOperand() && "Operand is unnamed");263return Name;264}265266InstructionOperand withNewName(StringRef NewName) const {267InstructionOperand Result = *this;268Result.Name = NewName;269return Result;270}271272void setIsDef(bool Value = true) { Def = Value; }273bool isDef() const { return Def; }274275void setType(PatternType NewType) {276assert((!Type || (Type == NewType)) && "Overwriting type!");277Type = NewType;278}279PatternType getType() const { return Type; }280281std::string describe() const;282void print(raw_ostream &OS) const;283284void dump() const;285286private:287std::optional<int64_t> Value;288StringRef Name;289PatternType Type;290bool Def = false;291};292293/// Base class for CodeGenInstructionPattern & PatFragPattern, which handles all294/// the boilerplate for patterns that have a list of operands for some (pseudo)295/// instruction.296class InstructionPattern : public Pattern {297public:298virtual ~InstructionPattern() = default;299300static bool classof(const Pattern *P) {301return P->getKind() == K_CodeGenInstruction || P->getKind() == K_PatFrag ||302P->getKind() == K_Builtin;303}304305template <typename... Ty> void addOperand(Ty &&...Init) {306Operands.emplace_back(std::forward<Ty>(Init)...);307}308309auto &operands() { return Operands; }310const auto &operands() const { return Operands; }311unsigned operands_size() const { return Operands.size(); }312InstructionOperand &getOperand(unsigned K) { return Operands[K]; }313const InstructionOperand &getOperand(unsigned K) const { return Operands[K]; }314315/// When this InstructionPattern is used as the match root, returns the316/// operands that must be redefined in the 'apply' pattern for the rule to be317/// valid.318///319/// For most patterns, this just returns the defs.320/// For PatFrag this only returns the root of the PF.321///322/// Returns an empty array on error.323virtual ArrayRef<InstructionOperand> getApplyDefsNeeded() const {324return {operands().begin(), getNumInstDefs()};325}326327auto named_operands() {328return make_filter_range(Operands,329[&](auto &O) { return O.isNamedOperand(); });330}331332auto named_operands() const {333return make_filter_range(Operands,334[&](auto &O) { return O.isNamedOperand(); });335}336337virtual bool isVariadic() const { return false; }338virtual unsigned getNumInstOperands() const = 0;339virtual unsigned getNumInstDefs() const = 0;340341bool hasAllDefs() const { return operands_size() >= getNumInstDefs(); }342343virtual StringRef getInstName() const = 0;344345/// Diagnoses all uses of special types in this Pattern and returns true if at346/// least one diagnostic was emitted.347bool diagnoseAllSpecialTypes(ArrayRef<SMLoc> Loc, Twine Msg) const;348349void reportUnreachable(ArrayRef<SMLoc> Locs) const;350virtual bool checkSemantics(ArrayRef<SMLoc> Loc);351352void print(raw_ostream &OS, bool PrintName = true) const override;353354protected:355InstructionPattern(unsigned K, StringRef Name) : Pattern(K, Name) {}356357virtual void printExtras(raw_ostream &OS) const {}358359SmallVector<InstructionOperand, 4> Operands;360};361362//===- OperandTable -------------------------------------------------------===//363364/// Maps InstructionPattern operands to their definitions. This allows us to tie365/// different patterns of a (apply), (match) or (patterns) set of patterns366/// together.367class OperandTable {368public:369bool addPattern(InstructionPattern *P,370function_ref<void(StringRef)> DiagnoseRedef);371372struct LookupResult {373LookupResult() = default;374LookupResult(InstructionPattern *Def) : Found(true), Def(Def) {}375376bool Found = false;377InstructionPattern *Def = nullptr;378379bool isLiveIn() const { return Found && !Def; }380};381382LookupResult lookup(StringRef OpName) const {383if (auto It = Table.find(OpName); It != Table.end())384return LookupResult(It->second);385return LookupResult();386}387388InstructionPattern *getDef(StringRef OpName) const {389return lookup(OpName).Def;390}391392void print(raw_ostream &OS, StringRef Name = "", StringRef Indent = "") const;393394auto begin() const { return Table.begin(); }395auto end() const { return Table.end(); }396397void dump() const;398399private:400StringMap<InstructionPattern *> Table;401};402403//===- MIFlagsInfo --------------------------------------------------------===//404405/// Helper class to contain data associated with a MIFlags operand.406class MIFlagsInfo {407public:408void addSetFlag(const Record *R);409void addUnsetFlag(const Record *R);410void addCopyFlag(StringRef InstName);411412const auto &set_flags() const { return SetF; }413const auto &unset_flags() const { return UnsetF; }414const auto ©_flags() const { return CopyF; }415416private:417SetVector<StringRef> SetF, UnsetF, CopyF;418};419420//===- CodeGenInstructionPattern ------------------------------------------===//421422/// Matches an instruction or intrinsic:423/// e.g. `G_ADD $x, $y, $z` or `int_amdgcn_cos $a`424///425/// Intrinsics are just normal instructions with a special operand for intrinsic426/// ID. Despite G_INTRINSIC opcodes being variadic, we consider that the427/// Intrinsic's info takes priority. This means we return:428/// - false for isVariadic() and other variadic-related queries.429/// - getNumInstDefs and getNumInstOperands use the intrinsic's in/out430/// operands.431class CodeGenInstructionPattern : public InstructionPattern {432public:433CodeGenInstructionPattern(const CodeGenInstruction &I, StringRef Name)434: InstructionPattern(K_CodeGenInstruction, Name), I(I) {}435436static bool classof(const Pattern *P) {437return P->getKind() == K_CodeGenInstruction;438}439440bool is(StringRef OpcodeName) const;441442void setIntrinsic(const CodeGenIntrinsic *I) { IntrinInfo = I; }443const CodeGenIntrinsic *getIntrinsic() const { return IntrinInfo; }444bool isIntrinsic() const { return IntrinInfo; }445446bool hasVariadicDefs() const;447bool isVariadic() const override;448unsigned getNumInstDefs() const override;449unsigned getNumInstOperands() const override;450451MIFlagsInfo &getOrCreateMIFlagsInfo();452const MIFlagsInfo *getMIFlagsInfo() const { return FI.get(); }453454const CodeGenInstruction &getInst() const { return I; }455StringRef getInstName() const override;456457private:458void printExtras(raw_ostream &OS) const override;459460const CodeGenInstruction &I;461const CodeGenIntrinsic *IntrinInfo = nullptr;462std::unique_ptr<MIFlagsInfo> FI;463};464465//===- OperandTypeChecker -------------------------------------------------===//466467/// This is a trivial type checker for all operands in a set of468/// InstructionPatterns.469///470/// It infers the type of each operand, check it's consistent with the known471/// type of the operand, and then sets all of the types in all operands in472/// propagateTypes.473///474/// It also handles verifying correctness of special types.475class OperandTypeChecker {476public:477OperandTypeChecker(ArrayRef<SMLoc> DiagLoc) : DiagLoc(DiagLoc) {}478479/// Step 1: Check each pattern one by one. All patterns that pass through here480/// are added to a common worklist so propagateTypes can access them.481bool check(InstructionPattern &P,482std::function<bool(const PatternType &)> VerifyTypeOfOperand);483484/// Step 2: Propagate all types. e.g. if one use of "$a" has type i32, make485/// all uses of "$a" have type i32.486void propagateTypes();487488protected:489ArrayRef<SMLoc> DiagLoc;490491private:492using InconsistentTypeDiagFn = std::function<void()>;493494void PrintSeenWithTypeIn(InstructionPattern &P, StringRef OpName,495PatternType Ty) const;496497struct OpTypeInfo {498PatternType Type;499InconsistentTypeDiagFn PrintTypeSrcNote = []() {};500};501502StringMap<OpTypeInfo> Types;503504SmallVector<InstructionPattern *, 16> Pats;505};506507//===- PatFrag ------------------------------------------------------------===//508509/// Represents a parsed GICombinePatFrag. This can be thought of as the510/// equivalent of a CodeGenInstruction, but for PatFragPatterns.511///512/// PatFrags are made of 3 things:513/// - Out parameters (defs)514/// - In parameters515/// - A set of pattern lists (alternatives).516///517/// If the PatFrag uses instruction patterns, the root must be one of the defs.518///519/// Note that this DOES NOT represent the use of the PatFrag, only its520/// definition. The use of the PatFrag in a Pattern is represented by521/// PatFragPattern.522///523/// PatFrags use the term "parameter" instead of operand because they're524/// essentially macros, and using that name avoids confusion. Other than that,525/// they're structured similarly to a MachineInstruction - all parameters526/// (operands) are in the same list, with defs at the start. This helps mapping527/// parameters to values, because, param N of a PatFrag is always operand N of a528/// PatFragPattern.529class PatFrag {530public:531static constexpr StringLiteral ClassName = "GICombinePatFrag";532533enum ParamKind {534PK_Root,535PK_MachineOperand,536PK_Imm,537};538539struct Param {540StringRef Name;541ParamKind Kind;542};543544using ParamVec = SmallVector<Param, 4>;545using ParamIt = ParamVec::const_iterator;546547/// Represents an alternative of the PatFrag. When parsing a GICombinePatFrag,548/// this is created from its "Alternatives" list. Each alternative is a list549/// of patterns written wrapped in a `(pattern ...)` dag init.550///551/// Each argument to the `pattern` DAG operator is parsed into a Pattern552/// instance.553struct Alternative {554OperandTable OpTable;555SmallVector<std::unique_ptr<Pattern>, 4> Pats;556};557558explicit PatFrag(const Record &Def);559560static StringRef getParamKindStr(ParamKind OK);561562StringRef getName() const;563564const Record &getDef() const { return Def; }565ArrayRef<SMLoc> getLoc() const;566567Alternative &addAlternative() { return Alts.emplace_back(); }568const Alternative &getAlternative(unsigned K) const { return Alts[K]; }569unsigned num_alternatives() const { return Alts.size(); }570571void addInParam(StringRef Name, ParamKind Kind);572iterator_range<ParamIt> in_params() const;573unsigned num_in_params() const { return Params.size() - NumOutParams; }574575void addOutParam(StringRef Name, ParamKind Kind);576iterator_range<ParamIt> out_params() const;577unsigned num_out_params() const { return NumOutParams; }578579unsigned num_roots() const;580unsigned num_params() const { return num_in_params() + num_out_params(); }581582/// Finds the operand \p Name and returns its index or -1 if not found.583/// Remember that all params are part of the same list, with out params at the584/// start. This means that the index returned can be used to access operands585/// of InstructionPatterns.586unsigned getParamIdx(StringRef Name) const;587const Param &getParam(unsigned K) const { return Params[K]; }588589bool canBeMatchRoot() const { return num_roots() == 1; }590591void print(raw_ostream &OS, StringRef Indent = "") const;592void dump() const;593594/// Checks if the in-param \p ParamName can be unbound or not.595/// \p ArgName is the name of the argument passed to the PatFrag.596///597/// An argument can be unbound only if, for all alternatives:598/// - There is no CXX pattern, OR:599/// - There is an InstructionPattern that binds the parameter.600///601/// e.g. in (MyPatFrag $foo), if $foo has never been seen before (= it's602/// unbound), this checks if MyPatFrag supports it or not.603bool handleUnboundInParam(StringRef ParamName, StringRef ArgName,604ArrayRef<SMLoc> DiagLoc) const;605606bool checkSemantics();607bool buildOperandsTables();608609private:610static void printParamsList(raw_ostream &OS, iterator_range<ParamIt> Params);611612void PrintError(Twine Msg) const;613614const Record &Def;615unsigned NumOutParams = 0;616ParamVec Params;617SmallVector<Alternative, 2> Alts;618};619620//===- PatFragPattern -----------------------------------------------------===//621622/// Represents a use of a GICombinePatFrag.623class PatFragPattern : public InstructionPattern {624public:625PatFragPattern(const PatFrag &PF, StringRef Name)626: InstructionPattern(K_PatFrag, Name), PF(PF) {}627628static bool classof(const Pattern *P) { return P->getKind() == K_PatFrag; }629630const PatFrag &getPatFrag() const { return PF; }631StringRef getInstName() const override { return PF.getName(); }632633unsigned getNumInstDefs() const override { return PF.num_out_params(); }634unsigned getNumInstOperands() const override { return PF.num_params(); }635636ArrayRef<InstructionOperand> getApplyDefsNeeded() const override;637638bool checkSemantics(ArrayRef<SMLoc> DiagLoc) override;639640/// Before emitting the patterns inside the PatFrag, add all necessary code641/// expansions to \p PatFragCEs imported from \p ParentCEs.642///643/// For a MachineOperand PatFrag parameter, this will fetch the expansion for644/// that operand from \p ParentCEs and add it to \p PatFragCEs. Errors can be645/// emitted if the MachineOperand reference is unbound.646///647/// For an Immediate PatFrag parameter this simply adds the integer value to648/// \p PatFragCEs as an expansion.649///650/// \param ParentCEs Contains all of the code expansions declared by the other651/// patterns emitted so far in the pattern list containing652/// this PatFragPattern.653/// \param PatFragCEs Output Code Expansions (usually empty)654/// \param DiagLoc Diagnostic loc in case an error occurs.655/// \return `true` on success, `false` on failure.656bool mapInputCodeExpansions(const CodeExpansions &ParentCEs,657CodeExpansions &PatFragCEs,658ArrayRef<SMLoc> DiagLoc) const;659660private:661const PatFrag &PF;662};663664//===- BuiltinPattern -----------------------------------------------------===//665666/// Represents builtin instructions such as "GIReplaceReg" and "GIEraseRoot".667enum BuiltinKind {668BI_ReplaceReg,669BI_EraseRoot,670};671672class BuiltinPattern : public InstructionPattern {673struct BuiltinInfo {674StringLiteral DefName;675BuiltinKind Kind;676unsigned NumOps;677unsigned NumDefs;678};679680static constexpr std::array<BuiltinInfo, 2> KnownBuiltins = {{681{"GIReplaceReg", BI_ReplaceReg, 2, 1},682{"GIEraseRoot", BI_EraseRoot, 0, 0},683}};684685public:686static constexpr StringLiteral ClassName = "GIBuiltinInst";687688BuiltinPattern(const Record &Def, StringRef Name)689: InstructionPattern(K_Builtin, Name), I(getBuiltinInfo(Def)) {}690691static bool classof(const Pattern *P) { return P->getKind() == K_Builtin; }692693unsigned getNumInstOperands() const override { return I.NumOps; }694unsigned getNumInstDefs() const override { return I.NumDefs; }695StringRef getInstName() const override { return I.DefName; }696BuiltinKind getBuiltinKind() const { return I.Kind; }697698bool checkSemantics(ArrayRef<SMLoc> Loc) override;699700private:701static BuiltinInfo getBuiltinInfo(const Record &Def);702703BuiltinInfo I;704};705706} // namespace gi707} // end namespace llvm708709#endif // ifndef LLVM_UTILS_GLOBALISEL_PATTERNS_H710711712