[InstCombine] Signed saturation tests. NFC
[llvm-complete.git] / lib / CodeGen / GCMetadata.cpp
blobc1d22ef89195e75ae89e18e1e092e5631c5ffc4a
1 //===-- GCMetadata.cpp - Garbage collector metadata -----------------------===//
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 GCFunctionInfo class and GCModuleInfo pass.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/CodeGen/GCMetadata.h"
15 #include "llvm/CodeGen/GCStrategy.h"
16 #include "llvm/CodeGen/Passes.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/MC/MCSymbol.h"
19 #include "llvm/Pass.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include <algorithm>
23 #include <cassert>
24 #include <memory>
25 #include <string>
27 using namespace llvm;
29 namespace {
31 class Printer : public FunctionPass {
32 static char ID;
34 raw_ostream &OS;
36 public:
37 explicit Printer(raw_ostream &OS) : FunctionPass(ID), OS(OS) {}
39 StringRef getPassName() const override;
40 void getAnalysisUsage(AnalysisUsage &AU) const override;
42 bool runOnFunction(Function &F) override;
43 bool doFinalization(Module &M) override;
46 } // end anonymous namespace
48 INITIALIZE_PASS(GCModuleInfo, "collector-metadata",
49 "Create Garbage Collector Module Metadata", false, false)
51 // -----------------------------------------------------------------------------
53 GCFunctionInfo::GCFunctionInfo(const Function &F, GCStrategy &S)
54 : F(F), S(S), FrameSize(~0LL) {}
56 GCFunctionInfo::~GCFunctionInfo() = default;
58 // -----------------------------------------------------------------------------
60 char GCModuleInfo::ID = 0;
62 GCModuleInfo::GCModuleInfo() : ImmutablePass(ID) {
63 initializeGCModuleInfoPass(*PassRegistry::getPassRegistry());
66 GCFunctionInfo &GCModuleInfo::getFunctionInfo(const Function &F) {
67 assert(!F.isDeclaration() && "Can only get GCFunctionInfo for a definition!");
68 assert(F.hasGC());
70 finfo_map_type::iterator I = FInfoMap.find(&F);
71 if (I != FInfoMap.end())
72 return *I->second;
74 GCStrategy *S = getGCStrategy(F.getGC());
75 Functions.push_back(std::make_unique<GCFunctionInfo>(F, *S));
76 GCFunctionInfo *GFI = Functions.back().get();
77 FInfoMap[&F] = GFI;
78 return *GFI;
81 void GCModuleInfo::clear() {
82 Functions.clear();
83 FInfoMap.clear();
84 GCStrategyList.clear();
87 // -----------------------------------------------------------------------------
89 char Printer::ID = 0;
91 FunctionPass *llvm::createGCInfoPrinter(raw_ostream &OS) {
92 return new Printer(OS);
95 StringRef Printer::getPassName() const {
96 return "Print Garbage Collector Information";
99 void Printer::getAnalysisUsage(AnalysisUsage &AU) const {
100 FunctionPass::getAnalysisUsage(AU);
101 AU.setPreservesAll();
102 AU.addRequired<GCModuleInfo>();
105 bool Printer::runOnFunction(Function &F) {
106 if (F.hasGC())
107 return false;
109 GCFunctionInfo *FD = &getAnalysis<GCModuleInfo>().getFunctionInfo(F);
111 OS << "GC roots for " << FD->getFunction().getName() << ":\n";
112 for (GCFunctionInfo::roots_iterator RI = FD->roots_begin(),
113 RE = FD->roots_end();
114 RI != RE; ++RI)
115 OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n";
117 OS << "GC safe points for " << FD->getFunction().getName() << ":\n";
118 for (GCFunctionInfo::iterator PI = FD->begin(), PE = FD->end(); PI != PE;
119 ++PI) {
121 OS << "\t" << PI->Label->getName() << ": " << "post-call"
122 << ", live = {";
124 for (GCFunctionInfo::live_iterator RI = FD->live_begin(PI),
125 RE = FD->live_end(PI);
126 ;) {
127 OS << " " << RI->Num;
128 if (++RI == RE)
129 break;
130 OS << ",";
133 OS << " }\n";
136 return false;
139 bool Printer::doFinalization(Module &M) {
140 GCModuleInfo *GMI = getAnalysisIfAvailable<GCModuleInfo>();
141 assert(GMI && "Printer didn't require GCModuleInfo?!");
142 GMI->clear();
143 return false;
146 GCStrategy *GCModuleInfo::getGCStrategy(const StringRef Name) {
147 // TODO: Arguably, just doing a linear search would be faster for small N
148 auto NMI = GCStrategyMap.find(Name);
149 if (NMI != GCStrategyMap.end())
150 return NMI->getValue();
152 for (auto& Entry : GCRegistry::entries()) {
153 if (Name == Entry.getName()) {
154 std::unique_ptr<GCStrategy> S = Entry.instantiate();
155 S->Name = Name;
156 GCStrategyMap[Name] = S.get();
157 GCStrategyList.push_back(std::move(S));
158 return GCStrategyList.back().get();
162 if (GCRegistry::begin() == GCRegistry::end()) {
163 // In normal operation, the registry should not be empty. There should
164 // be the builtin GCs if nothing else. The most likely scenario here is
165 // that we got here without running the initializers used by the Registry
166 // itself and it's registration mechanism.
167 const std::string error = ("unsupported GC: " + Name).str() +
168 " (did you remember to link and initialize the CodeGen library?)";
169 report_fatal_error(error);
170 } else
171 report_fatal_error(std::string("unsupported GC: ") + Name);