Reduce code duplication.
[oota-llvm.git] / lib / Transforms / Utils / MetaRenamer.cpp
1 //===- MetaRenamer.cpp - Rename everything with metasyntatic names --------===//
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 pass renames everything with metasyntatic names. The intent is to use
11 // this pass after bugpoint reduction to conceal the nature of the original
12 // program.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "llvm/Transforms/IPO.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/IR/DerivedTypes.h"
20 #include "llvm/IR/Function.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/IR/Type.h"
23 #include "llvm/IR/TypeFinder.h"
24 #include "llvm/Pass.h"
25 using namespace llvm;
26
27 namespace {
28
29   // This PRNG is from the ISO C spec. It is intentionally simple and
30   // unsuitable for cryptographic use. We're just looking for enough
31   // variety to surprise and delight users.
32   struct PRNG {
33     unsigned long next;
34
35     void srand(unsigned int seed) {
36       next = seed;
37     }
38
39     int rand() {
40       next = next * 1103515245 + 12345;
41       return (unsigned int)(next / 65536) % 32768;
42     }
43   };
44
45   static const char *const metaNames[] = {
46     // See http://en.wikipedia.org/wiki/Metasyntactic_variable
47     "foo", "bar", "baz", "quux", "barney", "snork", "zot", "blam", "hoge",
48     "wibble", "wobble", "widget", "wombat", "ham", "eggs", "pluto", "spam"
49   };
50
51   struct Renamer {
52     Renamer(unsigned int seed) {
53       prng.srand(seed);
54     }
55
56     const char *newName() {
57       return metaNames[prng.rand() % array_lengthof(metaNames)];
58     }
59
60     PRNG prng;
61   };
62   
63   struct MetaRenamer : public ModulePass {
64     static char ID; // Pass identification, replacement for typeid
65     MetaRenamer() : ModulePass(ID) {
66       initializeMetaRenamerPass(*PassRegistry::getPassRegistry());
67     }
68
69     void getAnalysisUsage(AnalysisUsage &AU) const override {
70       AU.setPreservesAll();
71     }
72
73     bool runOnModule(Module &M) override {
74       // Seed our PRNG with simple additive sum of ModuleID. We're looking to
75       // simply avoid always having the same function names, and we need to
76       // remain deterministic.
77       unsigned int randSeed = 0;
78       for (std::string::const_iterator I = M.getModuleIdentifier().begin(),
79            E = M.getModuleIdentifier().end(); I != E; ++I)
80         randSeed += *I;
81
82       Renamer renamer(randSeed);
83            
84       // Rename all aliases
85       for (Module::alias_iterator AI = M.alias_begin(), AE = M.alias_end();
86            AI != AE; ++AI) {
87         StringRef Name = AI->getName();
88         if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
89           continue;
90
91         AI->setName("alias");
92       }
93
94       // Rename all global variables
95       for (Module::global_iterator GI = M.global_begin(), GE = M.global_end();
96            GI != GE; ++GI) {
97         StringRef Name = GI->getName();
98         if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
99           continue;
100
101         GI->setName("global");
102       }
103
104       // Rename all struct types
105       TypeFinder StructTypes;
106       StructTypes.run(M, true);
107       for (unsigned i = 0, e = StructTypes.size(); i != e; ++i) {
108         StructType *STy = StructTypes[i];
109         if (STy->isLiteral() || STy->getName().empty()) continue;
110
111         SmallString<128> NameStorage;
112         STy->setName((Twine("struct.") +
113           renamer.newName()).toStringRef(NameStorage));
114       }
115
116       // Rename all functions
117       for (Module::iterator FI = M.begin(), FE = M.end();
118            FI != FE; ++FI) {
119         StringRef Name = FI->getName();
120         if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
121           continue;
122
123         FI->setName(renamer.newName());
124         runOnFunction(*FI);
125       }
126       return true;
127     }
128
129     bool runOnFunction(Function &F) {
130       for (Function::arg_iterator AI = F.arg_begin(), AE = F.arg_end();
131            AI != AE; ++AI)
132         if (!AI->getType()->isVoidTy())
133           AI->setName("arg");
134
135       for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
136         BB->setName("bb");
137
138         for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
139           if (!I->getType()->isVoidTy())
140             I->setName("tmp");
141       }
142       return true;
143     }
144   };
145 }
146
147 char MetaRenamer::ID = 0;
148 INITIALIZE_PASS(MetaRenamer, "metarenamer", 
149                 "Assign new names to everything", false, false)
150 //===----------------------------------------------------------------------===//
151 //
152 // MetaRenamer - Rename everything with metasyntactic names.
153 //
154 ModulePass *llvm::createMetaRenamerPass() {
155   return new MetaRenamer();
156 }