[all-commits] [llvm/llvm-project] 5587ab: [SimpleLoopUnswitch] Allow trivial unswitch when e...

Dark Steve via All-commits all-commits at lists.llvm.org
Mon Sep 21 22:21:35 PDT 2026


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 5587ab44938b396a01268f118fad8b32a59104a3
      https://github.com/llvm/llvm-project/commit/5587ab44938b396a01268f118fad8b32a59104a3
  Author: Dark Steve <Prasoon.Mishra at amd.com>
  Date:   2026-09-22 (Tue, 22 Sep 2026)

  Changed paths:
    M llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
    M llvm/test/Transforms/SimpleLoopUnswitch/nontrivial-unswitch-trivial-select.ll
    M llvm/test/Transforms/SimpleLoopUnswitch/pr138509.ll
    A llvm/test/Transforms/SimpleLoopUnswitch/trivial-unswitch-header-phi.ll

  Log Message:
  -----------
  [SimpleLoopUnswitch] Allow trivial unswitch when exit LCSSA is a header PHI (#222962)

Trivial unswitch bails on a common loop shape: a loop-invariant branch
in the header that exits the loop returning a loop-carried value.

```
  int acc = 0;
  for (...) {
    if (c)
      return acc;
    acc++;
  }
```

After loop-rotate the `if` sits in the header, and the exit reads the
header PHI `%acc`. Trivial unswitch hoists the branch into the
preheader, so the exit value must be available there. It only accepts
loop-invariant values, and `%acc` is not one, so it gives up. But `%acc`
is safe here: the branch is invariant, so if the loop exits it exits on
the first iteration, before the latch runs. On that edge `%acc` still
holds its entry value, 0.

This is not specific to the header. The same holds for any invariant
branch whose block dominates the latch, so every header PHI still holds
its entry value.

Allow the unswitch in these cases. When an exit's LCSSA incoming is a
header PHI (and the branch's block dominates the latch), replace it with
the value the PHI takes from the preheader (its entry value), which
keeps SSA valid.



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list