[PATCH] D110060: [LoopBoundSplit] Handle the case in which exiting block is loop header

JinGu Kang via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 27 08:47:47 PDT 2021


jaykang10 updated this revision to Diff 375276.
jaykang10 added a comment.

Update phi nodes in header of post-loop using LCSSA.

We can say that the start value of phi node in post-loop is the value of phi node at last iteration in pre-loop.  Let's see a simple example.

  for.body:
    %i = phi i16 [ 0, %entry ], [ %add, %cond.end ]
    %cmp1 = icmp ult i16 %i, 3
    br i1 %cmp1, label %cond.true, label %cond.false
  
  cond.true:
    br label %cond.end
  
  cond.false:
    br label %cond.end
  
  cond.end:
    %call = call i16 @foo()
    %add = add nuw nsw i16 %i, 1
    %cmp2 = icmp ult i16 %i, 11
    br i1 %cmp2, label %for.body, label %end

In above example, we can add a LCSSA Phi node in exit block of pre-loop and update the start value of the phi node with the LCSSA one as below.

  entry.split.split:
    %i.lcssa = phi i16 [ %add, %cond.end ]
  ...
  for.body.split.preheader:
    br label %for.body.split
  ...
  for.body.split:
    %i.split = phi i16 [ %add.split, %cond.end.split ], [ %i.lcssa, %for.body.split.preheader ]

In this example, the value of phi node at last iteration is `%add` because the exiting condition uses it.

If the exiting block is not loop latch, we need to update the phi node differently. Let's see other example.

  for.cond:                                         ; preds = %for.inc, %entry
    %i.0 = phi i16 [ 0, %entry ], [ %inc.0, %for.inc ]
    %exitcond.not = icmp eq i16 %i.0, 10
    br i1 %exitcond.not, label %for.end, label %for.body
  
  for.body:
    %cmp1 = icmp ult i16 %i.0, 5
    %arrayidx = getelementptr inbounds [10 x i16], [10 x i16]* @B, i16 0, i16 %i.0
    %0 = load i16, i16* %arrayidx, align 1
    br i1 %cmp1, label %if.then, label %if.else
  
  if.then:
    br label %for.inc
  
  if.else:
    br label %for.inc
  
  for.inc:
    %inc.0 = add nuw nsw i16 %i.0, 1

For above example, we can add the LCSSA phi node as below.

  entry.split.split:
    %i.0.lcssa = phi i16 [ %i.0, %for.cond ]
  ...
  for.cond.split.preheader:
    br label %for.cond.split
  ...
  for.cond.split:
    %i.0.split = phi i16 [ %inc.0.split, %for.inc.split ], [ %i.0.lcssa, %for.cond.split.preheader ]

In this example, the value of phi node at last iteration is `%i.0` because the exiting condition uses it.

Based on this logic, the patch is updated. @mkazantsev If you feel something wrong, please let me know.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D110060/new/

https://reviews.llvm.org/D110060

Files:
  llvm/lib/Transforms/Scalar/LoopBoundSplit.cpp
  llvm/test/Transforms/LoopBoundSplit/bug-loop-bound-split-phi-in-exit-block.ll
  llvm/test/Transforms/LoopBoundSplit/bug51866.ll
  llvm/test/Transforms/LoopBoundSplit/loop-bound-split.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D110060.375276.patch
Type: text/x-patch
Size: 23437 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210927/29ad2625/attachment.bin>


More information about the llvm-commits mailing list