Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 1 | //===-- GCMetadata.cpp - Garbage collector metadata -----------------------===// |
Gordon Henriksen | fc32822 | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Gordon Henriksen | fc32822 | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 10 | // This file implements the GCFunctionInfo class and GCModuleInfo pass. |
Gordon Henriksen | fc32822 | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Eugene Zelenko | 643c0a4 | 2017-06-07 23:53:32 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/STLExtras.h" |
Gordon Henriksen | 5a29c9e | 2008-08-17 12:56:54 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/GCMetadata.h" |
Chandler Carruth | 02d6288 | 2015-02-13 09:09:03 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/GCStrategy.h" |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/Passes.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Function.h" |
Chris Lattner | aba9bcb | 2010-03-14 07:27:07 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCSymbol.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 20 | #include "llvm/Pass.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 21 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | cf143a4 | 2009-08-23 03:13:20 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 643c0a4 | 2017-06-07 23:53:32 +0000 | [diff] [blame] | 23 | #include <algorithm> |
| 24 | #include <cassert> |
| 25 | #include <memory> |
| 26 | #include <string> |
| 27 | |
Gordon Henriksen | fc32822 | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 28 | using namespace llvm; |
| 29 | |
| 30 | namespace { |
Chris Lattner | cf143a4 | 2009-08-23 03:13:20 +0000 | [diff] [blame] | 31 | |
Philip Reames | 9994127 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 32 | class Printer : public FunctionPass { |
| 33 | static char ID; |
Eugene Zelenko | 643c0a4 | 2017-06-07 23:53:32 +0000 | [diff] [blame] | 34 | |
Philip Reames | 9994127 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 35 | raw_ostream &OS; |
Craig Topper | 9f998de | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 36 | |
Philip Reames | 9994127 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 37 | public: |
| 38 | explicit Printer(raw_ostream &OS) : FunctionPass(ID), OS(OS) {} |
Craig Topper | 9f998de | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 39 | |
Mehdi Amini | 67f335d | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 40 | StringRef getPassName() const override; |
Philip Reames | 9994127 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 41 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
Benjamin Kramer | 0633373 | 2013-02-19 16:51:44 +0000 | [diff] [blame] | 42 | |
Philip Reames | 9994127 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 43 | bool runOnFunction(Function &F) override; |
| 44 | bool doFinalization(Module &M) override; |
| 45 | }; |
Eugene Zelenko | 643c0a4 | 2017-06-07 23:53:32 +0000 | [diff] [blame] | 46 | |
| 47 | } // end anonymous namespace |
Gordon Henriksen | fc32822 | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 48 | |
Owen Anderson | d13db2c | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 49 | INITIALIZE_PASS(GCModuleInfo, "collector-metadata", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 50 | "Create Garbage Collector Module Metadata", false, false) |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 51 | |
Gordon Henriksen | fc32822 | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 52 | // ----------------------------------------------------------------------------- |
| 53 | |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 54 | GCFunctionInfo::GCFunctionInfo(const Function &F, GCStrategy &S) |
Philip Reames | 9994127 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 55 | : F(F), S(S), FrameSize(~0LL) {} |
Gordon Henriksen | fc32822 | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 56 | |
Eugene Zelenko | 643c0a4 | 2017-06-07 23:53:32 +0000 | [diff] [blame] | 57 | GCFunctionInfo::~GCFunctionInfo() = default; |
Gordon Henriksen | fc32822 | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 58 | |
| 59 | // ----------------------------------------------------------------------------- |
| 60 | |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 61 | char GCModuleInfo::ID = 0; |
Gordon Henriksen | fc32822 | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 62 | |
Philip Reames | 9994127 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 63 | GCModuleInfo::GCModuleInfo() : ImmutablePass(ID) { |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 64 | initializeGCModuleInfoPass(*PassRegistry::getPassRegistry()); |
| 65 | } |
Gordon Henriksen | fc32822 | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 66 | |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 67 | GCFunctionInfo &GCModuleInfo::getFunctionInfo(const Function &F) { |
Gordon Henriksen | 418b6e8 | 2008-08-17 16:18:50 +0000 | [diff] [blame] | 68 | assert(!F.isDeclaration() && "Can only get GCFunctionInfo for a definition!"); |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 69 | assert(F.hasGC()); |
Philip Reames | 9994127 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 70 | |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 71 | finfo_map_type::iterator I = FInfoMap.find(&F); |
| 72 | if (I != FInfoMap.end()) |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 73 | return *I->second; |
Philip Reames | 9994127 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 74 | |
Philip Reames | 2bdb238 | 2015-01-26 18:26:35 +0000 | [diff] [blame] | 75 | GCStrategy *S = getGCStrategy(F.getGC()); |
Eugene Zelenko | 643c0a4 | 2017-06-07 23:53:32 +0000 | [diff] [blame] | 76 | Functions.push_back(llvm::make_unique<GCFunctionInfo>(F, *S)); |
Philip Reames | 5e62b84 | 2014-12-11 01:47:23 +0000 | [diff] [blame] | 77 | GCFunctionInfo *GFI = Functions.back().get(); |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 78 | FInfoMap[&F] = GFI; |
| 79 | return *GFI; |
Gordon Henriksen | fc32822 | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 82 | void GCModuleInfo::clear() { |
Philip Reames | 5e62b84 | 2014-12-11 01:47:23 +0000 | [diff] [blame] | 83 | Functions.clear(); |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 84 | FInfoMap.clear(); |
Philip Reames | 2bdb238 | 2015-01-26 18:26:35 +0000 | [diff] [blame] | 85 | GCStrategyList.clear(); |
Gordon Henriksen | fc32822 | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | // ----------------------------------------------------------------------------- |
| 89 | |
| 90 | char Printer::ID = 0; |
| 91 | |
Chris Lattner | cf143a4 | 2009-08-23 03:13:20 +0000 | [diff] [blame] | 92 | FunctionPass *llvm::createGCInfoPrinter(raw_ostream &OS) { |
Gordon Henriksen | fc32822 | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 93 | return new Printer(OS); |
| 94 | } |
| 95 | |
Mehdi Amini | 67f335d | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 96 | StringRef Printer::getPassName() const { |
Gordon Henriksen | fc32822 | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 97 | return "Print Garbage Collector Information"; |
| 98 | } |
| 99 | |
| 100 | void Printer::getAnalysisUsage(AnalysisUsage &AU) const { |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 101 | FunctionPass::getAnalysisUsage(AU); |
Gordon Henriksen | fc32822 | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 102 | AU.setPreservesAll(); |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 103 | AU.addRequired<GCModuleInfo>(); |
Gordon Henriksen | fc32822 | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 106 | bool Printer::runOnFunction(Function &F) { |
Philip Reames | 9994127 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 107 | if (F.hasGC()) |
| 108 | return false; |
| 109 | |
Chris Lattner | aba9bcb | 2010-03-14 07:27:07 +0000 | [diff] [blame] | 110 | GCFunctionInfo *FD = &getAnalysis<GCModuleInfo>().getFunctionInfo(F); |
Philip Reames | 9994127 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 111 | |
Benjamin Kramer | a7b0cb7 | 2011-11-15 16:27:03 +0000 | [diff] [blame] | 112 | OS << "GC roots for " << FD->getFunction().getName() << ":\n"; |
Chris Lattner | aba9bcb | 2010-03-14 07:27:07 +0000 | [diff] [blame] | 113 | for (GCFunctionInfo::roots_iterator RI = FD->roots_begin(), |
Philip Reames | 9994127 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 114 | RE = FD->roots_end(); |
| 115 | RI != RE; ++RI) |
Chris Lattner | aba9bcb | 2010-03-14 07:27:07 +0000 | [diff] [blame] | 116 | OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n"; |
Philip Reames | 9994127 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 117 | |
Benjamin Kramer | a7b0cb7 | 2011-11-15 16:27:03 +0000 | [diff] [blame] | 118 | OS << "GC safe points for " << FD->getFunction().getName() << ":\n"; |
Philip Reames | 9994127 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 119 | for (GCFunctionInfo::iterator PI = FD->begin(), PE = FD->end(); PI != PE; |
| 120 | ++PI) { |
| 121 | |
Philip Reames | 4606305 | 2018-11-12 22:03:53 +0000 | [diff] [blame] | 122 | OS << "\t" << PI->Label->getName() << ": " << "post-call" |
Philip Reames | 9994127 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 123 | << ", live = {"; |
| 124 | |
Chris Lattner | aba9bcb | 2010-03-14 07:27:07 +0000 | [diff] [blame] | 125 | for (GCFunctionInfo::live_iterator RI = FD->live_begin(PI), |
Philip Reames | 9994127 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 126 | RE = FD->live_end(PI); |
| 127 | ;) { |
Chris Lattner | aba9bcb | 2010-03-14 07:27:07 +0000 | [diff] [blame] | 128 | OS << " " << RI->Num; |
| 129 | if (++RI == RE) |
| 130 | break; |
| 131 | OS << ","; |
Gordon Henriksen | fc32822 | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 132 | } |
Philip Reames | 9994127 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 133 | |
Chris Lattner | aba9bcb | 2010-03-14 07:27:07 +0000 | [diff] [blame] | 134 | OS << " }\n"; |
Gordon Henriksen | fc32822 | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 135 | } |
Philip Reames | 9994127 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 136 | |
Gordon Henriksen | fc32822 | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 137 | return false; |
| 138 | } |
| 139 | |
Benjamin Kramer | 0633373 | 2013-02-19 16:51:44 +0000 | [diff] [blame] | 140 | bool Printer::doFinalization(Module &M) { |
Duncan Sands | 1465d61 | 2009-01-28 13:14:17 +0000 | [diff] [blame] | 141 | GCModuleInfo *GMI = getAnalysisIfAvailable<GCModuleInfo>(); |
Benjamin Kramer | 0633373 | 2013-02-19 16:51:44 +0000 | [diff] [blame] | 142 | assert(GMI && "Printer didn't require GCModuleInfo?!"); |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 143 | GMI->clear(); |
Gordon Henriksen | fc32822 | 2007-09-27 22:18:46 +0000 | [diff] [blame] | 144 | return false; |
| 145 | } |
Philip Reames | 2bdb238 | 2015-01-26 18:26:35 +0000 | [diff] [blame] | 146 | |
Philip Reames | 2bdb238 | 2015-01-26 18:26:35 +0000 | [diff] [blame] | 147 | GCStrategy *GCModuleInfo::getGCStrategy(const StringRef Name) { |
| 148 | // TODO: Arguably, just doing a linear search would be faster for small N |
| 149 | auto NMI = GCStrategyMap.find(Name); |
| 150 | if (NMI != GCStrategyMap.end()) |
| 151 | return NMI->getValue(); |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 152 | |
Philip Reames | 2bdb238 | 2015-01-26 18:26:35 +0000 | [diff] [blame] | 153 | for (auto& Entry : GCRegistry::entries()) { |
| 154 | if (Name == Entry.getName()) { |
| 155 | std::unique_ptr<GCStrategy> S = Entry.instantiate(); |
| 156 | S->Name = Name; |
| 157 | GCStrategyMap[Name] = S.get(); |
| 158 | GCStrategyList.push_back(std::move(S)); |
| 159 | return GCStrategyList.back().get(); |
| 160 | } |
| 161 | } |
| 162 | |
Philip Reames | c4ab95d | 2015-04-26 22:00:34 +0000 | [diff] [blame] | 163 | if (GCRegistry::begin() == GCRegistry::end()) { |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 164 | // In normal operation, the registry should not be empty. There should |
Philip Reames | c4ab95d | 2015-04-26 22:00:34 +0000 | [diff] [blame] | 165 | // be the builtin GCs if nothing else. The most likely scenario here is |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 166 | // that we got here without running the initializers used by the Registry |
Philip Reames | c4ab95d | 2015-04-26 22:00:34 +0000 | [diff] [blame] | 167 | // itself and it's registration mechanism. |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 168 | const std::string error = ("unsupported GC: " + Name).str() + |
Philip Reames | c4ab95d | 2015-04-26 22:00:34 +0000 | [diff] [blame] | 169 | " (did you remember to link and initialize the CodeGen library?)"; |
| 170 | report_fatal_error(error); |
| 171 | } else |
| 172 | report_fatal_error(std::string("unsupported GC: ") + Name); |
Philip Reames | 2bdb238 | 2015-01-26 18:26:35 +0000 | [diff] [blame] | 173 | } |