LLVM  14.0.0git
MetaRenamer.cpp
Go to the documentation of this file.
1 //===- MetaRenamer.cpp - Rename everything with metasyntatic names --------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass renames everything with metasyntatic names. The intent is to use
10 // this pass after bugpoint reduction to conceal the nature of the original
11 // program.
12 //
13 //===----------------------------------------------------------------------===//
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"
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/PassManager.h"
30 #include "llvm/IR/Type.h"
31 #include "llvm/IR/TypeFinder.h"
32 #include "llvm/InitializePasses.h"
33 #include "llvm/Pass.h"
34 #include "llvm/Transforms/Utils.h"
35 
36 using namespace llvm;
37 
38 static const char *const metaNames[] = {
39  // See http://en.wikipedia.org/wiki/Metasyntactic_variable
40  "foo", "bar", "baz", "quux", "barney", "snork", "zot", "blam", "hoge",
41  "wibble", "wobble", "widget", "wombat", "ham", "eggs", "pluto", "spam"
42 };
43 
44 namespace {
45 // This PRNG is from the ISO C spec. It is intentionally simple and
46 // unsuitable for cryptographic use. We're just looking for enough
47 // variety to surprise and delight users.
48 struct PRNG {
49  unsigned long next;
50 
51  void srand(unsigned int seed) { next = seed; }
52 
53  int rand() {
54  next = next * 1103515245 + 12345;
55  return (unsigned int)(next / 65536) % 32768;
56  }
57 };
58 
59 struct Renamer {
60  Renamer(unsigned int seed) { prng.srand(seed); }
61 
62  const char *newName() {
63  return metaNames[prng.rand() % array_lengthof(metaNames)];
64  }
65 
66  PRNG prng;
67 };
68 
69 void MetaRename(Function &F) {
70  for (Argument &Arg : F.args())
71  if (!Arg.getType()->isVoidTy())
72  Arg.setName("arg");
73 
74  for (auto &BB : F) {
75  BB.setName("bb");
76 
77  for (auto &I : BB)
78  if (!I.getType()->isVoidTy())
79  I.setName("tmp");
80  }
81 }
82 
83 void MetaRename(Module &M,
85  // Seed our PRNG with simple additive sum of ModuleID. We're looking to
86  // simply avoid always having the same function names, and we need to
87  // remain deterministic.
88  unsigned int randSeed = 0;
89  for (auto C : M.getModuleIdentifier())
90  randSeed += C;
91 
92  Renamer renamer(randSeed);
93 
94  // Rename all aliases
95  for (auto AI = M.alias_begin(), AE = M.alias_end(); AI != AE; ++AI) {
96  StringRef Name = AI->getName();
97  if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
98  continue;
99 
100  AI->setName("alias");
101  }
102 
103  // Rename all global variables
104  for (GlobalVariable &GV : M.globals()) {
105  StringRef Name = GV.getName();
106  if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
107  continue;
108 
109  GV.setName("global");
110  }
111 
112  // Rename all struct types
113  TypeFinder StructTypes;
114  StructTypes.run(M, true);
115  for (StructType *STy : StructTypes) {
116  if (STy->isLiteral() || STy->getName().empty())
117  continue;
118 
119  SmallString<128> NameStorage;
120  STy->setName(
121  (Twine("struct.") + renamer.newName()).toStringRef(NameStorage));
122  }
123 
124  // Rename all functions
125  for (auto &F : M) {
126  StringRef Name = F.getName();
127  LibFunc Tmp;
128  // Leave library functions alone because their presence or absence could
129  // affect the behavior of other passes.
130  if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1) ||
131  GetTLI(F).getLibFunc(F, Tmp))
132  continue;
133 
134  // Leave @main alone. The output of -metarenamer might be passed to
135  // lli for execution and the latter needs a main entry point.
136  if (Name != "main")
137  F.setName(renamer.newName());
138 
139  MetaRename(F);
140  }
141 }
142 
143 struct MetaRenamer : public ModulePass {
144  // Pass identification, replacement for typeid
145  static char ID;
146 
147  MetaRenamer() : ModulePass(ID) {
149  }
150 
151  void getAnalysisUsage(AnalysisUsage &AU) const override {
153  AU.setPreservesAll();
154  }
155 
156  bool runOnModule(Module &M) override {
157  auto GetTLI = [this](Function &F) -> TargetLibraryInfo & {
158  return this->getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
159  };
160  MetaRename(M, GetTLI);
161  return true;
162  }
163 };
164 
165 } // end anonymous namespace
166 
167 char MetaRenamer::ID = 0;
168 
169 INITIALIZE_PASS_BEGIN(MetaRenamer, "metarenamer",
170  "Assign new names to everything", false, false)
173  "Assign new names to everything", false, false)
174 
175 //===----------------------------------------------------------------------===//
176 //
177 // MetaRenamer - Rename everything with metasyntactic names.
178 //
180  return new MetaRenamer();
181 }
182 
186  auto GetTLI = [&FAM](Function &F) -> TargetLibraryInfo & {
188  };
189  MetaRename(M, GetTLI);
190 
191  return PreservedAnalyses::all();
192 }
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
llvm::Argument
This class represents an incoming formal argument to a Function.
Definition: Argument.h:29
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::ModulePass
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:238
llvm::AnalysisManager::getResult
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:779
llvm::Function
Definition: Function.h:61
StringRef.h
Pass.h
llvm::GlobalVariable
Definition: GlobalVariable.h:40
FAM
FunctionAnalysisManager FAM
Definition: PassBuilderBindings.cpp:59
to
Should compile to
Definition: README.txt:449
Module.h
MetaRenamer.h
INITIALIZE_PASS_BEGIN
INITIALIZE_PASS_BEGIN(MetaRenamer, "metarenamer", "Assign new names to everything", false, false) INITIALIZE_PASS_END(MetaRenamer
STLExtras.h
everything
Assign new names to everything
Definition: MetaRenamer.cpp:173
new
Common register allocation spilling lr str ldr sxth r3 ldr mla r4 can lr mov lr str ldr sxth r3 mla r4 and then merge mul and lr str ldr sxth r3 mla r4 It also increase the likelihood the store may become dead bb27 Successors according to LLVM ID Predecessors according to mbb< bb27, 0x8b0a7c0 > Note ADDri is not a two address instruction its result reg1037 is an operand of the PHI node in bb76 and its operand reg1039 is the result of the PHI node We should treat it as a two address code and make sure the ADDri is scheduled after any node that reads reg1039 Use info(i.e. register scavenger) to assign it a free register to allow reuse the collector could move the objects and invalidate the derived pointer This is bad enough in the first but safe points can crop up unpredictably **array_addr i32 n y store obj * new
Definition: README.txt:125
F
#define F(x, y, z)
Definition: MD5.cpp:56
Arg
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
Definition: AMDGPULibCalls.cpp:206
Instruction.h
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
SmallString.h
llvm::LibFunc
LibFunc
Definition: TargetLibraryInfo.h:34
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
Twine.h
TypeFinder.h
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
llvm::initializeMetaRenamerPass
void initializeMetaRenamerPass(PassRegistry &)
TargetLibraryInfo.h
false
Definition: StackSlotColoring.cpp:142
llvm::MetaRenamerPass::run
PreservedAnalyses run(Module &, ModuleAnalysisManager &)
Definition: MetaRenamer.cpp:183
Utils.h
llvm::array_lengthof
constexpr size_t array_lengthof(T(&)[N])
Find the length of an array.
Definition: STLExtras.h:1394
Type.h
llvm::SmallString< 128 >
INITIALIZE_PASS_END
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:58
llvm::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:168
BasicBlock.h
llvm::TargetLibraryInfoWrapperPass
Definition: TargetLibraryInfo.h:463
INITIALIZE_PASS_DEPENDENCY
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::createMetaRenamerPass
ModulePass * createMetaRenamerPass()
Definition: MetaRenamer.cpp:179
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::StructType
Class to represent struct types.
Definition: DerivedTypes.h:213
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::TypeFinder::run
void run(const Module &M, bool onlyNamed)
Definition: TypeFinder.cpp:32
llvm::TypeFinder
TypeFinder - Walk over a module, identifying all of the types that are used by the module.
Definition: TypeFinder.h:30
metaNames
static const char *const metaNames[]
Definition: MetaRenamer.cpp:38
Argument.h
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
llvm::AnalysisUsage::setPreservesAll
void setPreservesAll()
Set by analyses that do not transform their input at all.
Definition: PassAnalysisSupport.h:130
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:161
GlobalVariable.h
Function.h
PassManager.h
llvm::TargetLibraryInfo
Provides information about what library functions are available for the current target.
Definition: TargetLibraryInfo.h:219
GlobalAlias.h
DerivedTypes.h
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44
llvm::InnerAnalysisManagerProxy
An analysis over an "outer" IR unit that provides access to an analysis manager over an "inner" IR un...
Definition: PassManager.h:936
BB
Common register allocation spilling lr str ldr sxth r3 ldr mla r4 can lr mov lr str ldr sxth r3 mla r4 and then merge mul and lr str ldr sxth r3 mla r4 It also increase the likelihood the store may become dead bb27 Successors according to LLVM BB
Definition: README.txt:39
llvm::AnalysisUsage::addRequired
AnalysisUsage & addRequired()
Definition: PassAnalysisSupport.h:75
InitializePasses.h
llvm::TargetLibraryAnalysis
Analysis pass providing the TargetLibraryInfo.
Definition: TargetLibraryInfo.h:438
metarenamer
metarenamer
Definition: MetaRenamer.cpp:172
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37