[PATCH] D38494: [ScalarEvolution] Handling for ICmp occuring in the evolution chain.

Sanjoy Das via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 4 12:11:16 PDT 2017


sanjoy added a comment.

With this IR:

  define void @f() {
  entry:
    br label %loop
  
  loop:
    %iv = phi i32 [ 0, %entry ], [ %iv.inc, %loop ]
    %iv.inc = add i32 %iv, 1
    %cmp = icmp ne i32 %iv.inc, 10
    %cmp.zext = zext i1 %cmp to i32
    br i1 %cmp, label %loop, label %leave
  
  leave:
    ret void
  }

and `opt -analyze -scalar-evolution` , on master:

  Printing analysis 'Scalar Evolution Analysis' for function 'f':
  Classifying expressions for: @f
    %iv = phi i32 [ 0, %entry ], [ %iv.inc, %loop ]
    -->  {0,+,1}<%loop> U: [0,10) S: [0,10)               Exits: 9                LoopDispositions: { %loop: Computable }
    %iv.inc = add i32 %iv, 1
    -->  {1,+,1}<%loop> U: [1,11) S: [1,11)               Exits: 10               LoopDispositions: { %loop: Computable }
    %cmp.zext = zext i1 %cmp to i32
    -->  (zext i1 %cmp to i32) U: [0,2) S: [0,2)          Exits: 0                LoopDispositions: { %loop: Variant }
  Determining loop execution counts for: @f
  Loop %loop: backedge-taken count is 9
  Loop %loop: max backedge-taken count is 9
  Loop %loop: Predicated backedge-taken count is 9
   Predicates:
  
  Loop %loop: Trip multiple is 10

and with your patch:

  Printing analysis 'Scalar Evolution Analysis' for function 'f':
  Classifying expressions for: @f
    %iv = phi i32 [ 0, %entry ], [ %iv.inc, %loop ]
    -->  {0,+,1}<%loop> U: [0,10) S: [0,10)               Exits: 9                LoopDispositions: { %loop: Computable }
    %iv.inc = add i32 %iv, 1
    -->  {1,+,1}<%loop> U: [1,11) S: [1,11)               Exits: 10               LoopDispositions: { %loop: Computable }
    %cmp.zext = zext i1 %cmp to i32
    -->  1 U: [1,2) S: [1,2)              Exits: 1                LoopDispositions: { %loop: Invariant }
  Determining loop execution counts for: @f
  Loop %loop: backedge-taken count is 9
  Loop %loop: max backedge-taken count is 9
  Loop %loop: Predicated backedge-taken count is 9
   Predicates:
  
  Loop %loop: Trip multiple is 10

Note that the SCEV for `%cmp.zext` is different.  I'm not sure how you got a
different answer on your end, since this should basically be the same as the IR
from Hal's example.  Perhaps in the example you tried the icmp feeding in to the
latch was a different llvm::Instruction than the icmp feeding into the zext?  If
so, that's a missing-CSE bug.


https://reviews.llvm.org/D38494





More information about the llvm-commits mailing list