2019-09-12 03:20:48 +02:00
|
|
|
//===- ReduceArguments.cpp - Specialized Delta Pass -----------------------===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements a function which calls the Generic Delta pass in order
|
2021-06-21 21:34:10 +02:00
|
|
|
// to reduce uninteresting Arguments from declared and defined functions.
|
2019-09-12 03:20:48 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ReduceArguments.h"
|
|
|
|
#include "Delta.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2021-06-21 21:34:10 +02:00
|
|
|
#include "llvm/IR/Intrinsics.h"
|
2019-09-12 03:20:48 +02:00
|
|
|
#include <set>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
/// Goes over OldF calls and replaces them with a call to NewF
|
|
|
|
static void replaceFunctionCalls(Function &OldF, Function &NewF,
|
|
|
|
const std::set<int> &ArgIndexesToKeep) {
|
|
|
|
const auto &Users = OldF.users();
|
|
|
|
for (auto I = Users.begin(), E = Users.end(); I != E; )
|
|
|
|
if (auto *CI = dyn_cast<CallInst>(*I++)) {
|
|
|
|
SmallVector<Value *, 8> Args;
|
|
|
|
for (auto ArgI = CI->arg_begin(), E = CI->arg_end(); ArgI != E; ++ArgI)
|
|
|
|
if (ArgIndexesToKeep.count(ArgI - CI->arg_begin()))
|
|
|
|
Args.push_back(*ArgI);
|
|
|
|
|
|
|
|
CallInst *NewCI = CallInst::Create(&NewF, Args);
|
|
|
|
NewCI->setCallingConv(NewF.getCallingConv());
|
|
|
|
if (!CI->use_empty())
|
|
|
|
CI->replaceAllUsesWith(NewCI);
|
|
|
|
ReplaceInstWithInst(CI, NewCI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 21:34:10 +02:00
|
|
|
/// Returns whether or not this function should be considered a candidate for
|
|
|
|
/// argument removal. Currently, functions with no arguments and intrinsics are
|
|
|
|
/// not considered. Intrinsics aren't considered because their signatures are
|
|
|
|
/// fixed.
|
|
|
|
static bool shouldRemoveArguments(const Function &F) {
|
|
|
|
return !F.arg_empty() && !F.isIntrinsic();
|
|
|
|
}
|
|
|
|
|
2019-09-12 03:20:48 +02:00
|
|
|
/// Removes out-of-chunk arguments from functions, and modifies their calls
|
|
|
|
/// accordingly. It also removes allocations of out-of-chunk arguments.
|
|
|
|
static void extractArgumentsFromModule(std::vector<Chunk> ChunksToKeep,
|
|
|
|
Module *Program) {
|
2020-07-08 11:05:15 +02:00
|
|
|
Oracle O(ChunksToKeep);
|
|
|
|
|
2019-09-12 03:20:48 +02:00
|
|
|
std::set<Argument *> ArgsToKeep;
|
|
|
|
std::vector<Function *> Funcs;
|
|
|
|
// Get inside-chunk arguments, as well as their parent function
|
|
|
|
for (auto &F : *Program)
|
2021-06-21 21:34:10 +02:00
|
|
|
if (shouldRemoveArguments(F)) {
|
2019-09-12 03:20:48 +02:00
|
|
|
Funcs.push_back(&F);
|
|
|
|
for (auto &A : F.args())
|
2020-07-08 11:05:15 +02:00
|
|
|
if (O.shouldKeep())
|
|
|
|
ArgsToKeep.insert(&A);
|
2019-09-12 03:20:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto *F : Funcs) {
|
|
|
|
ValueToValueMapTy VMap;
|
2020-07-04 23:48:05 +02:00
|
|
|
std::vector<WeakVH> InstToDelete;
|
2019-09-12 03:20:48 +02:00
|
|
|
for (auto &A : F->args())
|
|
|
|
if (!ArgsToKeep.count(&A)) {
|
|
|
|
// By adding undesired arguments to the VMap, CloneFunction will remove
|
|
|
|
// them from the resulting Function
|
|
|
|
VMap[&A] = UndefValue::get(A.getType());
|
|
|
|
for (auto *U : A.users())
|
|
|
|
if (auto *I = dyn_cast<Instruction>(*&U))
|
|
|
|
InstToDelete.push_back(I);
|
|
|
|
}
|
2020-07-04 23:48:05 +02:00
|
|
|
// Delete any (unique) instruction that uses the argument
|
|
|
|
for (Value *V : InstToDelete) {
|
|
|
|
if (!V)
|
|
|
|
continue;
|
|
|
|
auto *I = cast<Instruction>(V);
|
2019-09-12 03:20:48 +02:00
|
|
|
I->replaceAllUsesWith(UndefValue::get(I->getType()));
|
2020-07-20 21:25:15 +02:00
|
|
|
if (!I->isTerminator())
|
|
|
|
I->eraseFromParent();
|
2019-09-12 03:20:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// No arguments to reduce
|
|
|
|
if (VMap.empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
std::set<int> ArgIndexesToKeep;
|
2020-07-19 18:24:57 +02:00
|
|
|
for (auto &Arg : enumerate(F->args()))
|
|
|
|
if (ArgsToKeep.count(&Arg.value()))
|
|
|
|
ArgIndexesToKeep.insert(Arg.index());
|
2019-09-12 03:20:48 +02:00
|
|
|
|
|
|
|
auto *ClonedFunc = CloneFunction(F, VMap);
|
|
|
|
// In order to preserve function order, we move Clone after old Function
|
|
|
|
ClonedFunc->removeFromParent();
|
|
|
|
Program->getFunctionList().insertAfter(F->getIterator(), ClonedFunc);
|
|
|
|
|
|
|
|
replaceFunctionCalls(*F, *ClonedFunc, ArgIndexesToKeep);
|
|
|
|
// Rename Cloned Function to Old's name
|
2020-01-28 20:23:46 +01:00
|
|
|
std::string FName = std::string(F->getName());
|
2020-07-27 14:36:07 +02:00
|
|
|
F->replaceAllUsesWith(ConstantExpr::getBitCast(ClonedFunc, F->getType()));
|
2019-09-12 03:20:48 +02:00
|
|
|
F->eraseFromParent();
|
|
|
|
ClonedFunc->setName(FName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 21:34:10 +02:00
|
|
|
/// Counts the amount of arguments in functions and prints their respective
|
|
|
|
/// name, index, and parent function name
|
2019-09-12 03:20:48 +02:00
|
|
|
static int countArguments(Module *Program) {
|
|
|
|
// TODO: Silence index with --quiet flag
|
|
|
|
outs() << "----------------------------\n";
|
|
|
|
outs() << "Param Index Reference:\n";
|
|
|
|
int ArgsCount = 0;
|
|
|
|
for (auto &F : *Program)
|
2021-06-21 21:34:10 +02:00
|
|
|
if (shouldRemoveArguments(F)) {
|
2019-09-12 03:20:48 +02:00
|
|
|
outs() << " " << F.getName() << "\n";
|
|
|
|
for (auto &A : F.args())
|
|
|
|
outs() << "\t" << ++ArgsCount << ": " << A.getName() << "\n";
|
|
|
|
|
|
|
|
outs() << "----------------------------\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
return ArgsCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
void llvm::reduceArgumentsDeltaPass(TestRunner &Test) {
|
|
|
|
outs() << "*** Reducing Arguments...\n";
|
2019-09-19 00:30:25 +02:00
|
|
|
int ArgCount = countArguments(Test.getProgram());
|
2019-09-12 03:20:48 +02:00
|
|
|
runDeltaPass(Test, ArgCount, extractArgumentsFromModule);
|
|
|
|
}
|