1 //===- MatchDataInfo.cpp ----------------------------------------*- 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 //
10 //===----------------------------------------------------------------------===//
11 
12 #include "MatchDataInfo.h"
13 #include "llvm/Support/Debug.h"
14 #include "llvm/Support/raw_ostream.h"
15 
16 namespace llvm {
17 namespace gi {
18 
19 StringMap<std::vector<std::string>> AllMatchDataVars;
20 
getVariableName() const21 StringRef MatchDataInfo::getVariableName() const {
22   assert(hasVariableName());
23   return VarName;
24 }
25 
print(raw_ostream & OS) const26 void MatchDataInfo::print(raw_ostream &OS) const {
27   OS << "(MatchDataInfo pattern_symbol:" << PatternSymbol << " type:'" << Type
28      << "' var_name:" << (VarName.empty() ? "<unassigned>" : VarName) << ")";
29 }
30 
dump() const31 void MatchDataInfo::dump() const { print(dbgs()); }
32 
AssignMatchDataVariables(MutableArrayRef<MatchDataInfo> Infos)33 void AssignMatchDataVariables(MutableArrayRef<MatchDataInfo> Infos) {
34   static unsigned NextVarID = 0;
35 
36   StringMap<unsigned> SeenTypes;
37   for (auto &Info : Infos) {
38     unsigned &NumSeen = SeenTypes[Info.getType()];
39     auto &ExistingVars = AllMatchDataVars[Info.getType()];
40 
41     if (NumSeen == ExistingVars.size())
42       ExistingVars.push_back("MDInfo" + std::to_string(NextVarID++));
43 
44     Info.setVariableName(ExistingVars[NumSeen++]);
45   }
46 }
47 
48 } // namespace gi
49 } // namespace llvm
50