[GuardWidening] Choose right point for generating wide condition for branches. PR60234
authorMax Kazantsev <mkazantsev@azul.com>
Tue, 31 Jan 2023 05:29:26 +0000 (12:29 +0700)
committerMax Kazantsev <mkazantsev@azul.com>
Tue, 31 Jan 2023 05:29:26 +0000 (12:29 +0700)
commit5cb568a37a53a9b0fd8fc9c2c35870cad43623e9
tree65373db5d0120adcbbfbef7dd08f3127be2fd1fd
parentff96267b42021e3f0d886579e5405033a88b7222
[GuardWidening] Choose right point for generating wide condition for branches. PR60234

When guards are represented as widenable branches, there is a tricky
situation when the branch stays in loop but widenable condition doesn't.
It means that the widenable condition is loop-invariant, and some other
optimizations could have done changes using this fact.

If widening is allowed to create widened condition inside this loop,
and join the loop-invariant wc with some non-invariant facts, it can
cause miscompile. See example of this at https://github.com/llvm/llvm-project/issues/60234.

The solution is to adjust the point of generationg the wide condition,
and therefore of hoisting all related parts there. It should not be before
the branch, but before the widenable_condition call. The fact that `wc()`
and the wide condition are in the same block guarantees that they will
not violate the invariance property for any loop.

Differential Revision: https://reviews.llvm.org/D142693
Reviewed By: apilipenko
llvm/lib/Transforms/Scalar/GuardWidening.cpp
llvm/test/Transforms/GuardWidening/basic_widenable_condition_guards.ll
llvm/test/Transforms/GuardWidening/loop_invariant_widenable_condition.ll
llvm/test/Transforms/GuardWidening/mixed_guards.ll
llvm/test/Transforms/GuardWidening/pr60234.ll