[InstCombine] Combine lshr of add -> (a + b < a)
authorPierre van Houtryve <pierre.vanhoutryve@amd.com>
Fri, 6 Jan 2023 13:33:49 +0000 (08:33 -0500)
committerPierre van Houtryve <pierre.vanhoutryve@amd.com>
Tue, 10 Jan 2023 08:37:23 +0000 (03:37 -0500)
commitb3fdb7b0cba49e7f24fd8207c677b0541045755c
tree248a6c8aa57fca54125afb406b8a58c4975e4fdb
parent4e78f88561af26c74b4b7fa2a017cd836a9f9bf4
[InstCombine] Combine lshr of add -> (a + b < a)

Tries to perform
  (lshr (add (zext X), (zext Y)), K)
  ->  (icmp ult (add X, Y), X)
  where
    - The add's operands are zexts from a K-bits integer to a bigger type.
    - The add is only used by the shr, or by iK (or narrower) truncates.
    - The lshr type has more than 2 bits (other types are boolean math).
    - K > 1

This seems to be a pattern that just comes from OpenCL front-ends, so adding DAG/GISel combines doesn't seem to be worth the complexity.

Original patch D107552 by @abinavpp - adapted to use (a + b < a) instead of uaddo following discussion on the review.
See this issue https://github.com/RadeonOpenCompute/ROCm/issues/488

Reviewed By: spatel

Differential Revision: https://reviews.llvm.org/D138814
llvm/lib/Transforms/InstCombine/InstCombineInternal.h
llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
llvm/test/Transforms/InstCombine/lshr.ll
llvm/test/Transforms/InstCombine/shift-add.ll