[obj2yaml] - Fix a comment. NFC.
[llvm-complete.git] / tools / llvm-reduce / deltas / ReduceInstructions.cpp
blobb3497ad2dc02d3a634bc822f7a5e059ebff995f8
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.
12 //===----------------------------------------------------------------------===//
14 #include "ReduceInstructions.h"
16 using namespace llvm;
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(std::vector<Chunk> ChunksToKeep,
21 Module *Program) {
22 int I = 0, InstCount = 0;
23 std::set<Instruction *> InstToKeep;
25 for (auto &F : *Program)
26 for (auto &BB : F)
27 for (auto &Inst : BB)
28 if (I < (int)ChunksToKeep.size()) {
29 if (ChunksToKeep[I].contains(++InstCount))
30 InstToKeep.insert(&Inst);
31 if (ChunksToKeep[I].end == InstCount)
32 ++I;
35 std::vector<Instruction *> InstToDelete;
36 for (auto &F : *Program)
37 for (auto &BB : F)
38 for (auto &Inst : BB)
39 if (!InstToKeep.count(&Inst)) {
40 Inst.replaceAllUsesWith(UndefValue::get(Inst.getType()));
41 InstToDelete.push_back(&Inst);
44 for (auto &I : InstToDelete)
45 I->eraseFromParent();
48 /// Counts the amount of basic blocks and prints their name & respective index
49 static unsigned countInstructions(Module *Program) {
50 // TODO: Silence index with --quiet flag
51 outs() << "----------------------------\n";
52 int InstCount = 0;
53 for (auto &F : *Program)
54 for (auto &BB : F)
55 InstCount += BB.getInstList().size();
56 outs() << "Number of instructions: " << InstCount << "\n";
58 return InstCount;
61 void llvm::reduceInstructionsDeltaPass(TestRunner &Test) {
62 outs() << "*** Reducing Insructions...\n";
63 unsigned InstCount = countInstructions(Test.getProgram());
64 runDeltaPass(Test, InstCount, extractInstrFromModule);