[all-commits] [llvm/llvm-project] 8605b4: [ValueTracking] recognize sub X, (X -nsw Y) as not...

Sanjay Patel via All-commits all-commits at lists.llvm.org
Tue Jun 14 11:53:46 PDT 2022

  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 8605b4d8c5cc1e1f8a0547378ca1eb69a1a56840
  Author: Sanjay Patel <spatel at rotateright.com>
  Date:   2022-06-14 (Tue, 14 Jun 2022)

  Changed paths:
    M llvm/lib/Analysis/ValueTracking.cpp
    M llvm/test/Transforms/InstCombine/sub.ll

  Log Message:
  [ValueTracking] recognize sub X, (X -nsw Y) as not overflowing

This extends a similar pattern from D125500.
If we know that operand 1 (RHS) of a subtract is itself a
non-overflowing subtract from operand 0 (LHS), then the
final/outer subtract is also non-overflowing:

InstCombine uses this analysis to trigger a narrowing
optimization, so that is what the first changed test shows.

The last test models the motivating case from issue #48013.
In that example, we determine 'nsw' on the first sub from
the srem, then we determine that the 2nd sub can be narrowed,
and that leads to eliminating both subtracts.

This works for unsigned sub too, but I left that out to keep
the patch minimal. If this looks ok, I will follow up with
that change. There are also several missing subtract narrowing
optimizations demonstrated in the tests above the diffs shown
here - those should be handled in InstCombine with another set
of patches.

Differential Revision: https://reviews.llvm.org/D127754

More information about the All-commits mailing list