[MustExecute] Fix algorithmic bug in isGuaranteedToExecute. PR38514
authorMax Kazantsev <max.kazantsev@azul.com>
Fri, 17 Aug 2018 06:19:17 +0000 (06:19 +0000)
committerMax Kazantsev <max.kazantsev@azul.com>
Fri, 17 Aug 2018 06:19:17 +0000 (06:19 +0000)
commit7b78d3920c713de2555b5d0f476bc31a0d9aac5d
tree18ee8d8625eb2a73c7239d2e4e71942066e6cf63
parentcfa3e66b8e97f9e60e902def91e361dec9ad0577
[MustExecute] Fix algorithmic bug in isGuaranteedToExecute. PR38514

The description of `isGuaranteedToExecute` does not correspond to its implementation.
According to description, it should return `true` if an instruction is executed under the
assumption that its loop is *entered*. However there is a sophisticated alrogithm inside
that tries to prove that the instruction is executed if the loop is *exited*, which is not the
same thing for infinite loops. There is an attempt to protect from dealing with infinite loops
by prohibiting loops without exit blocks, however an infinite loop can have exit blocks.

As result of that, MustExecute can falsely consider some blocks that are never entered as
mustexec, and LICM can hoist dangerous instructions out of them basing on this fact.
This may introduce UB to programs which did not contain it initially.

This patch removes the problematic algorithm and replaced it with a one which tries to
prove what is required in description.

Differential Revision: https://reviews.llvm.org/D50558
Reviewed By: reames

llvm-svn: 339984
llvm/include/llvm/Analysis/MustExecute.h
llvm/lib/Analysis/MustExecute.cpp
llvm/test/Analysis/MustExecute/infinite_loops.ll
llvm/test/Analysis/MustExecute/loop-header.ll
llvm/test/Transforms/LICM/infinite_loops.ll