[llvm] eea11e7 - llvm-reduce: Add reduction pass to simplify instructions

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 16 17:40:08 PDT 2022


Author: Matt Arsenault
Date: 2022-06-16T20:39:27-04:00
New Revision: eea11e7369ca40c4c261a81e95e5ba5ee13fec20

URL: https://github.com/llvm/llvm-project/commit/eea11e7369ca40c4c261a81e95e5ba5ee13fec20
DIFF: https://github.com/llvm/llvm-project/commit/eea11e7369ca40c4c261a81e95e5ba5ee13fec20.diff

LOG: llvm-reduce: Add reduction pass to simplify instructions

Added: 
    llvm/test/tools/llvm-reduce/simplify-instructions.ll
    llvm/tools/llvm-reduce/deltas/SimplifyInstructions.cpp
    llvm/tools/llvm-reduce/deltas/SimplifyInstructions.h

Modified: 
    llvm/tools/llvm-reduce/CMakeLists.txt
    llvm/tools/llvm-reduce/DeltaManager.cpp

Removed: 
    


################################################################################
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 0000000000000..a9cd54f809bd6
--- /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 dccf147f168b3..fc478e60a5352 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 0d7d1a527171d..d947de88fdfc5 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 0000000000000..6e2e52aee9c1e
--- /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 0000000000000..215cffcd4d12e
--- /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


        


More information about the llvm-commits mailing list