2016-09-17 00:56:30 +08:00
|
|
|
//===- NameAnonGlobals.cpp - ThinLTO Support: Name Unnamed Globals --------===//
|
2016-04-13 05:35:28 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2016-09-17 00:56:30 +08:00
|
|
|
// This file implements naming anonymous globals to make sure they can be
|
|
|
|
// referred to by ThinLTO.
|
2016-04-13 05:35:28 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-09-17 00:56:30 +08:00
|
|
|
#include "llvm/Transforms/Utils/NameAnonGlobals.h"
|
2016-08-12 22:03:36 +08:00
|
|
|
|
2016-04-13 05:35:28 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/Support/MD5.h"
|
|
|
|
#include "llvm/Transforms/Utils/ModuleUtils.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2016-08-06 19:13:10 +08:00
|
|
|
namespace {
|
2016-04-13 05:35:28 +08:00
|
|
|
// Compute a "unique" hash for the module based on the name of the public
|
2016-09-17 00:56:30 +08:00
|
|
|
// globals.
|
2016-04-13 05:35:28 +08:00
|
|
|
class ModuleHasher {
|
|
|
|
Module &TheModule;
|
|
|
|
std::string TheHash;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ModuleHasher(Module &M) : TheModule(M) {}
|
|
|
|
|
|
|
|
/// Return the lazily computed hash.
|
|
|
|
std::string &get() {
|
|
|
|
if (!TheHash.empty())
|
|
|
|
// Cache hit :)
|
|
|
|
return TheHash;
|
|
|
|
|
|
|
|
MD5 Hasher;
|
|
|
|
for (auto &F : TheModule) {
|
|
|
|
if (F.isDeclaration() || F.hasLocalLinkage() || !F.hasName())
|
|
|
|
continue;
|
|
|
|
auto Name = F.getName();
|
|
|
|
Hasher.update(Name);
|
|
|
|
}
|
|
|
|
for (auto &GV : TheModule.globals()) {
|
|
|
|
if (GV.isDeclaration() || GV.hasLocalLinkage() || !GV.hasName())
|
|
|
|
continue;
|
|
|
|
auto Name = GV.getName();
|
|
|
|
Hasher.update(Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now return the result.
|
|
|
|
MD5::MD5Result Hash;
|
|
|
|
Hasher.final(Hash);
|
|
|
|
SmallString<32> Result;
|
|
|
|
MD5::stringifyResult(Hash, Result);
|
|
|
|
TheHash = Result.str();
|
|
|
|
return TheHash;
|
|
|
|
}
|
|
|
|
};
|
2016-08-06 19:13:10 +08:00
|
|
|
} // end anonymous namespace
|
2016-04-13 05:35:28 +08:00
|
|
|
|
2016-09-17 00:56:30 +08:00
|
|
|
// Rename all the anon globals in the module
|
|
|
|
bool llvm::nameUnamedGlobals(Module &M) {
|
2016-04-13 05:35:28 +08:00
|
|
|
bool Changed = false;
|
|
|
|
ModuleHasher ModuleHash(M);
|
|
|
|
int count = 0;
|
2016-09-17 00:56:30 +08:00
|
|
|
auto RenameIfNeed = [&](GlobalValue &GV) {
|
2016-09-17 00:56:25 +08:00
|
|
|
if (GV.hasName())
|
|
|
|
return;
|
|
|
|
GV.setName(Twine("anon.") + ModuleHash.get() + "." + Twine(count++));
|
2016-04-13 05:35:28 +08:00
|
|
|
Changed = true;
|
2016-09-17 00:56:25 +08:00
|
|
|
};
|
|
|
|
for (auto &GO : M.global_objects())
|
|
|
|
RenameIfNeed(GO);
|
|
|
|
for (auto &GA : M.aliases())
|
|
|
|
RenameIfNeed(GA);
|
|
|
|
|
2016-04-13 05:35:28 +08:00
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2016-09-17 00:56:30 +08:00
|
|
|
// Legacy pass that provides a name to every anon globals.
|
|
|
|
class NameAnonGlobalLegacyPass : public ModulePass {
|
2016-04-13 05:35:28 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
/// Pass identification, replacement for typeid
|
|
|
|
static char ID;
|
|
|
|
|
|
|
|
/// Specify pass name for debug output
|
2016-10-01 10:56:57 +08:00
|
|
|
StringRef getPassName() const override { return "Name Anon Globals"; }
|
2016-04-13 05:35:28 +08:00
|
|
|
|
2016-09-17 00:56:30 +08:00
|
|
|
explicit NameAnonGlobalLegacyPass() : ModulePass(ID) {}
|
2016-04-13 05:35:28 +08:00
|
|
|
|
2016-09-17 00:56:30 +08:00
|
|
|
bool runOnModule(Module &M) override { return nameUnamedGlobals(M); }
|
2016-04-13 05:35:28 +08:00
|
|
|
};
|
2016-09-17 00:56:30 +08:00
|
|
|
char NameAnonGlobalLegacyPass::ID = 0;
|
2016-04-13 05:35:28 +08:00
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2016-09-17 00:56:30 +08:00
|
|
|
PreservedAnalyses NameAnonGlobalPass::run(Module &M,
|
|
|
|
ModuleAnalysisManager &AM) {
|
|
|
|
if (!nameUnamedGlobals(M))
|
2016-08-12 22:03:36 +08:00
|
|
|
return PreservedAnalyses::all();
|
|
|
|
|
|
|
|
return PreservedAnalyses::none();
|
|
|
|
}
|
|
|
|
|
2016-09-17 00:56:30 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(NameAnonGlobalLegacyPass, "name-anon-globals",
|
|
|
|
"Provide a name to nameless globals", false, false)
|
|
|
|
INITIALIZE_PASS_END(NameAnonGlobalLegacyPass, "name-anon-globals",
|
|
|
|
"Provide a name to nameless globals", false, false)
|
2016-04-13 05:35:28 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
2016-09-17 00:56:30 +08:00
|
|
|
ModulePass *createNameAnonGlobalPass() {
|
|
|
|
return new NameAnonGlobalLegacyPass();
|
2016-08-12 22:03:36 +08:00
|
|
|
}
|
2016-04-13 05:35:28 +08:00
|
|
|
}
|