1 //===- PassRegistry.cpp - Pass Registration Implementation ----------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the PassRegistry, with which passes are registered on
10 // initialization, and supports the PassManager in dependency resolution.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/PassRegistry.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/Pass.h"
17 #include "llvm/PassInfo.h"
24 PassRegistry
*PassRegistry::getPassRegistry() {
25 static PassRegistry PassRegistryObj
;
26 return &PassRegistryObj
;
29 //===----------------------------------------------------------------------===//
33 PassRegistry::~PassRegistry() = default;
35 const PassInfo
*PassRegistry::getPassInfo(const void *TI
) const {
36 sys::SmartScopedReader
<true> Guard(Lock
);
37 return PassInfoMap
.lookup(TI
);
40 const PassInfo
*PassRegistry::getPassInfo(StringRef Arg
) const {
41 sys::SmartScopedReader
<true> Guard(Lock
);
42 return PassInfoStringMap
.lookup(Arg
);
45 //===----------------------------------------------------------------------===//
46 // Pass Registration mechanism
49 void PassRegistry::registerPass(const PassInfo
&PI
, bool ShouldFree
) {
50 sys::SmartScopedWriter
<true> Guard(Lock
);
52 PassInfoMap
.insert(std::make_pair(PI
.getTypeInfo(), &PI
)).second
;
53 assert(Inserted
&& "Pass registered multiple times!");
55 PassInfoStringMap
[PI
.getPassArgument()] = &PI
;
57 // Notify any listeners.
58 for (auto *Listener
: Listeners
)
59 Listener
->passRegistered(&PI
);
62 ToFree
.push_back(std::unique_ptr
<const PassInfo
>(&PI
));
65 void PassRegistry::enumerateWith(PassRegistrationListener
*L
) {
66 sys::SmartScopedReader
<true> Guard(Lock
);
67 for (auto PassInfoPair
: PassInfoMap
)
68 L
->passEnumerate(PassInfoPair
.second
);
71 void PassRegistry::addRegistrationListener(PassRegistrationListener
*L
) {
72 sys::SmartScopedWriter
<true> Guard(Lock
);
73 Listeners
.push_back(L
);
76 void PassRegistry::removeRegistrationListener(PassRegistrationListener
*L
) {
77 sys::SmartScopedWriter
<true> Guard(Lock
);
79 auto I
= llvm::find(Listeners
, L
);