[PATCH] D46197: [DA] Fix assertion with loop exiting variables

Dave Green via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 27 10:20:46 PDT 2018


dmgreen created this revision.
dmgreen added reviewers: sebpop, grosser, fhahn, hfinkel.
Herald added a subscriber: javed.absar.

Given code like:

  for(i = ...)
    A[i] = 1;
  A[i] = 2;

The scev for A[i]=2 is {A,+,4}<for>, even though it is outside the loop. This
can confuse DA into thinking it is in the loop, leading to it using a SIV test,
and asserting.


https://reviews.llvm.org/D46197

Files:
  lib/Analysis/DependenceAnalysis.cpp
  test/Analysis/DependenceAnalysis/LoopExitingIV.ll


Index: test/Analysis/DependenceAnalysis/LoopExitingIV.ll
===================================================================
--- /dev/null
+++ test/Analysis/DependenceAnalysis/LoopExitingIV.ll
@@ -0,0 +1,58 @@
+; RUN: opt < %s -da -analyze | FileCheck %s
+; Don't crash
+
+target datalayout = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64"
+
+; CHECK-LABEL: test
+define void @test(i32* nocapture %A, i32 %M) {
+; CHECK: da analyze - none!
+; CHECK: da analyze - output [|<]!
+; CHECK: da analyze - none!
+entry:
+  br label %for.body
+
+for.body:
+  %i = phi i32 [ %inc, %for.body ], [ 0, %entry ]
+  %arrayidx = getelementptr inbounds i32, i32* %A, i32 %i
+  store i32 1, i32* %arrayidx, align 4
+  %inc = add nuw nsw i32 %i, 1
+  %exitcond = icmp eq i32 %inc, %M
+  br i1 %exitcond, label %for.end, label %for.body
+
+for.end:
+  %arrayidx1 = getelementptr inbounds i32, i32* %A, i32 %i
+  store i32 2, i32* %arrayidx1, align 4
+  ret void
+}
+
+; CHECK-LABEL: extraouterloop
+define void @extraouterloop(i32* nocapture %A, i32 %M) {
+; CHECK: da analyze - consistent output [S 0]!
+; CHECK: da analyze - output [*|<]!
+; CHECK: da analyze - output [*]!
+
+entry:
+  br label %for.outer
+
+for.outer:
+  %z = phi i32 [ 0, %entry ], [ %zinc, %for.end ]
+  br label %for.body
+
+for.body:
+  %i = phi i32 [ %inc, %for.body ], [ 0, %for.outer ]
+  %arrayidx = getelementptr inbounds i32, i32* %A, i32 %i
+  store i32 1, i32* %arrayidx, align 4
+  %inc = add nuw nsw i32 %i, 1
+  %exitcond = icmp eq i32 %inc, %M
+  br i1 %exitcond, label %for.end, label %for.body
+
+for.end:
+  %arrayidx1 = getelementptr inbounds i32, i32* %A, i32 %i
+  store i32 2, i32* %arrayidx1, align 4
+  %zinc = add i32 %z, 1
+  %zcmp = icmp ult i32 %zinc, 100
+  br i1 %zcmp, label %for.outer, label %exit
+
+exit:
+  ret void
+}
Index: lib/Analysis/DependenceAnalysis.cpp
===================================================================
--- lib/Analysis/DependenceAnalysis.cpp
+++ lib/Analysis/DependenceAnalysis.cpp
@@ -869,6 +869,9 @@
   const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Src);
   if (!AddRec)
     return isLoopInvariant(Src, LoopNest);
+  if (AddRec->getLoop()->getLoopDepth() >
+      (LoopNest ? LoopNest->getLoopDepth() : 0))
+    return false;
   const SCEV *Start = AddRec->getStart();
   const SCEV *Step = AddRec->getStepRecurrence(*SE);
   const SCEV *UB = SE->getBackedgeTakenCount(AddRec->getLoop());
@@ -894,6 +897,9 @@
   const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Dst);
   if (!AddRec)
     return isLoopInvariant(Dst, LoopNest);
+  if (AddRec->getLoop()->getLoopDepth() >
+      (LoopNest ? LoopNest->getLoopDepth() : 0))
+    return false;
   const SCEV *Start = AddRec->getStart();
   const SCEV *Step = AddRec->getStepRecurrence(*SE);
   const SCEV *UB = SE->getBackedgeTakenCount(AddRec->getLoop());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D46197.144352.patch
Type: text/x-patch
Size: 2841 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180427/21acc309/attachment.bin>


More information about the llvm-commits mailing list