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