[PATCH] [PR16236] Be conservative when doing TypeBasedAlias Analysis of members of Struct

Karthik Bhat kv.bhat at samsung.com
Tue Apr 8 05:04:34 PDT 2014


Hi majnemer, sunfish, rengolin,

Hi All,
This patches fixes PR16236 aproblem with TypeBasedAlias Analysis. Below is my understanding please help me review this patch and if i'm thinking in the right direction.
The problem in this case seems to be that in case we have an union like -
  union
  {
    short f0;
    int f1;
  } u, *up = &u;

f0 and f1 may alias each other. But while constructing AliasSet for these in TypeBasedAlias Analysis during PathAliases we see that both the MDNodes corresponding to (f0 and f1) are not ancestor of each other and have a common root and thus conclude that they will Not alias each other. But in case of union this may not be correct f1 anf f0 may alias each other as thye operate on same underlining memory location. 

In this patch we check that if both correspond to StructTyID type we evaluate it conservatively so that we do not incorrectly conclude that these two types are NoAlias.

But doing this will also prevent promotion in case of structs were the 2 types will not alias each other but i'm not sure if we can differentiate between union and struct  in llvm AliasAnalysis.

Could someone please review the same and let me know if this approach is correct?

Thanks and Regards
Karthik Bhat

http://reviews.llvm.org/D3316

Files:
  test/Analysis/TypeBasedAliasAnalysis/placement-tbaa.ll
  lib/Analysis/TypeBasedAliasAnalysis.cpp

Index: test/Analysis/TypeBasedAliasAnalysis/placement-tbaa.ll
===================================================================
--- test/Analysis/TypeBasedAliasAnalysis/placement-tbaa.ll
+++ test/Analysis/TypeBasedAliasAnalysis/placement-tbaa.ll
@@ -16,9 +16,9 @@
 ;   return f->i;
 ; }
 
-; Basic AA says MayAlias, TBAA says NoAlias
+; Basic AA says MayAlias, TBAA also conservatively says MayAlias
 ; CHECK: MayAlias: i64* %i5, i8** %p
-; CHECK: NoAlias: store i64 %conv, i64* %i5, align 8, !tbaa !6 <->   store i8* null, i8** %p, align 8, !tbaa !9
+; CHECK: MayAlias: store i64 %conv, i64* %i5, align 8, !tbaa !6 <->   store i8* null, i8** %p, align 8, !tbaa !9
 
 %struct.Foo = type { i64 }
 %struct.Bar = type { i8* }
Index: lib/Analysis/TypeBasedAliasAnalysis.cpp
===================================================================
--- lib/Analysis/TypeBasedAliasAnalysis.cpp
+++ lib/Analysis/TypeBasedAliasAnalysis.cpp
@@ -448,6 +448,18 @@
   if (!EnableTBAA)
     return AliasAnalysis::alias(LocA, LocB);
 
+  // Be conservative when comparing elements of StructType as members of union
+  // MayAlias each other.
+  const Value* VA = LocA.Ptr;
+  const Value* VB = LocB.Ptr;
+  if (VA->getType()->getTypeID() == Type::PointerTyID &&
+      VB->getType()->getTypeID() == Type::PointerTyID) {
+    Type* TyA = cast<PointerType>(VA->getType())->getElementType();
+    Type* TyB = cast<PointerType>(VB->getType())->getElementType();
+    if(TyA->getTypeID() == Type::StructTyID &&
+       TyB->getTypeID() == Type::StructTyID)
+      return AliasAnalysis::alias(LocA, LocB);
+  }
   // Get the attached MDNodes. If either value lacks a tbaa MDNode, we must
   // be conservative.
   const MDNode *AM = LocA.TBAATag;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D3316.1.patch
Type: text/x-patch
Size: 1723 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140408/e171749f/attachment.bin>


More information about the llvm-commits mailing list