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"
17 /// Removes all the Defined Functions (as well as their calls)
18 /// that aren't inside any of the desired Chunks.
19 static void extractFunctionsFromModule(const std::vector
<Chunk
> &ChunksToKeep
,
21 // Get functions inside desired chunks
22 std::set
<Function
*> FuncsToKeep
;
23 unsigned I
= 0, FunctionCount
= 0;
24 for (auto &F
: *Program
)
25 if (I
< ChunksToKeep
.size()) {
26 if (ChunksToKeep
[I
].contains(++FunctionCount
))
27 FuncsToKeep
.insert(&F
);
28 if (FunctionCount
== ChunksToKeep
[I
].end
)
32 // Delete out-of-chunk functions, and replace their calls with undef
33 std::vector
<Function
*> FuncsToRemove
;
34 std::vector
<CallInst
*> CallsToRemove
;
35 for (auto &F
: *Program
)
36 if (!FuncsToKeep
.count(&F
)) {
37 for (auto U
: F
.users())
38 if (auto *Call
= dyn_cast
<CallInst
>(U
)) {
39 Call
->replaceAllUsesWith(UndefValue::get(Call
->getType()));
40 CallsToRemove
.push_back(Call
);
42 F
.replaceAllUsesWith(UndefValue::get(F
.getType()));
43 FuncsToRemove
.push_back(&F
);
46 for (auto *C
: CallsToRemove
)
49 for (auto *F
: FuncsToRemove
)
53 /// Counts the amount of non-declaration functions and prints their
54 /// respective name & index
55 static unsigned countFunctions(Module
*Program
) {
56 // TODO: Silence index with --quiet flag
57 errs() << "----------------------------\n";
58 errs() << "Function Index Reference:\n";
59 unsigned FunctionCount
= 0;
60 for (auto &F
: *Program
)
61 errs() << "\t" << ++FunctionCount
<< ": " << F
.getName() << "\n";
63 errs() << "----------------------------\n";
67 void llvm::reduceFunctionsDeltaPass(TestRunner
&Test
) {
68 errs() << "*** Reducing Functions...\n";
69 unsigned Functions
= countFunctions(Test
.getProgram());
70 runDeltaPass(Test
, Functions
, extractFunctionsFromModule
);
71 errs() << "----------------------------\n";