summaryrefslogtreecommitdiffstats
path: root/contrib/libs/llvm18/utils/TableGen/GlobalISel/MatchDataInfo.cpp
blob: b5c9e4f8c24852d8837165e7a70f74e3f869bbbc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//===- MatchDataInfo.cpp ----------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
//
//===----------------------------------------------------------------------===//

#include "MatchDataInfo.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"

namespace llvm {
namespace gi {

StringMap<std::vector<std::string>> AllMatchDataVars;

StringRef MatchDataInfo::getVariableName() const {
  assert(hasVariableName());
  return VarName;
}

void MatchDataInfo::print(raw_ostream &OS) const {
  OS << "(MatchDataInfo pattern_symbol:" << PatternSymbol << " type:'" << Type
     << "' var_name:" << (VarName.empty() ? "<unassigned>" : VarName) << ")";
}

void MatchDataInfo::dump() const { print(dbgs()); }

void AssignMatchDataVariables(MutableArrayRef<MatchDataInfo> Infos) {
  static unsigned NextVarID = 0;

  StringMap<unsigned> SeenTypes;
  for (auto &Info : Infos) {
    unsigned &NumSeen = SeenTypes[Info.getType()];
    auto &ExistingVars = AllMatchDataVars[Info.getType()];

    if (NumSeen == ExistingVars.size())
      ExistingVars.push_back("MDInfo" + std::to_string(NextVarID++));

    Info.setVariableName(ExistingVars[NumSeen++]);
  }
}

} // namespace gi
} // namespace llvm