[LoopPeel] Peel if it turns invariant loads dereferenceable.
authorFlorian Hahn <flo@fhahn.com>
Tue, 12 Oct 2021 10:10:40 +0000 (11:10 +0100)
committerFlorian Hahn <flo@fhahn.com>
Tue, 12 Oct 2021 10:42:28 +0000 (11:42 +0100)
commitcd0ba9dc58c5806f4e3cc9635ab1f64af6973a83
tree88ea8e04a6a590ee63042160f3f5ea01ad268eb3
parent6a1f50b84ae8f8a8087fcdbe5f27dae8c76878f1
[LoopPeel] Peel if it turns invariant loads dereferenceable.

This patch adds a new cost heuristic that allows peeling a single
iteration off read-only loops, if the loop contains a load that

    1. is feeding an exit condition,
    2. dominates the latch,
    3. is not already known to be dereferenceable,
    4. and has a loop invariant address.

If all non-latch exits are terminated with unreachable, such loads
in the loop are guaranteed to be dereferenceable after peeling,
enabling hoisting/CSE'ing them.

This enables vectorization of loops with certain runtime-checks, like
multiple calls to `std::vector::at` if the vector is passed as pointer.

Reviewed By: mkazantsev

Differential Revision: https://reviews.llvm.org/D108114
llvm/include/llvm/Transforms/Utils/LoopPeel.h
llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
llvm/lib/Transforms/Utils/LoopPeel.cpp
llvm/test/Transforms/LoopUnroll/peel-to-turn-invariant-accesses-dereferenceable.ll
llvm/test/Transforms/PhaseOrdering/AArch64/peel-multiple-unreachable-exits-for-vectorization.ll