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 #include "llvm/ADT/SetVector.h"
18 #include "llvm/IR/Instructions.h"
19 #include <set>
20 
21 using namespace llvm;
22 
23 /// Removes all the Defined Functions (as well as their calls)
24 /// that aren't inside any of the desired Chunks.
25 static void extractFunctionsFromModule(const std::vector<Chunk> &ChunksToKeep,
26                                        Module *Program) {
27   Oracle O(ChunksToKeep);
28 
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 calls with undef
36   std::vector<Function *> FuncsToRemove;
37   SetVector<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.insert(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 int countFunctions(Module *Program) {
59   // TODO: Silence index with --quiet flag
60   errs() << "----------------------------\n";
61   errs() << "Function Index Reference:\n";
62   int 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   int Functions = countFunctions(Test.getProgram());
73   runDeltaPass(Test, Functions, extractFunctionsFromModule);
74   errs() << "----------------------------\n";
75 }
76