[PATCH] D102615: [LoopDeletion] Break backedge if we can prove that the loop is exited on 1st iteration

Max Kazantsev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon May 17 04:55:16 PDT 2021


mkazantsev created this revision.
mkazantsev added reviewers: fhahn, reames, adpatel6, lebedev.ri, nikic.
Herald added subscribers: javed.absar, hiraditya.
mkazantsev requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This patch handles one particular case of one-iteration loops for which SCEV
cannot straightforwardly prove BECount = 1. The idea of the optimization is to
prove value of exiting conditions exactly on the 1st iteration, by replacing Phis
with their values according to the following two rules:

1. Header phis on the 1st iteration are equal to their input from predecessor;
2. In-loop Phi dominated by condition that is known on the 1st iteration is equal to one of its inputs that is dominated by taken branch of this known condition.

The later can be explained by the following example:

  if (cond)
   /      \
  ...      ...
   \      /
   phi(v1, v2)

If we somehow know the value of cond on the 1st iteration, we also know
the value of the `phi`.

We construct a map of in-loop instructions onto their SCEVs computed on the first
iteration, and if one of mustexecute loop-exiting conditions must be taken, we can
safely break the backedge.

It also seems that the parts of this algorithm can be promoted into SCEV, but provided
how narrow-specialized it is (it can only be used to prove that the loop has 0 BTC), it may
have negative and widespread compile time impact we want to avoid. Some parts of it
can be further promoted into SCEV one by one and very carefully.


https://reviews.llvm.org/D102615

Files:
  llvm/lib/Transforms/Scalar/LoopDeletion.cpp
  llvm/test/Transforms/LoopDeletion/eval_first_iteration.ll
  llvm/test/Transforms/LoopDeletion/unreachable-loops.ll
  llvm/test/Transforms/LoopDeletion/zero-btc.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D102615.345836.patch
Type: text/x-patch
Size: 20037 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210517/42a7896d/attachment.bin>


More information about the llvm-commits mailing list