Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 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 | |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "llvm/Transforms/IPO.h" |
Alex Rosenberg | af2808c | 2012-09-14 19:19:57 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/STLExtras.h" |
| 18 | #include "llvm/ADT/SmallString.h" |
Bryant Wong | def79b2 | 2017-03-23 23:21:07 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 20 | #include "llvm/IR/DerivedTypes.h" |
| 21 | #include "llvm/IR/Function.h" |
| 22 | #include "llvm/IR/Module.h" |
| 23 | #include "llvm/IR/Type.h" |
Chandler Carruth | dcb603f | 2013-01-07 15:43:51 +0000 | [diff] [blame] | 24 | #include "llvm/IR/TypeFinder.h" |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 25 | #include "llvm/Pass.h" |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
| 28 | namespace { |
| 29 | |
| 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. |
| 33 | struct PRNG { |
| 34 | unsigned long next; |
| 35 | |
| 36 | void srand(unsigned int seed) { |
| 37 | next = seed; |
| 38 | } |
| 39 | |
Dmitri Gribenko | 0011bbf | 2012-11-15 16:51:49 +0000 | [diff] [blame] | 40 | int rand() { |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 41 | next = next * 1103515245 + 12345; |
| 42 | return (unsigned int)(next / 65536) % 32768; |
| 43 | } |
| 44 | }; |
| 45 | |
Alex Rosenberg | 99805ed | 2015-08-26 06:11:38 +0000 | [diff] [blame] | 46 | static const char *const metaNames[] = { |
| 47 | // See http://en.wikipedia.org/wiki/Metasyntactic_variable |
| 48 | "foo", "bar", "baz", "quux", "barney", "snork", "zot", "blam", "hoge", |
| 49 | "wibble", "wobble", "widget", "wombat", "ham", "eggs", "pluto", "spam" |
| 50 | }; |
| 51 | |
| 52 | struct Renamer { |
| 53 | Renamer(unsigned int seed) { |
| 54 | prng.srand(seed); |
| 55 | } |
| 56 | |
| 57 | const char *newName() { |
| 58 | return metaNames[prng.rand() % array_lengthof(metaNames)]; |
| 59 | } |
| 60 | |
| 61 | PRNG prng; |
| 62 | }; |
| 63 | |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 64 | struct MetaRenamer : public ModulePass { |
| 65 | static char ID; // Pass identification, replacement for typeid |
| 66 | MetaRenamer() : ModulePass(ID) { |
| 67 | initializeMetaRenamerPass(*PassRegistry::getPassRegistry()); |
| 68 | } |
| 69 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 70 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Bryant Wong | def79b2 | 2017-03-23 23:21:07 +0000 | [diff] [blame] | 71 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 72 | AU.setPreservesAll(); |
| 73 | } |
| 74 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 75 | bool runOnModule(Module &M) override { |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 76 | // Seed our PRNG with simple additive sum of ModuleID. We're looking to |
| 77 | // simply avoid always having the same function names, and we need to |
| 78 | // remain deterministic. |
| 79 | unsigned int randSeed = 0; |
Alex Rosenberg | 81cfed2 | 2015-08-26 06:11:41 +0000 | [diff] [blame] | 80 | for (auto C : M.getModuleIdentifier()) |
| 81 | randSeed += C; |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 82 | |
Alex Rosenberg | 99805ed | 2015-08-26 06:11:38 +0000 | [diff] [blame] | 83 | Renamer renamer(randSeed); |
Alex Rosenberg | a0a19c1 | 2015-08-27 05:37:12 +0000 | [diff] [blame] | 84 | |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 85 | // Rename all aliases |
Alex Rosenberg | 81cfed2 | 2015-08-26 06:11:41 +0000 | [diff] [blame] | 86 | for (auto AI = M.alias_begin(), AE = M.alias_end(); AI != AE; ++AI) { |
Anton Korobeynikov | 4ec3ae7 | 2013-01-23 15:03:08 +0000 | [diff] [blame] | 87 | StringRef Name = AI->getName(); |
| 88 | if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1)) |
| 89 | continue; |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 90 | |
Anton Korobeynikov | 4ec3ae7 | 2013-01-23 15:03:08 +0000 | [diff] [blame] | 91 | AI->setName("alias"); |
| 92 | } |
Alex Rosenberg | 5b3404a | 2015-08-26 06:11:36 +0000 | [diff] [blame] | 93 | |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 94 | // Rename all global variables |
Alex Rosenberg | 81cfed2 | 2015-08-26 06:11:41 +0000 | [diff] [blame] | 95 | for (auto GI = M.global_begin(), GE = M.global_end(); GI != GE; ++GI) { |
Anton Korobeynikov | 4ec3ae7 | 2013-01-23 15:03:08 +0000 | [diff] [blame] | 96 | StringRef Name = GI->getName(); |
| 97 | if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1)) |
| 98 | continue; |
| 99 | |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 100 | GI->setName("global"); |
Anton Korobeynikov | 4ec3ae7 | 2013-01-23 15:03:08 +0000 | [diff] [blame] | 101 | } |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 102 | |
| 103 | // Rename all struct types |
| 104 | TypeFinder StructTypes; |
| 105 | StructTypes.run(M, true); |
Alex Rosenberg | 81cfed2 | 2015-08-26 06:11:41 +0000 | [diff] [blame] | 106 | for (StructType *STy : StructTypes) { |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 107 | if (STy->isLiteral() || STy->getName().empty()) continue; |
| 108 | |
| 109 | SmallString<128> NameStorage; |
Alex Rosenberg | 99805ed | 2015-08-26 06:11:38 +0000 | [diff] [blame] | 110 | STy->setName((Twine("struct.") + |
| 111 | renamer.newName()).toStringRef(NameStorage)); |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | // Rename all functions |
Bryant Wong | def79b2 | 2017-03-23 23:21:07 +0000 | [diff] [blame] | 115 | const TargetLibraryInfo &TLI = |
| 116 | getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
Alex Rosenberg | 81cfed2 | 2015-08-26 06:11:41 +0000 | [diff] [blame] | 117 | for (auto &F : M) { |
| 118 | StringRef Name = F.getName(); |
Bryant Wong | def79b2 | 2017-03-23 23:21:07 +0000 | [diff] [blame] | 119 | LibFunc Tmp; |
| 120 | // Leave library functions alone because their presence or absence could |
| 121 | // affect the behavior of other passes. |
| 122 | if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1) || |
| 123 | TLI.getLibFunc(F, Tmp)) |
Anton Korobeynikov | 4ec3ae7 | 2013-01-23 15:03:08 +0000 | [diff] [blame] | 124 | continue; |
| 125 | |
Alex Rosenberg | 81cfed2 | 2015-08-26 06:11:41 +0000 | [diff] [blame] | 126 | F.setName(renamer.newName()); |
| 127 | runOnFunction(F); |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 128 | } |
| 129 | return true; |
| 130 | } |
| 131 | |
| 132 | bool runOnFunction(Function &F) { |
Alex Rosenberg | 81cfed2 | 2015-08-26 06:11:41 +0000 | [diff] [blame] | 133 | for (auto AI = F.arg_begin(), AE = F.arg_end(); AI != AE; ++AI) |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 134 | if (!AI->getType()->isVoidTy()) |
| 135 | AI->setName("arg"); |
| 136 | |
Alex Rosenberg | 81cfed2 | 2015-08-26 06:11:41 +0000 | [diff] [blame] | 137 | for (auto &BB : F) { |
| 138 | BB.setName("bb"); |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 139 | |
Alex Rosenberg | 81cfed2 | 2015-08-26 06:11:41 +0000 | [diff] [blame] | 140 | for (auto &I : BB) |
| 141 | if (!I.getType()->isVoidTy()) |
| 142 | I.setName("tmp"); |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 143 | } |
| 144 | return true; |
| 145 | } |
| 146 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 147 | } |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 148 | |
| 149 | char MetaRenamer::ID = 0; |
Bryant Wong | def79b2 | 2017-03-23 23:21:07 +0000 | [diff] [blame] | 150 | INITIALIZE_PASS_BEGIN(MetaRenamer, "metarenamer", |
| 151 | "Assign new names to everything", false, false) |
| 152 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
| 153 | INITIALIZE_PASS_END(MetaRenamer, "metarenamer", |
| 154 | "Assign new names to everything", false, false) |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 155 | //===----------------------------------------------------------------------===// |
| 156 | // |
| 157 | // MetaRenamer - Rename everything with metasyntactic names. |
| 158 | // |
| 159 | ModulePass *llvm::createMetaRenamerPass() { |
| 160 | return new MetaRenamer(); |
| 161 | } |