xref: /llvm-project/llvm/tools/llvm-reduce/deltas/ReduceFunctions.cpp (revision bc265bd663233c4bfa222f1cc93ec472075a53ff)
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/Transforms/Utils/ModuleUtils.h"
20 #include <iterator>
21 #include <vector>
22 
23 using namespace llvm;
24 
25 /// Removes all the Defined Functions
26 /// that aren't inside any of the desired Chunks.
27 static void extractFunctionsFromModule(Oracle &O, ReducerWorkItem &WorkItem) {
28   Module &Program = WorkItem.getModule();
29 
30   // Record all out-of-chunk functions.
31   SmallPtrSet<Constant *, 8> FuncsToRemove;
32   for (Function &F : Program.functions()) {
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     if ((!F.isIntrinsic() || F.use_empty()) && !hasAliasOrBlockAddressUse(F) &&
37         !O.shouldKeep())
38       FuncsToRemove.insert(&F);
39   }
40 
41   removeFromUsedLists(Program, [&FuncsToRemove](Constant *C) {
42     return FuncsToRemove.count(C);
43   });
44 
45   // Then, drop body of each of them. We want to batch this and do nothing else
46   // here so that minimal number of remaining exteranal uses will remain.
47   for (Constant *F : FuncsToRemove)
48     F->dropAllReferences();
49 
50   // And finally, we can actually delete them.
51   for (Constant *F : FuncsToRemove) {
52     // Replace all *still* remaining uses with the default value.
53     F->replaceAllUsesWith(getDefaultValue(F->getType()));
54     // And finally, fully drop it.
55     cast<Function>(F)->eraseFromParent();
56   }
57 }
58 
59 void llvm::reduceFunctionsDeltaPass(TestRunner &Test) {
60   runDeltaPass(Test, extractFunctionsFromModule, "Reducing Functions");
61 }
62