llvm-reduce: Add reduction pass to simplify instructions

This commit is contained in:
Matt Arsenault 2022-06-08 22:09:47 -04:00 committed by Matt Arsenault
parent 6b8bd0f72d
commit eea11e7369
5 changed files with 88 additions and 0 deletions

View File

@ -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
; 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
}

View File

@ -43,6 +43,7 @@ add_llvm_tool(llvm-reduce
deltas/ReduceIRReferences.cpp
deltas/ReduceVirtualRegisters.cpp
deltas/ReduceRegisterUses.cpp
deltas/SimplifyInstructions.cpp
llvm-reduce.cpp
DEPENDS

View File

@ -38,6 +38,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;
@ -62,6 +63,7 @@ static cl::opt<std::string>
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) \

View File

@ -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 simplify Instructions in defined functions.
//
//===----------------------------------------------------------------------===//
#include "SimplifyInstructions.h"
#include "llvm/Analysis/InstructionSimplify.h"
#include "llvm/IR/Constants.h"
using namespace llvm;
/// Calls simplifyInstruction in each instruction in functions, and replaces
/// their values.
static void extractInstrFromModule(Oracle &O, Module &Program) {
std::vector<Instruction *> InstsToDelete;
const DataLayout &DL = Program.getDataLayout();
std::vector<Instruction *> 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);
}

View File

@ -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