Commit 016c56d9 by Andrew Scull

Handle UINT64_MAX edge case in switch lowering.

BUG= R=jvoung@chromium.org, jvoung Review URL: https://codereview.chromium.org/1247833003.
parent 8447bbae
...@@ -75,9 +75,13 @@ CaseClusterArray CaseCluster::clusterizeSwitch(Cfg *Func, ...@@ -75,9 +75,13 @@ CaseClusterArray CaseCluster::clusterizeSwitch(Cfg *Func,
// Replace everything with a jump table // Replace everything with a jump table
InstJumpTable *JumpTable = InstJumpTable *JumpTable =
InstJumpTable::create(Func, TotalRange, Inst->getLabelDefault()); InstJumpTable::create(Func, TotalRange, Inst->getLabelDefault());
for (const CaseCluster &Case : CaseClusters) for (const CaseCluster &Case : CaseClusters) {
for (uint64_t I = Case.Low; I <= Case.High; ++I) // Case.High could be UINT64_MAX which makes the loop awkward. Unwrap the
// last iteration to avoid wrap around problems.
for (uint64_t I = Case.Low; I < Case.High; ++I)
JumpTable->addTarget(I - MinValue, Case.Label); JumpTable->addTarget(I - MinValue, Case.Label);
JumpTable->addTarget(Case.High - MinValue, Case.Label);
}
CaseClusters.clear(); CaseClusters.clear();
CaseClusters.emplace_back(MinValue, MaxValue, JumpTable); CaseClusters.emplace_back(MinValue, MaxValue, JumpTable);
......
...@@ -188,3 +188,33 @@ return: ...@@ -188,3 +188,33 @@ return:
; CHECK-NEXT: jne ; CHECK-NEXT: jne
; CHECK-NEXT: cmp {{.*}},0x12 ; CHECK-NEXT: cmp {{.*}},0x12
; CHECK-NEXT: je ; CHECK-NEXT: je
; Test for correct 64-bit jump table with UINT64_MAX as one of the values.
define internal i32 @testJumpTable64(i64 %a) {
entry:
switch i64 %a, label %sw.default [
i64 -6, label %return
i64 -4, label %sw.bb1
i64 -3, label %sw.bb2
i64 -1, label %sw.bb3
]
sw.bb1:
br label %return
sw.bb2:
br label %return
sw.bb3:
br label %return
sw.default:
br label %return
return:
%retval.0 = phi i32 [ 5, %sw.default ], [ 4, %sw.bb3 ], [ 3, %sw.bb2 ], [ 2, %sw.bb1 ], [ 1, %entry ]
ret i32 %retval.0
}
; TODO(ascull): this should generate a jump table. For now, just make sure it
; doesn't crash the compiler.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment