[all-commits] [llvm/llvm-project] 090290: [InstCombine] Fold max/min when incrementing/decre...

Alex MacLean via All-commits all-commits at lists.llvm.org
Tue Jun 10 07:56:18 PDT 2025


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 09029045a88b48591ce645bae640fc3bc8b58b63
      https://github.com/llvm/llvm-project/commit/09029045a88b48591ce645bae640fc3bc8b58b63
  Author: Alex MacLean <amaclean at nvidia.com>
  Date:   2025-06-10 (Tue, 10 Jun 2025)

  Changed paths:
    M llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
    M llvm/test/Transforms/InstCombine/minmax-fold.ll

  Log Message:
  -----------
  [InstCombine] Fold max/min when incrementing/decrementing by 1 (#142466)

Add the following folds for integer min max folding in InstCombine:
 -  (X > Y) ? X : (Y - 1) ==> MIN(X, Y - 1)
 -  (X < Y) ? X : (Y + 1) ==> MAX(X, Y + 1)

These are safe when overflow corresponding to the sign of the comparison
is poison. (proof https://alive2.llvm.org/ce/z/oj5iiI).

The most common of these patterns is likely the minimum case which
occurs in some internal library code when clamping an integer index to a
range (The maximum cases are included for completeness). Here is a
simplified example:

int clampToWidth(int idx, int width) {
  if (idx >= width)
    return width - 1;
  return idx;
}

https://cuda.godbolt.org/z/nhPzWrc3W



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