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. extractInstrFromModule(std::vector<Chunk> ChunksToKeep,Module * Program)20static void extractInstrFromModule(std::vector<Chunk> ChunksToKeep, 21 Module *Program) { 22 Oracle O(ChunksToKeep); 23 24 std::set<Instruction *> InstToKeep; 25 26 for (auto &F : *Program) 27 for (auto &BB : F) { 28 // Removing the terminator would make the block invalid. Only iterate over 29 // instructions before the terminator. 30 InstToKeep.insert(BB.getTerminator()); 31 for (auto &Inst : make_range(BB.begin(), std::prev(BB.end()))) 32 if (O.shouldKeep()) 33 InstToKeep.insert(&Inst); 34 } 35 36 std::vector<Instruction *> InstToDelete; 37 for (auto &F : *Program) 38 for (auto &BB : F) 39 for (auto &Inst : BB) 40 if (!InstToKeep.count(&Inst)) { 41 Inst.replaceAllUsesWith(UndefValue::get(Inst.getType())); 42 InstToDelete.push_back(&Inst); 43 } 44 45 for (auto &I : InstToDelete) 46 I->eraseFromParent(); 47 } 48 49 /// Counts the amount of basic blocks and prints their name & respective index countInstructions(Module * Program)50static unsigned countInstructions(Module *Program) { 51 // TODO: Silence index with --quiet flag 52 outs() << "----------------------------\n"; 53 int InstCount = 0; 54 for (auto &F : *Program) 55 for (auto &BB : F) 56 // Well-formed blocks have terminators, which we cannot remove. 57 InstCount += BB.getInstList().size() - 1; 58 outs() << "Number of instructions: " << InstCount << "\n"; 59 60 return InstCount; 61 } 62 reduceInstructionsDeltaPass(TestRunner & Test)63void llvm::reduceInstructionsDeltaPass(TestRunner &Test) { 64 outs() << "*** Reducing Instructions...\n"; 65 unsigned InstCount = countInstructions(Test.getProgram()); 66 runDeltaPass(Test, InstCount, extractInstrFromModule); 67 } 68