[PATCH] D46900: [BasicAA] Fix handling of invariant group launders

Krzysztof Pszeniczny via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue May 15 13:10:56 PDT 2018


amharc created this revision.
amharc added a reviewer: Prazek.
Herald added subscribers: llvm-commits, hiraditya, JDevlieghere.

A recent patch (rL331587 <https://reviews.llvm.org/rL331587>) to Capture Tracking taught it that the `launder_invariant_group` intrinsic captures its argument only by returning it. Unfortunately, BasicAA still considered every call instruction as a possible escape source and hence concluded that the result of a `launder_invariant_group` call cannot alias any local non-escaping value. This led to bug 37458 <https://bugs.llvm.org/show_bug.cgi?id=37458>.

This patch updates the relevant check for escape sources in BasicAA.


Repository:
  rL LLVM

https://reviews.llvm.org/D46900

Files:
  llvm/lib/Analysis/BasicAliasAnalysis.cpp
  llvm/test/Analysis/BasicAA/invariant_group.ll


Index: llvm/test/Analysis/BasicAA/invariant_group.ll
===================================================================
--- /dev/null
+++ llvm/test/Analysis/BasicAA/invariant_group.ll
@@ -0,0 +1,30 @@
+; RUN: opt < %s -basicaa -gvn -S | FileCheck %s
+
+; The input *.ll had been adapted from bug 37458:
+;
+; struct A { virtual void f(); int n; };
+;
+; int h() {
+;     A a;
+;     a.n = 42;
+;     return __builtin_launder(&a)->n;
+; }
+
+%struct.A = type <{ i8*, i8 }>
+
+; CHECK: testLaunderInvariantGroupIsNotEscapeSource
+define i8 @testLaunderInvariantGroupIsNotEscapeSource() {
+entry:
+  %a = alloca %struct.A, align 8
+  %a.bitcast = bitcast %struct.A* %a to i8*
+  %n = getelementptr inbounds %struct.A, %struct.A* %a, i64 0, i32 1
+  store i8 42, i8* %n
+  %a.laundered = call i8* @llvm.launder.invariant.group.p0i8(i8* nonnull %a.bitcast)
+  %n.laundered = getelementptr inbounds i8, i8* %a.laundered, i64 8
+  %v = load i8, i8* %n.laundered
+; make sure that the load from %n.laundered to %v aliases the store of 42 to %n
+; CHECK: ret i8 42
+  ret i8 %v
+}
+
+declare i8* @llvm.launder.invariant.group.p0i8(i8*)
Index: llvm/lib/Analysis/BasicAliasAnalysis.cpp
===================================================================
--- llvm/lib/Analysis/BasicAliasAnalysis.cpp
+++ llvm/lib/Analysis/BasicAliasAnalysis.cpp
@@ -132,7 +132,17 @@
 /// Returns true if the pointer is one which would have been considered an
 /// escape by isNonEscapingLocalObject.
 static bool isEscapeSource(const Value *V) {
-  if (isa<CallInst>(V) || isa<InvokeInst>(V) || isa<Argument>(V))
+  if (auto CS = ImmutableCallSite(V)) {
+    // launder_invariant_group captures its argument only by returning it,
+    // so it might not be considered an escape by isNonEscapingLocalObject
+    if (CS.getIntrinsicID() == Intrinsic::launder_invariant_group) {
+      return false;
+    }
+
+    return true;
+  }
+
+  if (isa<Argument>(V))
     return true;
 
   // The load case works because isNonEscapingLocalObject considers all


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D46900.146901.patch
Type: text/x-patch
Size: 2021 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180515/6d60d164/attachment.bin>


More information about the llvm-commits mailing list