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