1 //===- NameAnonFunctions.cpp - ThinLTO Summary-based Function Import ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements naming anonymous function to make sure they can be
11 // refered to by ThinLTO.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/IR/Module.h"
17 #include "llvm/Support/MD5.h"
18 #include "llvm/Transforms/Utils/ModuleUtils.h"
22 // Compute a "unique" hash for the module based on the name of the public
29 ModuleHasher(Module &M) : TheModule(M) {}
31 /// Return the lazily computed hash.
38 for (auto &F : TheModule) {
39 if (F.isDeclaration() || F.hasLocalLinkage() || !F.hasName())
41 auto Name = F.getName();
44 for (auto &GV : TheModule.globals()) {
45 if (GV.isDeclaration() || GV.hasLocalLinkage() || !GV.hasName())
47 auto Name = GV.getName();
51 // Now return the result.
54 SmallString<32> Result;
55 MD5::stringifyResult(Hash, Result);
56 TheHash = Result.str();
61 // Rename all the anon functions in the module
62 bool llvm::nameUnamedFunctions(Module &M) {
64 ModuleHasher ModuleHash(M);
69 F.setName(Twine("anon.") + ModuleHash.get() + "." + Twine(count++));
77 // Simple pass that provides a name to every anon function.
78 class NameAnonFunction : public ModulePass {
81 /// Pass identification, replacement for typeid
84 /// Specify pass name for debug output
85 const char *getPassName() const override { return "Name Anon Functions"; }
87 explicit NameAnonFunction() : ModulePass(ID) {}
89 bool runOnModule(Module &M) override { return nameUnamedFunctions(M); }
91 char NameAnonFunction::ID = 0;
93 } // anonymous namespace
95 INITIALIZE_PASS_BEGIN(NameAnonFunction, "name-anon-functions",
96 "Provide a name to nameless functions", false, false)
97 INITIALIZE_PASS_END(NameAnonFunction, "name-anon-functions",
98 "Provide a name to nameless functions", false, false)
101 ModulePass *createNameAnonFunctionPass() { return new NameAnonFunction(); }