|
14 | 14 |
|
15 | 15 | #include "ReduceFunctions.h"
|
16 | 16 | #include "Delta.h"
|
17 |
| -#include "llvm/ADT/SetVector.h" |
| 17 | +#include "llvm/ADT/STLExtras.h" |
18 | 18 | #include "llvm/IR/Instructions.h"
|
19 |
| -#include <set> |
| 19 | +#include <iterator> |
| 20 | +#include <vector> |
20 | 21 |
|
21 | 22 | using namespace llvm;
|
22 | 23 |
|
23 |
| -/// Removes all the Defined Functions (as well as their calls) |
| 24 | +/// Removes all the Defined Functions |
24 | 25 | /// that aren't inside any of the desired Chunks.
|
25 | 26 | static void extractFunctionsFromModule(const std::vector<Chunk> &ChunksToKeep,
|
26 | 27 | Module *Program) {
|
27 | 28 | Oracle O(ChunksToKeep);
|
28 | 29 |
|
29 |
| - // Get functions inside desired chunks |
30 |
| - std::set<Function *> FuncsToKeep; |
31 |
| - for (auto &F : *Program) |
32 |
| - if (O.shouldKeep()) |
33 |
| - FuncsToKeep.insert(&F); |
34 |
| - |
35 |
| - // Delete out-of-chunk functions, and replace their users with undef |
36 |
| - std::vector<Function *> FuncsToRemove; |
37 |
| - SetVector<Instruction *> InstrsToRemove; |
38 |
| - for (auto &F : *Program) |
39 |
| - if (!FuncsToKeep.count(&F)) { |
40 |
| - for (auto U : F.users()) { |
41 |
| - U->replaceAllUsesWith(UndefValue::get(U->getType())); |
42 |
| - if (auto *I = dyn_cast<Instruction>(U)) |
43 |
| - InstrsToRemove.insert(I); |
44 |
| - } |
45 |
| - FuncsToRemove.push_back(&F); |
46 |
| - } |
| 30 | + // Record all out-of-chunk functions. |
| 31 | + std::vector<std::reference_wrapper<Function>> FuncsToRemove; |
| 32 | + copy_if(Program->functions(), std::back_inserter(FuncsToRemove), |
| 33 | + [&O](auto &unused) { return !O.shouldKeep(); }); |
47 | 34 |
|
48 |
| - for (auto *I : InstrsToRemove) |
49 |
| - I->eraseFromParent(); |
| 35 | + // Then, drop body of each of them. We want to batch this and do nothing else |
| 36 | + // here so that minimal number of remaining exteranal uses will remain. |
| 37 | + for (Function &F : FuncsToRemove) |
| 38 | + F.dropAllReferences(); |
50 | 39 |
|
51 |
| - for (auto *F : FuncsToRemove) |
52 |
| - F->eraseFromParent(); |
| 40 | + // And finally, we can actually delete them. |
| 41 | + for (Function &F : FuncsToRemove) { |
| 42 | + // Replace all *still* remaining uses with undef. |
| 43 | + F.replaceAllUsesWith(UndefValue::get(F.getType())); |
| 44 | + // And finally, fully drop it. |
| 45 | + F.eraseFromParent(); |
| 46 | + } |
53 | 47 | }
|
54 | 48 |
|
55 | 49 | /// Counts the amount of non-declaration functions and prints their
|
|
0 commit comments