[CodeGen] Introduce generic TBAA access descriptors

With this patch we implement a concept of TBAA access descriptors
that are capable of representing both scalar and struct-path
accesses in a generic way.

This is part of D37826 reworked to be a separate patch to
simplify review.

Differential Revision: https://reviews.llvm.org/D38456

llvm-svn: 314780
diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index cd2e8e1..74ad9dc 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -1375,9 +1375,7 @@
                                                SourceLocation Loc) {
   return EmitLoadOfScalar(lvalue.getAddress(), lvalue.isVolatile(),
                           lvalue.getType(), Loc, lvalue.getBaseInfo(),
-                          lvalue.getTBAAAccessType(),
-                          lvalue.getTBAABaseType(), lvalue.getTBAAOffset(),
-                          lvalue.isNontemporal());
+                          lvalue.getTBAAInfo(), lvalue.isNontemporal());
 }
 
 static bool hasBooleanRepresentation(QualType Ty) {
@@ -1487,9 +1485,7 @@
                                                QualType Ty,
                                                SourceLocation Loc,
                                                LValueBaseInfo BaseInfo,
-                                               llvm::MDNode *TBAAAccessType,
-                                               QualType TBAABaseType,
-                                               uint64_t TBAAOffset,
+                                               TBAAAccessInfo TBAAInfo,
                                                bool isNontemporal) {
   if (!CGM.getCodeGenOpts().PreserveVec3Type) {
     // For better performance, handle vector loads differently.
@@ -1518,7 +1514,7 @@
 
   // Atomic operations have to be done on integral types.
   LValue AtomicLValue =
-      LValue::MakeAddr(Addr, Ty, getContext(), BaseInfo, TBAAAccessType);
+      LValue::MakeAddr(Addr, Ty, getContext(), BaseInfo, TBAAInfo.AccessType);
   if (Ty->isAtomicType() || LValueIsSuitableForInlineAtomic(AtomicLValue)) {
     return EmitAtomicLoad(AtomicLValue, Loc).getScalarVal();
   }
@@ -1529,11 +1525,11 @@
         Load->getContext(), llvm::ConstantAsMetadata::get(Builder.getInt32(1)));
     Load->setMetadata(CGM.getModule().getMDKindID("nontemporal"), Node);
   }
-  if (TBAAAccessType) {
+  if (TBAAInfo.AccessType) {
     bool MayAlias = BaseInfo.getMayAlias();
     llvm::MDNode *TBAA = MayAlias
         ? CGM.getTBAAMayAliasTypeInfo()
-        : CGM.getTBAAStructTagInfo(TBAABaseType, TBAAAccessType, TBAAOffset);
+        : CGM.getTBAAStructTagInfo(TBAAInfo);
     if (TBAA)
       CGM.DecorateInstructionWithTBAA(Load, TBAA, MayAlias);
   }
@@ -1576,11 +1572,8 @@
 void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, Address Addr,
                                         bool Volatile, QualType Ty,
                                         LValueBaseInfo BaseInfo,
-                                        llvm::MDNode *TBAAAccessType,
-                                        bool isInit, QualType TBAABaseType,
-                                        uint64_t TBAAOffset,
-                                        bool isNontemporal) {
-
+                                        TBAAAccessInfo TBAAInfo,
+                                        bool isInit, bool isNontemporal) {
   if (!CGM.getCodeGenOpts().PreserveVec3Type) {
     // Handle vectors differently to get better performance.
     if (Ty->isVectorType()) {
@@ -1606,7 +1599,7 @@
   Value = EmitToMemory(Value, Ty);
 
   LValue AtomicLValue =
-      LValue::MakeAddr(Addr, Ty, getContext(), BaseInfo, TBAAAccessType);
+      LValue::MakeAddr(Addr, Ty, getContext(), BaseInfo, TBAAInfo.AccessType);
   if (Ty->isAtomicType() ||
       (!isInit && LValueIsSuitableForInlineAtomic(AtomicLValue))) {
     EmitAtomicStore(RValue::get(Value), AtomicLValue, isInit);
@@ -1620,11 +1613,11 @@
                           llvm::ConstantAsMetadata::get(Builder.getInt32(1)));
     Store->setMetadata(CGM.getModule().getMDKindID("nontemporal"), Node);
   }
-  if (TBAAAccessType) {
+  if (TBAAInfo.AccessType) {
     bool MayAlias = BaseInfo.getMayAlias();
     llvm::MDNode *TBAA = MayAlias
         ? CGM.getTBAAMayAliasTypeInfo()
-        : CGM.getTBAAStructTagInfo(TBAABaseType, TBAAAccessType, TBAAOffset);
+        : CGM.getTBAAStructTagInfo(TBAAInfo);
     if (TBAA)
       CGM.DecorateInstructionWithTBAA(Store, TBAA, MayAlias);
   }
@@ -1634,9 +1627,7 @@
                                         bool isInit) {
   EmitStoreOfScalar(value, lvalue.getAddress(), lvalue.isVolatile(),
                     lvalue.getType(), lvalue.getBaseInfo(),
-                    lvalue.getTBAAAccessType(), isInit,
-                    lvalue.getTBAABaseType(), lvalue.getTBAAOffset(),
-                    lvalue.isNontemporal());
+                    lvalue.getTBAAInfo(), isInit, lvalue.isNontemporal());
 }
 
 /// EmitLoadOfLValue - Given an expression that represents a value lvalue, this
@@ -3776,10 +3767,13 @@
         getContext().getASTRecordLayout(field->getParent());
     // Set the base type to be the base type of the base LValue and
     // update offset to be relative to the base type.
-    LV.setTBAABaseType(mayAlias ? getContext().CharTy : base.getTBAABaseType());
-    LV.setTBAAOffset(mayAlias ? 0 : base.getTBAAOffset() +
-                     Layout.getFieldOffset(field->getFieldIndex()) /
-                                           getContext().getCharWidth());
+    unsigned CharWidth = getContext().getCharWidth();
+    TBAAAccessInfo TBAAInfo = mayAlias ?
+      TBAAAccessInfo(CGM.getTBAAMayAliasTypeInfo()) :
+      TBAAAccessInfo(base.getTBAAInfo().BaseType, CGM.getTBAATypeInfo(type),
+                     base.getTBAAInfo().Offset + Layout.getFieldOffset(
+                         field->getFieldIndex()) / CharWidth);
+    LV.setTBAAInfo(TBAAInfo);
   }
 
   // __weak attribute on a field is ignored.