1 //===- MetaRenamer.cpp - Rename everything with metasyntatic names --------===//
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 pass renames everything with metasyntatic names. The intent is to use
11 // this pass after bugpoint reduction to conceal the nature of the original
14 //===----------------------------------------------------------------------===//
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/Analysis/TargetLibraryInfo.h"
21 #include "llvm/IR/Argument.h"
22 #include "llvm/IR/BasicBlock.h"
23 #include "llvm/IR/DerivedTypes.h"
24 #include "llvm/IR/Function.h"
25 #include "llvm/IR/GlobalAlias.h"
26 #include "llvm/IR/GlobalVariable.h"
27 #include "llvm/IR/Instruction.h"
28 #include "llvm/IR/Module.h"
29 #include "llvm/IR/Type.h"
30 #include "llvm/IR/TypeFinder.h"
31 #include "llvm/Pass.h"
32 #include "llvm/Transforms/Utils.h"
36 static const char *const metaNames[] = {
37 // See http://en.wikipedia.org/wiki/Metasyntactic_variable
38 "foo", "bar", "baz", "quux", "barney", "snork", "zot", "blam", "hoge",
39 "wibble", "wobble", "widget", "wombat", "ham", "eggs", "pluto", "spam"
44 // This PRNG is from the ISO C spec. It is intentionally simple and
45 // unsuitable for cryptographic use. We're just looking for enough
46 // variety to surprise and delight users.
50 void srand(unsigned int seed) {
55 next = next * 1103515245 + 12345;
56 return (unsigned int)(next / 65536) % 32768;
61 Renamer(unsigned int seed) {
65 const char *newName() {
66 return metaNames[prng.rand() % array_lengthof(metaNames)];
72 struct MetaRenamer : public ModulePass {
73 // Pass identification, replacement for typeid
76 MetaRenamer() : ModulePass(ID) {
77 initializeMetaRenamerPass(*PassRegistry::getPassRegistry());
80 void getAnalysisUsage(AnalysisUsage &AU) const override {
81 AU.addRequired<TargetLibraryInfoWrapperPass>();
85 bool runOnModule(Module &M) override {
86 // Seed our PRNG with simple additive sum of ModuleID. We're looking to
87 // simply avoid always having the same function names, and we need to
88 // remain deterministic.
89 unsigned int randSeed = 0;
90 for (auto C : M.getModuleIdentifier())
93 Renamer renamer(randSeed);
96 for (auto AI = M.alias_begin(), AE = M.alias_end(); AI != AE; ++AI) {
97 StringRef Name = AI->getName();
98 if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
101 AI->setName("alias");
104 // Rename all global variables
105 for (auto GI = M.global_begin(), GE = M.global_end(); GI != GE; ++GI) {
106 StringRef Name = GI->getName();
107 if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
110 GI->setName("global");
113 // Rename all struct types
114 TypeFinder StructTypes;
115 StructTypes.run(M, true);
116 for (StructType *STy : StructTypes) {
117 if (STy->isLiteral() || STy->getName().empty()) continue;
119 SmallString<128> NameStorage;
120 STy->setName((Twine("struct.") +
121 renamer.newName()).toStringRef(NameStorage));
124 // Rename all functions
125 const TargetLibraryInfo &TLI =
126 getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
128 StringRef Name = F.getName();
130 // Leave library functions alone because their presence or absence could
131 // affect the behavior of other passes.
132 if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1) ||
133 TLI.getLibFunc(F, Tmp))
136 // Leave @main alone. The output of -metarenamer might be passed to
137 // lli for execution and the latter needs a main entry point.
139 F.setName(renamer.newName());
146 bool runOnFunction(Function &F) {
147 for (auto AI = F.arg_begin(), AE = F.arg_end(); AI != AE; ++AI)
148 if (!AI->getType()->isVoidTy())
155 if (!I.getType()->isVoidTy())
162 } // end anonymous namespace
164 char MetaRenamer::ID = 0;
166 INITIALIZE_PASS_BEGIN(MetaRenamer, "metarenamer",
167 "Assign new names to everything", false, false)
168 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
169 INITIALIZE_PASS_END(MetaRenamer, "metarenamer",
170 "Assign new names to everything", false, false)
172 //===----------------------------------------------------------------------===//
174 // MetaRenamer - Rename everything with metasyntactic names.
176 ModulePass *llvm::createMetaRenamerPass() {
177 return new MetaRenamer();