[llvm] [SimplifyCFG] Don't perform "redirecting phis between unmergeable BBs and successor BB" optimization when not using jump tables. (PR #160560)
Amara Emerson via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 24 09:56:34 PDT 2025
https://github.com/aemerson created https://github.com/llvm/llvm-project/pull/160560
This optimization works well for most cases but implicitly assumes that the
backend will be able to generate jump tables (see "And lookup table optimization
should convert it into add %arg 1." in PR #67275 description).
When -fno-jump-tables is used however this regresses code size, which is
especially painful in targets like armv7 where there's already high register
pressure.
rdar://157858055
>From 20937a5bd2f6034dd9f31e67b459465b37d8e34f Mon Sep 17 00:00:00 2001
From: Amara Emerson <amara at apple.com>
Date: Wed, 24 Sep 2025 09:37:45 -0700
Subject: [PATCH] [SimplifyCFG] Don't perform "redirecting phis between
unmergeable BBs and successor BB" optimization when not using jump tables.
This optimization works well for most cases but implicitly assumes that the
backend will be able to generate jump tables (see "And lookup table optimization
should convert it into add %arg 1." in PR #67275 description).
When -fno-jump-tables is used however this regresses code size, which is
especially painful in targets like armv7 where there's already high register
pressure.
rdar://157858055
---
llvm/lib/Transforms/Utils/Local.cpp | 6 ++++
.../Transforms/SimplifyCFG/branch-fold.ll | 34 +++++++++++++++++++
2 files changed, 40 insertions(+)
diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp
index 123881e276584..1a3d543160137 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -1159,6 +1159,12 @@ bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
bool BBPhisMergeable = BBKillable || CanRedirectPredsOfEmptyBBToSucc(
BB, Succ, BBPreds, CommonPred);
+ // If the function has the "no-jump-tables" attribute, merging phis
+ // can make size worse at -Oz.
+ auto *MF = BB->getParent();
+ if (MF->hasMinSize() && MF->hasFnAttribute("no-jump-tables"))
+ BBPhisMergeable = false;
+
if ((!BBKillable && !BBPhisMergeable) || introduceTooManyPhiEntries(BB, Succ))
return false;
diff --git a/llvm/test/Transforms/SimplifyCFG/branch-fold.ll b/llvm/test/Transforms/SimplifyCFG/branch-fold.ll
index 8e7b91ea172be..2f4c5d9590e0e 100644
--- a/llvm/test/Transforms/SimplifyCFG/branch-fold.ll
+++ b/llvm/test/Transforms/SimplifyCFG/branch-fold.ll
@@ -145,8 +145,42 @@ Succ:
ret i8 %phi2
}
+define i8 @common_pred_no_jt(i8 noundef %arg, i1 %c1, i1 %c2) #0 {
+; CHECK-LABEL: @common_pred_no_jt(
+; CHECK-NEXT: Pred:
+; CHECK-NEXT: call void @dummy()
+; CHECK-NEXT: br i1 [[C1:%.*]], label [[COMMONPRED:%.*]], label [[SUCC:%.*]]
+; CHECK: CommonPred:
+; CHECK-NEXT: call void @dummy()
+; CHECK-NEXT: br i1 [[C2:%.*]], label [[SUCC1:%.*]], label [[SUCC]]
+; CHECK: BB:
+; CHECK-NEXT: [[PHI1:%.*]] = phi i8 [ 0, [[PRED:%.*]] ], [ 1, [[COMMONPRED]] ]
+; CHECK-NEXT: br label [[SUCC1]]
+; CHECK: Succ:
+; CHECK-NEXT: [[PHI2:%.*]] = phi i8 [ [[PHI1]], [[SUCC]] ], [ 4, [[COMMONPRED]] ]
+; CHECK-NEXT: ret i8 [[PHI2]]
+;
+Pred:
+call void @dummy()
+ br i1 %c1, label %CommonPred, label %BB
+
+CommonPred:
+call void @dummy()
+ br i1 %c2, label %Succ, label %BB
+
+BB:
+ %phi1 = phi i8 [ 0, %Pred ], [1, %CommonPred]
+ br label %Succ
+
+Succ:
+ %phi2 = phi i8 [ %phi1, %BB ], [ 4, %CommonPred ]
+ ret i8 %phi2
+}
declare void @dummy()
+attributes #0 = { minsize "no-jump-tables"="true" }
+
+
!0 = !{!"branch_weights", i32 3, i32 7}
!1 = !{!"branch_weights", i32 11, i32 4}
;.
More information about the llvm-commits
mailing list