[llvm] a41c95c - [LNICM] Fix infinite loop

via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 16 20:55:35 PDT 2021


Author: Whitney Tsang
Date: 2021-08-17T12:55:22+09:00
New Revision: a41c95c0e3c24076df7b9449e350e04c5c073126

URL: https://github.com/llvm/llvm-project/commit/a41c95c0e3c24076df7b9449e350e04c5c073126
DIFF: https://github.com/llvm/llvm-project/commit/a41c95c0e3c24076df7b9449e350e04c5c073126.diff

LOG: [LNICM] Fix infinite loop

There is a bug introduced by https://reviews.llvm.org/D107219 which causes an infinite loop, when there are more than 2 levels PHINode chain.

Reviewed By: uint256_t

Differential Revision: https://reviews.llvm.org/D108166

Added: 
    

Modified: 
    llvm/lib/Transforms/Scalar/LICM.cpp
    llvm/test/Transforms/LICM/lnicm-sink.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/LICM.cpp b/llvm/lib/Transforms/Scalar/LICM.cpp
index 9e5e7d2a5935b..5e48809ff1aef 100644
--- a/llvm/lib/Transforms/Scalar/LICM.cpp
+++ b/llvm/lib/Transforms/Scalar/LICM.cpp
@@ -1491,7 +1491,7 @@ static bool isNotUsedOrFreeInLoop(const Instruction &I, const Loop *CurLoop,
                UI->getNumOperands() == 1) {
           if (!CurLoop->contains(UI))
             break;
-          UI = cast<Instruction>(U->user_back());
+          UI = cast<Instruction>(UI->user_back());
         }
       }
     }

diff  --git a/llvm/test/Transforms/LICM/lnicm-sink.ll b/llvm/test/Transforms/LICM/lnicm-sink.ll
index 0dc47e5f1a4a0..a94f5f39ab426 100644
--- a/llvm/test/Transforms/LICM/lnicm-sink.ll
+++ b/llvm/test/Transforms/LICM/lnicm-sink.ll
@@ -61,6 +61,72 @@ for.end7:
   ret double %t.0.lcssa
 }
 
+; double sin(double);
+; int abs(int);
+; double test(double x, int y[10]) {
+;   double t = 0; int s = 0;
+;   for (int i = 0; i < 10; i++) {
+;     for (int k = 0; k < 10; k++) {
+;       for (int j = 0; j < 10; j++) {
+;         t = sin(x);
+;         s = abs(i);
+;       }
+;     }
+;     y[i] = s;
+;   }
+;   return t;
+; }
+;
+define dso_local double @test2(double %x, i32* noalias %y) {
+entry:
+  br label %for.body
+
+for.body:
+  %i.02 = phi i32 [ 0, %entry ], [ %inc6, %for.end ]
+  br label %for.k
+
+for.k:
+  %k = phi i64 [ 0, %for.body ], [ %inc.k, %for.end.k ]
+  br label %for.body3
+
+; CHECK: for.body3:
+; LNICM: call i32 @abs(i32 %i.02)
+; LICM-NOT: call i32 @abs(i32 %i.02)
+for.body3:
+  %j.01 = phi i32 [ 0, %for.k ], [ %inc, %for.body3 ]
+  %call = call double @sin(double %x)
+  %call4 = call i32 @abs(i32 %i.02)
+  %inc = add nsw i32 %j.01, 1
+  %cmp2 = icmp slt i32 %inc, 10
+  br i1 %cmp2, label %for.body3, label %for.end.k
+
+for.end.k:
+  %s.lcssa.k = phi i32 [ %call4, %for.body3 ]
+  %t.lcssa.k = phi double [ %call, %for.body3 ]
+  %inc.k = add nsw i64 %k, 1
+  %cmp.k = icmp slt i64 %inc.k, 10
+  br i1 %cmp.k, label %for.k, label %for.end
+
+; CHECK: for.end:
+; LICM: call i32 @abs(i32 %i.02)
+; LNICM-NOT: call i32 @abs(i32 %i.02)
+for.end:
+  %s.1.lcssa = phi i32 [ %s.lcssa.k, %for.end.k ]
+  %t.1.lcssa = phi double [ %t.lcssa.k, %for.end.k ]
+  %idxprom = sext i32 %i.02 to i64
+  %arrayidx = getelementptr inbounds i32, i32* %y, i64 %idxprom
+  store i32 %s.1.lcssa, i32* %arrayidx, align 4
+  %inc6 = add nsw i32 %i.02, 1
+  %cmp = icmp slt i32 %inc6, 10
+  br i1 %cmp, label %for.body, label %for.end7
+
+; CHECK: for.end7:
+; CHECK: call double @sin(double %x)
+for.end7:
+  %t.0.lcssa = phi double [ %t.1.lcssa, %for.end ]
+  ret double %t.0.lcssa
+}
+
 declare dso_local double @sin(double) #0
 
 declare dso_local i32 @abs(i32) #0


        


More information about the llvm-commits mailing list