Issue 234896
Summary SCEV unable to determine trip count in straightforward loop with `switch` exit
Labels new issue
Assignees
Reporter osa1
    ```
; RUN: opt -passes='print<scalar-evolution>' -disable-output %s

declare void @g()

define void @switch_unreachable_default() {
entry:
  br label %loop

loop:
  %i = phi i32 [ 0, %entry ], [ %inc, %latch ]
  switch i32 %i, label %unreachable [
    i32 3, label %exit
    i32 0, label %latch
 i32 1, label %latch
    i32 2, label %latch
  ]

unreachable:
 unreachable

latch:
  call void @g()
  %inc = add nuw nsw i32 %i, 1
  br label %loop

exit:
  ret void
}
```

Here SCEV is unable to calculate trip count, but it's easy to see that it's 4. Current output:

```
Determining loop execution counts for: @switch_unreachable_default
Loop %loop: Unpredictable backedge-taken count.
Loop %loop: Unpredictable constant max backedge-taken count.
Loop %loop: Unpredictable symbolic max backedge-taken count.
```

I came up with this IR while investigating some `SimplifyCFG` and `JumpTableToSwitch` transformations and how they interact. It's easy to come up with C code that generates this, but I have no evidence that it matters in practice, yet (even if the pattern occurs in real world, I think there's one other blocker before we can have this IR).

The idea here is that if SCEV can calcualte the trip count, we can unroll this loop, which allows inlining function calls in a C loop like:

```
static void (*const t[])(void) = { f0, f1, f2, 0 };
for (int i = 0; t[i]; i++) t[i]();
```

cc @fhahn 
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to