1 //===- ReduceFunctions.cpp - Specialized Delta Pass -----------------------===//
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
7 //===----------------------------------------------------------------------===//
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
13 //===----------------------------------------------------------------------===//
15 #include "ReduceFunctions.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SetVector.h"
20 #include "llvm/Transforms/Utils/ModuleUtils.h"
26 /// Removes all the Defined Functions
27 /// that aren't inside any of the desired Chunks.
28 static void extractFunctionsFromModule(Oracle
&O
, ReducerWorkItem
&WorkItem
) {
29 Module
&Program
= WorkItem
.getModule();
31 // Record all out-of-chunk functions.
32 SmallPtrSet
<Constant
*, 8> FuncsToRemove
;
33 for (Function
&F
: Program
.functions()) {
34 // Intrinsics don't have function bodies that are useful to
35 // reduce. Additionally, intrinsics may have additional operand
36 // constraints. But, do drop intrinsics that are not referenced.
37 if ((!F
.isIntrinsic() || F
.use_empty()) && !hasAliasOrBlockAddressUse(F
) &&
39 FuncsToRemove
.insert(&F
);
42 removeFromUsedLists(Program
, [&FuncsToRemove
](Constant
*C
) {
43 return FuncsToRemove
.count(C
);
46 // Then, drop body of each of them. We want to batch this and do nothing else
47 // here so that minimal number of remaining exteranal uses will remain.
48 for (Constant
*F
: FuncsToRemove
)
49 F
->dropAllReferences();
51 // And finally, we can actually delete them.
52 for (Constant
*F
: FuncsToRemove
) {
53 // Replace all *still* remaining uses with the default value.
54 F
->replaceAllUsesWith(getDefaultValue(F
->getType()));
55 // And finally, fully drop it.
56 cast
<Function
>(F
)->eraseFromParent();
60 void llvm::reduceFunctionsDeltaPass(TestRunner
&Test
) {
61 runDeltaPass(Test
, extractFunctionsFromModule
, "Reducing Functions");