[sanitizer] Improve FreeBSD ASLR detection
[llvm-project.git] / llvm / tools / llvm-reduce / deltas / ReduceGlobalVars.cpp
blob1651a37eaa12b0a50576d607b812a41fccc15e5d
1 //===- ReduceGlobalVars.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 Global Variables in the provided Module.
12 //===----------------------------------------------------------------------===//
14 #include "ReduceGlobalVars.h"
15 #include "llvm/IR/Constants.h"
16 #include <set>
18 using namespace llvm;
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())
25 if (O.shouldKeep())
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(),
32 InitGVsToKeep.end());
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) {
49 if (!V)
50 continue;
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);