diff --git a/llvm/test/Reduce/remove-attrs.ll b/llvm/test/Reduce/remove-attrs.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Reduce/remove-attrs.ll @@ -0,0 +1,45 @@ +; Test that llvm-reduce can remove uninteresting function attributes from function definitions as well as their calls +; +; RUN: llvm-reduce --run-only params --test %p/Inputs/interestingness-test.sh --test-arg %lli %s +; RUN: cat reduced.ll | FileCheck %s +; REQUIRES: plugins, shell + +@x = global i32 0, align 4 +@.str = private unnamed_addr constant [4 x i8] c"%i\0A\00", align 1 + +; CHECK: %used +; CHECK-NOT: %unused +define void @func(i32* dereferenceable(4) %used, i32 %unused, i32* dereferenceable(4) %unused2) { +entry: + %used.addr = alloca i32*, align 8 + %unused.addr = alloca i32, align 4 + %unused2.addr = alloca i32*, align 8 + store i32* %used, i32** %used.addr, align 8 + store i32 %unused, i32* %unused.addr, align 4 + store i32* %unused2, i32** %unused2.addr, align 8 + %0 = load i32*, i32** %used.addr, align 8 + store i32 10, i32* %0, align 4 + ret void +} + +define i32 @func2(i32 %unused) { +entry: + %unused.addr = alloca i32, align 4 + store i32 %unused, i32* %unused.addr, align 4 + ret i32 0 +} + +define i32 @main() { +entry: + %retval = alloca i32, align 4 + store i32 0, i32* %retval, align 4 + ; CHECK: call void @func(i32* @x) + call void @func(i32* dereferenceable(4) @x, i32 15, i32* dereferenceable(4) @x) + ; CHECK: call i32 @func2() + %call = call i32 @func2(i32 5) + %0 = load i32, i32* @x, align 4 + %call1 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %0) + ret i32 0 +} + +declare i32 @printf(i8*, ...) diff --git a/llvm/tools/llvm-reduce/CMakeLists.txt b/llvm/tools/llvm-reduce/CMakeLists.txt --- a/llvm/tools/llvm-reduce/CMakeLists.txt +++ b/llvm/tools/llvm-reduce/CMakeLists.txt @@ -19,6 +19,7 @@ deltas/ReduceFunctions.cpp deltas/ReduceGlobalVars.cpp deltas/ReduceMetadata.cpp + deltas/ReduceArguments.cpp DEPENDS intrinsics_gen diff --git a/llvm/tools/llvm-reduce/DeltaManager.h b/llvm/tools/llvm-reduce/DeltaManager.h --- a/llvm/tools/llvm-reduce/DeltaManager.h +++ b/llvm/tools/llvm-reduce/DeltaManager.h @@ -13,6 +13,7 @@ #include "TestRunner.h" #include "deltas/Delta.h" +#include "deltas/ReduceArguments.h" #include "deltas/ReduceFunctions.h" #include "deltas/ReduceGlobalVars.h" #include "deltas/ReduceMetadata.h" @@ -28,6 +29,8 @@ reduceGlobalsDeltaPass(Tester); else if (Pass == "metadata") reduceMetadataDeltaPass(Tester); + else if (Pass == "params") + reduceArgumentsDeltaPass(Tester); // TODO: Implement the remaining Delta Passes } } diff --git a/llvm/tools/llvm-reduce/deltas/ReduceArguments.h b/llvm/tools/llvm-reduce/deltas/ReduceArguments.h new file mode 100644 --- /dev/null +++ b/llvm/tools/llvm-reduce/deltas/ReduceArguments.h @@ -0,0 +1,21 @@ +//===- ReduceArguments.h - 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 +// to reduce uninteresting Arguments from defined functions. +// +//===----------------------------------------------------------------------===// + +#include "Delta.h" +#include "llvm/IR/Argument.h" +#include "llvm/Transforms/Utils/Cloning.h" +#include "llvm/Transforms/Utils/BasicBlockUtils.h" + +namespace llvm { +void reduceArgumentsDeltaPass(TestRunner &Test); +} // namespace llvm diff --git a/llvm/tools/llvm-reduce/deltas/ReduceArguments.cpp b/llvm/tools/llvm-reduce/deltas/ReduceArguments.cpp new file mode 100644 --- /dev/null +++ b/llvm/tools/llvm-reduce/deltas/ReduceArguments.cpp @@ -0,0 +1,140 @@ +//===- 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 +// to reduce uninteresting Arguments from defined functions. +// +//===----------------------------------------------------------------------===// + +#include "ReduceArguments.h" + +/// Goes over OldF calls and replaces them with a call to NewF +static void replaceFunctionCalls(Function *OldF, Function *NewF, + const std::set &ArgIndexesToKeep) { + for (auto U : OldF->users()) + if (auto *CI = dyn_cast(*&U)) { + SmallVector 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); + } +} + +/// Deletes allocation instructions of arguments that are deleted +static void removeUnusedAllocas(Function *F, const std::set &ArgIndexesToKeep) { + std::vector ToDelete; + int AllocaCount = 0; + for (auto &BB : *F) + for (auto &I : BB) + if (auto *AI = dyn_cast(&I)) + if (!ArgIndexesToKeep.count(AllocaCount++)) + ToDelete.push_back(AI); + + for (auto &AI : ToDelete) { + AI->replaceAllUsesWith(UndefValue::get(AI->getType())); + AI->eraseFromParent(); + } +} + +/// Removes out-of-chunk arguments from functions, and modifies their calls +/// accordingly. It also removes allocations of out-of-chunk arguments. +/// @returns the Module stripped of out-of-chunk functions +static std::unique_ptr +extractFunctionsFromModule(std::vector ChunksToKeep, Module *Program) { + std::unique_ptr Clone = CloneModule(*Program); + + int I = 0, ArgCount = 0; + std::set ArgsToKeep; + std::vector Funcs; + // Get inside-chunk arguments, as well as their parent function + for (auto &F : *Clone) + if (!F.isDeclaration()) { + Funcs.push_back(&F); + for (auto &A : F.args()) + if (I < ChunksToKeep.size()) { + if (ChunksToKeep[I].contains(++ArgCount)) + ArgsToKeep.insert(&A); + if (ChunksToKeep[I].end == ArgCount) + ++I; + } + } + + for (auto &F : Funcs) { + ValueToValueMapTy VMap; + std::vector InstToDelete; + 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(*&U)) + InstToDelete.push_back(I); + } + // Delete any instruction that uses the argument + for (auto I : InstToDelete) { + I->replaceAllUsesWith(UndefValue::get(I->getType())); + I->eraseFromParent(); + } + + // No arguments to reduce + if (VMap.empty()) + continue; + + std::set ArgIndexesToKeep; + int ArgI = 0; + for (auto &Arg : F->args()) + if (ArgsToKeep.count(&Arg)) + ArgIndexesToKeep.insert(ArgI++); + + auto *ClonedFunc = CloneFunction(F, VMap); + // In order to preserve function order, we move Clone after old Function + ClonedFunc->removeFromParent(); + Clone->getFunctionList().insertAfter(F->getIterator(), ClonedFunc); + + replaceFunctionCalls(F, ClonedFunc, ArgIndexesToKeep); + removeUnusedAllocas(ClonedFunc, ArgIndexesToKeep); + // Rename Cloned Function to Old's name + auto FName = F->getName(); + F->eraseFromParent(); + ClonedFunc->setName(FName); + } + + return Clone; +} + +/// Counts the amount of arguments in non-declaration functions and prints their +/// respective name, index, and parent function name +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) + if (!F.isDeclaration() && F.arg_size()) { + outs() << " " << F.getName() << "\n"; + for (auto &P : F.args()) + outs() << "\t" << ++ArgsCount << ": " << P.getName() << "\n"; + + outs() << "----------------------------\n"; + } + + return ArgsCount; +} + +void llvm::reduceArgumentsDeltaPass(TestRunner &Test) { + outs() << "*** Reducing Arguments...\n"; + int ArgCount = countArguments(Test.getProgram()); + runDeltaPass(Test, ArgCount, extractFunctionsFromModule); +} \ No newline at end of file