[LVI][CVP] Make use of condition known at use
authorNikita Popov <npopov@redhat.com>
Wed, 11 Jan 2023 10:58:00 +0000 (11:58 +0100)
committerNikita Popov <npopov@redhat.com>
Thu, 12 Jan 2023 15:41:31 +0000 (16:41 +0100)
commit4f772b095525059521f2f88112d29dcfaa178101
tree9cbfb3c319920d0e38f7d6ebf8f3eb285128f4ce
parent0d674cf0eae84a7b83999d2122c1609a8ee2b16d
[LVI][CVP] Make use of condition known at use

When an instruction is only used in a select or phi operand, we might
be able to make use of additional information from the select/branch
condition. For example in

  %sub = call i16 @llvm.usub.sat.i16(i16 %x, i16 10)
  %cmp = icmp uge i16 %x, 10
  %sel = select i1 %cmp, i16 %sub, i16 42

the usub.sat is only used in a select where %x uge 10 is known to
hold, so we can fold it based on that knowledge.

This addresses the regression reported at
https://reviews.llvm.org/D140798#4039748, but also provides a
solution to a recurring problem we've had, where we fail to make
use of range information after a branch+phi has been converted
into a select. Our current solution to this is to hope that IPSCCP
can perform the fold before that happens, but handling this in LVI
is a somewhat more general solution.

Currently we only make use of this for the willNotOverflow() fold,
but I plan to adjust other folds to use the new API as well.

Differential Revision: https://reviews.llvm.org/D141482
llvm/include/llvm/Analysis/LazyValueInfo.h
llvm/lib/Analysis/LazyValueInfo.cpp
llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
llvm/test/Transforms/CorrelatedValuePropagation/cond-at-use.ll