Mehdi Amini | 27d2379 | 2016-09-16 16:56:30 +0000 | [diff] [blame] | 1 | //===- NameAnonGlobals.cpp - ThinLTO Support: Name Unnamed Globals --------===// |
Mehdi Amini | d5faa26 | 2016-04-12 21:35:28 +0000 | [diff] [blame] | 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 | // |
Mehdi Amini | 27d2379 | 2016-09-16 16:56:30 +0000 | [diff] [blame] | 10 | // This file implements naming anonymous globals to make sure they can be |
| 11 | // referred to by ThinLTO. |
Mehdi Amini | d5faa26 | 2016-04-12 21:35:28 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Mehdi Amini | 27d2379 | 2016-09-16 16:56:30 +0000 | [diff] [blame] | 15 | #include "llvm/Transforms/Utils/NameAnonGlobals.h" |
Teresa Johnson | 4223dd8 | 2016-08-12 14:03:36 +0000 | [diff] [blame] | 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 |
Mehdi Amini | 27d2379 | 2016-09-16 16:56:30 +0000 | [diff] [blame] | 26 | // globals. |
Mehdi Amini | d5faa26 | 2016-04-12 21:35:28 +0000 | [diff] [blame] | 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 | |
Mehdi Amini | 27d2379 | 2016-09-16 16:56:30 +0000 | [diff] [blame] | 65 | // Rename all the anon globals in the module |
| 66 | bool llvm::nameUnamedGlobals(Module &M) { |
Mehdi Amini | d5faa26 | 2016-04-12 21:35:28 +0000 | [diff] [blame] | 67 | bool Changed = false; |
| 68 | ModuleHasher ModuleHash(M); |
| 69 | int count = 0; |
Mehdi Amini | 27d2379 | 2016-09-16 16:56:30 +0000 | [diff] [blame] | 70 | auto RenameIfNeed = [&](GlobalValue &GV) { |
Mehdi Amini | 2cac787 | 2016-09-16 16:56:25 +0000 | [diff] [blame] | 71 | if (GV.hasName()) |
| 72 | return; |
| 73 | GV.setName(Twine("anon.") + ModuleHash.get() + "." + Twine(count++)); |
Mehdi Amini | d5faa26 | 2016-04-12 21:35:28 +0000 | [diff] [blame] | 74 | Changed = true; |
Mehdi Amini | 2cac787 | 2016-09-16 16:56:25 +0000 | [diff] [blame] | 75 | }; |
| 76 | for (auto &GO : M.global_objects()) |
| 77 | RenameIfNeed(GO); |
| 78 | for (auto &GA : M.aliases()) |
| 79 | RenameIfNeed(GA); |
| 80 | |
Mehdi Amini | d5faa26 | 2016-04-12 21:35:28 +0000 | [diff] [blame] | 81 | return Changed; |
| 82 | } |
| 83 | |
| 84 | namespace { |
| 85 | |
Mehdi Amini | 27d2379 | 2016-09-16 16:56:30 +0000 | [diff] [blame] | 86 | // Legacy pass that provides a name to every anon globals. |
| 87 | class NameAnonGlobalLegacyPass : public ModulePass { |
Mehdi Amini | d5faa26 | 2016-04-12 21:35:28 +0000 | [diff] [blame] | 88 | |
| 89 | public: |
| 90 | /// Pass identification, replacement for typeid |
| 91 | static char ID; |
| 92 | |
| 93 | /// Specify pass name for debug output |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 94 | StringRef getPassName() const override { return "Name Anon Globals"; } |
Mehdi Amini | d5faa26 | 2016-04-12 21:35:28 +0000 | [diff] [blame] | 95 | |
Mehdi Amini | 27d2379 | 2016-09-16 16:56:30 +0000 | [diff] [blame] | 96 | explicit NameAnonGlobalLegacyPass() : ModulePass(ID) {} |
Mehdi Amini | d5faa26 | 2016-04-12 21:35:28 +0000 | [diff] [blame] | 97 | |
Mehdi Amini | 27d2379 | 2016-09-16 16:56:30 +0000 | [diff] [blame] | 98 | bool runOnModule(Module &M) override { return nameUnamedGlobals(M); } |
Mehdi Amini | d5faa26 | 2016-04-12 21:35:28 +0000 | [diff] [blame] | 99 | }; |
Mehdi Amini | 27d2379 | 2016-09-16 16:56:30 +0000 | [diff] [blame] | 100 | char NameAnonGlobalLegacyPass::ID = 0; |
Mehdi Amini | d5faa26 | 2016-04-12 21:35:28 +0000 | [diff] [blame] | 101 | |
| 102 | } // anonymous namespace |
| 103 | |
Mehdi Amini | 27d2379 | 2016-09-16 16:56:30 +0000 | [diff] [blame] | 104 | PreservedAnalyses NameAnonGlobalPass::run(Module &M, |
| 105 | ModuleAnalysisManager &AM) { |
| 106 | if (!nameUnamedGlobals(M)) |
Teresa Johnson | 4223dd8 | 2016-08-12 14:03:36 +0000 | [diff] [blame] | 107 | return PreservedAnalyses::all(); |
| 108 | |
| 109 | return PreservedAnalyses::none(); |
| 110 | } |
| 111 | |
Mehdi Amini | 27d2379 | 2016-09-16 16:56:30 +0000 | [diff] [blame] | 112 | INITIALIZE_PASS_BEGIN(NameAnonGlobalLegacyPass, "name-anon-globals", |
| 113 | "Provide a name to nameless globals", false, false) |
| 114 | INITIALIZE_PASS_END(NameAnonGlobalLegacyPass, "name-anon-globals", |
| 115 | "Provide a name to nameless globals", false, false) |
Mehdi Amini | d5faa26 | 2016-04-12 21:35:28 +0000 | [diff] [blame] | 116 | |
| 117 | namespace llvm { |
Mehdi Amini | 27d2379 | 2016-09-16 16:56:30 +0000 | [diff] [blame] | 118 | ModulePass *createNameAnonGlobalPass() { |
| 119 | return new NameAnonGlobalLegacyPass(); |
Teresa Johnson | 4223dd8 | 2016-08-12 14:03:36 +0000 | [diff] [blame] | 120 | } |
Mehdi Amini | d5faa26 | 2016-04-12 21:35:28 +0000 | [diff] [blame] | 121 | } |