[llvm] machinebackend pass test (PR #122487)

Kun Liu via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 10 15:01:51 PST 2025


https://github.com/Ryan-hub-bit updated https://github.com/llvm/llvm-project/pull/122487

>From 9e95cde15ce30630e7118fe046b6dff2d7adf7d9 Mon Sep 17 00:00:00 2001
From: Liu <kliu14 at tulane.edu>
Date: Fri, 10 Jan 2025 10:21:22 -0600
Subject: [PATCH 1/2] machinebackend pass test

---
 llvm/lib/Target/X86/X86MatchJumptablePass.cpp | 44 +++++++++++++++++++
 llvm/lib/Target/X86/X86MatchJumptablePass.h   |  0
 2 files changed, 44 insertions(+)
 create mode 100644 llvm/lib/Target/X86/X86MatchJumptablePass.cpp
 create mode 100644 llvm/lib/Target/X86/X86MatchJumptablePass.h

diff --git a/llvm/lib/Target/X86/X86MatchJumptablePass.cpp b/llvm/lib/Target/X86/X86MatchJumptablePass.cpp
new file mode 100644
index 00000000000000..bfb6b1b7f89f18
--- /dev/null
+++ b/llvm/lib/Target/X86/X86MatchJumptablePass.cpp
@@ -0,0 +1,44 @@
+#include "X86.h"
+#include "llvm/CodeGen/MachineFunctionPass.h"
+#include "llvm/CodeGen/MachineInstr.h"
+#include "llvm/CodeGen/MachineRegisterInfo.h"
+#include "llvm/Support/Debug.h"
+#include "llvm/Support/raw_ostream.h"
+
+
+using namespace llvm;
+
+namespace {
+  class X86MatchJumptablePass : public MachineFunctionPass {
+  public:
+    static char ID;
+
+    X86MatchJumptablePass() : MachineFunctionPass(ID) {}
+
+    bool runOnMachineFunction(MachineFunction &MF) override {
+      LLVM_DEBUG(dbgs() << "Running X86MyBackendPass on function: "
+                        << MF.getName() << "\n");
+
+      // Example: Iterate through instructions
+      for (auto &MBB : MF) {
+        for (auto &MI : MBB) {
+          // Process instructions here
+          LLVM_DEBUG(dbgs() << "Instruction: " << MI << "\n");
+        }
+      }
+
+      return false; // Return true if the pass modifies the function
+    }
+
+    StringRef getPassName() const override {
+      return "X86 My Backend Pass";
+    }
+  };
+}
+
+char X86MatchJumptablePass::ID = 0;
+
+// Register the pass
+FunctionPass *llvm::createX86MatchJumptablePass() {
+  return new X86MatchJumptablePass();
+}
diff --git a/llvm/lib/Target/X86/X86MatchJumptablePass.h b/llvm/lib/Target/X86/X86MatchJumptablePass.h
new file mode 100644
index 00000000000000..e69de29bb2d1d6

>From 27bee8dbc8fa03e331302e799429b7450c2b1166 Mon Sep 17 00:00:00 2001
From: ryan <liu864579887 at gmail.com>
Date: Fri, 10 Jan 2025 17:01:39 -0600
Subject: [PATCH 2/2] good

---
 .../llvm/Transforms/IPO/JumpTableFinder.h     | 62 +++++++++++++++++++
 llvm/lib/Target/X86/CMakeLists.txt            |  1 +
 llvm/lib/Target/X86/X86MatchJumptablePass.cpp | 15 +++--
 llvm/lib/Target/X86/X86MatchJumptablePass.h   | 14 +++++
 llvm/lib/Target/X86/X86TargetMachine.cpp      |  3 +-
 5 files changed, 90 insertions(+), 5 deletions(-)
 create mode 100644 llvm/include/llvm/Transforms/IPO/JumpTableFinder.h

diff --git a/llvm/include/llvm/Transforms/IPO/JumpTableFinder.h b/llvm/include/llvm/Transforms/IPO/JumpTableFinder.h
new file mode 100644
index 00000000000000..9d2fb3b405f8c8
--- /dev/null
+++ b/llvm/include/llvm/Transforms/IPO/JumpTableFinder.h
@@ -0,0 +1,62 @@
+// #ifndef LLVM_ANALYSIS_JUMPTABLEFINDERPASS_H
+// #define LLVM_ANALYSIS_JUMPTABLEFINDERPASS_H
+
+// #include "llvm/IR/PassManager.h"
+// #include "llvm/IR/Module.h"
+// #include "llvm/Support/raw_ostream.h"
+// #include <set>
+
+// namespace llvm {
+
+// class JumptableFinderPass : public PassInfoMixin<JumptableFinderPass> {
+// public:
+//     /// Main entry point for the pass. Analyzes the module to find and analyze jump tables.
+//     PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
+//     /// Implementation of the jump table finder.
+//     void jumptableFinderImpl(Module &M);
+
+//     /// Analyze a SwitchInst for potential jump table patterns.
+//     void findJumpTableFromSwitch(SwitchInst *SI);
+
+//     /// Analyze a GetElementPtrInst for jump table patterns.
+//     void analyzeJumpTable(GetElementPtrInst *GEP);
+
+//     /// Analyze the index computation of a jump table.
+//     void analyzeIndex(Value *Index);
+
+//     /// Find all potential targets for a jump table.
+//     void findTargets(GetElementPtrInst *GEP, std::set<BasicBlock*> &Targets);
+
+//     /// Check the density of a SwitchInst's cases to determine if it forms a jump table.
+//     bool checkDensity(SwitchInst *SI);
+
+//     /// Check if a GetElementPtrInst leads to an indirect branch.
+//     bool leadsToIndirectBranch(GetElementPtrInst *GEP);
+// };
+
+// } // namespace llvm
+
+// #endif // LLVM_ANALYSIS_JUMPTABLEFINDERPASS_H
+
+#ifndef LLVM_TRANSFORMS_IPO_JUMPTABLEFINDER_H
+#define LLVM_TRANSFORMS_IPO_JUMPTABLEFINDER_H
+
+#include "llvm/IR/Module.h"
+#include "llvm/IR/PassManager.h" // For PassInfoMixin and PreservedAnalyses
+#include "llvm/IR/Function.h"
+#include "llvm/IR/Instructions.h"
+#include "llvm/IR/Constants.h"
+#include "llvm/Support/raw_ostream.h"
+#include <set>
+
+namespace llvm {
+
+class JumptableFinderPass : public PassInfoMixin<JumptableFinderPass> {
+public:
+    // Entry point for the pass
+    PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
+};
+
+} // namespace llvm
+
+#endif // LLVM_TRANSFORMS_IPO_JUMPTABLEFINDER_H
diff --git a/llvm/lib/Target/X86/CMakeLists.txt b/llvm/lib/Target/X86/CMakeLists.txt
index 9553a8619feb51..4be4576a6a9057 100644
--- a/llvm/lib/Target/X86/CMakeLists.txt
+++ b/llvm/lib/Target/X86/CMakeLists.txt
@@ -23,6 +23,7 @@ tablegen(LLVM X86GenFoldTables.inc -gen-x86-fold-tables -asmwriternum=1)
 add_public_tablegen_target(X86CommonTableGen)
 
 set(sources
+X86MatchJumptablePass.cpp
   X86ArgumentStackSlotRebase.cpp
   X86AsmPrinter.cpp
   X86AvoidTrailingCall.cpp
diff --git a/llvm/lib/Target/X86/X86MatchJumptablePass.cpp b/llvm/lib/Target/X86/X86MatchJumptablePass.cpp
index bfb6b1b7f89f18..c3683dacbdf8fe 100644
--- a/llvm/lib/Target/X86/X86MatchJumptablePass.cpp
+++ b/llvm/lib/Target/X86/X86MatchJumptablePass.cpp
@@ -4,7 +4,9 @@
 #include "llvm/CodeGen/MachineRegisterInfo.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/raw_ostream.h"
+#include "X86MatchJumptablePass.h"
 
+#define DEBUG_TYPE "x86-my-pass"
 
 using namespace llvm;
 
@@ -38,7 +40,12 @@ namespace {
 
 char X86MatchJumptablePass::ID = 0;
 
-// Register the pass
-FunctionPass *llvm::createX86MatchJumptablePass() {
-  return new X86MatchJumptablePass();
-}
+// Ensure the function is in the llvm namespace
+namespace llvm {
+  
+  // Define the pass
+  FunctionPass *createX86MatchJumptablePass() {
+    return new X86MatchJumptablePass();
+  }
+
+} // end llvm namespace
diff --git a/llvm/lib/Target/X86/X86MatchJumptablePass.h b/llvm/lib/Target/X86/X86MatchJumptablePass.h
index e69de29bb2d1d6..67233bf729aec6 100644
--- a/llvm/lib/Target/X86/X86MatchJumptablePass.h
+++ b/llvm/lib/Target/X86/X86MatchJumptablePass.h
@@ -0,0 +1,14 @@
+#ifndef LLVM_LIB_TARGET_X86_X86MATCHJUMPTABLEPASS_H
+#define LLVM_LIB_TARGET_X86_X86MATCHJUMPTABLEPASS_H
+
+
+#pragma once
+#include "llvm/CodeGen/MachineFunctionPass.h"
+
+namespace llvm {
+
+  FunctionPass *createX86MatchJumptablePass();
+
+} // end namespace llvm
+
+#endif // LLVM_LIB_TARGET_X86_X86MATCHJUMPTABLEPASS_H
\ No newline at end of file
diff --git a/llvm/lib/Target/X86/X86TargetMachine.cpp b/llvm/lib/Target/X86/X86TargetMachine.cpp
index 20dfdd27b33df6..b9c13fda07dfc7 100644
--- a/llvm/lib/Target/X86/X86TargetMachine.cpp
+++ b/llvm/lib/Target/X86/X86TargetMachine.cpp
@@ -12,6 +12,7 @@
 
 #include "X86TargetMachine.h"
 #include "MCTargetDesc/X86MCTargetDesc.h"
+#include "X86MatchJumptablePass.h"  // Include the header with the function declaration
 #include "TargetInfo/X86TargetInfo.h"
 #include "X86.h"
 #include "X86MachineFunctionInfo.h"
@@ -464,7 +465,7 @@ MachineFunctionInfo *X86TargetMachine::createMachineFunctionInfo(
 
 void X86PassConfig::addIRPasses() {
   addPass(createAtomicExpandLegacyPass());
-
+  addPass(llvm::createX86MatchJumptablePass());
   // We add both pass anyway and when these two passes run, we skip the pass
   // based on the option level and option attribute.
   addPass(createX86LowerAMXIntrinsicsPass());



More information about the llvm-commits mailing list