1 //===- ReduceFunctions.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 functions (and any instruction that calls it) in the provided 11 // Module. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "ReduceFunctions.h" 16 #include "Delta.h" 17 #include "llvm/ADT/SetVector.h" 18 #include "llvm/IR/Instructions.h" 19 #include <set> 20 21 using namespace llvm; 22 23 /// Removes all the Defined Functions (as well as their calls) 24 /// that aren't inside any of the desired Chunks. 25 static void extractFunctionsFromModule(const std::vector<Chunk> &ChunksToKeep, 26 Module *Program) { 27 Oracle O(ChunksToKeep); 28 29 // Get functions inside desired chunks 30 std::set<Function *> FuncsToKeep; 31 for (auto &F : *Program) 32 if (O.shouldKeep()) 33 FuncsToKeep.insert(&F); 34 35 // Delete out-of-chunk functions, and replace their users with undef 36 std::vector<Function *> FuncsToRemove; 37 SetVector<Instruction *> InstrsToRemove; 38 for (auto &F : *Program) 39 if (!FuncsToKeep.count(&F)) { 40 for (auto U : F.users()) { 41 U->replaceAllUsesWith(UndefValue::get(U->getType())); 42 if (auto *I = dyn_cast<Instruction>(U)) 43 InstrsToRemove.insert(I); 44 } 45 FuncsToRemove.push_back(&F); 46 } 47 48 for (auto *I : InstrsToRemove) 49 I->eraseFromParent(); 50 51 for (auto *F : FuncsToRemove) 52 F->eraseFromParent(); 53 } 54 55 /// Counts the amount of non-declaration functions and prints their 56 /// respective name & index 57 static int countFunctions(Module *Program) { 58 // TODO: Silence index with --quiet flag 59 errs() << "----------------------------\n"; 60 errs() << "Function Index Reference:\n"; 61 int FunctionCount = 0; 62 for (auto &F : *Program) 63 errs() << "\t" << ++FunctionCount << ": " << F.getName() << "\n"; 64 65 errs() << "----------------------------\n"; 66 return FunctionCount; 67 } 68 69 void llvm::reduceFunctionsDeltaPass(TestRunner &Test) { 70 errs() << "*** Reducing Functions...\n"; 71 int Functions = countFunctions(Test.getProgram()); 72 runDeltaPass(Test, Functions, extractFunctionsFromModule); 73 errs() << "----------------------------\n"; 74 } 75