Skip to content

Commit 20937a5

Browse files
committed
[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
1 parent 8df194f commit 20937a5

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

llvm/lib/Transforms/Utils/Local.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,12 @@ bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
11591159
bool BBPhisMergeable = BBKillable || CanRedirectPredsOfEmptyBBToSucc(
11601160
BB, Succ, BBPreds, CommonPred);
11611161

1162+
// If the function has the "no-jump-tables" attribute, merging phis
1163+
// can make size worse at -Oz.
1164+
auto *MF = BB->getParent();
1165+
if (MF->hasMinSize() && MF->hasFnAttribute("no-jump-tables"))
1166+
BBPhisMergeable = false;
1167+
11621168
if ((!BBKillable && !BBPhisMergeable) || introduceTooManyPhiEntries(BB, Succ))
11631169
return false;
11641170

llvm/test/Transforms/SimplifyCFG/branch-fold.ll

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,42 @@ Succ:
145145
ret i8 %phi2
146146
}
147147

148+
define i8 @common_pred_no_jt(i8 noundef %arg, i1 %c1, i1 %c2) #0 {
149+
; CHECK-LABEL: @common_pred_no_jt(
150+
; CHECK-NEXT: Pred:
151+
; CHECK-NEXT: call void @dummy()
152+
; CHECK-NEXT: br i1 [[C1:%.*]], label [[COMMONPRED:%.*]], label [[SUCC:%.*]]
153+
; CHECK: CommonPred:
154+
; CHECK-NEXT: call void @dummy()
155+
; CHECK-NEXT: br i1 [[C2:%.*]], label [[SUCC1:%.*]], label [[SUCC]]
156+
; CHECK: BB:
157+
; CHECK-NEXT: [[PHI1:%.*]] = phi i8 [ 0, [[PRED:%.*]] ], [ 1, [[COMMONPRED]] ]
158+
; CHECK-NEXT: br label [[SUCC1]]
159+
; CHECK: Succ:
160+
; CHECK-NEXT: [[PHI2:%.*]] = phi i8 [ [[PHI1]], [[SUCC]] ], [ 4, [[COMMONPRED]] ]
161+
; CHECK-NEXT: ret i8 [[PHI2]]
162+
;
163+
Pred:
164+
call void @dummy()
165+
br i1 %c1, label %CommonPred, label %BB
166+
167+
CommonPred:
168+
call void @dummy()
169+
br i1 %c2, label %Succ, label %BB
170+
171+
BB:
172+
%phi1 = phi i8 [ 0, %Pred ], [1, %CommonPred]
173+
br label %Succ
174+
175+
Succ:
176+
%phi2 = phi i8 [ %phi1, %BB ], [ 4, %CommonPred ]
177+
ret i8 %phi2
178+
}
148179
declare void @dummy()
149180

181+
attributes #0 = { minsize "no-jump-tables"="true" }
182+
183+
150184
!0 = !{!"branch_weights", i32 3, i32 7}
151185
!1 = !{!"branch_weights", i32 11, i32 4}
152186
;.

0 commit comments

Comments
 (0)