#include "llvm/Transforms/Utils/MetaRenamer.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/IR/Argument.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalAlias.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/PassManager.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/TypeFinder.h"
#include "llvm/InitializePasses.h"
#include "llvm/Pass.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Transforms/Utils.h"
using namespace llvm;
static cl::opt<std::string> RenameExcludeFunctionPrefixes(
"rename-exclude-function-prefixes",
cl::desc("Prefixes for functions that don't need to be renamed, separated "
"by a comma"),
cl::Hidden);
static cl::opt<std::string> RenameExcludeAliasPrefixes(
"rename-exclude-alias-prefixes",
cl::desc("Prefixes for aliases that don't need to be renamed, separated "
"by a comma"),
cl::Hidden);
static cl::opt<std::string> RenameExcludeGlobalPrefixes(
"rename-exclude-global-prefixes",
cl::desc(
"Prefixes for global values that don't need to be renamed, separated "
"by a comma"),
cl::Hidden);
static cl::opt<std::string> RenameExcludeStructPrefixes(
"rename-exclude-struct-prefixes",
cl::desc("Prefixes for structs that don't need to be renamed, separated "
"by a comma"),
cl::Hidden);
static const char *const metaNames[] = {
"foo", "bar", "baz", "quux", "barney", "snork", "zot", "blam", "hoge",
"wibble", "wobble", "widget", "wombat", "ham", "eggs", "pluto", "spam"
};
namespace {
struct PRNG {
unsigned long next;
void srand(unsigned int seed) { next = seed; }
int rand() {
next = next * 1103515245 + 12345;
return (unsigned int)(next / 65536) % 32768;
}
};
struct Renamer {
Renamer(unsigned int seed) { prng.srand(seed); }
const char *newName() {
return metaNames[prng.rand() % array_lengthof(metaNames)];
}
PRNG prng;
};
static void
parseExcludedPrefixes(StringRef PrefixesStr,
SmallVectorImpl<StringRef> &ExcludedPrefixes) {
for (;;) {
auto PrefixesSplit = PrefixesStr.split(',');
if (PrefixesSplit.first.empty())
break;
ExcludedPrefixes.push_back(PrefixesSplit.first);
PrefixesStr = PrefixesSplit.second;
}
}
void MetaRename(Function &F) {
for (Argument &Arg : F.args())
if (!Arg.getType()->isVoidTy())
Arg.setName("arg");
for (auto &BB : F) {
BB.setName("bb");
for (auto &I : BB)
if (!I.getType()->isVoidTy())
I.setName("tmp");
}
}
void MetaRename(Module &M,
function_ref<TargetLibraryInfo &(Function &)> GetTLI) {
unsigned int randSeed = 0;
for (auto C : M.getModuleIdentifier())
randSeed += C;
Renamer renamer(randSeed);
SmallVector<StringRef, 8> ExcludedAliasesPrefixes;
SmallVector<StringRef, 8> ExcludedGlobalsPrefixes;
SmallVector<StringRef, 8> ExcludedStructsPrefixes;
SmallVector<StringRef, 8> ExcludedFuncPrefixes;
parseExcludedPrefixes(RenameExcludeAliasPrefixes, ExcludedAliasesPrefixes);
parseExcludedPrefixes(RenameExcludeGlobalPrefixes, ExcludedGlobalsPrefixes);
parseExcludedPrefixes(RenameExcludeStructPrefixes, ExcludedStructsPrefixes);
parseExcludedPrefixes(RenameExcludeFunctionPrefixes, ExcludedFuncPrefixes);
auto IsNameExcluded = [](StringRef &Name,
SmallVectorImpl<StringRef> &ExcludedPrefixes) {
return any_of(ExcludedPrefixes,
[&Name](auto &Prefix) { return Name.startswith(Prefix); });
};
for (GlobalAlias &GA : M.aliases()) {
StringRef Name = GA.getName();
if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1) ||
IsNameExcluded(Name, ExcludedAliasesPrefixes))
continue;
GA.setName("alias");
}
for (GlobalVariable &GV : M.globals()) {
StringRef Name = GV.getName();
if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1) ||
IsNameExcluded(Name, ExcludedGlobalsPrefixes))
continue;
GV.setName("global");
}
TypeFinder StructTypes;
StructTypes.run(M, true);
for (StructType *STy : StructTypes) {
StringRef Name = STy->getName();
if (STy->isLiteral() || Name.empty() ||
IsNameExcluded(Name, ExcludedStructsPrefixes))
continue;
SmallString<128> NameStorage;
STy->setName(
(Twine("struct.") + renamer.newName()).toStringRef(NameStorage));
}
for (auto &F : M) {
StringRef Name = F.getName();
LibFunc Tmp;
if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1) ||
GetTLI(F).getLibFunc(F, Tmp) ||
IsNameExcluded(Name, ExcludedFuncPrefixes))
continue;
if (Name != "main")
F.setName(renamer.newName());
MetaRename(F);
}
}
struct MetaRenamer : public ModulePass {
static char ID;
MetaRenamer() : ModulePass(ID) {
initializeMetaRenamerPass(*PassRegistry::getPassRegistry());
}
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.addRequired<TargetLibraryInfoWrapperPass>();
AU.setPreservesAll();
}
bool runOnModule(Module &M) override {
auto GetTLI = [this](Function &F) -> TargetLibraryInfo & {
return this->getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
};
MetaRename(M, GetTLI);
return true;
}
};
}
char MetaRenamer::ID = 0;
INITIALIZE_PASS_BEGIN(MetaRenamer, "metarenamer",
"Assign new names to everything", false, false)
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
INITIALIZE_PASS_END(MetaRenamer, "metarenamer",
"Assign new names to everything", false, false)
ModulePass *llvm::createMetaRenamerPass() {
return new MetaRenamer();
}
PreservedAnalyses MetaRenamerPass::run(Module &M, ModuleAnalysisManager &AM) {
FunctionAnalysisManager &FAM =
AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
auto GetTLI = [&FAM](Function &F) -> TargetLibraryInfo & {
return FAM.getResult<TargetLibraryAnalysis>(F);
};
MetaRename(M, GetTLI);
return PreservedAnalyses::all();
}