blob: 6dc3520b61ac18af3a19f0a666664538d3afbec1 [file] [log] [blame]
Mehdi Aminid5faa262016-04-12 21:35:28 +00001//===- 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 Johnson4223dd82016-08-12 14:03:36 +000015#include "llvm/Transforms/Utils/NameAnonFunctions.h"
16
Mehdi Aminid5faa262016-04-12 21:35:28 +000017#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
22using namespace llvm;
23
Benjamin Kramerb7d33112016-08-06 11:13:10 +000024namespace {
Mehdi Aminid5faa262016-04-12 21:35:28 +000025// Compute a "unique" hash for the module based on the name of the public
26// functions.
27class ModuleHasher {
28 Module &TheModule;
29 std::string TheHash;
30
31public:
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 Kramerb7d33112016-08-06 11:13:10 +000063} // end anonymous namespace
Mehdi Aminid5faa262016-04-12 21:35:28 +000064
65// Rename all the anon functions in the module
66bool 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
79namespace {
80
Teresa Johnson4223dd82016-08-12 14:03:36 +000081// Legacy pass that provides a name to every anon function.
82class NameAnonFunctionLegacyPass : public ModulePass {
Mehdi Aminid5faa262016-04-12 21:35:28 +000083
84public:
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 Johnson4223dd82016-08-12 14:03:36 +000091 explicit NameAnonFunctionLegacyPass() : ModulePass(ID) {}
Mehdi Aminid5faa262016-04-12 21:35:28 +000092
93 bool runOnModule(Module &M) override { return nameUnamedFunctions(M); }
94};
Teresa Johnson4223dd82016-08-12 14:03:36 +000095char NameAnonFunctionLegacyPass::ID = 0;
Mehdi Aminid5faa262016-04-12 21:35:28 +000096
97} // anonymous namespace
98
Teresa Johnson4223dd82016-08-12 14:03:36 +000099PreservedAnalyses NameAnonFunctionPass::run(Module &M,
100 ModuleAnalysisManager &AM) {
101 if (!nameUnamedFunctions(M))
102 return PreservedAnalyses::all();
103
104 return PreservedAnalyses::none();
105}
106
107INITIALIZE_PASS_BEGIN(NameAnonFunctionLegacyPass, "name-anon-functions",
Mehdi Aminid5faa262016-04-12 21:35:28 +0000108 "Provide a name to nameless functions", false, false)
Teresa Johnson4223dd82016-08-12 14:03:36 +0000109INITIALIZE_PASS_END(NameAnonFunctionLegacyPass, "name-anon-functions",
Mehdi Aminid5faa262016-04-12 21:35:28 +0000110 "Provide a name to nameless functions", false, false)
111
112namespace llvm {
Teresa Johnson4223dd82016-08-12 14:03:36 +0000113ModulePass *createNameAnonFunctionPass() {
114 return new NameAnonFunctionLegacyPass();
115}
Mehdi Aminid5faa262016-04-12 21:35:28 +0000116}