Handle UINT64_MAX edge case in switch lowering.
BUG=
R=jvoung@chromium.org, jvoung
Review URL: https://codereview.chromium.org/1247833003.
diff --git a/src/IceSwitchLowering.cpp b/src/IceSwitchLowering.cpp
index 06894e6..37e67c1 100644
--- a/src/IceSwitchLowering.cpp
+++ b/src/IceSwitchLowering.cpp
@@ -75,9 +75,13 @@
// Replace everything with a jump table
InstJumpTable *JumpTable =
InstJumpTable::create(Func, TotalRange, Inst->getLabelDefault());
- for (const CaseCluster &Case : CaseClusters)
- for (uint64_t I = Case.Low; I <= Case.High; ++I)
+ for (const CaseCluster &Case : CaseClusters) {
+ // 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(Case.High - MinValue, Case.Label);
+ }
CaseClusters.clear();
CaseClusters.emplace_back(MinValue, MaxValue, JumpTable);
diff --git a/tests_lit/llvm2ice_tests/adv-switch-opt.ll b/tests_lit/llvm2ice_tests/adv-switch-opt.ll
index 1d10931..8da727f 100644
--- a/tests_lit/llvm2ice_tests/adv-switch-opt.ll
+++ b/tests_lit/llvm2ice_tests/adv-switch-opt.ll
@@ -188,3 +188,33 @@
; CHECK-NEXT: jne
; CHECK-NEXT: cmp {{.*}},0x12
; 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.