1 //===- ReduceArguments.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 uninteresting Arguments from defined functions.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "ReduceInstructions.h"
15 
16 using namespace llvm;
17 
18 /// Removes out-of-chunk arguments from functions, and modifies their calls
19 /// accordingly. It also removes allocations of out-of-chunk arguments.
20 static void extractInstrFromModule(Oracle &O, Module &Program) {
21   std::vector<Instruction *> InitInstToKeep;
22 
23   for (auto &F : Program)
24     for (auto &BB : F) {
25       // Removing the terminator would make the block invalid. Only iterate over
26       // instructions before the terminator.
27       InitInstToKeep.push_back(BB.getTerminator());
28       for (auto &Inst : make_range(BB.begin(), std::prev(BB.end())))
29         if (O.shouldKeep())
30           InitInstToKeep.push_back(&Inst);
31     }
32 
33   // We create a vector first, then convert it to a set, so that we don't have
34   // to pay the cost of rebalancing the set frequently if the order we insert
35   // the elements doesn't match the order they should appear inside the set.
36   std::set<Instruction *> InstToKeep(InitInstToKeep.begin(),
37                                      InitInstToKeep.end());
38 
39   std::vector<Instruction *> InstToDelete;
40   for (auto &F : Program)
41     for (auto &BB : F)
42       for (auto &Inst : BB)
43         if (!InstToKeep.count(&Inst)) {
44           Inst.replaceAllUsesWith(UndefValue::get(Inst.getType()));
45           InstToDelete.push_back(&Inst);
46         }
47 
48   for (auto &I : InstToDelete)
49     I->eraseFromParent();
50 }
51 
52 void llvm::reduceInstructionsDeltaPass(TestRunner &Test) {
53   outs() << "*** Reducing Instructions...\n";
54   runDeltaPass(Test, extractInstrFromModule);
55 }
56