1//===- PassRegistry.cpp - Pass Registration Implementation ----------------===//
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// This file implements the PassRegistry, with which passes are registered on
10// initialization, and supports the PassManager in dependency resolution.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/PassRegistry.h"
15#include "llvm/ADT/STLExtras.h"
16#include "llvm/Pass.h"
17#include "llvm/PassInfo.h"
18#include <cassert>
19#include <memory>
20
21using namespace llvm;
22
23PassRegistry *PassRegistry::getPassRegistry() {
24 static PassRegistry PassRegistryObj;
25 return &PassRegistryObj;
26}
27
28//===----------------------------------------------------------------------===//
29// Accessors
30//
31
32PassRegistry::~PassRegistry() = default;
33
34const PassInfo *PassRegistry::getPassInfo(const void *TI) const {
35 sys::SmartScopedReader<true> Guard(Lock);
36 return PassInfoMap.lookup(Val: TI);
37}
38
39const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const {
40 sys::SmartScopedReader<true> Guard(Lock);
41 return PassInfoStringMap.lookup(Key: Arg);
42}
43
44//===----------------------------------------------------------------------===//
45// Pass Registration mechanism
46//
47
48void PassRegistry::registerPass(const PassInfo &PI, bool ShouldFree) {
49 sys::SmartScopedWriter<true> Guard(Lock);
50 bool Inserted =
51 PassInfoMap.insert(KV: std::make_pair(x: PI.getTypeInfo(), y: &PI)).second;
52 assert(Inserted && "Pass registered multiple times!");
53 (void)Inserted;
54 PassInfoStringMap[PI.getPassArgument()] = &PI;
55
56 // Notify any listeners.
57 for (auto *Listener : Listeners)
58 Listener->passRegistered(&PI);
59
60 if (ShouldFree)
61 ToFree.push_back(x: std::unique_ptr<const PassInfo>(&PI));
62}
63
64void PassRegistry::enumerateWith(PassRegistrationListener *L) {
65 sys::SmartScopedReader<true> Guard(Lock);
66 for (auto PassInfoPair : PassInfoMap)
67 L->passEnumerate(PassInfoPair.second);
68}
69
70void PassRegistry::addRegistrationListener(PassRegistrationListener *L) {
71 sys::SmartScopedWriter<true> Guard(Lock);
72 Listeners.push_back(x: L);
73}
74
75void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) {
76 sys::SmartScopedWriter<true> Guard(Lock);
77
78 auto I = llvm::find(Range&: Listeners, Val: L);
79 Listeners.erase(position: I);
80}
81