| 1 | //===- bolt/Rewrite/MetadataManager.cpp -----------------------------------===// |
|---|---|
| 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 | #include "bolt/Rewrite/MetadataManager.h" |
| 10 | #include "llvm/Support/Debug.h" |
| 11 | |
| 12 | #undef DEBUG_TYPE |
| 13 | #define DEBUG_TYPE "bolt-metadata" |
| 14 | |
| 15 | using namespace llvm; |
| 16 | using namespace bolt; |
| 17 | |
| 18 | void MetadataManager::registerRewriter( |
| 19 | std::unique_ptr<MetadataRewriter> Rewriter) { |
| 20 | Rewriters.emplace_back(Args: std::move(Rewriter)); |
| 21 | } |
| 22 | |
| 23 | void MetadataManager::runSectionInitializers() { |
| 24 | for (auto &Rewriter : Rewriters) { |
| 25 | LLVM_DEBUG(dbgs() << "BOLT-DEBUG: invoking "<< Rewriter->getName() |
| 26 | << " after reading sections\n"); |
| 27 | if (Error E = Rewriter->sectionInitializer()) { |
| 28 | errs() << "BOLT-ERROR: while running "<< Rewriter->getName() |
| 29 | << " after reading sections: "<< toString(E: std::move(E)) << '\n'; |
| 30 | exit(status: 1); |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | void MetadataManager::runInitializersPreCFG() { |
| 36 | for (auto &Rewriter : Rewriters) { |
| 37 | LLVM_DEBUG(dbgs() << "BOLT-DEBUG: invoking "<< Rewriter->getName() |
| 38 | << " before CFG construction\n"); |
| 39 | if (Error E = Rewriter->preCFGInitializer()) { |
| 40 | errs() << "BOLT-ERROR: while running "<< Rewriter->getName() |
| 41 | << " in pre-CFG state: "<< toString(E: std::move(E)) << '\n'; |
| 42 | exit(status: 1); |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | void MetadataManager::runInitializersPostCFG() { |
| 48 | for (auto &Rewriter : Rewriters) { |
| 49 | LLVM_DEBUG(dbgs() << "BOLT-DEBUG: invoking "<< Rewriter->getName() |
| 50 | << " after CFG construction\n"); |
| 51 | if (Error E = Rewriter->postCFGInitializer()) { |
| 52 | errs() << "BOLT-ERROR: while running "<< Rewriter->getName() |
| 53 | << " in CFG state: "<< toString(E: std::move(E)) << '\n'; |
| 54 | exit(status: 1); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | void MetadataManager::runFinalizersPreEmit() { |
| 60 | for (auto &Rewriter : Rewriters) { |
| 61 | LLVM_DEBUG(dbgs() << "BOLT-DEBUG: invoking "<< Rewriter->getName() |
| 62 | << " before emitting binary context\n"); |
| 63 | if (Error E = Rewriter->preEmitFinalizer()) { |
| 64 | errs() << "BOLT-ERROR: while running "<< Rewriter->getName() |
| 65 | << " before emit: "<< toString(E: std::move(E)) << '\n'; |
| 66 | exit(status: 1); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void MetadataManager::runFinalizersAfterEmit() { |
| 72 | for (auto &Rewriter : Rewriters) { |
| 73 | LLVM_DEBUG(dbgs() << "BOLT-DEBUG: invoking "<< Rewriter->getName() |
| 74 | << " after emit\n"); |
| 75 | if (Error E = Rewriter->postEmitFinalizer()) { |
| 76 | errs() << "BOLT-ERROR: while running "<< Rewriter->getName() |
| 77 | << " after emit: "<< toString(E: std::move(E)) << '\n'; |
| 78 | exit(status: 1); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 |
