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 "Utils.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/Instructions.h"
21 #include <iterator>
22 #include <vector>
23
24 using namespace llvm;
25
26 /// Removes all the Defined Functions
27 /// that aren't inside any of the desired Chunks.
extractFunctionsFromModule(Oracle & O,Module & Program)28 static void extractFunctionsFromModule(Oracle &O, Module &Program) {
29 // Record all out-of-chunk functions.
30 std::vector<std::reference_wrapper<Function>> FuncsToRemove;
31 copy_if(Program.functions(), std::back_inserter(FuncsToRemove),
32 [&O](Function &F) {
33 // Intrinsics don't have function bodies that are useful to
34 // reduce. Additionally, intrinsics may have additional operand
35 // constraints. But, do drop intrinsics that are not referenced.
36 return (!F.isIntrinsic() || F.use_empty()) && !O.shouldKeep();
37 });
38
39 // Then, drop body of each of them. We want to batch this and do nothing else
40 // here so that minimal number of remaining exteranal uses will remain.
41 for (Function &F : FuncsToRemove)
42 F.dropAllReferences();
43
44 // And finally, we can actually delete them.
45 for (Function &F : FuncsToRemove) {
46 // Replace all *still* remaining uses with the default value.
47 F.replaceAllUsesWith(getDefaultValue(F.getType()));
48 // And finally, fully drop it.
49 F.eraseFromParent();
50 }
51 }
52
reduceFunctionsDeltaPass(TestRunner & Test)53 void llvm::reduceFunctionsDeltaPass(TestRunner &Test) {
54 errs() << "*** Reducing Functions...\n";
55 runDeltaPass(Test, extractFunctionsFromModule);
56 errs() << "----------------------------\n";
57 }
58