OSDN Git Service

Handle UINT64_MAX edge case in switch lowering.
authorAndrew Scull <ascull@google.com>
Thu, 23 Jul 2015 21:31:12 +0000 (14:31 -0700)
committerAndrew Scull <ascull@google.com>
Thu, 23 Jul 2015 21:31:12 +0000 (14:31 -0700)
BUG=
R=jvoung@chromium.org, jvoung

Review URL: https://codereview.chromium.org/1247833003.

src/IceSwitchLowering.cpp
tests_lit/llvm2ice_tests/adv-switch-opt.ll

index 06894e6..37e67c1 100644 (file)
@@ -75,9 +75,13 @@ CaseClusterArray CaseCluster::clusterizeSwitch(Cfg *Func,
   // 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);
index 1d10931..8da727f 100644 (file)
@@ -188,3 +188,33 @@ return:
 ; 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.