| 1 | //===- ReduceFunctions.cpp - Specialized Delta Pass -----------------------===// |
|---|---|
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements a function which calls the Generic Delta pass in order |
| 10 | // to reduce functions (and any instruction that calls it) in the provided |
| 11 | // Module. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "ReduceFunctions.h" |
| 16 | #include "Utils.h" |
| 17 | #include "llvm/Transforms/Utils/Cloning.h" |
| 18 | #include "llvm/Transforms/Utils/ModuleUtils.h" |
| 19 | |
| 20 | using namespace llvm; |
| 21 | |
| 22 | /// Removes all the Defined Functions |
| 23 | /// that aren't inside any of the desired Chunks. |
| 24 | void llvm::reduceFunctionsDeltaPass(Oracle &O, ReducerWorkItem &WorkItem) { |
| 25 | Module &Program = WorkItem.getModule(); |
| 26 | |
| 27 | // Record all out-of-chunk functions. |
| 28 | SmallPtrSet<Constant *, 8> FuncsToRemove; |
| 29 | for (Function &F : Program.functions()) { |
| 30 | // Intrinsics don't have function bodies that are useful to |
| 31 | // reduce. Additionally, intrinsics may have additional operand |
| 32 | // constraints. But, do drop intrinsics that are not referenced. |
| 33 | if ((!F.isIntrinsic() || F.use_empty()) && !hasAliasUse(F) && |
| 34 | !O.shouldKeep()) |
| 35 | FuncsToRemove.insert(Ptr: &F); |
| 36 | } |
| 37 | |
| 38 | removeFromUsedLists(M&: Program, ShouldRemove: [&FuncsToRemove](Constant *C) { |
| 39 | return FuncsToRemove.count(Ptr: C); |
| 40 | }); |
| 41 | |
| 42 | // Then, drop body of each of them. We want to batch this and do nothing else |
| 43 | // here so that minimal number of remaining external uses will remain. |
| 44 | for (Constant *F : FuncsToRemove) |
| 45 | F->dropAllReferences(); |
| 46 | |
| 47 | // And finally, we can actually delete them. |
| 48 | for (Constant *F : FuncsToRemove) { |
| 49 | // Replace all *still* remaining uses with the default value. |
| 50 | F->replaceAllUsesWith(V: getDefaultValue(T: F->getType())); |
| 51 | // And finally, fully drop it. |
| 52 | cast<Function>(Val: F)->eraseFromParent(); |
| 53 | } |
| 54 | } |
| 55 |