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