[LoopDeletion] Allows deletion of possibly infinite side-effect free loops
authorAtmn Patel <a335pate@uwaterloo.ca>
Wed, 23 Dec 2020 02:54:21 +0000 (21:54 -0500)
committerAtmn <atmndp@gmail.com>
Thu, 31 Dec 2020 02:43:01 +0000 (21:43 -0500)
commit6f1503d59854b331f1f970d39839619b0a26bbc7
treef55b0586778a06d41393fb1a7d0c500850cc5122
parent8886be242de8af6531204e2586b023f03b56992a
[LoopDeletion] Allows deletion of possibly infinite side-effect free loops

From C11 and C++11 onwards, a forward-progress requirement has been
introduced for both languages. In the case of C, loops with non-constant
conditionals that do not have any observable side-effects (as defined by
6.8.5p6) can be assumed by the implementation to terminate, and in the
case of C++, this assumption extends to all functions. The clang
frontend will emit the `mustprogress` function attribute for C++
functions (D86233, D85393, D86841) and emit the loop metadata
`llvm.loop.mustprogress` for every loop in C11 or later that has a
non-constant conditional.

This patch modifies LoopDeletion so that only loops with
the `llvm.loop.mustprogress` metadata or loops contained in functions
that are required to make progress (`mustprogress` or `willreturn`) are
checked for observable side-effects. If these loops do not have an
observable side-effect, then we delete them.

Loops without observable side-effects that do not satisfy the above
conditions will not be deleted.

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D86844
clang/test/Misc/loop-opt-setup.c
llvm/include/llvm/Transforms/Utils/LoopUtils.h
llvm/lib/Transforms/Scalar/LoopDeletion.cpp
llvm/lib/Transforms/Utils/LoopUtils.cpp
llvm/test/Transforms/LoopDeletion/mustprogress.ll [new file with mode: 0644]
llvm/test/Transforms/LoopDeletion/no-exit-blocks.ll