Mehdi Amini | d5faa26 | 2016-04-12 21:35:28 +0000 | [diff] [blame] | 1 | //===- NameAnonFunctions.cpp - ThinLTO Summary-based Function Import ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements naming anonymous function to make sure they can be |
| 11 | // refered to by ThinLTO. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Teresa Johnson | 4223dd8 | 2016-08-12 14:03:36 +0000 | [diff] [blame] | 15 | #include "llvm/Transforms/Utils/NameAnonFunctions.h" |
| 16 | |
Mehdi Amini | d5faa26 | 2016-04-12 21:35:28 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallString.h" |
| 18 | #include "llvm/IR/Module.h" |
| 19 | #include "llvm/Support/MD5.h" |
| 20 | #include "llvm/Transforms/Utils/ModuleUtils.h" |
| 21 | |
| 22 | using namespace llvm; |
| 23 | |
Benjamin Kramer | b7d3311 | 2016-08-06 11:13:10 +0000 | [diff] [blame] | 24 | namespace { |
Mehdi Amini | d5faa26 | 2016-04-12 21:35:28 +0000 | [diff] [blame] | 25 | // Compute a "unique" hash for the module based on the name of the public |
| 26 | // functions. |
| 27 | class ModuleHasher { |
| 28 | Module &TheModule; |
| 29 | std::string TheHash; |
| 30 | |
| 31 | public: |
| 32 | ModuleHasher(Module &M) : TheModule(M) {} |
| 33 | |
| 34 | /// Return the lazily computed hash. |
| 35 | std::string &get() { |
| 36 | if (!TheHash.empty()) |
| 37 | // Cache hit :) |
| 38 | return TheHash; |
| 39 | |
| 40 | MD5 Hasher; |
| 41 | for (auto &F : TheModule) { |
| 42 | if (F.isDeclaration() || F.hasLocalLinkage() || !F.hasName()) |
| 43 | continue; |
| 44 | auto Name = F.getName(); |
| 45 | Hasher.update(Name); |
| 46 | } |
| 47 | for (auto &GV : TheModule.globals()) { |
| 48 | if (GV.isDeclaration() || GV.hasLocalLinkage() || !GV.hasName()) |
| 49 | continue; |
| 50 | auto Name = GV.getName(); |
| 51 | Hasher.update(Name); |
| 52 | } |
| 53 | |
| 54 | // Now return the result. |
| 55 | MD5::MD5Result Hash; |
| 56 | Hasher.final(Hash); |
| 57 | SmallString<32> Result; |
| 58 | MD5::stringifyResult(Hash, Result); |
| 59 | TheHash = Result.str(); |
| 60 | return TheHash; |
| 61 | } |
| 62 | }; |
Benjamin Kramer | b7d3311 | 2016-08-06 11:13:10 +0000 | [diff] [blame] | 63 | } // end anonymous namespace |
Mehdi Amini | d5faa26 | 2016-04-12 21:35:28 +0000 | [diff] [blame] | 64 | |
| 65 | // Rename all the anon functions in the module |
| 66 | bool llvm::nameUnamedFunctions(Module &M) { |
| 67 | bool Changed = false; |
| 68 | ModuleHasher ModuleHash(M); |
| 69 | int count = 0; |
| 70 | for (auto &F : M) { |
| 71 | if (F.hasName()) |
| 72 | continue; |
| 73 | F.setName(Twine("anon.") + ModuleHash.get() + "." + Twine(count++)); |
| 74 | Changed = true; |
| 75 | } |
| 76 | return Changed; |
| 77 | } |
| 78 | |
| 79 | namespace { |
| 80 | |
Teresa Johnson | 4223dd8 | 2016-08-12 14:03:36 +0000 | [diff] [blame] | 81 | // Legacy pass that provides a name to every anon function. |
| 82 | class NameAnonFunctionLegacyPass : public ModulePass { |
Mehdi Amini | d5faa26 | 2016-04-12 21:35:28 +0000 | [diff] [blame] | 83 | |
| 84 | public: |
| 85 | /// Pass identification, replacement for typeid |
| 86 | static char ID; |
| 87 | |
| 88 | /// Specify pass name for debug output |
| 89 | const char *getPassName() const override { return "Name Anon Functions"; } |
| 90 | |
Teresa Johnson | 4223dd8 | 2016-08-12 14:03:36 +0000 | [diff] [blame] | 91 | explicit NameAnonFunctionLegacyPass() : ModulePass(ID) {} |
Mehdi Amini | d5faa26 | 2016-04-12 21:35:28 +0000 | [diff] [blame] | 92 | |
| 93 | bool runOnModule(Module &M) override { return nameUnamedFunctions(M); } |
| 94 | }; |
Teresa Johnson | 4223dd8 | 2016-08-12 14:03:36 +0000 | [diff] [blame] | 95 | char NameAnonFunctionLegacyPass::ID = 0; |
Mehdi Amini | d5faa26 | 2016-04-12 21:35:28 +0000 | [diff] [blame] | 96 | |
| 97 | } // anonymous namespace |
| 98 | |
Teresa Johnson | 4223dd8 | 2016-08-12 14:03:36 +0000 | [diff] [blame] | 99 | PreservedAnalyses NameAnonFunctionPass::run(Module &M, |
| 100 | ModuleAnalysisManager &AM) { |
| 101 | if (!nameUnamedFunctions(M)) |
| 102 | return PreservedAnalyses::all(); |
| 103 | |
| 104 | return PreservedAnalyses::none(); |
| 105 | } |
| 106 | |
| 107 | INITIALIZE_PASS_BEGIN(NameAnonFunctionLegacyPass, "name-anon-functions", |
Mehdi Amini | d5faa26 | 2016-04-12 21:35:28 +0000 | [diff] [blame] | 108 | "Provide a name to nameless functions", false, false) |
Teresa Johnson | 4223dd8 | 2016-08-12 14:03:36 +0000 | [diff] [blame] | 109 | INITIALIZE_PASS_END(NameAnonFunctionLegacyPass, "name-anon-functions", |
Mehdi Amini | d5faa26 | 2016-04-12 21:35:28 +0000 | [diff] [blame] | 110 | "Provide a name to nameless functions", false, false) |
| 111 | |
| 112 | namespace llvm { |
Teresa Johnson | 4223dd8 | 2016-08-12 14:03:36 +0000 | [diff] [blame] | 113 | ModulePass *createNameAnonFunctionPass() { |
| 114 | return new NameAnonFunctionLegacyPass(); |
| 115 | } |
Mehdi Amini | d5faa26 | 2016-04-12 21:35:28 +0000 | [diff] [blame] | 116 | } |