15adb3d2aSDavid Blaikie //===- ReduceArguments.cpp - Specialized Delta Pass -----------------------===//
25adb3d2aSDavid Blaikie //
35adb3d2aSDavid Blaikie // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45adb3d2aSDavid Blaikie // See https://llvm.org/LICENSE.txt for license information.
55adb3d2aSDavid Blaikie // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65adb3d2aSDavid Blaikie //
75adb3d2aSDavid Blaikie //===----------------------------------------------------------------------===//
85adb3d2aSDavid Blaikie //
95adb3d2aSDavid Blaikie // This file implements a function which calls the Generic Delta pass in order
10a2403588SLangston Barrett // to reduce uninteresting Arguments from declared and defined functions.
115adb3d2aSDavid Blaikie //
125adb3d2aSDavid Blaikie //===----------------------------------------------------------------------===//
135adb3d2aSDavid Blaikie
145adb3d2aSDavid Blaikie #include "ReduceArguments.h"
155adb3d2aSDavid Blaikie #include "Delta.h"
162962f9dfSJohn Regehr #include "Utils.h"
175adb3d2aSDavid Blaikie #include "llvm/ADT/SmallVector.h"
18a5bbc6efSBill Wendling #include "llvm/IR/Constants.h"
19a494ae43Sserge-sans-paille #include "llvm/IR/Instructions.h"
20a2403588SLangston Barrett #include "llvm/IR/Intrinsics.h"
215adb3d2aSDavid Blaikie #include <set>
225adb3d2aSDavid Blaikie #include <vector>
235adb3d2aSDavid Blaikie
245adb3d2aSDavid Blaikie using namespace llvm;
255adb3d2aSDavid Blaikie
265adb3d2aSDavid Blaikie /// Goes over OldF calls and replaces them with a call to NewF
replaceFunctionCalls(Function & OldF,Function & NewF,const std::set<int> & ArgIndexesToKeep)275adb3d2aSDavid Blaikie static void replaceFunctionCalls(Function &OldF, Function &NewF,
285adb3d2aSDavid Blaikie const std::set<int> &ArgIndexesToKeep) {
295adb3d2aSDavid Blaikie const auto &Users = OldF.users();
305adb3d2aSDavid Blaikie for (auto I = Users.begin(), E = Users.end(); I != E; )
315adb3d2aSDavid Blaikie if (auto *CI = dyn_cast<CallInst>(*I++)) {
3257fbb9edSFlorian Hahn // Skip uses in call instructions where OldF isn't the called function
3357fbb9edSFlorian Hahn // (e.g. if OldF is an argument of the call).
3457fbb9edSFlorian Hahn if (CI->getCalledFunction() != &OldF)
3557fbb9edSFlorian Hahn continue;
365adb3d2aSDavid Blaikie SmallVector<Value *, 8> Args;
375adb3d2aSDavid Blaikie for (auto ArgI = CI->arg_begin(), E = CI->arg_end(); ArgI != E; ++ArgI)
385adb3d2aSDavid Blaikie if (ArgIndexesToKeep.count(ArgI - CI->arg_begin()))
395adb3d2aSDavid Blaikie Args.push_back(*ArgI);
405adb3d2aSDavid Blaikie
415adb3d2aSDavid Blaikie CallInst *NewCI = CallInst::Create(&NewF, Args);
425adb3d2aSDavid Blaikie NewCI->setCallingConv(NewF.getCallingConv());
435adb3d2aSDavid Blaikie if (!CI->use_empty())
445adb3d2aSDavid Blaikie CI->replaceAllUsesWith(NewCI);
455adb3d2aSDavid Blaikie ReplaceInstWithInst(CI, NewCI);
465adb3d2aSDavid Blaikie }
475adb3d2aSDavid Blaikie }
485adb3d2aSDavid Blaikie
49a2403588SLangston Barrett /// Returns whether or not this function should be considered a candidate for
50a2403588SLangston Barrett /// argument removal. Currently, functions with no arguments and intrinsics are
51a2403588SLangston Barrett /// not considered. Intrinsics aren't considered because their signatures are
52a2403588SLangston Barrett /// fixed.
shouldRemoveArguments(const Function & F)53a2403588SLangston Barrett static bool shouldRemoveArguments(const Function &F) {
54a2403588SLangston Barrett return !F.arg_empty() && !F.isIntrinsic();
55a2403588SLangston Barrett }
56a2403588SLangston Barrett
575adb3d2aSDavid Blaikie /// Removes out-of-chunk arguments from functions, and modifies their calls
585adb3d2aSDavid Blaikie /// accordingly. It also removes allocations of out-of-chunk arguments.
extractArgumentsFromModule(Oracle & O,ReducerWorkItem & WorkItem)5923cc36e4SMatt Arsenault static void extractArgumentsFromModule(Oracle &O, ReducerWorkItem &WorkItem) {
6023cc36e4SMatt Arsenault Module &Program = WorkItem.getModule();
612f161736SDwight Guth std::vector<Argument *> InitArgsToKeep;
625adb3d2aSDavid Blaikie std::vector<Function *> Funcs;
635adb3d2aSDavid Blaikie // Get inside-chunk arguments, as well as their parent function
6477bc3ba3SArthur Eubanks for (auto &F : Program)
65a2403588SLangston Barrett if (shouldRemoveArguments(F)) {
665adb3d2aSDavid Blaikie Funcs.push_back(&F);
675adb3d2aSDavid Blaikie for (auto &A : F.args())
68a39c7ab9SRoman Lebedev if (O.shouldKeep())
692f161736SDwight Guth InitArgsToKeep.push_back(&A);
705adb3d2aSDavid Blaikie }
715adb3d2aSDavid Blaikie
722f161736SDwight Guth // We create a vector first, then convert it to a set, so that we don't have
732f161736SDwight Guth // to pay the cost of rebalancing the set frequently if the order we insert
742f161736SDwight Guth // the elements doesn't match the order they should appear inside the set.
752f161736SDwight Guth std::set<Argument *> ArgsToKeep(InitArgsToKeep.begin(), InitArgsToKeep.end());
762f161736SDwight Guth
775adb3d2aSDavid Blaikie for (auto *F : Funcs) {
785adb3d2aSDavid Blaikie ValueToValueMapTy VMap;
79fbbb6884SRoman Lebedev std::vector<WeakVH> InstToDelete;
805adb3d2aSDavid Blaikie for (auto &A : F->args())
815adb3d2aSDavid Blaikie if (!ArgsToKeep.count(&A)) {
825adb3d2aSDavid Blaikie // By adding undesired arguments to the VMap, CloneFunction will remove
835adb3d2aSDavid Blaikie // them from the resulting Function
842962f9dfSJohn Regehr VMap[&A] = getDefaultValue(A.getType());
855adb3d2aSDavid Blaikie for (auto *U : A.users())
865adb3d2aSDavid Blaikie if (auto *I = dyn_cast<Instruction>(*&U))
875adb3d2aSDavid Blaikie InstToDelete.push_back(I);
885adb3d2aSDavid Blaikie }
89fbbb6884SRoman Lebedev // Delete any (unique) instruction that uses the argument
90fbbb6884SRoman Lebedev for (Value *V : InstToDelete) {
91fbbb6884SRoman Lebedev if (!V)
92fbbb6884SRoman Lebedev continue;
93fbbb6884SRoman Lebedev auto *I = cast<Instruction>(V);
942962f9dfSJohn Regehr I->replaceAllUsesWith(getDefaultValue(I->getType()));
95ce052110SRoman Lebedev if (!I->isTerminator())
965adb3d2aSDavid Blaikie I->eraseFromParent();
975adb3d2aSDavid Blaikie }
985adb3d2aSDavid Blaikie
995adb3d2aSDavid Blaikie // No arguments to reduce
1005adb3d2aSDavid Blaikie if (VMap.empty())
1015adb3d2aSDavid Blaikie continue;
1025adb3d2aSDavid Blaikie
1035adb3d2aSDavid Blaikie std::set<int> ArgIndexesToKeep;
104b9db89fbSJakub Kuderski for (const auto &[Index, Arg] : enumerate(F->args()))
105b9db89fbSJakub Kuderski if (ArgsToKeep.count(&Arg))
106b9db89fbSJakub Kuderski ArgIndexesToKeep.insert(Index);
1075adb3d2aSDavid Blaikie
1085adb3d2aSDavid Blaikie auto *ClonedFunc = CloneFunction(F, VMap);
1095adb3d2aSDavid Blaikie // In order to preserve function order, we move Clone after old Function
1105adb3d2aSDavid Blaikie ClonedFunc->removeFromParent();
11177bc3ba3SArthur Eubanks Program.getFunctionList().insertAfter(F->getIterator(), ClonedFunc);
1125adb3d2aSDavid Blaikie
1135adb3d2aSDavid Blaikie replaceFunctionCalls(*F, *ClonedFunc, ArgIndexesToKeep);
1145adb3d2aSDavid Blaikie // Rename Cloned Function to Old's name
115adcd0268SBenjamin Kramer std::string FName = std::string(F->getName());
116*eed067e9SYoungsuk Kim F->replaceAllUsesWith(ClonedFunc);
1175adb3d2aSDavid Blaikie F->eraseFromParent();
1185adb3d2aSDavid Blaikie ClonedFunc->setName(FName);
1195adb3d2aSDavid Blaikie }
1205adb3d2aSDavid Blaikie }
1215adb3d2aSDavid Blaikie
reduceArgumentsDeltaPass(TestRunner & Test)1225adb3d2aSDavid Blaikie void llvm::reduceArgumentsDeltaPass(TestRunner &Test) {
1232592ccdeSArthur Eubanks runDeltaPass(Test, extractArgumentsFromModule, "Reducing Arguments");
1245adb3d2aSDavid Blaikie }
125