[llvm] Reland [SimplifyCFG] Delete the unnecessary range check for small mask operation (PR #70542)

via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 1 06:31:30 PDT 2023


================
@@ -107,3 +103,61 @@ lor.end:
   %0 = phi i8 [ 15, %sw.bb0 ], [ 6, %sw.bb1 ], [ 7, %sw.bb2 ], [ 0, %default ]
   ret i8 %0
 }
+
+; Negative test: The default branch is reachable, but has no result.
+define i1 @switch_lookup_with_small_i1_default_no_results(i32 %x) {
+; CHECK-LABEL: @switch_lookup_with_small_i1_default_no_results(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[AND:%.*]] = and i32 [[X:%.*]], 15
+; CHECK-NEXT:    ret i1 false
+;
+entry:
+  %and = and i32 %x, 15
+  switch i32 %and, label %default [
+  i32 4, label %phi.end
+  i32 2, label %phi.end
+  i32 10, label %phi.end
+  i32 9, label %phi.end
+  i32 1, label %sw.bb1.i
+  i32 3, label %sw.bb1.i
+  i32 5, label %sw.bb1.i
+  i32 0, label %sw.bb1.i
+  i32 6, label %sw.bb1.i
+  i32 7, label %sw.bb1.i
+  i32 8, label %sw.bb1.i
+  ]
+
+sw.bb1.i:                                     ; preds = %entry, %entry, %entry, %entry, %entry, %entry, %entry
+  br label %phi.end
+
+default:                                      ; preds = %entry
+  unreachable
----------------
zmodem wrote:

Since the `default` block starts with an `unreachable` instruction, `DefaultIsReachable` will be false: https://github.com/llvm/llvm-project/blob/6477b41a0b56919c42f44c3499ed32720101b43b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp#L6601-L6602

I was thinking of something like this, where the default doesn't yield a constant result, so we can't grow the table:

```
bool g(int x);

bool f(int x) {
    switch (x % 8) {
    case 0: return true;
    case 1: return false;
    case 2: return false;
    case 3: return false;
    case 4: return true;
    case 5: return false;
    case 6: return true;

    default: return g(x);
    }
}
```

https://github.com/llvm/llvm-project/pull/70542


More information about the llvm-commits mailing list