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 function bodies in the provided Module.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "ReduceFunctionBodies.h"
15 #include "Delta.h"
16 #include "llvm/IR/GlobalValue.h"
17 
18 using namespace llvm;
19 
20 /// Removes all the bodies of defined functions that aren't inside any of the
21 /// desired Chunks.
22 static void
extractFunctionBodiesFromModule(const std::vector<Chunk> & ChunksToKeep,Module * Program)23 extractFunctionBodiesFromModule(const std::vector<Chunk> &ChunksToKeep,
24                                 Module *Program) {
25   Oracle O(ChunksToKeep);
26 
27   // Delete out-of-chunk function bodies
28   std::vector<Function *> FuncDefsToReduce;
29   for (auto &F : *Program)
30     if (!F.isDeclaration() && !O.shouldKeep()) {
31       F.deleteBody();
32       F.setComdat(nullptr);
33     }
34 }
35 
36 /// Counts the amount of non-declaration functions and prints their
37 /// respective name & index
countFunctionDefinitions(Module * Program)38 static int countFunctionDefinitions(Module *Program) {
39   // TODO: Silence index with --quiet flag
40   errs() << "----------------------------\n";
41   errs() << "Function Definition Index Reference:\n";
42   int FunctionDefinitionCount = 0;
43   for (auto &F : *Program)
44     if (!F.isDeclaration())
45       errs() << "\t" << ++FunctionDefinitionCount << ": " << F.getName()
46              << "\n";
47 
48   errs() << "----------------------------\n";
49   return FunctionDefinitionCount;
50 }
51 
reduceFunctionBodiesDeltaPass(TestRunner & Test)52 void llvm::reduceFunctionBodiesDeltaPass(TestRunner &Test) {
53   errs() << "*** Reducing Function Bodies...\n";
54   int Functions = countFunctionDefinitions(Test.getProgram());
55   runDeltaPass(Test, Functions, extractFunctionBodiesFromModule);
56   errs() << "----------------------------\n";
57 }
58