[PATCH] D122671: [BasicAA] Check if V is itself a PHI in isValueEqualInPotentialCycles

Justin Holewinski via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 29 10:53:18 PDT 2022


jholewinski created this revision.
Herald added subscribers: jeroen.dobbelaere, hiraditya.
Herald added a project: All.
jholewinski requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

BasicAAResult::isValueEqualInPotentialCycles uses previously visited
PHI nodes to determine if a value is provably equal across cycles.
But it does not consider the case where the value itself is a PHI
node that is reachable from itself, implying a cycle in the CFG. This
change teaches isValueEqualInPotentialCycles to handle this case by
explicitly checking if the incoming value is a PHI and checking if it
is reachable from itself.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D122671

Files:
  llvm/lib/Analysis/BasicAliasAnalysis.cpp
  llvm/test/Analysis/BasicAA/value-equal-cycle-phi.ll


Index: llvm/test/Analysis/BasicAA/value-equal-cycle-phi.ll
===================================================================
--- /dev/null
+++ llvm/test/Analysis/BasicAA/value-equal-cycle-phi.ll
@@ -0,0 +1,40 @@
+; RUN: opt -aa-pipeline=basic-aa -passes=aa-eval -print-all-alias-modref-info -disable-output < %s 2>&1 | FileCheck %s
+
+; CHECK-LABEL: may_alias_ptr
+; CHECK: MayAlias: float* %p, float* %p.1
+
+define void @may_alias_ptr(float* %arr, float* %out, i32* %idx) local_unnamed_addr {
+entry:
+  br label %header
+
+header:
+  %i = phi i32 [ 0, %entry ], [ %i.next, %latch ]
+  %p.3 = getelementptr i32, i32* %idx, i32 %i
+  %t = load i32, i32* %p.3, align 4
+  %a = icmp eq i32 %t, 0
+  br i1 %a, label %if.then, label %if.else
+
+if.then:
+  ; BasicAA cannot know whether the prior iteration wrote to arr[i], so it must
+  ; return MayAlias between %p and %p.1.
+  %p = getelementptr inbounds float, float* %arr, i32 %i
+  %v = load float, float* %p, align 4
+  %p.2 = getelementptr inbounds float, float* %out, i32 0
+  store float %v, float* %p.2, align 4
+  br label %latch
+
+if.else:
+  ; This will write to arr[i+1], which will be arr[i] on the next iteration
+  %i.0 = add nuw nsw i32 %i, 1
+  %p.1 = getelementptr float, float* %arr, i32 %i.0
+  store float 0.0, float* %p.1, align 4
+  br label %latch
+
+latch:
+  %i.next = add nuw nsw i32 %i, 1
+  %c = icmp ult i32 %i.next, 4
+  br i1 %c, label %header, label %exit
+
+exit:
+  ret void
+}
Index: llvm/lib/Analysis/BasicAliasAnalysis.cpp
===================================================================
--- llvm/lib/Analysis/BasicAliasAnalysis.cpp
+++ llvm/lib/Analysis/BasicAliasAnalysis.cpp
@@ -1779,6 +1779,16 @@
   if (!Inst)
     return true;
 
+  // Check if the value is itself a PHI node that is reachable from itself,
+  // implying a cycle that can cause the value to have a different value on
+  // different iterations. In this case, return false to indicate that we cannot
+  // prove the two values will be equal across cycles.
+  if (const PHINode *PN = dyn_cast<const PHINode>(Inst)) {
+    if (isPotentiallyReachable(PN->getParent(), PN->getParent(), nullptr, DT)) {
+      return false;
+    }
+  }
+
   if (VisitedPhiBBs.empty())
     return true;
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D122671.418928.patch
Type: text/x-patch
Size: 2248 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220329/92fa7695/attachment.bin>


More information about the llvm-commits mailing list