Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 1 | //===-- GCMetadata.cpp - Garbage collector metadata -----------------------===// |
Gordon Henriksen | 613afce | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Gordon Henriksen | 613afce | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 9 | // This file implements the GCFunctionInfo class and GCModuleInfo pass. |
Gordon Henriksen | 613afce | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Eugene Zelenko | 6ac7a34 | 2017-06-07 23:53:32 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/STLExtras.h" |
Gordon Henriksen | bcef14d | 2008-08-17 12:56:54 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/GCMetadata.h" |
Chandler Carruth | 71f308a | 2015-02-13 09:09:03 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/GCStrategy.h" |
Gordon Henriksen | 7843c16 | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/Passes.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/Function.h" |
Chris Lattner | 1065f49 | 2010-03-14 07:27:07 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCSymbol.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/Pass.h" |
Torok Edwin | ccb29cd | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 20 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 565449d | 2009-08-23 03:13:20 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 6ac7a34 | 2017-06-07 23:53:32 +0000 | [diff] [blame] | 22 | #include <algorithm> |
| 23 | #include <cassert> |
| 24 | #include <memory> |
| 25 | #include <string> |
| 26 | |
Gordon Henriksen | 613afce | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
| 29 | namespace { |
Chris Lattner | 565449d | 2009-08-23 03:13:20 +0000 | [diff] [blame] | 30 | |
Philip Reames | 3631953 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 31 | class Printer : public FunctionPass { |
| 32 | static char ID; |
Eugene Zelenko | 6ac7a34 | 2017-06-07 23:53:32 +0000 | [diff] [blame] | 33 | |
Philip Reames | 3631953 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 34 | raw_ostream &OS; |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 35 | |
Philip Reames | 3631953 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 36 | public: |
| 37 | explicit Printer(raw_ostream &OS) : FunctionPass(ID), OS(OS) {} |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 38 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 39 | StringRef getPassName() const override; |
Philip Reames | 3631953 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 40 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
Benjamin Kramer | b3aa2b8 | 2013-02-19 16:51:44 +0000 | [diff] [blame] | 41 | |
Philip Reames | 3631953 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 42 | bool runOnFunction(Function &F) override; |
| 43 | bool doFinalization(Module &M) override; |
| 44 | }; |
Eugene Zelenko | 6ac7a34 | 2017-06-07 23:53:32 +0000 | [diff] [blame] | 45 | |
| 46 | } // end anonymous namespace |
Gordon Henriksen | 613afce | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 47 | |
Owen Anderson | a57b97e | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 48 | INITIALIZE_PASS(GCModuleInfo, "collector-metadata", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 49 | "Create Garbage Collector Module Metadata", false, false) |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 50 | |
Gordon Henriksen | 613afce | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 51 | // ----------------------------------------------------------------------------- |
| 52 | |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 53 | GCFunctionInfo::GCFunctionInfo(const Function &F, GCStrategy &S) |
Philip Reames | 3631953 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 54 | : F(F), S(S), FrameSize(~0LL) {} |
Gordon Henriksen | 613afce | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 55 | |
Eugene Zelenko | 6ac7a34 | 2017-06-07 23:53:32 +0000 | [diff] [blame] | 56 | GCFunctionInfo::~GCFunctionInfo() = default; |
Gordon Henriksen | 613afce | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 57 | |
| 58 | // ----------------------------------------------------------------------------- |
| 59 | |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 60 | char GCModuleInfo::ID = 0; |
Gordon Henriksen | 613afce | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 61 | |
Philip Reames | 3631953 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 62 | GCModuleInfo::GCModuleInfo() : ImmutablePass(ID) { |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 63 | initializeGCModuleInfoPass(*PassRegistry::getPassRegistry()); |
| 64 | } |
Gordon Henriksen | 613afce | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 65 | |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 66 | GCFunctionInfo &GCModuleInfo::getFunctionInfo(const Function &F) { |
Gordon Henriksen | e431adb | 2008-08-17 16:18:50 +0000 | [diff] [blame] | 67 | assert(!F.isDeclaration() && "Can only get GCFunctionInfo for a definition!"); |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 68 | assert(F.hasGC()); |
Philip Reames | 3631953 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 69 | |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 70 | finfo_map_type::iterator I = FInfoMap.find(&F); |
| 71 | if (I != FInfoMap.end()) |
Gordon Henriksen | 7843c16 | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 72 | return *I->second; |
Philip Reames | 3631953 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 73 | |
Philip Reames | 56a0393 | 2015-01-26 18:26:35 +0000 | [diff] [blame] | 74 | GCStrategy *S = getGCStrategy(F.getGC()); |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 75 | Functions.push_back(std::make_unique<GCFunctionInfo>(F, *S)); |
Philip Reames | 1e30897 | 2014-12-11 01:47:23 +0000 | [diff] [blame] | 76 | GCFunctionInfo *GFI = Functions.back().get(); |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 77 | FInfoMap[&F] = GFI; |
| 78 | return *GFI; |
Gordon Henriksen | 613afce | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 81 | void GCModuleInfo::clear() { |
Philip Reames | 1e30897 | 2014-12-11 01:47:23 +0000 | [diff] [blame] | 82 | Functions.clear(); |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 83 | FInfoMap.clear(); |
Philip Reames | 56a0393 | 2015-01-26 18:26:35 +0000 | [diff] [blame] | 84 | GCStrategyList.clear(); |
Gordon Henriksen | 613afce | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // ----------------------------------------------------------------------------- |
| 88 | |
| 89 | char Printer::ID = 0; |
| 90 | |
Chris Lattner | 565449d | 2009-08-23 03:13:20 +0000 | [diff] [blame] | 91 | FunctionPass *llvm::createGCInfoPrinter(raw_ostream &OS) { |
Gordon Henriksen | 613afce | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 92 | return new Printer(OS); |
| 93 | } |
| 94 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 95 | StringRef Printer::getPassName() const { |
Gordon Henriksen | 613afce | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 96 | return "Print Garbage Collector Information"; |
| 97 | } |
| 98 | |
| 99 | void Printer::getAnalysisUsage(AnalysisUsage &AU) const { |
Gordon Henriksen | 7843c16 | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 100 | FunctionPass::getAnalysisUsage(AU); |
Gordon Henriksen | 613afce | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 101 | AU.setPreservesAll(); |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 102 | AU.addRequired<GCModuleInfo>(); |
Gordon Henriksen | 613afce | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Gordon Henriksen | 7843c16 | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 105 | bool Printer::runOnFunction(Function &F) { |
Philip Reames | 3631953 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 106 | if (F.hasGC()) |
| 107 | return false; |
| 108 | |
Chris Lattner | 1065f49 | 2010-03-14 07:27:07 +0000 | [diff] [blame] | 109 | GCFunctionInfo *FD = &getAnalysis<GCModuleInfo>().getFunctionInfo(F); |
Philip Reames | 3631953 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 110 | |
Benjamin Kramer | 1f97a5a | 2011-11-15 16:27:03 +0000 | [diff] [blame] | 111 | OS << "GC roots for " << FD->getFunction().getName() << ":\n"; |
Chris Lattner | 1065f49 | 2010-03-14 07:27:07 +0000 | [diff] [blame] | 112 | for (GCFunctionInfo::roots_iterator RI = FD->roots_begin(), |
Philip Reames | 3631953 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 113 | RE = FD->roots_end(); |
| 114 | RI != RE; ++RI) |
Chris Lattner | 1065f49 | 2010-03-14 07:27:07 +0000 | [diff] [blame] | 115 | OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n"; |
Philip Reames | 3631953 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 116 | |
Benjamin Kramer | 1f97a5a | 2011-11-15 16:27:03 +0000 | [diff] [blame] | 117 | OS << "GC safe points for " << FD->getFunction().getName() << ":\n"; |
Philip Reames | 3631953 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 118 | for (GCFunctionInfo::iterator PI = FD->begin(), PE = FD->end(); PI != PE; |
| 119 | ++PI) { |
| 120 | |
Philip Reames | e44a55d | 2018-11-12 22:03:53 +0000 | [diff] [blame] | 121 | OS << "\t" << PI->Label->getName() << ": " << "post-call" |
Philip Reames | 3631953 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 122 | << ", live = {"; |
| 123 | |
Chris Lattner | 1065f49 | 2010-03-14 07:27:07 +0000 | [diff] [blame] | 124 | for (GCFunctionInfo::live_iterator RI = FD->live_begin(PI), |
Philip Reames | 3631953 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 125 | RE = FD->live_end(PI); |
| 126 | ;) { |
Chris Lattner | 1065f49 | 2010-03-14 07:27:07 +0000 | [diff] [blame] | 127 | OS << " " << RI->Num; |
| 128 | if (++RI == RE) |
| 129 | break; |
| 130 | OS << ","; |
Gordon Henriksen | 613afce | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 131 | } |
Philip Reames | 3631953 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 132 | |
Chris Lattner | 1065f49 | 2010-03-14 07:27:07 +0000 | [diff] [blame] | 133 | OS << " }\n"; |
Gordon Henriksen | 613afce | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 134 | } |
Philip Reames | 3631953 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 135 | |
Gordon Henriksen | 613afce | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 136 | return false; |
| 137 | } |
| 138 | |
Benjamin Kramer | b3aa2b8 | 2013-02-19 16:51:44 +0000 | [diff] [blame] | 139 | bool Printer::doFinalization(Module &M) { |
Duncan Sands | 5a913d6 | 2009-01-28 13:14:17 +0000 | [diff] [blame] | 140 | GCModuleInfo *GMI = getAnalysisIfAvailable<GCModuleInfo>(); |
Benjamin Kramer | b3aa2b8 | 2013-02-19 16:51:44 +0000 | [diff] [blame] | 141 | assert(GMI && "Printer didn't require GCModuleInfo?!"); |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 142 | GMI->clear(); |
Gordon Henriksen | 613afce | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 143 | return false; |
| 144 | } |
Philip Reames | 56a0393 | 2015-01-26 18:26:35 +0000 | [diff] [blame] | 145 | |
Philip Reames | 56a0393 | 2015-01-26 18:26:35 +0000 | [diff] [blame] | 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(); |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 151 | |
Philip Reames | 56a0393 | 2015-01-26 18:26:35 +0000 | [diff] [blame] | 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(); |
| 159 | } |
| 160 | } |
| 161 | |
Philip Reames | 20c24f1 | 2015-04-26 22:00:34 +0000 | [diff] [blame] | 162 | if (GCRegistry::begin() == GCRegistry::end()) { |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 163 | // In normal operation, the registry should not be empty. There should |
Philip Reames | 20c24f1 | 2015-04-26 22:00:34 +0000 | [diff] [blame] | 164 | // be the builtin GCs if nothing else. The most likely scenario here is |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 165 | // that we got here without running the initializers used by the Registry |
Philip Reames | 20c24f1 | 2015-04-26 22:00:34 +0000 | [diff] [blame] | 166 | // itself and it's registration mechanism. |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 167 | const std::string error = ("unsupported GC: " + Name).str() + |
Philip Reames | 20c24f1 | 2015-04-26 22:00:34 +0000 | [diff] [blame] | 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); |
Philip Reames | 56a0393 | 2015-01-26 18:26:35 +0000 | [diff] [blame] | 172 | } |