[llvm] [BranchFolding] Add an option to tail merge only bbs without successors (PR #101969)

Vladimir Radosavljevic via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 21 08:19:40 PDT 2024


https://github.com/vladimirradosavljevic updated https://github.com/llvm/llvm-project/pull/101969

>From 70d3c15f1d842ae79ea8dab8f70b88ee8dd27445 Mon Sep 17 00:00:00 2001
From: Vladimir Radosavljevic <vr at matterlabs.dev>
Date: Mon, 5 Aug 2024 14:26:37 +0200
Subject: [PATCH] [BranchFolding] Add a hook to tail merge only bbs without
 successors

This patch adds a new hook `TargetInstrInfo::shouldTailMergeOnlyBBsWithoutSucc`
so targets can override it. This is beneficial for targets where jumps are costly.
---
 llvm/include/llvm/CodeGen/TargetInstrInfo.h | 8 ++++++++
 llvm/lib/CodeGen/BranchFolding.cpp          | 3 +++
 2 files changed, 11 insertions(+)

diff --git a/llvm/include/llvm/CodeGen/TargetInstrInfo.h b/llvm/include/llvm/CodeGen/TargetInstrInfo.h
index 882cadea223695..fa514d7915ce36 100644
--- a/llvm/include/llvm/CodeGen/TargetInstrInfo.h
+++ b/llvm/include/llvm/CodeGen/TargetInstrInfo.h
@@ -2236,6 +2236,14 @@ class TargetInstrInfo : public MCInstrInfo {
     return 3;
   }
 
+  /// Return true if the target prefers to tail merge only basic blocks that
+  /// do not have successors. This is beneficial for targets where jumps
+  /// are costly.
+  virtual bool
+  shouldTailMergeOnlyBBsWithoutSucc(const MachineFunction &MF) const {
+    return false;
+  }
+
   /// Returns the callee operand from the given \p MI.
   virtual const MachineOperand &getCalleeOperand(const MachineInstr &MI) const {
     return MI.getOperand(0);
diff --git a/llvm/lib/CodeGen/BranchFolding.cpp b/llvm/lib/CodeGen/BranchFolding.cpp
index 1dc278586f1178..c886e701341c87 100644
--- a/llvm/lib/CodeGen/BranchFolding.cpp
+++ b/llvm/lib/CodeGen/BranchFolding.cpp
@@ -1035,6 +1035,9 @@ bool BranchFolder::TailMergeBlocks(MachineFunction &MF) {
   if (MergePotentials.size() >= 2)
     MadeChange |= TryTailMergeBlocks(nullptr, nullptr, MinCommonTailLength);
 
+  if (TII->shouldTailMergeOnlyBBsWithoutSucc(MF))
+    return MadeChange;
+
   // Look at blocks (IBB) with multiple predecessors (PBB).
   // We change each predecessor to a canonical form, by
   // (1) temporarily removing any unconditional branch from the predecessor



More information about the llvm-commits mailing list