diff --git a/llvm/test/tools/llvm-reduce/run-ir-passes.ll b/llvm/test/tools/llvm-reduce/run-ir-passes.ll new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-reduce/run-ir-passes.ll @@ -0,0 +1,28 @@ +; RUN: llvm-reduce --delta-passes=ir-passes --test FileCheck --test-arg --check-prefixes=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t +; RUN: FileCheck --check-prefixes=CHECK-FINAL --input-file=%t %s + +; CHECK-INTERESTINGNESS-LABEL: @f1 +; CHECK-INTERESTINGNESS: add +; CHECK-INTERESTINGNESS-LABEL: @f2 +; CHECK-INTERESTINGNESS: add +; CHECK-INTERESTINGNESS: add + +; CHECK-FINAL-LABEL: @f1 +; CHECK-FINAL: add i32 %a, 10 +; CHECK-FINAL-LABEL: @f2 +; CHECK-FINAL: add i32 %a, 5 +; CHECK-FINAL: add i32 %b, 5 + +define i32 @f1(i32 %a) { + %b = add i32 %a, 5 + %c = add i32 %b, 5 + ret i32 %c +} + +define i32 @f2(i32 %a) { + %b = add i32 %a, 5 + %c = add i32 %b, 5 + ret i32 %c +} + +declare void @f3() \ No newline at end of file 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 @@ -10,6 +10,7 @@ IRReader MC MIRParser + Passes Support Target TransformUtils @@ -39,6 +40,7 @@ deltas/ReduceOperandsSkip.cpp deltas/ReduceOperandsToArgs.cpp deltas/ReduceInstructionsMIR.cpp + deltas/RunIRPasses.cpp llvm-reduce.cpp DEPENDS diff --git a/llvm/tools/llvm-reduce/DeltaManager.cpp b/llvm/tools/llvm-reduce/DeltaManager.cpp --- a/llvm/tools/llvm-reduce/DeltaManager.cpp +++ b/llvm/tools/llvm-reduce/DeltaManager.cpp @@ -33,6 +33,7 @@ #include "deltas/ReduceOperandsSkip.h" #include "deltas/ReduceOperandsToArgs.h" #include "deltas/ReduceSpecialGlobals.h" +#include "deltas/RunIRPasses.h" #include "llvm/Support/CommandLine.h" using namespace llvm; @@ -49,6 +50,7 @@ DELTA_PASS("aliases", reduceAliasesDeltaPass) \ DELTA_PASS("function-bodies", reduceFunctionBodiesDeltaPass) \ DELTA_PASS("functions", reduceFunctionsDeltaPass) \ + DELTA_PASS("ir-passes", runIRPassesDeltaPass) \ DELTA_PASS("basic-blocks", reduceBasicBlocksDeltaPass) \ DELTA_PASS("global-values", reduceGlobalValuesDeltaPass) \ DELTA_PASS("global-objects", reduceGlobalObjectsDeltaPass) \ diff --git a/llvm/tools/llvm-reduce/deltas/RunIRPasses.h b/llvm/tools/llvm-reduce/deltas/RunIRPasses.h new file mode 100644 --- /dev/null +++ b/llvm/tools/llvm-reduce/deltas/RunIRPasses.h @@ -0,0 +1,18 @@ +//===- RunIRPasses.h ------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_TOOLS_LLVM_REDUCE_DELTAS_RUNPASSES_H +#define LLVM_TOOLS_LLVM_REDUCE_DELTAS_RUNPASSES_H + +#include "Delta.h" + +namespace llvm { +void runIRPassesDeltaPass(TestRunner &Test); +} // namespace llvm + +#endif diff --git a/llvm/tools/llvm-reduce/deltas/RunIRPasses.cpp b/llvm/tools/llvm-reduce/deltas/RunIRPasses.cpp new file mode 100644 --- /dev/null +++ b/llvm/tools/llvm-reduce/deltas/RunIRPasses.cpp @@ -0,0 +1,48 @@ +//===- RunIRPasses.cpp ----------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "RunIRPasses.h" +#include "Delta.h" +#include "llvm/Passes/PassBuilder.h" +#include "llvm/Transforms/InstCombine/InstCombine.h" +#include "llvm/Transforms/Scalar/SimplifyCFG.h" + +using namespace llvm; + +static void runPasses(Oracle &O, Module &Program) { + InstCombinePass IC; + SimplifyCFGPass SCFG; + + LoopAnalysisManager LAM; + FunctionAnalysisManager FAM; + CGSCCAnalysisManager CGAM; + ModuleAnalysisManager MAM; + + PassBuilder PB; + + PB.registerModuleAnalyses(MAM); + PB.registerCGSCCAnalyses(CGAM); + PB.registerFunctionAnalyses(FAM); + PB.registerLoopAnalyses(LAM); + PB.crossRegisterProxies(LAM, FAM, CGAM, MAM); + + for (auto &F : Program) { + if (F.isDeclaration()) + continue; + if (!O.shouldKeep()) + IC.run(F, FAM); + if (!O.shouldKeep()) + SCFG.run(F, FAM); + } +} + +void llvm::runIRPassesDeltaPass(TestRunner &Test) { + errs() << "*** Running passes ...\n"; + runDeltaPass(Test, runPasses); + errs() << "----------------------------\n"; +} diff --git a/llvm/utils/gn/secondary/llvm/tools/llvm-reduce/BUILD.gn b/llvm/utils/gn/secondary/llvm/tools/llvm-reduce/BUILD.gn --- a/llvm/utils/gn/secondary/llvm/tools/llvm-reduce/BUILD.gn +++ b/llvm/utils/gn/secondary/llvm/tools/llvm-reduce/BUILD.gn @@ -3,6 +3,7 @@ "//llvm/lib/CodeGen/MIRParser", "//llvm/lib/IR", "//llvm/lib/IRReader", + "//llvm/lib/Passes", "//llvm/lib/Support", "//llvm/lib/Target", "//llvm/lib/Target:TargetsToBuild", @@ -33,6 +34,7 @@ "deltas/ReduceOperandsSkip.cpp", "deltas/ReduceOperandsToArgs.cpp", "deltas/ReduceSpecialGlobals.cpp", + "deltas/RunIRPasses.cpp", "llvm-reduce.cpp", ] }