diff --git a/llvm/lib/Transforms/Utils/MetaRenamer.cpp b/llvm/lib/Transforms/Utils/MetaRenamer.cpp --- a/llvm/lib/Transforms/Utils/MetaRenamer.cpp +++ b/llvm/lib/Transforms/Utils/MetaRenamer.cpp @@ -15,6 +15,7 @@ #include "llvm/Transforms/Utils/MetaRenamer.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallString.h" +#include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/Twine.h" #include "llvm/Analysis/TargetLibraryInfo.h" @@ -31,10 +32,36 @@ #include "llvm/IR/TypeFinder.h" #include "llvm/InitializePasses.h" #include "llvm/Pass.h" +#include "llvm/Support/CommandLine.h" #include "llvm/Transforms/Utils.h" using namespace llvm; +static cl::opt RenameExcludeFunctionsPrefixes( + "rename-exclude-functions-prefixes", + cl::desc("Prefixes for functions that don't need to be renamed, separated " + "by a comma"), + cl::Hidden); + +static cl::opt RenameExcludeAliasesPrefixes( + "rename-exclude-aliases-prefixes", + cl::desc("Prefixes for aliases that don't need to be renamed, separated " + "by a comma"), + cl::Hidden); + +static cl::opt RenameExcludeGlobalsPrefixes( + "rename-exclude-globals-prefixes", + cl::desc( + "Prefixes for global values that don't need to be renamed, separated " + "by a comma"), + cl::Hidden); + +static cl::opt RenameExcludeStructsPrefixes( + "rename-exclude-structs-prefixes", + cl::desc("Prefixes for structs that don't need to be renamed, separated " + "by a comma"), + cl::Hidden); + static const char *const metaNames[] = { // See http://en.wikipedia.org/wiki/Metasyntactic_variable "foo", "bar", "baz", "quux", "barney", "snork", "zot", "blam", "hoge", @@ -66,6 +93,18 @@ PRNG prng; }; +static void +parseExcludedPrefixes(StringRef PrefixesStr, + SmallVectorImpl &ExcludedPrefixes) { + for (;;) { + auto PrefixesSplit = PrefixesStr.split(','); + if (PrefixesSplit.first.empty()) + break; + ExcludedPrefixes.push_back(PrefixesSplit.first); + PrefixesStr = PrefixesSplit.second; + } +} + void MetaRename(Function &F) { for (Argument &Arg : F.args()) if (!Arg.getType()->isVoidTy()) @@ -91,10 +130,26 @@ Renamer renamer(randSeed); + SmallVector ExcludedAliasesPrefixes; + SmallVector ExcludedGlobalsPrefixes; + SmallVector ExcludedStructsPrefixes; + SmallVector ExcludedFuncPrefixes; + parseExcludedPrefixes(RenameExcludeAliasesPrefixes, ExcludedAliasesPrefixes); + parseExcludedPrefixes(RenameExcludeGlobalsPrefixes, ExcludedGlobalsPrefixes); + parseExcludedPrefixes(RenameExcludeStructsPrefixes, ExcludedStructsPrefixes); + parseExcludedPrefixes(RenameExcludeFunctionsPrefixes, ExcludedFuncPrefixes); + + auto IsNameExcluded = [](StringRef &Name, + SmallVectorImpl &ExcludedPrefixes) { + return any_of(ExcludedPrefixes, + [&Name](auto &Prefix) { return Name.startswith(Prefix); }); + }; + // Rename all aliases for (GlobalAlias &GA : M.aliases()) { StringRef Name = GA.getName(); - if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1)) + if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1) || + IsNameExcluded(Name, ExcludedAliasesPrefixes)) continue; GA.setName("alias"); @@ -103,7 +158,8 @@ // Rename all global variables for (GlobalVariable &GV : M.globals()) { StringRef Name = GV.getName(); - if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1)) + if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1) || + IsNameExcluded(Name, ExcludedGlobalsPrefixes)) continue; GV.setName("global"); @@ -113,7 +169,9 @@ TypeFinder StructTypes; StructTypes.run(M, true); for (StructType *STy : StructTypes) { - if (STy->isLiteral() || STy->getName().empty()) + StringRef Name = STy->getName(); + if (STy->isLiteral() || Name.empty() || + IsNameExcluded(Name, ExcludedStructsPrefixes)) continue; SmallString<128> NameStorage; @@ -128,7 +186,8 @@ // 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) || - GetTLI(F).getLibFunc(F, Tmp)) + GetTLI(F).getLibFunc(F, Tmp) || + IsNameExcluded(Name, ExcludedFuncPrefixes)) continue; // Leave @main alone. The output of -metarenamer might be passed to diff --git a/llvm/test/Transforms/MetaRenamer/exclude-names.ll b/llvm/test/Transforms/MetaRenamer/exclude-names.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/MetaRenamer/exclude-names.ll @@ -0,0 +1,39 @@ +; RUN: opt -passes=metarenamer -rename-exclude-functions-prefixes=my_func -rename-exclude-globals-prefixes=my_global -rename-exclude-structs-prefixes=my_struct -rename-exclude-aliases-prefixes=my_alias -S %s | FileCheck %s + +; CHECK: %my_struct1 = type { i8*, i32 } +; CHECK: %my_struct2 = type { i8*, i32 } +; CHECK: @my_global1 = global i32 42 +; CHECK: @my_global2 = global i32 24 +; CHECK: @my_alias1 = alias i32, i32* @my_global1 +; CHECK: @my_alias2 = alias i32, i32* @my_global2 +; CHECK: declare void @my_func1 +; CHECK: declare void @my_func2 +; CHECK: call void @my_func1 +; CHECK: call void @my_func2 +; CHECK: load i32, i32* @my_global1 +; CHECK: load i32, i32* @my_global2 +; CHECK: load i32, i32* @my_alias1 +; CHECK: load i32, i32* @my_alias2 +; CHECK: alloca %my_struct1 +; CHECK: alloca %my_struct2 + +%my_struct1 = type { i8*, i32 } +%my_struct2 = type { i8*, i32 } +@my_global1 = global i32 42 +@my_global2 = global i32 24 +@my_alias1 = alias i32, i32* @my_global1 +@my_alias2 = alias i32, i32* @my_global2 +declare void @my_func1() +declare void @my_func2() + +define void @some_func() { + call void @my_func1() + call void @my_func2() + %a = load i32, i32* @my_global1 + %b = load i32, i32* @my_global2 + %c = load i32, i32* @my_alias1 + %d = load i32, i32* @my_alias2 + %e = alloca %my_struct1 + %f = alloca %my_struct2 + ret void +}