[llvm] [Mips][BranchFolding] Enable safe tail merging (PR #225382)

Jiaxun Yang via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 22 06:11:10 PDT 2026


================
@@ -57,6 +59,23 @@ const MipsInstrInfo *MipsInstrInfo::create(MipsSubtarget &STI) {
   return createMipsSEInstrInfo(STI);
 }
 
+bool MipsInstrInfo::isLegalToSplitMBBAt(
+    MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI) const {
+  // Keep tail merging disabled for MIPS16, whose far branches clobber $ra.
+  if (Subtarget.inMips16Mode())
+    return false;
+
+  // Long branch expansion clobbers $at. Do not let tail merging introduce a
----------------
FlyGoat wrote:

I just took a closer look and unfortunately, it's not trival to make `BranchFolding` respect liveness.

The tricky thing is to inspect the new branch before changing the block. Backends can can rewrite existing terminators in `insertBranch()`, i.e.  `insertBranch()` is not always reversible, so we can't simply inserting and removing a trial branch when it isn’t safe.

https://github.com/llvm/llvm-project/pull/225382


More information about the llvm-commits mailing list