1//===- MatchDataInfo.h ------------------------------------------*- C++ -*-===// 2// 3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4// See https://llvm.org/LICENSE.txt for license information. 5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6// 7//===----------------------------------------------------------------------===// 8// 9/// \file Contains utilities related to handling "match data" for GlobalISel 10/// Combiners. Match data allows for setting some arbitrary data in the "match" 11/// phase and pass it down to the "apply" phase. 12// 13//===----------------------------------------------------------------------===// 14 15#ifndef LLVM_UTILS_MIRPATTERNS_MATCHDATAINFO_H 16#define LLVM_UTILS_MIRPATTERNS_MATCHDATAINFO_H 17 18#include "llvm/ADT/ArrayRef.h" 19#include "llvm/ADT/StringMap.h" 20#include "llvm/ADT/StringRef.h" 21#include <string> 22#include <vector> 23 24namespace llvm { 25 26class raw_ostream; 27 28namespace gi { 29 30/// Represents MatchData defined by the match stage and required by the apply 31/// stage. 32/// 33/// This allows the plumbing of arbitrary data from C++ predicates between the 34/// stages. 35/// 36/// When this class is initially created, it only has a pattern symbol and a 37/// type. When all of the MatchDatas declarations of a given pattern have been 38/// parsed, `AssignVariables` must be called to assign storage variable names to 39/// each MatchDataInfo. 40class MatchDataInfo { 41 StringRef PatternSymbol; 42 StringRef Type; 43 std::string VarName; 44 45public: 46 static constexpr StringLiteral StructTypeName = "MatchInfosTy"; 47 static constexpr StringLiteral StructName = "MatchInfos"; 48 49 MatchDataInfo(StringRef PatternSymbol, StringRef Type) 50 : PatternSymbol(PatternSymbol), Type(Type.trim()) {} 51 52 StringRef getPatternSymbol() const { return PatternSymbol; }; 53 StringRef getType() const { return Type; }; 54 55 bool hasVariableName() const { return !VarName.empty(); } 56 void setVariableName(StringRef Name) { VarName = Name; } 57 StringRef getVariableName() const; 58 59 std::string getQualifiedVariableName() const { 60 return StructName.str() + "." + getVariableName().str(); 61 } 62 63 void print(raw_ostream &OS) const; 64 void dump() const; 65}; 66 67/// Pool of type -> variables used to emit MatchData variables declarations. 68/// 69/// e.g. if the map contains "int64_t" -> ["MD0", "MD1"], then two variable 70/// declarations must be emitted: `int64_t MD0` and `int64_t MD1`. 71/// 72/// This has a static lifetime and will outlive all the `MatchDataInfo` objects 73/// by design. It needs a static lifetime so the backends can emit variable 74/// declarations after processing all the inputs. 75extern StringMap<std::vector<std::string>> AllMatchDataVars; 76 77/// Assign variable names to all MatchDatas used by a pattern. This must be 78/// called after all MatchData decls have been parsed for a given processing 79/// unit (e.g. a combine rule) 80/// 81/// Requires an array of MatchDataInfo so we can handle cases where a pattern 82/// uses multiple instances of the same MatchData type. 83/// 84/// Writes to \ref AllMatchDataVars. 85void AssignMatchDataVariables(MutableArrayRef<MatchDataInfo> Infos); 86 87} // namespace gi 88} // end namespace llvm 89 90#endif // ifndef LLVM_UTILS_MIRPATTERNS_MATCHDATAINFO_H 91