Skip to content

Commit 39bb790

Browse files
authored
[SimplifyCFG] switch: Do Not Transform the Default Case if the Condition is Too Wide (#77831)
#76669 taught SimplifyCFG to handle switches when `default` has only one case. When the `switch`'s condition is wider than 64 bit, the current implementation can calculate the wrong default value. This PR skips cases where the condition is too wide.
1 parent b32001a commit 39bb790

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -5623,6 +5623,11 @@ static bool eliminateDeadSwitchCases(SwitchInst *SI, DomTreeUpdater *DTU,
56235623
// optimization, such as lookup tables.
56245624
if (SI->getNumCases() == AllNumCases - 1) {
56255625
assert(NumUnknownBits > 1 && "Should be canonicalized to a branch");
5626+
IntegerType *CondTy = cast<IntegerType>(Cond->getType());
5627+
if (CondTy->getIntegerBitWidth() > 64 ||
5628+
!DL.fitsInLegalInteger(CondTy->getIntegerBitWidth()))
5629+
return false;
5630+
56265631
uint64_t MissingCaseVal = 0;
56275632
for (const auto &Case : SI->cases())
56285633
MissingCaseVal ^= Case.getCaseValue()->getValue().getLimitedValue();

llvm/test/Transforms/SimplifyCFG/switch-dead-default.ll

+33
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,39 @@ default:
313313

314314
declare void @llvm.assume(i1)
315315

316+
define zeroext i1 @test8(i128 %a) {
317+
; We should not transform conditions wider than 64 bit.
318+
; CHECK-LABEL: define zeroext i1 @test8(
319+
; CHECK-SAME: i128 [[A:%.*]]) {
320+
; CHECK-NEXT: entry:
321+
; CHECK-NEXT: [[TMP0:%.*]] = and i128 [[A]], 3894222643901120721397872246915072
322+
; CHECK-NEXT: switch i128 [[TMP0]], label [[LOR_RHS:%.*]] [
323+
; CHECK-NEXT: i128 1298074214633706907132624082305024, label [[LOR_END:%.*]]
324+
; CHECK-NEXT: i128 2596148429267413814265248164610048, label [[LOR_END]]
325+
; CHECK-NEXT: i128 3894222643901120721397872246915072, label [[LOR_END]]
326+
; CHECK-NEXT: ]
327+
; CHECK: lor.rhs:
328+
; CHECK-NEXT: br label [[LOR_END]]
329+
; CHECK: lor.end:
330+
; CHECK-NEXT: [[TMP1:%.*]] = phi i1 [ true, [[ENTRY:%.*]] ], [ false, [[LOR_RHS]] ], [ true, [[ENTRY]] ], [ true, [[ENTRY]] ]
331+
; CHECK-NEXT: ret i1 [[TMP1]]
332+
;
333+
entry:
334+
%0 = and i128 %a, 3894222643901120721397872246915072
335+
switch i128 %0, label %lor.rhs [
336+
i128 1298074214633706907132624082305024, label %lor.end
337+
i128 2596148429267413814265248164610048, label %lor.end
338+
i128 3894222643901120721397872246915072, label %lor.end
339+
]
340+
341+
lor.rhs: ; preds = %entry
342+
br label %lor.end
343+
344+
lor.end: ; preds = %entry, %entry, %entry, %lor.rhs
345+
%1 = phi i1 [ true, %entry ], [ false, %lor.rhs ], [ true, %entry ], [ true, %entry ]
346+
ret i1 %1
347+
}
348+
316349
!0 = !{!"branch_weights", i32 8, i32 4, i32 2, i32 1}
317350
;.
318351
; CHECK: [[PROF0]] = !{!"branch_weights", i32 0, i32 4, i32 2, i32 1, i32 8}

0 commit comments

Comments
 (0)