xref: /llvm-project/llvm/lib/CodeGen/GCMetadata.cpp (revision 4d8bf6ea7fa8f68f46b6f0b6d0ae7d114904914a)
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.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/CodeGen/GCMetadata.h"
14 #include "llvm/ADT/StringExtras.h"
15 #include "llvm/CodeGen/Passes.h"
16 #include "llvm/IR/Function.h"
17 #include "llvm/InitializePasses.h"
18 #include "llvm/MC/MCSymbol.h"
19 #include "llvm/Pass.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include <cassert>
22 #include <memory>
23 #include <string>
24 
25 using namespace llvm;
26 
27 INITIALIZE_PASS(GCModuleInfo, "collector-metadata",
28                 "Create Garbage Collector Module Metadata", false, false)
29 
30 // -----------------------------------------------------------------------------
31 
32 GCFunctionInfo::GCFunctionInfo(const Function &F, GCStrategy &S)
33     : F(F), S(S), FrameSize(~0LL) {}
34 
35 GCFunctionInfo::~GCFunctionInfo() = default;
36 
37 // -----------------------------------------------------------------------------
38 
39 char GCModuleInfo::ID = 0;
40 
41 GCModuleInfo::GCModuleInfo() : ImmutablePass(ID) {
42   initializeGCModuleInfoPass(*PassRegistry::getPassRegistry());
43 }
44 
45 GCFunctionInfo &GCModuleInfo::getFunctionInfo(const Function &F) {
46   assert(!F.isDeclaration() && "Can only get GCFunctionInfo for a definition!");
47   assert(F.hasGC());
48 
49   finfo_map_type::iterator I = FInfoMap.find(&F);
50   if (I != FInfoMap.end())
51     return *I->second;
52 
53   GCStrategy *S = getGCStrategy(F.getGC());
54   Functions.push_back(std::make_unique<GCFunctionInfo>(F, *S));
55   GCFunctionInfo *GFI = Functions.back().get();
56   FInfoMap[&F] = GFI;
57   return *GFI;
58 }
59 
60 void GCModuleInfo::clear() {
61   Functions.clear();
62   FInfoMap.clear();
63   GCStrategyList.clear();
64 }
65 
66 // -----------------------------------------------------------------------------
67 
68 GCStrategy *GCModuleInfo::getGCStrategy(const StringRef Name) {
69   // TODO: Arguably, just doing a linear search would be faster for small N
70   auto NMI = GCStrategyMap.find(Name);
71   if (NMI != GCStrategyMap.end())
72     return NMI->getValue();
73 
74   std::unique_ptr<GCStrategy> S = llvm::getGCStrategy(Name);
75   S->Name = std::string(Name);
76   GCStrategyMap[Name] = S.get();
77   GCStrategyList.push_back(std::move(S));
78   return GCStrategyList.back().get();
79 }
80