[PATCH] D108166: [LNICM] Fix infinite loop

Whitney Tsang via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 16 15:10:53 PDT 2021


Whitney created this revision.
Whitney added a reviewer: uint256_t.
Whitney added a project: LLVM.
Herald added subscribers: asbirlea, hiraditya.
Whitney requested review of this revision.
Herald added a subscriber: llvm-commits.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D108166

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


Index: llvm/test/Transforms/LICM/lnicm-sink.ll
===================================================================
--- llvm/test/Transforms/LICM/lnicm-sink.ll
+++ llvm/test/Transforms/LICM/lnicm-sink.ll
@@ -61,6 +61,72 @@
   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
Index: llvm/lib/Transforms/Scalar/LICM.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/LICM.cpp
+++ llvm/lib/Transforms/Scalar/LICM.cpp
@@ -1491,7 +1491,7 @@
                UI->getNumOperands() == 1) {
           if (!CurLoop->contains(UI))
             break;
-          UI = cast<Instruction>(U->user_back());
+          UI = cast<Instruction>(UI->user_back());
         }
       }
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D108166.366748.patch
Type: text/x-patch
Size: 2583 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210816/76d9bcc0/attachment.bin>


More information about the llvm-commits mailing list