[PATCH] D146811: [LSR] Preserve LCSSA when rewriting instruction with PHI user

Dmitry Makogon via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 24 07:18:53 PDT 2023


dmakogon created this revision.
dmakogon added reviewers: mkazantsev, nikic, fhahn, reames, dmgreen.
Herald added subscribers: kosarev, StephenFan, kerbowa, hiraditya, jvesely.
Herald added a project: All.
dmakogon requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Fixes https://github.com/llvm/llvm-project/issues/61182.
LoopStrengthReduce may sometimes break LCSSA form when applying a rewrite for an instruction used in a PHI.
It happens if:

1. The PHI is in a loop exit block,
2. The edge from the corresponding exiting block to that exit is critical,
3. The PHI has at least two inputs coming from loop blocks,
4. The rewritten instruction is inserted in the loop.

An example of initial CFG:

  LoopBlock  <--  ExitingBlock1   ExitingBlock2  
                      |                 |
                      v                 |
                  ExitBlock  <----------+

In such case we split the critical edge:

  LoopBlock  <--  ExitingBlock1   ExitingBlock2  
                      |                |
                      v                v
                   CritEdge          split
                      |                |
                      v                |
                  ExitBlock  <---------+
             (not exit anymore)

and then replace PHI inputs with the rewritten instruction. However ExitBlock is no longer a loop exit, so LCSSA form is broken.

This patch fixes it by collecting all inserted instructions for PHIs whose parent block is not a loop exit and then forming LCSSA for them.


https://reviews.llvm.org/D146811

Files:
  llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
  llvm/test/Transforms/LoopStrengthReduce/2011-10-03-CritEdgeMerge.ll
  llvm/test/Transforms/LoopStrengthReduce/AMDGPU/lsr-invalid-ptr-extend.ll
  llvm/test/Transforms/LoopStrengthReduce/X86/2011-11-29-postincphi.ll
  llvm/test/Transforms/LoopStrengthReduce/X86/expander-crashes.ll
  llvm/test/Transforms/LoopStrengthReduce/X86/expander-reused-value-insert-point.ll
  llvm/test/Transforms/LoopStrengthReduce/X86/sibling-loops.ll
  llvm/test/Transforms/LoopStrengthReduce/callbr-critical-edge-splitting.ll
  llvm/test/Transforms/LoopStrengthReduce/depth-limit-overrun.ll
  llvm/test/Transforms/LoopStrengthReduce/post-inc-icmpzero.ll
  llvm/test/Transforms/LoopStrengthReduce/preserve-lcssa.ll
  llvm/test/Transforms/LoopStrengthReduce/uglygep.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D146811.508074.patch
Type: text/x-patch
Size: 25253 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230324/a7ab6219/attachment.bin>


More information about the llvm-commits mailing list