[PATCH] D51099: [GVN] Invalidate cached info for phis when setting dead predecessors to undef

John Brawn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 22 05:51:25 PDT 2018


john.brawn created this revision.
john.brawn added reviewers: fhahn, efriedma, reames, wmi.

When GVN sets the incoming value for a phi to undef because the incoming block is unreachable it needs to also invalidate the cached info for that phi in MemoryDependenceAnalysis, otherwise later queries will return stale information.


Repository:
  rL LLVM

https://reviews.llvm.org/D51099

Files:
  lib/Transforms/Scalar/GVN.cpp
  test/Transforms/GVN/unreachable-predecessor.ll


Index: test/Transforms/GVN/unreachable-predecessor.ll
===================================================================
--- /dev/null
+++ test/Transforms/GVN/unreachable-predecessor.ll
@@ -0,0 +1,38 @@
+; RUN: opt < %s -gvn -S | FileCheck %s
+
+; loop.then is not reachable from loop, so we should be able to deduce that the
+; store through %phi2 cannot alias %ptr1.
+
+; CHECK-LABEL: @test1
+define void @test1(i32* %ptr1, i32* %ptr2) {
+; CHECK-LABEL: entry:
+; CHECK: %[[GEP:.*]] = getelementptr inbounds i32, i32* %ptr1, i64 1
+; CHECK: %[[VAL1:.*]] = load i32, i32* %[[GEP]]
+entry:
+  br label %loop.preheader
+
+loop.preheader:
+  %gep1 = getelementptr inbounds i32, i32* %ptr1, i64 1
+  br label %loop
+
+; CHECK-LABEL: loop:
+; CHECK-NOT: load
+loop:
+  %phi1 = phi i32* [ %gep1, %loop.preheader ], [ %phi2, %loop.then ]
+  %val1 = load i32, i32* %phi1
+  br i1 false, label %loop.then, label %loop.if
+
+loop.if:
+  %gep2 = getelementptr inbounds i32, i32* %gep1, i64 1
+  %val2 = load i32, i32* %gep2
+  %cmp = icmp slt i32 %val1, %val2
+  br label %loop.then
+
+; CHECK-LABEL: loop.then
+; CHECK: store i32 %[[VAL1]], i32* %phi2
+loop.then:
+  %phi2 = phi i32* [ %ptr2, %loop ], [ %gep2, %loop.if ]
+  store i32 %val1, i32* %phi2
+  store i32 0, i32* %ptr1
+  br label %loop
+}
Index: lib/Transforms/Scalar/GVN.cpp
===================================================================
--- lib/Transforms/Scalar/GVN.cpp
+++ lib/Transforms/Scalar/GVN.cpp
@@ -2457,6 +2457,8 @@
         PHINode &Phi = cast<PHINode>(*II);
         Phi.setIncomingValue(Phi.getBasicBlockIndex(P),
                              UndefValue::get(Phi.getType()));
+        if (MD && Phi.getType()->isPtrOrPtrVectorTy())
+          MD->invalidateCachedPointerInfo(&Phi);
       }
     }
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D51099.161926.patch
Type: text/x-patch
Size: 1779 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180822/2f23fae3/attachment.bin>


More information about the llvm-commits mailing list