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/STLExtras.h" 18 #include "llvm/IR/Constants.h" 19 #include "llvm/IR/Instructions.h" 20 #include <iterator> 21 #include <vector> 22 23 using namespace llvm; 24 25 /// Removes all the Defined Functions 26 /// that aren't inside any of the desired Chunks. 27 static void extractFunctionsFromModule(Oracle &O, Module &Program) { 28 // Record all out-of-chunk functions. 29 std::vector<std::reference_wrapper<Function>> FuncsToRemove; 30 copy_if(Program.functions(), std::back_inserter(FuncsToRemove), 31 [&O](Function &F) { 32 // Intrinsics don't have function bodies that are useful to 33 // reduce. Additionally, intrinsics may have additional operand 34 // constraints. But, do drop intrinsics that are not referenced. 35 return (!F.isIntrinsic() || F.use_empty()) && !O.shouldKeep(); 36 }); 37 38 // Then, drop body of each of them. We want to batch this and do nothing else 39 // here so that minimal number of remaining exteranal uses will remain. 40 for (Function &F : FuncsToRemove) 41 F.dropAllReferences(); 42 43 // And finally, we can actually delete them. 44 for (Function &F : FuncsToRemove) { 45 // Replace all *still* remaining uses with undef. 46 F.replaceAllUsesWith(UndefValue::get(F.getType())); 47 // And finally, fully drop it. 48 F.eraseFromParent(); 49 } 50 } 51 52 void llvm::reduceFunctionsDeltaPass(TestRunner &Test) { 53 errs() << "*** Reducing Functions...\n"; 54 runDeltaPass(Test, extractFunctionsFromModule); 55 errs() << "----------------------------\n"; 56 } 57