[llvm] 77bc3ba - [NFC][llvm-reduce] Cleanup types
Arthur Eubanks via llvm-commits
llvm-commits at lists.llvm.org
Sun Oct 10 18:12:53 PDT 2021
Author: Arthur Eubanks
Date: 2021-10-10T18:07:28-07:00
New Revision: 77bc3ba3650eccde2f1cf7326e76eb6c14ed08a3
URL: https://github.com/llvm/llvm-project/commit/77bc3ba3650eccde2f1cf7326e76eb6c14ed08a3
DIFF: https://github.com/llvm/llvm-project/commit/77bc3ba3650eccde2f1cf7326e76eb6c14ed08a3.diff
LOG: [NFC][llvm-reduce] Cleanup types
Use Module& wherever possible.
Since every reduction immediately turns Chunks into an Oracle, directly pass Oracle instead.
Reviewed By: hans
Differential Revision: https://reviews.llvm.org/D111122
Added:
Modified:
llvm/tools/llvm-reduce/TestRunner.cpp
llvm/tools/llvm-reduce/TestRunner.h
llvm/tools/llvm-reduce/deltas/Delta.cpp
llvm/tools/llvm-reduce/deltas/Delta.h
llvm/tools/llvm-reduce/deltas/ReduceAliases.cpp
llvm/tools/llvm-reduce/deltas/ReduceArguments.cpp
llvm/tools/llvm-reduce/deltas/ReduceAttributes.cpp
llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp
llvm/tools/llvm-reduce/deltas/ReduceFunctionBodies.cpp
llvm/tools/llvm-reduce/deltas/ReduceFunctions.cpp
llvm/tools/llvm-reduce/deltas/ReduceGlobalValues.cpp
llvm/tools/llvm-reduce/deltas/ReduceGlobalVarInitializers.cpp
llvm/tools/llvm-reduce/deltas/ReduceGlobalVars.cpp
llvm/tools/llvm-reduce/deltas/ReduceInstructions.cpp
llvm/tools/llvm-reduce/deltas/ReduceMetadata.cpp
llvm/tools/llvm-reduce/deltas/ReduceModuleData.cpp
llvm/tools/llvm-reduce/deltas/ReduceOperandBundles.cpp
llvm/tools/llvm-reduce/deltas/ReduceOperands.cpp
llvm/tools/llvm-reduce/deltas/ReduceSpecialGlobals.cpp
llvm/tools/llvm-reduce/llvm-reduce.cpp
Removed:
################################################################################
diff --git a/llvm/tools/llvm-reduce/TestRunner.cpp b/llvm/tools/llvm-reduce/TestRunner.cpp
index d0e195d5697c9..a3cd717ccd986 100644
--- a/llvm/tools/llvm-reduce/TestRunner.cpp
+++ b/llvm/tools/llvm-reduce/TestRunner.cpp
@@ -10,8 +10,11 @@
using namespace llvm;
-TestRunner::TestRunner(StringRef TestName, const std::vector<std::string> &TestArgs)
- : TestName(TestName), TestArgs(TestArgs) {
+TestRunner::TestRunner(StringRef TestName,
+ const std::vector<std::string> &TestArgs,
+ std::unique_ptr<Module> Program)
+ : TestName(TestName), TestArgs(TestArgs), Program(std::move(Program)) {
+ assert(this->Program && "Initialized with null program?");
}
/// Runs the interestingness test, passes file to be tested as first argument
diff --git a/llvm/tools/llvm-reduce/TestRunner.h b/llvm/tools/llvm-reduce/TestRunner.h
index b7fb44e820628..6edc1a1192fa7 100644
--- a/llvm/tools/llvm-reduce/TestRunner.h
+++ b/llvm/tools/llvm-reduce/TestRunner.h
@@ -24,16 +24,20 @@ namespace llvm {
// respective filename.
class TestRunner {
public:
- TestRunner(StringRef TestName, const std::vector<std::string> &TestArgs);
+ TestRunner(StringRef TestName, const std::vector<std::string> &TestArgs,
+ std::unique_ptr<Module> Program);
/// Runs the interesting-ness test for the specified file
/// @returns 0 if test was successful, 1 if otherwise
int run(StringRef Filename);
/// Returns the most reduced version of the original testcase
- Module *getProgram() const { return Program.get(); }
+ Module &getProgram() const { return *Program; }
- void setProgram(std::unique_ptr<Module> P) { Program = std::move(P); }
+ void setProgram(std::unique_ptr<Module> P) {
+ assert(P && "Setting null program?");
+ Program = std::move(P);
+ }
private:
StringRef TestName;
diff --git a/llvm/tools/llvm-reduce/deltas/Delta.cpp b/llvm/tools/llvm-reduce/deltas/Delta.cpp
index 10597641b4804..f14b326c7e0bd 100644
--- a/llvm/tools/llvm-reduce/deltas/Delta.cpp
+++ b/llvm/tools/llvm-reduce/deltas/Delta.cpp
@@ -27,7 +27,7 @@ static cl::opt<bool> AbortOnInvalidReduction(
"abort-on-invalid-reduction",
cl::desc("Abort if any reduction results in invalid IR"));
-void writeOutput(llvm::Module *M, llvm::StringRef Message);
+void writeOutput(llvm::Module &M, llvm::StringRef Message);
bool isReduced(Module &M, TestRunner &Test, SmallString<128> &CurrentFilepath) {
// Write Module to tmp file
@@ -97,25 +97,22 @@ static bool increaseGranularity(std::vector<Chunk> &Chunks) {
/// given test.
void llvm::runDeltaPass(
TestRunner &Test, int Targets,
- std::function<void(const std::vector<Chunk> &, Module *)>
- ExtractChunksFromModule) {
+ function_ref<void(Oracle &, Module &)> ExtractChunksFromModule) {
assert(Targets >= 0);
if (!Targets) {
errs() << "\nNothing to reduce\n";
return;
}
- if (Module *Program = Test.getProgram()) {
- SmallString<128> CurrentFilepath;
- if (!isReduced(*Program, Test, CurrentFilepath)) {
- errs() << "\nInput isn't interesting! Verify interesting-ness test\n";
- exit(1);
- }
-
- assert(!verifyModule(*Program, &errs()) &&
- "input module is broken before making changes");
+ SmallString<128> CurrentFilepath;
+ if (!isReduced(Test.getProgram(), Test, CurrentFilepath)) {
+ errs() << "\nInput isn't interesting! Verify interesting-ness test\n";
+ exit(1);
}
+ assert(!verifyModule(Test.getProgram(), &errs()) &&
+ "input module is broken before making changes");
+
std::vector<Chunk> ChunksStillConsideredInteresting = {{1, Targets}};
std::unique_ptr<Module> ReducedProgram;
@@ -140,12 +137,13 @@ void llvm::runDeltaPass(
});
// Clone module before hacking it up..
- std::unique_ptr<Module> Clone = CloneModule(*Test.getProgram());
+ std::unique_ptr<Module> Clone = CloneModule(Test.getProgram());
// Generate Module with only Targets inside Current Chunks
- ExtractChunksFromModule(CurrentChunks, Clone.get());
+ Oracle O(CurrentChunks);
+ ExtractChunksFromModule(O, *Clone);
// Some reductions may result in invalid IR. Skip such reductions.
- if (verifyModule(*Clone.get(), &errs())) {
+ if (verifyModule(*Clone, &errs())) {
if (AbortOnInvalidReduction) {
errs() << "Invalid reduction\n";
exit(1);
@@ -171,7 +169,7 @@ void llvm::runDeltaPass(
UninterestingChunks.insert(ChunkToCheckForUninterestingness);
ReducedProgram = std::move(Clone);
errs() << " **** SUCCESS | lines: " << getLines(CurrentFilepath) << "\n";
- writeOutput(ReducedProgram.get(), "Saved new best reduction to ");
+ writeOutput(*ReducedProgram, "Saved new best reduction to ");
}
// Delete uninteresting chunks
erase_if(ChunksStillConsideredInteresting,
diff --git a/llvm/tools/llvm-reduce/deltas/Delta.h b/llvm/tools/llvm-reduce/deltas/Delta.h
index 69bd4d2cad503..8ebfc9aca4d95 100644
--- a/llvm/tools/llvm-reduce/deltas/Delta.h
+++ b/llvm/tools/llvm-reduce/deltas/Delta.h
@@ -101,9 +101,9 @@ class Oracle {
///
/// Other implementations of the Delta Debugging algorithm can also be found in
/// the CReduce, Delta, and Lithium projects.
-void runDeltaPass(TestRunner &Test, int Targets,
- std::function<void(const std::vector<Chunk> &, Module *)>
- ExtractChunksFromModule);
+void runDeltaPass(
+ TestRunner &Test, int Targets,
+ function_ref<void(Oracle &, Module &)> ExtractChunksFromModule);
} // namespace llvm
#endif
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceAliases.cpp b/llvm/tools/llvm-reduce/deltas/ReduceAliases.cpp
index 41be4baa980a1..dedaef860fcf3 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceAliases.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceAliases.cpp
@@ -20,11 +20,8 @@ using namespace llvm;
/// Removes all aliases aren't inside any of the
/// desired Chunks.
-static void extractAliasesFromModule(const std::vector<Chunk> &ChunksToKeep,
- Module *Program) {
- Oracle O(ChunksToKeep);
-
- for (auto &GA : make_early_inc_range(Program->aliases())) {
+static void extractAliasesFromModule(Oracle &O, Module &Program) {
+ for (auto &GA : make_early_inc_range(Program.aliases())) {
if (!O.shouldKeep()) {
GA.replaceAllUsesWith(GA.getAliasee());
GA.eraseFromParent();
@@ -33,12 +30,12 @@ static void extractAliasesFromModule(const std::vector<Chunk> &ChunksToKeep,
}
/// Counts the amount of aliases and prints their respective name & index.
-static int countAliases(Module *Program) {
+static int countAliases(Module &Program) {
// TODO: Silence index with --quiet flag
errs() << "----------------------------\n";
errs() << "Aliases Index Reference:\n";
int Count = 0;
- for (auto &GA : Program->aliases())
+ for (auto &GA : Program.aliases())
errs() << "\t" << ++Count << ": " << GA.getName() << "\n";
errs() << "----------------------------\n";
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceArguments.cpp b/llvm/tools/llvm-reduce/deltas/ReduceArguments.cpp
index f36ef4f7bfb29..d3fcf53f46f99 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceArguments.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceArguments.cpp
@@ -53,14 +53,11 @@ static bool shouldRemoveArguments(const Function &F) {
/// Removes out-of-chunk arguments from functions, and modifies their calls
/// accordingly. It also removes allocations of out-of-chunk arguments.
-static void extractArgumentsFromModule(std::vector<Chunk> ChunksToKeep,
- Module *Program) {
- Oracle O(ChunksToKeep);
-
+static void extractArgumentsFromModule(Oracle &O, Module &Program) {
std::set<Argument *> ArgsToKeep;
std::vector<Function *> Funcs;
// Get inside-chunk arguments, as well as their parent function
- for (auto &F : *Program)
+ for (auto &F : Program)
if (shouldRemoveArguments(F)) {
Funcs.push_back(&F);
for (auto &A : F.args())
@@ -102,7 +99,7 @@ static void extractArgumentsFromModule(std::vector<Chunk> ChunksToKeep,
auto *ClonedFunc = CloneFunction(F, VMap);
// In order to preserve function order, we move Clone after old Function
ClonedFunc->removeFromParent();
- Program->getFunctionList().insertAfter(F->getIterator(), ClonedFunc);
+ Program.getFunctionList().insertAfter(F->getIterator(), ClonedFunc);
replaceFunctionCalls(*F, *ClonedFunc, ArgIndexesToKeep);
// Rename Cloned Function to Old's name
@@ -115,12 +112,12 @@ static void extractArgumentsFromModule(std::vector<Chunk> ChunksToKeep,
/// Counts the amount of arguments in functions and prints their respective
/// name, index, and parent function name
-static int countArguments(Module *Program) {
+static int countArguments(Module &Program) {
// TODO: Silence index with --quiet flag
outs() << "----------------------------\n";
outs() << "Param Index Reference:\n";
int ArgsCount = 0;
- for (auto &F : *Program)
+ for (auto &F : Program)
if (shouldRemoveArguments(F)) {
outs() << " " << F.getName() << "\n";
for (auto &A : F.args())
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceAttributes.cpp b/llvm/tools/llvm-reduce/deltas/ReduceAttributes.cpp
index 74aa39f6c7e19..b2791b22e9043 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceAttributes.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceAttributes.cpp
@@ -49,14 +49,14 @@ using AttrPtrVecVecTy = SmallVector<AttrPtrIdxVecVecTy, 3>;
/// Given ChunksToKeep, produce a map of global variables/functions/calls
/// and indexes of attributes to be preserved for each of them.
class AttributeRemapper : public InstVisitor<AttributeRemapper> {
- Oracle O;
+ Oracle &O;
public:
DenseMap<GlobalVariable *, AttrPtrVecTy> GlobalVariablesToRefine;
DenseMap<Function *, AttrPtrVecVecTy> FunctionsToRefine;
DenseMap<CallBase *, AttrPtrVecVecTy> CallsToRefine;
- explicit AttributeRemapper(ArrayRef<Chunk> ChunksToKeep) : O(ChunksToKeep) {}
+ explicit AttributeRemapper(Oracle &O) : O(O) {}
void visitModule(Module &M) {
for (GlobalVariable &GV : M.getGlobalList())
@@ -167,12 +167,11 @@ AttributeList convertAttributeRefVecToAttributeList(
}
/// Removes out-of-chunk attributes from module.
-static void extractAttributesFromModule(std::vector<Chunk> ChunksToKeep,
- Module *Program) {
- AttributeRemapper R(ChunksToKeep);
+static void extractAttributesFromModule(Oracle &O, Module &Program) {
+ AttributeRemapper R(O);
R.visit(Program);
- LLVMContext &C = Program->getContext();
+ LLVMContext &C = Program.getContext();
for (const auto &I : R.GlobalVariablesToRefine)
I.first->setAttributes(convertAttributeRefToAttributeSet(C, I.second));
for (const auto &I : R.FunctionsToRefine)
@@ -182,7 +181,7 @@ static void extractAttributesFromModule(std::vector<Chunk> ChunksToKeep,
}
/// Counts the amount of attributes.
-static int countAttributes(Module *Program) {
+static int countAttributes(Module &Program) {
AttributeCounter C;
// TODO: Silence index with --quiet flag
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp b/llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp
index 5a1905d842af0..825993de53c76 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp
@@ -87,19 +87,16 @@ static void removeUninterestingBBsFromSwitch(SwitchInst &SwInst,
/// Removes out-of-chunk arguments from functions, and modifies their calls
/// accordingly. It also removes allocations of out-of-chunk arguments.
-static void extractBasicBlocksFromModule(std::vector<Chunk> ChunksToKeep,
- Module *Program) {
- Oracle O(ChunksToKeep);
-
+static void extractBasicBlocksFromModule(Oracle &O, Module &Program) {
std::set<BasicBlock *> BBsToKeep;
- for (auto &F : *Program)
+ for (auto &F : Program)
for (auto &BB : F)
if (O.shouldKeep())
BBsToKeep.insert(&BB);
std::vector<BasicBlock *> BBsToDelete;
- for (auto &F : *Program)
+ for (auto &F : Program)
for (auto &BB : F) {
if (!BBsToKeep.count(&BB)) {
BBsToDelete.push_back(&BB);
@@ -110,7 +107,7 @@ static void extractBasicBlocksFromModule(std::vector<Chunk> ChunksToKeep,
}
// Replace terminators that reference out-of-chunk BBs
- for (auto &F : *Program)
+ for (auto &F : Program)
for (auto &BB : F) {
if (auto *SwInst = dyn_cast<SwitchInst>(BB.getTerminator()))
removeUninterestingBBsFromSwitch(*SwInst, BBsToKeep);
@@ -128,11 +125,11 @@ static void extractBasicBlocksFromModule(std::vector<Chunk> ChunksToKeep,
}
/// Counts the amount of basic blocks and prints their name & respective index
-static int countBasicBlocks(Module *Program) {
+static int countBasicBlocks(Module &Program) {
// TODO: Silence index with --quiet flag
outs() << "----------------------------\n";
int BBCount = 0;
- for (auto &F : *Program)
+ for (auto &F : Program)
for (auto &BB : F) {
if (BB.hasName())
outs() << "\t" << ++BBCount << ": " << BB.getName() << "\n";
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceFunctionBodies.cpp b/llvm/tools/llvm-reduce/deltas/ReduceFunctionBodies.cpp
index 99be76eac3e4d..4a91e43987ca2 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceFunctionBodies.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceFunctionBodies.cpp
@@ -19,14 +19,10 @@ using namespace llvm;
/// Removes all the bodies of defined functions that aren't inside any of the
/// desired Chunks.
-static void
-extractFunctionBodiesFromModule(const std::vector<Chunk> &ChunksToKeep,
- Module *Program) {
- Oracle O(ChunksToKeep);
-
+static void extractFunctionBodiesFromModule(Oracle &O, Module &Program) {
// Delete out-of-chunk function bodies
std::vector<Function *> FuncDefsToReduce;
- for (auto &F : *Program)
+ for (auto &F : Program)
if (!F.isDeclaration() && !O.shouldKeep()) {
F.deleteBody();
F.setComdat(nullptr);
@@ -35,12 +31,12 @@ extractFunctionBodiesFromModule(const std::vector<Chunk> &ChunksToKeep,
/// Counts the amount of non-declaration functions and prints their
/// respective name & index
-static int countFunctionDefinitions(Module *Program) {
+static int countFunctionDefinitions(Module &Program) {
// TODO: Silence index with --quiet flag
errs() << "----------------------------\n";
errs() << "Function Definition Index Reference:\n";
int FunctionDefinitionCount = 0;
- for (auto &F : *Program)
+ for (auto &F : Program)
if (!F.isDeclaration())
errs() << "\t" << ++FunctionDefinitionCount << ": " << F.getName()
<< "\n";
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceFunctions.cpp b/llvm/tools/llvm-reduce/deltas/ReduceFunctions.cpp
index d100935ee422c..b033f1a05671a 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceFunctions.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceFunctions.cpp
@@ -23,13 +23,10 @@ using namespace llvm;
/// Removes all the Defined Functions
/// that aren't inside any of the desired Chunks.
-static void extractFunctionsFromModule(const std::vector<Chunk> &ChunksToKeep,
- Module *Program) {
- Oracle O(ChunksToKeep);
-
+static void extractFunctionsFromModule(Oracle &O, Module &Program) {
// Record all out-of-chunk functions.
std::vector<std::reference_wrapper<Function>> FuncsToRemove;
- copy_if(Program->functions(), std::back_inserter(FuncsToRemove),
+ copy_if(Program.functions(), std::back_inserter(FuncsToRemove),
[&O](Function &F) {
// Intrinsics don't have function bodies that are useful to
// reduce. Additionally, intrinsics may have additional operand
@@ -53,12 +50,12 @@ static void extractFunctionsFromModule(const std::vector<Chunk> &ChunksToKeep,
/// Counts the amount of functions and prints their
/// respective name & index
-static int countFunctions(Module *Program) {
+static int countFunctions(Module &Program) {
// TODO: Silence index with --quiet flag
errs() << "----------------------------\n";
errs() << "Function Index Reference:\n";
int FunctionCount = 0;
- for (auto &F : *Program) {
+ for (auto &F : Program) {
if (F.isIntrinsic() && !F.use_empty())
continue;
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceGlobalValues.cpp b/llvm/tools/llvm-reduce/deltas/ReduceGlobalValues.cpp
index 4d918aa586a49..53509400c8aa6 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceGlobalValues.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceGlobalValues.cpp
@@ -22,12 +22,9 @@ static bool isValidDSOLocalReductionGV(GlobalValue &GV) {
}
/// Sets dso_local to false for all global values.
-static void extractGVsFromModule(std::vector<Chunk> ChunksToKeep,
- Module *Program) {
- Oracle O(ChunksToKeep);
-
+static void extractGVsFromModule(Oracle &O, Module &Program) {
// remove dso_local from global values
- for (auto &GV : Program->global_values())
+ for (auto &GV : Program.global_values())
if (isValidDSOLocalReductionGV(GV) && !O.shouldKeep()) {
GV.setDSOLocal(false);
}
@@ -35,12 +32,12 @@ static void extractGVsFromModule(std::vector<Chunk> ChunksToKeep,
/// Counts the amount of global values with dso_local and displays their
/// respective name & index
-static int countGVs(Module *Program) {
+static int countGVs(Module &Program) {
// TODO: Silence index with --quiet flag
outs() << "----------------------------\n";
outs() << "GlobalValue Index Reference:\n";
int GVCount = 0;
- for (auto &GV : Program->global_values())
+ for (auto &GV : Program.global_values())
if (isValidDSOLocalReductionGV(GV))
outs() << "\t" << ++GVCount << ": " << GV.getName() << "\n";
outs() << "----------------------------\n";
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceGlobalVarInitializers.cpp b/llvm/tools/llvm-reduce/deltas/ReduceGlobalVarInitializers.cpp
index fd5a5d1f02c6a..14c6a414e4b15 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceGlobalVarInitializers.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceGlobalVarInitializers.cpp
@@ -18,12 +18,9 @@
using namespace llvm;
/// Removes all the Initialized GVs that aren't inside the desired Chunks.
-static void extractGVsFromModule(std::vector<Chunk> ChunksToKeep,
- Module *Program) {
- Oracle O(ChunksToKeep);
-
+static void extractGVsFromModule(Oracle &O, Module &Program) {
// Drop initializers of out-of-chunk GVs
- for (auto &GV : Program->globals())
+ for (auto &GV : Program.globals())
if (GV.hasInitializer() && !O.shouldKeep()) {
GV.setInitializer(nullptr);
GV.setLinkage(GlobalValue::LinkageTypes::ExternalLinkage);
@@ -33,12 +30,12 @@ static void extractGVsFromModule(std::vector<Chunk> ChunksToKeep,
/// Counts the amount of initialized GVs and displays their
/// respective name & index
-static int countGVs(Module *Program) {
+static int countGVs(Module &Program) {
// TODO: Silence index with --quiet flag
outs() << "----------------------------\n";
outs() << "GlobalVariable Index Reference:\n";
int GVCount = 0;
- for (auto &GV : Program->globals())
+ for (auto &GV : Program.globals())
if (GV.hasInitializer())
outs() << "\t" << ++GVCount << ": " << GV.getName() << "\n";
outs() << "----------------------------\n";
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceGlobalVars.cpp b/llvm/tools/llvm-reduce/deltas/ReduceGlobalVars.cpp
index 525eff99b863a..af685d003f942 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceGlobalVars.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceGlobalVars.cpp
@@ -18,20 +18,17 @@
using namespace llvm;
/// Removes all the GVs that aren't inside the desired Chunks.
-static void extractGVsFromModule(std::vector<Chunk> ChunksToKeep,
- Module *Program) {
- Oracle O(ChunksToKeep);
-
+static void extractGVsFromModule(Oracle &O, Module &Program) {
// Get GVs inside desired chunks
std::set<GlobalVariable *> GVsToKeep;
- for (auto &GV : Program->globals())
+ for (auto &GV : Program.globals())
if (O.shouldKeep())
GVsToKeep.insert(&GV);
// Delete out-of-chunk GVs and their uses
std::vector<GlobalVariable *> ToRemove;
std::vector<WeakVH> InstToRemove;
- for (auto &GV : Program->globals())
+ for (auto &GV : Program.globals())
if (!GVsToKeep.count(&GV)) {
for (auto *U : GV.users())
if (auto *Inst = dyn_cast<Instruction>(U))
@@ -56,12 +53,12 @@ static void extractGVsFromModule(std::vector<Chunk> ChunksToKeep,
/// Counts the amount of GVs and displays their
/// respective name & index
-static int countGVs(Module *Program) {
+static int countGVs(Module &Program) {
// TODO: Silence index with --quiet flag
outs() << "----------------------------\n";
outs() << "GlobalVariable Index Reference:\n";
int GVCount = 0;
- for (auto &GV : Program->globals())
+ for (auto &GV : Program.globals())
outs() << "\t" << ++GVCount << ": " << GV.getName() << "\n";
outs() << "----------------------------\n";
return GVCount;
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceInstructions.cpp b/llvm/tools/llvm-reduce/deltas/ReduceInstructions.cpp
index 3e37ec5e1751c..01df5b6ba4d6d 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceInstructions.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceInstructions.cpp
@@ -17,13 +17,10 @@ using namespace llvm;
/// Removes out-of-chunk arguments from functions, and modifies their calls
/// accordingly. It also removes allocations of out-of-chunk arguments.
-static void extractInstrFromModule(std::vector<Chunk> ChunksToKeep,
- Module *Program) {
- Oracle O(ChunksToKeep);
-
+static void extractInstrFromModule(Oracle &O, Module &Program) {
std::set<Instruction *> InstToKeep;
- for (auto &F : *Program)
+ for (auto &F : Program)
for (auto &BB : F) {
// Removing the terminator would make the block invalid. Only iterate over
// instructions before the terminator.
@@ -34,7 +31,7 @@ static void extractInstrFromModule(std::vector<Chunk> ChunksToKeep,
}
std::vector<Instruction *> InstToDelete;
- for (auto &F : *Program)
+ for (auto &F : Program)
for (auto &BB : F)
for (auto &Inst : BB)
if (!InstToKeep.count(&Inst)) {
@@ -47,11 +44,11 @@ static void extractInstrFromModule(std::vector<Chunk> ChunksToKeep,
}
/// Counts the amount of basic blocks and prints their name & respective index
-static unsigned countInstructions(Module *Program) {
+static unsigned countInstructions(Module &Program) {
// TODO: Silence index with --quiet flag
outs() << "----------------------------\n";
int InstCount = 0;
- for (auto &F : *Program)
+ for (auto &F : Program)
for (auto &BB : F)
// Well-formed blocks have terminators, which we cannot remove.
InstCount += BB.getInstList().size() - 1;
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceMetadata.cpp b/llvm/tools/llvm-reduce/deltas/ReduceMetadata.cpp
index 8eb4533848d92..066108ef12e83 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceMetadata.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceMetadata.cpp
@@ -22,13 +22,10 @@ using namespace llvm;
/// Removes all the Named and Unnamed Metadata Nodes, as well as any debug
/// functions that aren't inside the desired Chunks.
-static void extractMetadataFromModule(const std::vector<Chunk> &ChunksToKeep,
- Module *Program) {
- Oracle O(ChunksToKeep);
-
+static void extractMetadataFromModule(Oracle &O, Module &Program) {
// Get out-of-chunk Named metadata nodes
SmallVector<NamedMDNode *> NamedNodesToDelete;
- for (NamedMDNode &MD : Program->named_metadata())
+ for (NamedMDNode &MD : Program.named_metadata())
if (!O.shouldKeep())
NamedNodesToDelete.push_back(&MD);
@@ -40,14 +37,14 @@ static void extractMetadataFromModule(const std::vector<Chunk> &ChunksToKeep,
// Delete out-of-chunk metadata attached to globals.
SmallVector<std::pair<unsigned, MDNode *>> MDs;
- for (GlobalVariable &GV : Program->globals()) {
+ for (GlobalVariable &GV : Program.globals()) {
GV.getAllMetadata(MDs);
for (std::pair<unsigned, MDNode *> &MD : MDs)
if (!O.shouldKeep())
GV.setMetadata(MD.first, NULL);
}
- for (Function &F : *Program) {
+ for (Function &F : Program) {
// Delete out-of-chunk metadata attached to functions.
F.getAllMetadata(MDs);
for (std::pair<unsigned, MDNode *> &MD : MDs)
@@ -64,13 +61,13 @@ static void extractMetadataFromModule(const std::vector<Chunk> &ChunksToKeep,
}
}
-static int countMetadataTargets(Module *Program) {
- int NamedMetadataNodes = Program->named_metadata_size();
+static int countMetadataTargets(Module &Program) {
+ int NamedMetadataNodes = Program.named_metadata_size();
// Get metadata attached to globals.
int GlobalMetadataArgs = 0;
SmallVector<std::pair<unsigned, MDNode *>> MDs;
- for (GlobalVariable &GV : Program->globals()) {
+ for (GlobalVariable &GV : Program.globals()) {
GV.getAllMetadata(MDs);
GlobalMetadataArgs += MDs.size();
}
@@ -78,7 +75,7 @@ static int countMetadataTargets(Module *Program) {
// Get metadata attached to functions & instructions.
int FunctionMetadataArgs = 0;
int InstructionMetadataArgs = 0;
- for (Function &F : *Program) {
+ for (Function &F : Program) {
F.getAllMetadata(MDs);
FunctionMetadataArgs += MDs.size();
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceModuleData.cpp b/llvm/tools/llvm-reduce/deltas/ReduceModuleData.cpp
index c51386938d6c9..c134600e2f877 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceModuleData.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceModuleData.cpp
@@ -14,33 +14,31 @@
using namespace llvm;
-static void clearModuleData(std::vector<Chunk> ChunksToKeep, Module *Program) {
- Oracle O(ChunksToKeep);
-
- if (!Program->getModuleIdentifier().empty() && !O.shouldKeep())
- Program->setModuleIdentifier("");
- if (!Program->getSourceFileName().empty() && !O.shouldKeep())
- Program->setSourceFileName("");
- if (!Program->getDataLayoutStr().empty() && !O.shouldKeep())
- Program->setDataLayout("");
- if (!Program->getTargetTriple().empty() && !O.shouldKeep())
- Program->setTargetTriple("");
+static void clearModuleData(Oracle &O, Module &Program) {
+ if (!Program.getModuleIdentifier().empty() && !O.shouldKeep())
+ Program.setModuleIdentifier("");
+ if (!Program.getSourceFileName().empty() && !O.shouldKeep())
+ Program.setSourceFileName("");
+ if (!Program.getDataLayoutStr().empty() && !O.shouldKeep())
+ Program.setDataLayout("");
+ if (!Program.getTargetTriple().empty() && !O.shouldKeep())
+ Program.setTargetTriple("");
// TODO: clear line by line rather than all at once
- if (!Program->getModuleInlineAsm().empty() && !O.shouldKeep())
- Program->setModuleInlineAsm("");
+ if (!Program.getModuleInlineAsm().empty() && !O.shouldKeep())
+ Program.setModuleInlineAsm("");
}
-static int countModuleData(Module *M) {
+static int countModuleData(Module &M) {
int Count = 0;
- if (!M->getModuleIdentifier().empty())
+ if (!M.getModuleIdentifier().empty())
++Count;
- if (!M->getSourceFileName().empty())
+ if (!M.getSourceFileName().empty())
++Count;
- if (!M->getDataLayoutStr().empty())
+ if (!M.getDataLayoutStr().empty())
++Count;
- if (!M->getTargetTriple().empty())
+ if (!M.getTargetTriple().empty())
++Count;
- if (!M->getModuleInlineAsm().empty())
+ if (!M.getModuleInlineAsm().empty())
++Count;
return Count;
}
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceOperandBundles.cpp b/llvm/tools/llvm-reduce/deltas/ReduceOperandBundles.cpp
index 77cb73837c827..41e794f357408 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceOperandBundles.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceOperandBundles.cpp
@@ -37,13 +37,12 @@ namespace {
/// Given ChunksToKeep, produce a map of calls and indexes of operand bundles
/// to be preserved for each call.
class OperandBundleRemapper : public InstVisitor<OperandBundleRemapper> {
- Oracle O;
+ Oracle &O;
public:
DenseMap<CallBase *, std::vector<unsigned>> CallsToRefine;
- explicit OperandBundleRemapper(ArrayRef<Chunk> ChunksToKeep)
- : O(ChunksToKeep) {}
+ explicit OperandBundleRemapper(Oracle &O) : O(O) {}
/// So far only CallBase sub-classes can have operand bundles.
/// Let's see which of the operand bundles of this call are to be kept.
@@ -96,9 +95,8 @@ static void maybeRewriteCallWithDifferentBundles(
}
/// Removes out-of-chunk operand bundles from calls.
-static void extractOperandBundesFromModule(std::vector<Chunk> ChunksToKeep,
- Module *Program) {
- OperandBundleRemapper R(ChunksToKeep);
+static void extractOperandBundesFromModule(Oracle &O, Module &Program) {
+ OperandBundleRemapper R(O);
R.visit(Program);
for (const auto &I : R.CallsToRefine)
@@ -106,7 +104,7 @@ static void extractOperandBundesFromModule(std::vector<Chunk> ChunksToKeep,
}
/// Counts the amount of operand bundles.
-static int countOperandBundes(Module *Program) {
+static int countOperandBundes(Module &Program) {
OperandBundleCounter C;
// TODO: Silence index with --quiet flag
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceOperands.cpp b/llvm/tools/llvm-reduce/deltas/ReduceOperands.cpp
index f8fd63e3b9871..f697746909e1e 100755
--- a/llvm/tools/llvm-reduce/deltas/ReduceOperands.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceOperands.cpp
@@ -34,12 +34,9 @@ static bool canReduceOperand(Use &Op) {
}
/// Sets Operands to undef.
-static void extractOperandsFromModule(std::vector<Chunk> ChunksToKeep,
- Module *Program) {
- Oracle O(ChunksToKeep);
-
+static void extractOperandsFromModule(Oracle &O, Module &Program) {
// Extract Operands from the module.
- for (auto &F : Program->functions()) {
+ for (auto &F : Program.functions()) {
for (auto &I : instructions(&F)) {
for (auto &Op : I.operands()) {
// Filter Operands then set to undef.
@@ -53,9 +50,9 @@ static void extractOperandsFromModule(std::vector<Chunk> ChunksToKeep,
}
/// Counts the amount of operands in the module that can be reduced.
-static int countOperands(Module *Program) {
+static int countOperands(Module &Program) {
int Count = 0;
- for (auto &F : Program->functions()) {
+ for (auto &F : Program.functions()) {
for (auto &I : instructions(&F)) {
for (auto &Op : I.operands()) {
if (canReduceOperand(Op)) {
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceSpecialGlobals.cpp b/llvm/tools/llvm-reduce/deltas/ReduceSpecialGlobals.cpp
index dedeac8287f7b..1030d6315b2d1 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceSpecialGlobals.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceSpecialGlobals.cpp
@@ -26,13 +26,9 @@ static StringRef SpecialGlobalNames[] = {"llvm.used", "llvm.compiler.used"};
/// Removes all special globals aren't inside any of the
/// desired Chunks.
-static void
-extractSpecialGlobalsFromModule(const std::vector<Chunk> &ChunksToKeep,
- Module *Program) {
- Oracle O(ChunksToKeep);
-
+static void extractSpecialGlobalsFromModule(Oracle &O, Module &Program) {
for (StringRef Name : SpecialGlobalNames) {
- if (auto *Used = Program->getNamedGlobal(Name)) {
+ if (auto *Used = Program.getNamedGlobal(Name)) {
Used->replaceAllUsesWith(UndefValue::get(Used->getType()));
Used->eraseFromParent();
}
@@ -41,13 +37,13 @@ extractSpecialGlobalsFromModule(const std::vector<Chunk> &ChunksToKeep,
/// Counts the amount of special globals and prints their
/// respective name & index
-static int countSpecialGlobals(Module *Program) {
+static int countSpecialGlobals(Module &Program) {
// TODO: Silence index with --quiet flag
errs() << "----------------------------\n";
errs() << "Special Globals Index Reference:\n";
int Count = 0;
for (StringRef Name : SpecialGlobalNames) {
- if (auto *Used = Program->getNamedGlobal(Name))
+ if (auto *Used = Program.getNamedGlobal(Name))
errs() << "\t" << ++Count << ": " << Used->getName() << "\n";
}
errs() << "----------------------------\n";
diff --git a/llvm/tools/llvm-reduce/llvm-reduce.cpp b/llvm/tools/llvm-reduce/llvm-reduce.cpp
index 43dd15f43d463..90b7ed5f8272e 100644
--- a/llvm/tools/llvm-reduce/llvm-reduce.cpp
+++ b/llvm/tools/llvm-reduce/llvm-reduce.cpp
@@ -86,7 +86,7 @@ static std::unique_ptr<Module> parseInputFile(StringRef Filename,
return Result;
}
-void writeOutput(Module *M, StringRef Message) {
+void writeOutput(Module &M, StringRef Message) {
if (ReplaceInput) // In-place
OutputFilename = InputFilename.c_str();
else if (OutputFilename.empty() || OutputFilename == "-")
@@ -98,7 +98,7 @@ void writeOutput(Module *M, StringRef Message) {
errs() << "Error opening output file: " << EC.message() << "!\n";
exit(1);
}
- M->print(Out, /*AnnotationWriter=*/nullptr);
+ M.print(Out, /*AnnotationWriter=*/nullptr);
errs() << Message << OutputFilename << "\n";
}
@@ -122,21 +122,16 @@ int main(int Argc, char **Argv) {
}
// Initialize test environment
- TestRunner Tester(TestFilename, TestArguments);
- Tester.setProgram(std::move(OriginalProgram));
+ TestRunner Tester(TestFilename, TestArguments, std::move(OriginalProgram));
// Try to reduce code
runDeltaPasses(Tester);
- if (!Tester.getProgram()) {
- errs() << "\nCouldnt reduce input :/\n";
- } else {
- // Print reduced file to STDOUT
- if (OutputFilename == "-")
- Tester.getProgram()->print(outs(), nullptr);
- else
- writeOutput(Tester.getProgram(), "\nDone reducing! Reduced testcase: ");
- }
+ // Print reduced file to STDOUT
+ if (OutputFilename == "-")
+ Tester.getProgram().print(outs(), nullptr);
+ else
+ writeOutput(Tester.getProgram(), "\nDone reducing! Reduced testcase: ");
return 0;
}
More information about the llvm-commits
mailing list