1 //===- ReduceGlobalVars.cpp - Specialized Delta Pass ----------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements a function which calls the Generic Delta pass in order
10 // to reduce Global Variables in the provided Module.
12 //===----------------------------------------------------------------------===//
14 #include "ReduceGlobalVars.h"
15 #include "llvm/IR/Constants.h"
20 /// Removes all the GVs that aren't inside the desired Chunks.
21 static void extractGVsFromModule(Oracle
&O
, Module
&Program
) {
22 // Get GVs inside desired chunks
23 std::vector
<GlobalVariable
*> InitGVsToKeep
;
24 for (auto &GV
: Program
.globals())
26 InitGVsToKeep
.push_back(&GV
);
28 // We create a vector first, then convert it to a set, so that we don't have
29 // to pay the cost of rebalancing the set frequently if the order we insert
30 // the elements doesn't match the order they should appear inside the set.
31 std::set
<GlobalVariable
*> GVsToKeep(InitGVsToKeep
.begin(),
34 // Delete out-of-chunk GVs and their uses
35 std::vector
<GlobalVariable
*> ToRemove
;
36 std::vector
<WeakVH
> InstToRemove
;
37 for (auto &GV
: Program
.globals())
38 if (!GVsToKeep
.count(&GV
)) {
39 for (auto *U
: GV
.users())
40 if (auto *Inst
= dyn_cast
<Instruction
>(U
))
41 InstToRemove
.push_back(Inst
);
43 GV
.replaceAllUsesWith(UndefValue::get(GV
.getType()));
44 ToRemove
.push_back(&GV
);
47 // Delete (unique) Instruction uses of unwanted GVs
48 for (Value
*V
: InstToRemove
) {
51 auto *Inst
= cast
<Instruction
>(V
);
52 Inst
->replaceAllUsesWith(UndefValue::get(Inst
->getType()));
53 Inst
->eraseFromParent();
56 for (auto *GV
: ToRemove
)
57 GV
->eraseFromParent();
60 void llvm::reduceGlobalsDeltaPass(TestRunner
&Test
) {
61 outs() << "*** Reducing GVs...\n";
62 runDeltaPass(Test
, extractGVsFromModule
);