aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Arsenault <Matthew.Arsenault@amd.com>2022-06-08 22:09:47 -0400
committerMatt Arsenault <arsenm2@gmail.com>2022-06-16 20:39:27 -0400
commiteea11e7369ca40c4c261a81e95e5ba5ee13fec20 (patch)
treef3f028242627306375d933e7bb93c7cceec2be76
parent6b8bd0f72df8dadc7cb23ccaa4ba3bb257e367a5 (diff)
downloadllvm-eea11e7369ca40c4c261a81e95e5ba5ee13fec20.zip
llvm-eea11e7369ca40c4c261a81e95e5ba5ee13fec20.tar.gz
llvm-eea11e7369ca40c4c261a81e95e5ba5ee13fec20.tar.bz2
llvm-reduce: Add reduction pass to simplify instructions
-rw-r--r--llvm/test/tools/llvm-reduce/simplify-instructions.ll17
-rw-r--r--llvm/tools/llvm-reduce/CMakeLists.txt1
-rw-r--r--llvm/tools/llvm-reduce/DeltaManager.cpp2
-rw-r--r--llvm/tools/llvm-reduce/deltas/SimplifyInstructions.cpp50
-rw-r--r--llvm/tools/llvm-reduce/deltas/SimplifyInstructions.h18
5 files changed, 88 insertions, 0 deletions
diff --git a/llvm/test/tools/llvm-reduce/simplify-instructions.ll b/llvm/test/tools/llvm-reduce/simplify-instructions.ll
new file mode 100644
index 0000000..a9cd54f
--- /dev/null
+++ b/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
+; 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
+}
diff --git a/llvm/tools/llvm-reduce/CMakeLists.txt b/llvm/tools/llvm-reduce/CMakeLists.txt
index dccf147..fc478e6 100644
--- a/llvm/tools/llvm-reduce/CMakeLists.txt
+++ b/llvm/tools/llvm-reduce/CMakeLists.txt
@@ -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
diff --git a/llvm/tools/llvm-reduce/DeltaManager.cpp b/llvm/tools/llvm-reduce/DeltaManager.cpp
index 0d7d1a5..d947de8 100644
--- a/llvm/tools/llvm-reduce/DeltaManager.cpp
+++ b/llvm/tools/llvm-reduce/DeltaManager.cpp
@@ -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) \
diff --git a/llvm/tools/llvm-reduce/deltas/SimplifyInstructions.cpp b/llvm/tools/llvm-reduce/deltas/SimplifyInstructions.cpp
new file mode 100644
index 0000000..6e2e52a
--- /dev/null
+++ b/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 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);
+}
diff --git a/llvm/tools/llvm-reduce/deltas/SimplifyInstructions.h b/llvm/tools/llvm-reduce/deltas/SimplifyInstructions.h
new file mode 100644
index 0000000..215cffc
--- /dev/null
+++ b/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