Index: llvm/test/tools/llvm-reduce/simplify-instructions.ll =================================================================== --- /dev/null +++ llvm/test/tools/llvm-reduce/simplify-instructions.ll @@ -0,0 +1,17 @@ +; RUN: llvm-reduce -abort-on-invalid-reduction -simplify-mir --delta-passes=simplify-instructions -mtriple=amdgcn-amd-amdhsa -mcpu=gfx900 --test FileCheck --test-arg --check-prefix=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t 2> %t.log +; RUN: FileCheck --check-prefix=RESULT %s < %t + +; CHECK-INTERESTINGNESS: ret + +; RESULT: %add4 = add i32 %arg0, %arg1 +; RESULT: ret i32 %add4 + +define i32 @func(i32 %arg0, i32 %arg1) { +entry: + %add0 = add i32 %arg0, 0 + %add1 = add i32 %add0, 0 + %add2 = add i32 %add1, 0 + %add3 = add i32 %arg1, 0 + %add4 = add i32 %add2, %add3 + ret i32 %add4 +} Index: llvm/tools/llvm-reduce/CMakeLists.txt =================================================================== --- llvm/tools/llvm-reduce/CMakeLists.txt +++ llvm/tools/llvm-reduce/CMakeLists.txt @@ -44,6 +44,7 @@ deltas/ReduceIRReferences.cpp deltas/ReduceVirtualRegisters.cpp deltas/ReduceRegisterUses.cpp + deltas/SimplifyInstructions.cpp llvm-reduce.cpp DEPENDS Index: llvm/tools/llvm-reduce/DeltaManager.cpp =================================================================== --- llvm/tools/llvm-reduce/DeltaManager.cpp +++ llvm/tools/llvm-reduce/DeltaManager.cpp @@ -39,6 +39,7 @@ #include "deltas/ReduceRegisterUses.h" #include "deltas/ReduceSpecialGlobals.h" #include "deltas/ReduceVirtualRegisters.h" +#include "deltas/SimplifyInstructions.h" #include "llvm/Support/CommandLine.h" using namespace llvm; @@ -63,6 +64,7 @@ DELTA_PASS("metadata", reduceMetadataDeltaPass) \ DELTA_PASS("arguments", reduceArgumentsDeltaPass) \ DELTA_PASS("instructions", reduceInstructionsDeltaPass) \ + DELTA_PASS("simplify-instructions", simplifyInstructionsDeltaPass) \ DELTA_PASS("operands-zero", reduceOperandsZeroDeltaPass) \ DELTA_PASS("operands-one", reduceOperandsOneDeltaPass) \ DELTA_PASS("operands-undef", reduceOperandsUndefDeltaPass) \ Index: llvm/tools/llvm-reduce/deltas/SimplifyInstructions.h =================================================================== --- /dev/null +++ llvm/tools/llvm-reduce/deltas/SimplifyInstructions.h @@ -0,0 +1,18 @@ +//===- SimplifyInstructions.h - Specialized Delta Pass ----------*- C++- *-===// +// +// 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_SIMPLIFYINSTRUCTIONS_H +#define LLVM_TOOLS_LLVM_REDUCE_DELTAS_SIMPLIFYINSTRUCTIONS_H + +#include "Delta.h" + +namespace llvm { +void simplifyInstructionsDeltaPass(TestRunner &Test); +} // namespace llvm + +#endif Index: llvm/tools/llvm-reduce/deltas/SimplifyInstructions.cpp =================================================================== --- /dev/null +++ llvm/tools/llvm-reduce/deltas/SimplifyInstructions.cpp @@ -0,0 +1,50 @@ +//===- SimplifyInstructions.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 Instructions from defined functions. +// +//===----------------------------------------------------------------------===// + +#include "SimplifyInstructions.h" +#include "llvm/Analysis/InstructionSimplify.h" +#include "llvm/IR/Constants.h" + +using namespace llvm; + +/// Removes out-of-chunk arguments from functions, and modifies their calls +/// accordingly. It also removes allocations of out-of-chunk arguments. +static void extractInstrFromModule(Oracle &O, Module &Program) { + std::vector InstsToDelete; + + const DataLayout &DL = Program.getDataLayout(); + + std::vector InstToDelete; + for (auto &F : Program) { + for (auto &BB : F) { + for (auto &Inst : BB) { + if (O.shouldKeep()) + continue; + + SimplifyQuery Q(DL, &Inst); + if (Value *Simplified = SimplifyInstruction(&Inst, Q)) { + Inst.replaceAllUsesWith(Simplified); + InstToDelete.push_back(&Inst); + } + } + } + } + + for (Instruction *I : InstToDelete) + I->eraseFromParent(); +} + +void llvm::simplifyInstructionsDeltaPass(TestRunner &Test) { + outs() << "*** Simplifying Instructions...\n"; + runDeltaPass(Test, extractInstrFromModule); +}