2012-09-11 10:46:18 +08:00
|
|
|
//===- MetaRenamer.cpp - Rename everything with metasyntatic names --------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-09-11 10:46:18 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass renames everything with metasyntatic names. The intent is to use
|
|
|
|
// this pass after bugpoint reduction to conceal the nature of the original
|
|
|
|
// program.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-09-15 03:19:57 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
2017-10-27 09:09:08 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
2017-03-24 07:21:07 +08:00
|
|
|
#include "llvm/Analysis/TargetLibraryInfo.h"
|
2017-10-27 09:09:08 +08:00
|
|
|
#include "llvm/IR/Argument.h"
|
|
|
|
#include "llvm/IR/BasicBlock.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
2017-10-27 09:09:08 +08:00
|
|
|
#include "llvm/IR/GlobalAlias.h"
|
|
|
|
#include "llvm/IR/GlobalVariable.h"
|
|
|
|
#include "llvm/IR/Instruction.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/IR/Type.h"
|
2013-01-07 23:43:51 +08:00
|
|
|
#include "llvm/IR/TypeFinder.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-14 05:15:01 +08:00
|
|
|
#include "llvm/InitializePasses.h"
|
2012-09-11 10:46:18 +08:00
|
|
|
#include "llvm/Pass.h"
|
2018-03-29 01:44:36 +08:00
|
|
|
#include "llvm/Transforms/Utils.h"
|
2017-10-27 09:09:08 +08:00
|
|
|
|
2012-09-11 10:46:18 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2017-10-27 09:09:08 +08:00
|
|
|
static const char *const metaNames[] = {
|
|
|
|
// See http://en.wikipedia.org/wiki/Metasyntactic_variable
|
|
|
|
"foo", "bar", "baz", "quux", "barney", "snork", "zot", "blam", "hoge",
|
|
|
|
"wibble", "wobble", "widget", "wombat", "ham", "eggs", "pluto", "spam"
|
|
|
|
};
|
|
|
|
|
2012-09-11 10:46:18 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// This PRNG is from the ISO C spec. It is intentionally simple and
|
|
|
|
// unsuitable for cryptographic use. We're just looking for enough
|
|
|
|
// variety to surprise and delight users.
|
|
|
|
struct PRNG {
|
|
|
|
unsigned long next;
|
|
|
|
|
|
|
|
void srand(unsigned int seed) {
|
|
|
|
next = seed;
|
|
|
|
}
|
|
|
|
|
2012-11-16 00:51:49 +08:00
|
|
|
int rand() {
|
2012-09-11 10:46:18 +08:00
|
|
|
next = next * 1103515245 + 12345;
|
|
|
|
return (unsigned int)(next / 65536) % 32768;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-08-26 14:11:38 +08:00
|
|
|
struct Renamer {
|
|
|
|
Renamer(unsigned int seed) {
|
|
|
|
prng.srand(seed);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *newName() {
|
|
|
|
return metaNames[prng.rand() % array_lengthof(metaNames)];
|
|
|
|
}
|
|
|
|
|
|
|
|
PRNG prng;
|
|
|
|
};
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2012-09-11 10:46:18 +08:00
|
|
|
struct MetaRenamer : public ModulePass {
|
2017-10-27 09:09:08 +08:00
|
|
|
// Pass identification, replacement for typeid
|
|
|
|
static char ID;
|
|
|
|
|
2012-09-11 10:46:18 +08:00
|
|
|
MetaRenamer() : ModulePass(ID) {
|
|
|
|
initializeMetaRenamerPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
2014-03-05 17:10:37 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2017-03-24 07:21:07 +08:00
|
|
|
AU.addRequired<TargetLibraryInfoWrapperPass>();
|
2012-09-11 10:46:18 +08:00
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
|
2014-03-05 17:10:37 +08:00
|
|
|
bool runOnModule(Module &M) override {
|
2012-09-11 10:46:18 +08:00
|
|
|
// Seed our PRNG with simple additive sum of ModuleID. We're looking to
|
|
|
|
// simply avoid always having the same function names, and we need to
|
|
|
|
// remain deterministic.
|
|
|
|
unsigned int randSeed = 0;
|
2015-08-26 14:11:41 +08:00
|
|
|
for (auto C : M.getModuleIdentifier())
|
|
|
|
randSeed += C;
|
2012-09-11 10:46:18 +08:00
|
|
|
|
2015-08-26 14:11:38 +08:00
|
|
|
Renamer renamer(randSeed);
|
2015-08-27 13:37:12 +08:00
|
|
|
|
2012-09-11 10:46:18 +08:00
|
|
|
// Rename all aliases
|
2015-08-26 14:11:41 +08:00
|
|
|
for (auto AI = M.alias_begin(), AE = M.alias_end(); AI != AE; ++AI) {
|
2013-01-23 23:03:08 +08:00
|
|
|
StringRef Name = AI->getName();
|
|
|
|
if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
|
|
|
|
continue;
|
2012-09-11 10:46:18 +08:00
|
|
|
|
2013-01-23 23:03:08 +08:00
|
|
|
AI->setName("alias");
|
|
|
|
}
|
2015-08-26 14:11:36 +08:00
|
|
|
|
2012-09-11 10:46:18 +08:00
|
|
|
// Rename all global variables
|
2015-08-26 14:11:41 +08:00
|
|
|
for (auto GI = M.global_begin(), GE = M.global_end(); GI != GE; ++GI) {
|
2013-01-23 23:03:08 +08:00
|
|
|
StringRef Name = GI->getName();
|
|
|
|
if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
|
|
|
|
continue;
|
|
|
|
|
2012-09-11 10:46:18 +08:00
|
|
|
GI->setName("global");
|
2013-01-23 23:03:08 +08:00
|
|
|
}
|
2012-09-11 10:46:18 +08:00
|
|
|
|
|
|
|
// Rename all struct types
|
|
|
|
TypeFinder StructTypes;
|
|
|
|
StructTypes.run(M, true);
|
2015-08-26 14:11:41 +08:00
|
|
|
for (StructType *STy : StructTypes) {
|
2012-09-11 10:46:18 +08:00
|
|
|
if (STy->isLiteral() || STy->getName().empty()) continue;
|
|
|
|
|
|
|
|
SmallString<128> NameStorage;
|
2015-08-26 14:11:38 +08:00
|
|
|
STy->setName((Twine("struct.") +
|
|
|
|
renamer.newName()).toStringRef(NameStorage));
|
2012-09-11 10:46:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rename all functions
|
2015-08-26 14:11:41 +08:00
|
|
|
for (auto &F : M) {
|
|
|
|
StringRef Name = F.getName();
|
2017-03-24 07:21:07 +08:00
|
|
|
LibFunc Tmp;
|
|
|
|
// Leave library functions alone because their presence or absence could
|
|
|
|
// affect the behavior of other passes.
|
|
|
|
if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1) ||
|
Change TargetLibraryInfo analysis passes to always require Function
Summary:
This is the first change to enable the TLI to be built per-function so
that -fno-builtin* handling can be migrated to use function attributes.
See discussion on D61634 for background. This is an enabler for fixing
handling of these options for LTO, for example.
This change should not affect behavior, as the provided function is not
yet used to build a specifically per-function TLI, but rather enables
that migration.
Most of the changes were very mechanical, e.g. passing a Function to the
legacy analysis pass's getTLI interface, or in Module level cases,
adding a callback. This is similar to the way the per-function TTI
analysis works.
There was one place where we were looking for builtins but not in the
context of a specific function. See FindCXAAtExit in
lib/Transforms/IPO/GlobalOpt.cpp. I'm somewhat concerned my workaround
could provide the wrong behavior in some corner cases. Suggestions
welcome.
Reviewers: chandlerc, hfinkel
Subscribers: arsenm, dschuff, jvesely, nhaehnle, mehdi_amini, javed.absar, sbc100, jgravelle-google, eraman, aheejin, steven_wu, george.burgess.iv, dexonsmith, jfb, asbirlea, gchatelet, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D66428
llvm-svn: 371284
2019-09-07 11:09:36 +08:00
|
|
|
getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F).getLibFunc(
|
|
|
|
F, Tmp))
|
2013-01-23 23:03:08 +08:00
|
|
|
continue;
|
|
|
|
|
2017-08-01 13:14:45 +08:00
|
|
|
// Leave @main alone. The output of -metarenamer might be passed to
|
|
|
|
// lli for execution and the latter needs a main entry point.
|
|
|
|
if (Name != "main")
|
|
|
|
F.setName(renamer.newName());
|
|
|
|
|
2015-08-26 14:11:41 +08:00
|
|
|
runOnFunction(F);
|
2012-09-11 10:46:18 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnFunction(Function &F) {
|
2015-08-26 14:11:41 +08:00
|
|
|
for (auto AI = F.arg_begin(), AE = F.arg_end(); AI != AE; ++AI)
|
2012-09-11 10:46:18 +08:00
|
|
|
if (!AI->getType()->isVoidTy())
|
|
|
|
AI->setName("arg");
|
|
|
|
|
2015-08-26 14:11:41 +08:00
|
|
|
for (auto &BB : F) {
|
|
|
|
BB.setName("bb");
|
2012-09-11 10:46:18 +08:00
|
|
|
|
2015-08-26 14:11:41 +08:00
|
|
|
for (auto &I : BB)
|
|
|
|
if (!I.getType()->isVoidTy())
|
|
|
|
I.setName("tmp");
|
2012-09-11 10:46:18 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
2017-10-27 09:09:08 +08:00
|
|
|
|
|
|
|
} // end anonymous namespace
|
2012-09-11 10:46:18 +08:00
|
|
|
|
|
|
|
char MetaRenamer::ID = 0;
|
2017-10-27 09:09:08 +08:00
|
|
|
|
2017-03-24 07:21:07 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(MetaRenamer, "metarenamer",
|
|
|
|
"Assign new names to everything", false, false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
|
|
|
|
INITIALIZE_PASS_END(MetaRenamer, "metarenamer",
|
|
|
|
"Assign new names to everything", false, false)
|
2017-10-27 09:09:08 +08:00
|
|
|
|
2012-09-11 10:46:18 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// MetaRenamer - Rename everything with metasyntactic names.
|
|
|
|
//
|
|
|
|
ModulePass *llvm::createMetaRenamerPass() {
|
|
|
|
return new MetaRenamer();
|
|
|
|
}
|