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 declared and defined functions. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "ReduceArguments.h" 15 #include "Delta.h" 16 #include "llvm/ADT/SmallVector.h" 17 #include "llvm/IR/Intrinsics.h" 18 #include <set> 19 #include <vector> 20 21 using namespace llvm; 22 23 /// Goes over OldF calls and replaces them with a call to NewF 24 static void replaceFunctionCalls(Function &OldF, Function &NewF, 25 const std::set<int> &ArgIndexesToKeep) { 26 const auto &Users = OldF.users(); 27 for (auto I = Users.begin(), E = Users.end(); I != E; ) 28 if (auto *CI = dyn_cast<CallInst>(*I++)) { 29 // Skip uses in call instructions where OldF isn't the called function 30 // (e.g. if OldF is an argument of the call). 31 if (CI->getCalledFunction() != &OldF) 32 continue; 33 SmallVector<Value *, 8> Args; 34 for (auto ArgI = CI->arg_begin(), E = CI->arg_end(); ArgI != E; ++ArgI) 35 if (ArgIndexesToKeep.count(ArgI - CI->arg_begin())) 36 Args.push_back(*ArgI); 37 38 CallInst *NewCI = CallInst::Create(&NewF, Args); 39 NewCI->setCallingConv(NewF.getCallingConv()); 40 if (!CI->use_empty()) 41 CI->replaceAllUsesWith(NewCI); 42 ReplaceInstWithInst(CI, NewCI); 43 } 44 } 45 46 /// Returns whether or not this function should be considered a candidate for 47 /// argument removal. Currently, functions with no arguments and intrinsics are 48 /// not considered. Intrinsics aren't considered because their signatures are 49 /// fixed. 50 static bool shouldRemoveArguments(const Function &F) { 51 return !F.arg_empty() && !F.isIntrinsic(); 52 } 53 54 /// Removes out-of-chunk arguments from functions, and modifies their calls 55 /// accordingly. It also removes allocations of out-of-chunk arguments. 56 static void extractArgumentsFromModule(Oracle &O, Module &Program) { 57 std::vector<Argument *> InitArgsToKeep; 58 std::vector<Function *> Funcs; 59 // Get inside-chunk arguments, as well as their parent function 60 for (auto &F : Program) 61 if (shouldRemoveArguments(F)) { 62 Funcs.push_back(&F); 63 for (auto &A : F.args()) 64 if (O.shouldKeep()) 65 InitArgsToKeep.push_back(&A); 66 } 67 68 // We create a vector first, then convert it to a set, so that we don't have 69 // to pay the cost of rebalancing the set frequently if the order we insert 70 // the elements doesn't match the order they should appear inside the set. 71 std::set<Argument *> ArgsToKeep(InitArgsToKeep.begin(), InitArgsToKeep.end()); 72 73 for (auto *F : Funcs) { 74 ValueToValueMapTy VMap; 75 std::vector<WeakVH> InstToDelete; 76 for (auto &A : F->args()) 77 if (!ArgsToKeep.count(&A)) { 78 // By adding undesired arguments to the VMap, CloneFunction will remove 79 // them from the resulting Function 80 VMap[&A] = UndefValue::get(A.getType()); 81 for (auto *U : A.users()) 82 if (auto *I = dyn_cast<Instruction>(*&U)) 83 InstToDelete.push_back(I); 84 } 85 // Delete any (unique) instruction that uses the argument 86 for (Value *V : InstToDelete) { 87 if (!V) 88 continue; 89 auto *I = cast<Instruction>(V); 90 I->replaceAllUsesWith(UndefValue::get(I->getType())); 91 if (!I->isTerminator()) 92 I->eraseFromParent(); 93 } 94 95 // No arguments to reduce 96 if (VMap.empty()) 97 continue; 98 99 std::set<int> ArgIndexesToKeep; 100 for (auto &Arg : enumerate(F->args())) 101 if (ArgsToKeep.count(&Arg.value())) 102 ArgIndexesToKeep.insert(Arg.index()); 103 104 auto *ClonedFunc = CloneFunction(F, VMap); 105 // In order to preserve function order, we move Clone after old Function 106 ClonedFunc->removeFromParent(); 107 Program.getFunctionList().insertAfter(F->getIterator(), ClonedFunc); 108 109 replaceFunctionCalls(*F, *ClonedFunc, ArgIndexesToKeep); 110 // Rename Cloned Function to Old's name 111 std::string FName = std::string(F->getName()); 112 F->replaceAllUsesWith(ConstantExpr::getBitCast(ClonedFunc, F->getType())); 113 F->eraseFromParent(); 114 ClonedFunc->setName(FName); 115 } 116 } 117 118 void llvm::reduceArgumentsDeltaPass(TestRunner &Test) { 119 outs() << "*** Reducing Arguments...\n"; 120 runDeltaPass(Test, extractArgumentsFromModule); 121 } 122