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/Transforms/IPO.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/Function.h"
21 #include "llvm/Module.h"
22 #include "llvm/Pass.h"
23 #include "llvm/Type.h"
24 #include "llvm/TypeFinder.h"
30 // This PRNG is from the ISO C spec. It is intentionally simple and
31 // unsuitable for cryptographic use. We're just looking for enough
32 // variety to surprise and delight users.
36 void srand(unsigned int seed) {
41 next = next * 1103515245 + 12345;
42 return (unsigned int)(next / 65536) % 32768;
46 struct MetaRenamer : public ModulePass {
47 static char ID; // Pass identification, replacement for typeid
48 MetaRenamer() : ModulePass(ID) {
49 initializeMetaRenamerPass(*PassRegistry::getPassRegistry());
52 void getAnalysisUsage(AnalysisUsage &AU) const {
56 bool runOnModule(Module &M) {
57 static const char *metaNames[] = {
58 // See http://en.wikipedia.org/wiki/Metasyntactic_variable
59 "foo", "bar", "baz", "quux", "barney", "snork", "zot", "blam", "hoge",
60 "wibble", "wobble", "widget", "wombat", "ham", "eggs", "pluto", "spam"
63 // Seed our PRNG with simple additive sum of ModuleID. We're looking to
64 // simply avoid always having the same function names, and we need to
65 // remain deterministic.
66 unsigned int randSeed = 0;
67 for (std::string::const_iterator I = M.getModuleIdentifier().begin(),
68 E = M.getModuleIdentifier().end(); I != E; ++I)
75 for (Module::alias_iterator AI = M.alias_begin(), AE = M.alias_end();
79 // Rename all global variables
80 for (Module::global_iterator GI = M.global_begin(), GE = M.global_end();
82 GI->setName("global");
84 // Rename all struct types
85 TypeFinder StructTypes;
86 StructTypes.run(M, true);
87 for (unsigned i = 0, e = StructTypes.size(); i != e; ++i) {
88 StructType *STy = StructTypes[i];
89 if (STy->isLiteral() || STy->getName().empty()) continue;
91 SmallString<128> NameStorage;
92 STy->setName((Twine("struct.") + metaNames[prng.rand() %
93 array_lengthof(metaNames)]).toStringRef(NameStorage));
96 // Rename all functions
97 for (Module::iterator FI = M.begin(), FE = M.end();
99 FI->setName(metaNames[prng.rand() % array_lengthof(metaNames)]);
105 bool runOnFunction(Function &F) {
106 for (Function::arg_iterator AI = F.arg_begin(), AE = F.arg_end();
108 if (!AI->getType()->isVoidTy())
111 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
114 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
115 if (!I->getType()->isVoidTy())
123 char MetaRenamer::ID = 0;
124 INITIALIZE_PASS(MetaRenamer, "metarenamer",
125 "Assign new names to everything", false, false)
126 //===----------------------------------------------------------------------===//
128 // MetaRenamer - Rename everything with metasyntactic names.
130 ModulePass *llvm::createMetaRenamerPass() {
131 return new MetaRenamer();