[llvm] [polly] [SimpleLoopUnswitch] Generalize the notion of trivial unswitching (PR #193989)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 12 09:24:43 PDT 2026


================
@@ -0,0 +1,177 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; REQUIRES: asserts
+; RUN: opt -passes='loop-mssa(simple-loop-unswitch),verify<loops>' -S < %s | FileCheck %s
+; RUN: opt -passes='loop-mssa(simple-loop-unswitch)' -disable-output -stats -info-output-file - < %s | FileCheck --check-prefix=STATS %s
+
+;; Check that a loop-invariant guard branch in a perfect nest is trivially
+;; unswitched.  The outer loop's header branches to the latch (skipping the
+;; inner loop entirely) when N == 0, or falls through into the inner loop.
+;; Because N is loop-invariant, the new code in unswitchTrivialBranch rewires
+;; the latch arm to point at the outer-loop exit, making the branch look like
+;; an ordinary exit branch and allowing the standard trivial-unswitch logic to
+;; hoist it out of the outer loop.
+;;
+;; Source:
+;;   void f(int M, int N, int *A, int *B) {
+;;     for (int j = 0; j < M; j++) {
+;;       if (N == 0) continue;          // invariant guard branches to latch
+;;       for (int i = 0; i < N; i++)
+;;         A[i] = B[i] + 1;
+;;     }
+;;   }
----------------
nikic wrote:

Can you please update the PR description? I found your description there very confusing, because your initial example doesn't actually contain any guards -- at least for anyone who is not familiar with Fortran peculiarities. The example here is much clearer.

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


More information about the llvm-commits mailing list