[llvm] [SimplifyCFG] Replace unreachable switch lookup table holes with poison (PR #94990)

via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 18 18:38:55 PDT 2024


================
@@ -6281,8 +6282,15 @@ SwitchLookupTable::SwitchLookupTable(
     uint64_t Idx = (CaseVal->getValue() - Offset->getValue()).getLimitedValue();
     TableContents[Idx] = CaseRes;
 
-    if (CaseRes != SingleValue)
-      SingleValue = nullptr;
+    if (CaseRes != SingleValue) {
----------------
DianQK wrote:

Ah, I saw `constant_hole_unreachable_default_firstpoison`.

You can change `SingleValue && isa<UndefValue>(SingleValue)` to `isa_and_present<UndefValue>(SingleValue)`.

But here, you can directly change it to:
```c++
    if (SingleValue && CaseRes != SingleValue) {
      if (isa<UndefValue>(SingleValue))
        // All of the switch cases until now have returned undef; ignore them
        // and use this value as the single constant value.
        SingleValue = CaseRes;
      else if (!isa<UndefValue>(CaseRes))
        SingleValue = nullptr;
    }
```

Hmm, I think you still need to handle scenarios involving a mix of `undef` and `poison`.
For example:

```llvm
define i32 @constant_hole_unreachable_default_undef_poison(i32 %x) {
; CHECK-LABEL: @constant_hole_unreachable_default_firstpoison(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    ret i32 poison
;
entry:
  switch i32 %x, label %sw.default [
  i32 0, label %bb.undef
  i32 2, label %bb.poison
  i32 3, label %bb.poison
  i32 4, label %bb.poison
  ]

sw.default: unreachable
bb.undef: br label %return
bb.poison: br label %return

return:
  %res = phi i32 [ undef, %bb.undef ], [ poison, %bb.poison ]
  ret i32 %res
}
```

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


More information about the llvm-commits mailing list