[llvm] My dce (PR #120468)

via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 18 11:05:16 PST 2024


https://github.com/RashmiSalankar created https://github.com/llvm/llvm-project/pull/120468

None

>From 645debab0879a98f13755f5b531f93345708eb80 Mon Sep 17 00:00:00 2001
From: Rashmi Salankar <rashmi.salankar at gmail.com>
Date: Fri, 13 Dec 2024 01:06:54 +0530
Subject: [PATCH 1/3] Dummy pass works now

---
 .../Transforms/Utils/MyDeadCodeEliminationPass.h   | 14 ++++++++++++++
 llvm/lib/Passes/PassBuilder.cpp                    |  1 +
 llvm/lib/Passes/PassRegistry.def                   |  1 +
 llvm/lib/Transforms/Utils/CMakeLists.txt           |  5 +++++
 .../Transforms/Utils/MyDeadCodeEliminationPass.cpp | 10 ++++++++++
 5 files changed, 31 insertions(+)
 create mode 100644 llvm/include/llvm/Transforms/Utils/MyDeadCodeEliminationPass.h
 create mode 100644 llvm/lib/Transforms/Utils/MyDeadCodeEliminationPass.cpp

diff --git a/llvm/include/llvm/Transforms/Utils/MyDeadCodeEliminationPass.h b/llvm/include/llvm/Transforms/Utils/MyDeadCodeEliminationPass.h
new file mode 100644
index 00000000000000..1642827e6b2e05
--- /dev/null
+++ b/llvm/include/llvm/Transforms/Utils/MyDeadCodeEliminationPass.h
@@ -0,0 +1,14 @@
+#ifndef LLVM_TRANSFORMS_MYDEADCODEELIMINATIONPASS_H
+#define LLVM_TRANSFORMS_MYDEADCODEELIMINATIONPASS_H
+
+#include "llvm/IR/PassManager.h"
+
+namespace llvm {
+
+class MyDeadCodeEliminationPass : public PassInfoMixin<MyDeadCodeEliminationPass> {
+public:
+  PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
+};
+
+} // namespace llvm
+#endif // LLVM_TRANSFORMS_MYDEADCODEELIMINATIONPASS_H
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index 77dea7d06d0900..80809e3ea1b486 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -302,6 +302,7 @@
 #include "llvm/Transforms/Scalar/StructurizeCFG.h"
 #include "llvm/Transforms/Scalar/TailRecursionElimination.h"
 #include "llvm/Transforms/Scalar/WarnMissedTransforms.h"
+#include "llvm/Transforms/Utils/MyDeadCodeEliminationPass.h"
 #include "llvm/Transforms/Utils/AddDiscriminators.h"
 #include "llvm/Transforms/Utils/AssumeBundleBuilder.h"
 #include "llvm/Transforms/Utils/BreakCriticalEdges.h"
diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def
index 772ec5fd10e633..c797955ee969e9 100644
--- a/llvm/lib/Passes/PassRegistry.def
+++ b/llvm/lib/Passes/PassRegistry.def
@@ -328,6 +328,7 @@ FUNCTION_ALIAS_ANALYSIS("tbaa", TypeBasedAA())
 #ifndef FUNCTION_PASS
 #define FUNCTION_PASS(NAME, CREATE_PASS)
 #endif
+FUNCTION_PASS("my-dce", MyDeadCodeEliminationPass())
 FUNCTION_PASS("aa-eval", AAEvaluator())
 FUNCTION_PASS("adce", ADCEPass())
 FUNCTION_PASS("add-discriminators", AddDiscriminatorsPass())
diff --git a/llvm/lib/Transforms/Utils/CMakeLists.txt b/llvm/lib/Transforms/Utils/CMakeLists.txt
index 65bd3080662c4d..221cb31e20dd9a 100644
--- a/llvm/lib/Transforms/Utils/CMakeLists.txt
+++ b/llvm/lib/Transforms/Utils/CMakeLists.txt
@@ -1,4 +1,5 @@
 add_llvm_component_library(LLVMTransformUtils
+  MyDeadCodeEliminationPass.cpp	
   AddDiscriminators.cpp
   AMDGPUEmitPrintf.cpp
   ASanStackFrameLayout.cpp
@@ -93,6 +94,10 @@ add_llvm_component_library(LLVMTransformUtils
   ${LLVM_MAIN_INCLUDE_DIR}/llvm/Transforms
   ${LLVM_MAIN_INCLUDE_DIR}/llvm/Transforms/Utils
 
+  target_include_directories(LLVMMyDeadCodeElimination PRIVATE
+    ${LLVM_MAIN_INCLUDE_DIR}
+  )
+
   DEPENDS
   intrinsics_gen
 
diff --git a/llvm/lib/Transforms/Utils/MyDeadCodeEliminationPass.cpp b/llvm/lib/Transforms/Utils/MyDeadCodeEliminationPass.cpp
new file mode 100644
index 00000000000000..bb35a05a8140f3
--- /dev/null
+++ b/llvm/lib/Transforms/Utils/MyDeadCodeEliminationPass.cpp
@@ -0,0 +1,10 @@
+#include "llvm/Transforms/Utils/MyDeadCodeEliminationPass.h"
+
+using namespace llvm;
+
+PreservedAnalyses MyDeadCodeEliminationPass::run(Function &F,
+                                      FunctionAnalysisManager &AM) {
+  errs() << "I'm here in my Pass" << "\n";
+  return PreservedAnalyses::all();
+/* The PreservedAnalyses return value says that all analyses (e.g. dominator tree) are still valid after this pass since we didn’t modify any functions */
+}

>From a61f476669c59178cf5f2604a68f6718a7dcbc15 Mon Sep 17 00:00:00 2001
From: Rashmi Salankar <rashmi.salankar at gmail.com>
Date: Sun, 15 Dec 2024 03:38:58 +0530
Subject: [PATCH 2/3] Basic implementation of Potential Dead Code intructions
 done.

---
 .../Utils/MyDeadCodeEliminationPass.h         | 12 +++++--
 .../Utils/MyDeadCodeEliminationPass.cpp       | 35 ++++++++++++++++---
 2 files changed, 40 insertions(+), 7 deletions(-)

diff --git a/llvm/include/llvm/Transforms/Utils/MyDeadCodeEliminationPass.h b/llvm/include/llvm/Transforms/Utils/MyDeadCodeEliminationPass.h
index 1642827e6b2e05..3876ee32851322 100644
--- a/llvm/include/llvm/Transforms/Utils/MyDeadCodeEliminationPass.h
+++ b/llvm/include/llvm/Transforms/Utils/MyDeadCodeEliminationPass.h
@@ -1,14 +1,20 @@
-#ifndef LLVM_TRANSFORMS_MYDEADCODEELIMINATIONPASS_H
-#define LLVM_TRANSFORMS_MYDEADCODEELIMINATIONPASS_H
+#ifndef LLVM_TRANSFORMS_UTILS_MYDEADCODEELIMINATIONPASS_H
+#define LLVM_TRANSFORMS_UTILS_MYDEADCODEELIMINATIONPASS_H
 
 #include "llvm/IR/PassManager.h"
+#include "llvm/IR/Function.h"
 
 namespace llvm {
 
 class MyDeadCodeEliminationPass : public PassInfoMixin<MyDeadCodeEliminationPass> {
 public:
   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
+
+private:
+  void analyzeInstructions(Function &F);
 };
 
 } // namespace llvm
-#endif // LLVM_TRANSFORMS_MYDEADCODEELIMINATIONPASS_H
+
+#endif // LLVM_TRANSFORMS_UTILS_MYDEADCODEELIMINATIONPASS_H
+
diff --git a/llvm/lib/Transforms/Utils/MyDeadCodeEliminationPass.cpp b/llvm/lib/Transforms/Utils/MyDeadCodeEliminationPass.cpp
index bb35a05a8140f3..8045989adf8f2b 100644
--- a/llvm/lib/Transforms/Utils/MyDeadCodeEliminationPass.cpp
+++ b/llvm/lib/Transforms/Utils/MyDeadCodeEliminationPass.cpp
@@ -1,10 +1,37 @@
 #include "llvm/Transforms/Utils/MyDeadCodeEliminationPass.h"
+#include "llvm/IR/BasicBlock.h"
+#include "llvm/IR/Instruction.h"
+#include "llvm/IR/Instructions.h"
+#include "llvm/Support/raw_ostream.h"
 
 using namespace llvm;
 
-PreservedAnalyses MyDeadCodeEliminationPass::run(Function &F,
-                                      FunctionAnalysisManager &AM) {
+PreservedAnalyses MyDeadCodeEliminationPass::run(Function &F, FunctionAnalysisManager &AM) {
   errs() << "I'm here in my Pass" << "\n";
-  return PreservedAnalyses::all();
-/* The PreservedAnalyses return value says that all analyses (e.g. dominator tree) are still valid after this pass since we didn’t modify any functions */
+
+  // Call the helper function to analyze instructions
+  analyzeInstructions(F);
+
+  return PreservedAnalyses::all(); // Preserve analyses since the code isn't modified.
 }
+
+void MyDeadCodeEliminationPass::analyzeInstructions(Function &F) {
+  for (BasicBlock &BB : F) {
+    for (Instruction &I : BB) {
+      errs() << "Instruction: " << I << "\n";
+
+      // Check if the instruction is "dead" (not used anywhere)
+      if (I.use_empty()) {
+        errs() << "Potential dead instruction: " << I << "\n";
+      }
+
+      // Count instruction types
+      if (isa<LoadInst>(&I)) {
+        errs() << "Load Instruction\n";
+      } else if (isa<StoreInst>(&I)) {
+        errs() << "Store Instruction\n";
+      }
+    }
+  }
+}
+

>From 9a6d64525443d1383a22670e7825a0a33dc87da8 Mon Sep 17 00:00:00 2001
From: Rashmi Salankar <rashmi.salankar at gmail.com>
Date: Sun, 15 Dec 2024 03:58:07 +0530
Subject: [PATCH 3/3] Implemented Iterative Solution for probable dead
 statements

---
 .../Utils/MyDeadCodeEliminationPass.h         |  9 +++-
 .../Utils/MyDeadCodeEliminationPass.cpp       | 54 +++++++++++++------
 2 files changed, 46 insertions(+), 17 deletions(-)

diff --git a/llvm/include/llvm/Transforms/Utils/MyDeadCodeEliminationPass.h b/llvm/include/llvm/Transforms/Utils/MyDeadCodeEliminationPass.h
index 3876ee32851322..24187cb4436b76 100644
--- a/llvm/include/llvm/Transforms/Utils/MyDeadCodeEliminationPass.h
+++ b/llvm/include/llvm/Transforms/Utils/MyDeadCodeEliminationPass.h
@@ -3,15 +3,22 @@
 
 #include "llvm/IR/PassManager.h"
 #include "llvm/IR/Function.h"
+#include "llvm/IR/Instruction.h"
+#include <unordered_set>
 
 namespace llvm {
 
 class MyDeadCodeEliminationPass : public PassInfoMixin<MyDeadCodeEliminationPass> {
 public:
+  // Main run method for the pass
   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
 
 private:
-  void analyzeInstructions(Function &F);
+  // Helper function for iterative analysis
+  void analyzeInstructionsIteratively(Function &F);
+
+  // Helper function to check if an instruction is dead
+  bool isInstructionDead(Instruction *Inst, const std::unordered_set<Instruction *> &potentialDeadInstructions);
 };
 
 } // namespace llvm
diff --git a/llvm/lib/Transforms/Utils/MyDeadCodeEliminationPass.cpp b/llvm/lib/Transforms/Utils/MyDeadCodeEliminationPass.cpp
index 8045989adf8f2b..f906341acd3a5f 100644
--- a/llvm/lib/Transforms/Utils/MyDeadCodeEliminationPass.cpp
+++ b/llvm/lib/Transforms/Utils/MyDeadCodeEliminationPass.cpp
@@ -3,35 +3,57 @@
 #include "llvm/IR/Instruction.h"
 #include "llvm/IR/Instructions.h"
 #include "llvm/Support/raw_ostream.h"
+#include <unordered_set>
 
 using namespace llvm;
 
 PreservedAnalyses MyDeadCodeEliminationPass::run(Function &F, FunctionAnalysisManager &AM) {
   errs() << "I'm here in my Pass" << "\n";
 
-  // Call the helper function to analyze instructions
-  analyzeInstructions(F);
+  // Call the helper function to iteratively analyze instructions
+  analyzeInstructionsIteratively(F);
 
   return PreservedAnalyses::all(); // Preserve analyses since the code isn't modified.
 }
 
-void MyDeadCodeEliminationPass::analyzeInstructions(Function &F) {
-  for (BasicBlock &BB : F) {
-    for (Instruction &I : BB) {
-      errs() << "Instruction: " << I << "\n";
-
-      // Check if the instruction is "dead" (not used anywhere)
-      if (I.use_empty()) {
-        errs() << "Potential dead instruction: " << I << "\n";
+void MyDeadCodeEliminationPass::analyzeInstructionsIteratively(Function &F) {
+  std::unordered_set<Instruction *> potentialDeadInstructions; // To track potential dead instructions
+  bool foundNewDead; // Flag to track if we find new dead instructions in an iteration
+
+  do {
+    foundNewDead = false; // Reset the flag at the start of each iteration
+
+    for (BasicBlock &BB : F) {
+      for (Instruction &I : BB) {
+        // Skip if already identified as dead
+        if (potentialDeadInstructions.count(&I)) {
+          continue;
+        }
+
+        // Check if the instruction is "dead" (not used anywhere or used only by dead instructions)
+        if (isInstructionDead(&I, potentialDeadInstructions)) {
+          errs() << "Potential dead instruction: " << I << "\n";
+          potentialDeadInstructions.insert(&I);
+          foundNewDead = true; // Mark that we found a new dead instruction
+        }
       }
+    }
+  } while (foundNewDead); // Continue until no new dead instructions are found
+}
 
-      // Count instruction types
-      if (isa<LoadInst>(&I)) {
-        errs() << "Load Instruction\n";
-      } else if (isa<StoreInst>(&I)) {
-        errs() << "Store Instruction\n";
-      }
+bool MyDeadCodeEliminationPass::isInstructionDead(Instruction *Inst, const std::unordered_set<Instruction *> &potentialDeadInstructions) {
+  // Check if the instruction's result is not used, or all users are in the potentialDeadInstructions set
+  if (Inst->use_empty()) {
+    return true; // No users, definitely dead
+  }
+
+  for (const Use &U : Inst->uses()) {
+    auto *User = dyn_cast<Instruction>(U.getUser());
+    if (!User || potentialDeadInstructions.find(User) == potentialDeadInstructions.end()) {
+      return false; // Found a user that is not "dead"
     }
   }
+
+  return true; // All users are "dead"
 }
 



More information about the llvm-commits mailing list