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 17 /// Removes all the Defined Functions (as well as their calls) 18 /// that aren't inside any of the desired Chunks. 19 static void extractFunctionsFromModule(const std::vector<Chunk> &ChunksToKeep, 20 Module *Program) { 21 // Get functions inside desired chunks 22 std::set<Function *> FuncsToKeep; 23 unsigned I = 0, FunctionCount = 0; 24 for (auto &F : *Program) 25 if (I < ChunksToKeep.size()) { 26 if (ChunksToKeep[I].contains(++FunctionCount)) 27 FuncsToKeep.insert(&F); 28 if (FunctionCount == ChunksToKeep[I].end) 29 ++I; 30 } 31 32 // Delete out-of-chunk functions, and replace their calls with undef 33 std::vector<Function *> FuncsToRemove; 34 std::vector<CallInst *> CallsToRemove; 35 for (auto &F : *Program) 36 if (!FuncsToKeep.count(&F)) { 37 for (auto U : F.users()) 38 if (auto *Call = dyn_cast<CallInst>(U)) { 39 Call->replaceAllUsesWith(UndefValue::get(Call->getType())); 40 CallsToRemove.push_back(Call); 41 } 42 F.replaceAllUsesWith(UndefValue::get(F.getType())); 43 FuncsToRemove.push_back(&F); 44 } 45 46 for (auto *C : CallsToRemove) 47 C->eraseFromParent(); 48 49 for (auto *F : FuncsToRemove) 50 F->eraseFromParent(); 51 } 52 53 /// Counts the amount of non-declaration functions and prints their 54 /// respective name & index 55 static unsigned countFunctions(Module *Program) { 56 // TODO: Silence index with --quiet flag 57 outs() << "----------------------------\n"; 58 outs() << "Function Index Reference:\n"; 59 unsigned FunctionCount = 0; 60 for (auto &F : *Program) 61 outs() << "\t" << ++FunctionCount << ": " << F.getName() << "\n"; 62 63 outs() << "----------------------------\n"; 64 return FunctionCount; 65 } 66 67 void llvm::reduceFunctionsDeltaPass(TestRunner &Test) { 68 outs() << "*** Reducing Functions...\n"; 69 unsigned Functions = countFunctions(Test.getProgram()); 70 runDeltaPass(Test, Functions, extractFunctionsFromModule); 71 outs() << "----------------------------\n"; 72 }