[InstCombine] Push freeze through recurrence phi
authorNikita Popov <npopov@redhat.com>
Thu, 16 Jun 2022 10:13:37 +0000 (12:13 +0200)
committerNikita Popov <npopov@redhat.com>
Fri, 17 Jun 2022 13:01:41 +0000 (15:01 +0200)
commitc6b88cb9184fc6c17bb3d9c2c9568646dc46638d
tree87d55f9f9e261b9064c4b5049c98e4008dd50b34
parent7aac15d5df6cfa03b802e055b63227a95fa1734e
[InstCombine] Push freeze through recurrence phi

We really want to push freezes through recurrence phis, so that we
freeze only the start value, rather than the IV value on every
iteration. foldOpIntoPhi() already handles this for the case where
the transfer function doesn't produce poison, e.g.
%iv.next = add %iv, 1. However, this does not work if nowrap flags
are present, e.g. the very common %iv.next = add nuw %iv, 1 case.

This patch adds a fold that pushes freeze instructions to the start
value by checking whether all backedge values will be non-poison
after poison generating flags have been dropped. This allows pushing
freezes out of loops in most cases. I suspect that this also
obsoletes the CanonicalizeFreezeInLoops pass, and we can probably
drop it.

Fixes https://github.com/llvm/llvm-project/issues/56048.

Differential Revision: https://reviews.llvm.org/D127960
llvm/lib/Transforms/InstCombine/InstCombineInternal.h
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
llvm/test/Transforms/InstCombine/freeze.ll