forked from OSchip/llvm-project
[Reduce] Rewrite function body delta pass again
It is not enough to replace all uses of users of the function with undef, the users, we only drop instruction users, so they may stick around. Let's try different approach - first drop bodies for all the functions we will drop, which should take care of blockaddress issue the previous rewrite was dealing with; then, after dropping *all* such bodies, replace remaining uses with undef (thus all the uses are either outside of functions, or are in kept functions) and then finally drop functions. This seems to work, and passes the *existing* test coverage, but it is possible that a new issue will be discovered later :) A new (previously crashing) test added.
This commit is contained in:
parent
48887c4e81
commit
d4c3f20285
|
@ -0,0 +1,14 @@
|
|||
; RUN: llvm-reduce --test FileCheck --test-arg --check-prefixes=CHECK-ALL,CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t
|
||||
; RUN: cat %t | FileCheck --check-prefixes=CHECK-ALL,CHECK-FINAL %s
|
||||
|
||||
; CHECK-INTERESTINGNESS: @alias =
|
||||
; CHECK-FINAL: @alias = alias void (i32), void (i32)* undef
|
||||
|
||||
@alias = alias void (i32), void (i32)* @func
|
||||
|
||||
; CHECK-FINAL-NOT: @func()
|
||||
|
||||
define void @func(i32 %arg) {
|
||||
entry:
|
||||
ret void
|
||||
}
|
|
@ -14,42 +14,36 @@
|
|||
|
||||
#include "ReduceFunctions.h"
|
||||
#include "Delta.h"
|
||||
#include "llvm/ADT/SetVector.h"
|
||||
#include "llvm/ADT/STLExtras.h"
|
||||
#include "llvm/IR/Instructions.h"
|
||||
#include <set>
|
||||
#include <iterator>
|
||||
#include <vector>
|
||||
|
||||
using namespace llvm;
|
||||
|
||||
/// Removes all the Defined Functions (as well as their calls)
|
||||
/// 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);
|
||||
|
||||
// Get functions inside desired chunks
|
||||
std::set<Function *> FuncsToKeep;
|
||||
for (auto &F : *Program)
|
||||
if (O.shouldKeep())
|
||||
FuncsToKeep.insert(&F);
|
||||
// Record all out-of-chunk functions.
|
||||
std::vector<std::reference_wrapper<Function>> FuncsToRemove;
|
||||
copy_if(Program->functions(), std::back_inserter(FuncsToRemove),
|
||||
[&O](auto &unused) { return !O.shouldKeep(); });
|
||||
|
||||
// Delete out-of-chunk functions, and replace their users with undef
|
||||
std::vector<Function *> FuncsToRemove;
|
||||
SetVector<Instruction *> InstrsToRemove;
|
||||
for (auto &F : *Program)
|
||||
if (!FuncsToKeep.count(&F)) {
|
||||
for (auto U : F.users()) {
|
||||
U->replaceAllUsesWith(UndefValue::get(U->getType()));
|
||||
if (auto *I = dyn_cast<Instruction>(U))
|
||||
InstrsToRemove.insert(I);
|
||||
}
|
||||
FuncsToRemove.push_back(&F);
|
||||
}
|
||||
// Then, drop body of each of them. We want to batch this and do nothing else
|
||||
// here so that minimal number of remaining exteranal uses will remain.
|
||||
for (Function &F : FuncsToRemove)
|
||||
F.dropAllReferences();
|
||||
|
||||
for (auto *I : InstrsToRemove)
|
||||
I->eraseFromParent();
|
||||
|
||||
for (auto *F : FuncsToRemove)
|
||||
F->eraseFromParent();
|
||||
// And finally, we can actually delete them.
|
||||
for (Function &F : FuncsToRemove) {
|
||||
// Replace all *still* remaining uses with undef.
|
||||
F.replaceAllUsesWith(UndefValue::get(F.getType()));
|
||||
// And finally, fully drop it.
|
||||
F.eraseFromParent();
|
||||
}
|
||||
}
|
||||
|
||||
/// Counts the amount of non-declaration functions and prints their
|
||||
|
|
Loading…
Reference in New Issue