diff --git a/llvm/test/Reduce/remove-alias.ll b/llvm/test/Reduce/remove-alias.ll --- a/llvm/test/Reduce/remove-alias.ll +++ b/llvm/test/Reduce/remove-alias.ll @@ -5,15 +5,12 @@ ; CHECK-INTERESTINGNESS: define void @fn3 -; CHECK-FINAL: $a1 -; CHECK-FINAL: $a2 -; CHECK-FINAL: $a3 -; CHECK-FINAL: $a4 +; CHECK-FINAL-NOT: = alias -; CHECK-FINAL: define void @fn1 -; CHECK-FINAL: define void @fn2 +; CHECK-FINAL-NOT: define void @fn1 +; CHECK-FINAL-NOT: define void @fn2 ; CHECK-FINAL: define void @fn3 -; CHECK-FINAL: define void @fn4 +; CHECK-FINAL-NOT: define void @fn4 @"$a1" = alias void (), void ()* @fn1 @"$a2" = alias void (), void ()* @fn2 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 @@ -13,6 +13,7 @@ add_llvm_tool(llvm-reduce TestRunner.cpp deltas/Delta.cpp + deltas/ReduceAliasAndSpecialGlobals.cpp deltas/ReduceArguments.cpp deltas/ReduceAttributes.cpp deltas/ReduceBasicBlocks.cpp 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/ReduceAliasAndSpecialGlobals.h" #include "deltas/ReduceArguments.h" #include "deltas/ReduceAttributes.h" #include "deltas/ReduceBasicBlocks.h" @@ -27,6 +28,7 @@ // TODO: Add CLI option to run only specified Passes (for unit tests) inline void runDeltaPasses(TestRunner &Tester) { + reduceAliasAndSpecialGlobalsDeltaPass(Tester); reduceFunctionBodiesDeltaPass(Tester); reduceFunctionsDeltaPass(Tester); reduceBasicBlocksDeltaPass(Tester); diff --git a/llvm/tools/llvm-reduce/deltas/ReduceAliasAndSpecialGlobals.h b/llvm/tools/llvm-reduce/deltas/ReduceAliasAndSpecialGlobals.h new file mode 100644 --- /dev/null +++ b/llvm/tools/llvm-reduce/deltas/ReduceAliasAndSpecialGlobals.h @@ -0,0 +1,18 @@ +//===- ReduceAliasAndSpecialGlobals.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 aliases and special globals in the provided Module. + +//===----------------------------------------------------------------------===// + +#include "Delta.h" + +namespace llvm { +void reduceAliasAndSpecialGlobalsDeltaPass(TestRunner &Test); +} // namespace llvm diff --git a/llvm/tools/llvm-reduce/deltas/ReduceAliasAndSpecialGlobals.cpp b/llvm/tools/llvm-reduce/deltas/ReduceAliasAndSpecialGlobals.cpp new file mode 100644 --- /dev/null +++ b/llvm/tools/llvm-reduce/deltas/ReduceAliasAndSpecialGlobals.cpp @@ -0,0 +1,69 @@ +//===- ReduceAliasAndSpecialGlobals.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 aliases and special globals in the provided Module. +// +//===----------------------------------------------------------------------===// + +#include "ReduceAliasAndSpecialGlobals.h" +#include "Delta.h" +#include "llvm/IR/Constants.h" +#include "llvm/IR/GlobalValue.h" + +using namespace llvm; + +static auto SpecialGlobalNames = {"llvm.used", "llvm.compiler.used"}; + +/// Removes all aliases and special globals that aren't inside any of the +/// desired Chunks. +static void +extractAliasAndSpecialGlobalsFromModule(const std::vector &ChunksToKeep, + Module *Program) { + Oracle O(ChunksToKeep); + + for (auto &GA : make_early_inc_range(Program->aliases())) { + if (!O.shouldKeep()) { + GA.replaceAllUsesWith(UndefValue::get(GA.getType())); + GA.eraseFromParent(); + } + } + + for (const char *Name : SpecialGlobalNames) { + if (auto *Used = Program->getNamedGlobal(Name)) { + Used->replaceAllUsesWith(UndefValue::get(Used->getType())); + Used->eraseFromParent(); + } + } +} + +/// Counts the amount of aliases and special globals and prints their +/// respective name & index +static int countAliasAndSpecialGlobals(Module *Program) { + // TODO: Silence index with --quiet flag + errs() << "----------------------------\n"; + errs() << "Aliases And Special Globals Index Reference:\n"; + int Count = 0; + for (auto &GA : Program->aliases()) + errs() << "\t" << ++Count << ": " << GA.getName() << "\n"; + + for (const char *Name : SpecialGlobalNames) { + if (auto *Used = Program->getNamedGlobal(Name)) + errs() << "\t" << ++Count << ": " << Used->getName() << "\n"; + } + + errs() << "----------------------------\n"; + return Count; +} + +void llvm::reduceAliasAndSpecialGlobalsDeltaPass(TestRunner &Test) { + errs() << "*** Reducing Aliases and Special Globals...\n"; + int Functions = countAliasAndSpecialGlobals(Test.getProgram()); + runDeltaPass(Test, Functions, extractAliasAndSpecialGlobalsFromModule); + errs() << "----------------------------\n"; +}