kosarev created this revision.
kosarev added a project: clang.

Repository:
  rL LLVM

https://reviews.llvm.org/D39177

Files:
  lib/CodeGen/CGBlocks.cpp
  lib/CodeGen/CGExpr.cpp
  lib/CodeGen/CGOpenMPRuntime.cpp
  lib/CodeGen/CGStmtOpenMP.cpp
  lib/CodeGen/CodeGenFunction.h
  test/CodeGen/tbaa-reference.cpp

Index: test/CodeGen/tbaa-reference.cpp
===================================================================
--- test/CodeGen/tbaa-reference.cpp
+++ test/CodeGen/tbaa-reference.cpp
@@ -6,24 +6,32 @@
 
 struct B {
   S &s;
-  B(S &s) : s(s) {}
-  void bar();
+  B(S &s);
+  S &get();
 };
 
-void foo(S &s) {
-  B b(s);
-  b.bar();
-}
-
-// CHECK-LABEL: _Z3fooR1S
-// Check initialization of the reference parameter in foo().
-// CHECK: store %struct.S* {{.*}}, %struct.S** {{.*}}, !tbaa [[TAG_pointer:!.*]]
-//
+B::B(S &s) : s(s) {
 // CHECK-LABEL: _ZN1BC2ER1S
-// TODO: Check loading of the reference parameter in B::B(S&).
-// Check initialization of B::s in B::B(S&).
+// Check initialization of the reference parameter.
+// CHECK: store %struct.S* {{.*}}, %struct.S** {{.*}}, !tbaa [[TAG_pointer:!.*]]
+
+// Check loading of the reference parameter.
+// CHECK: load %struct.S*, %struct.S** {{.*}}, !tbaa [[TAG_pointer]]
+
+// Check initialization of the reference member.
 // CHECK: store %struct.S* {{.*}}, %struct.S** {{.*}}, !tbaa [[TAG_pointer]]
-//
+}
+
+S &B::get() {
+// CHECK-LABEL: _ZN1B3getEv
+// Check that we access the reference as a structure member.
+// CHECK: load %struct.S*, %struct.S** {{.*}}, !tbaa [[TAG_B_s:!.*]]
+  return s;
+}
+
 // CHECK-DAG: [[TAG_pointer]] = !{[[TYPE_pointer:!.*]], [[TYPE_pointer]], i64 0}
+// CHECK-DAG: [[TAG_B_s]] = !{[[TYPE_B:!.*]], [[TYPE_pointer]], i64 0}
+//
+// CHECK-DAG: [[TYPE_B]] = !{!"_ZTS1B", [[TYPE_pointer]], i64 0}
 // CHECK-DAG: [[TYPE_pointer]] = !{!"any pointer", [[TYPE_char:!.*]], i64 0}
 // CHECK-DAG: [[TYPE_char]] = !{!"omnipotent char", {{!.*}}, i64 0}
Index: lib/CodeGen/CodeGenFunction.h
===================================================================
--- lib/CodeGen/CodeGenFunction.h
+++ lib/CodeGen/CodeGenFunction.h
@@ -1952,10 +1952,25 @@
                                            LValueBaseInfo *BaseInfo = nullptr,
                                            TBAAAccessInfo *TBAAInfo = nullptr);
 
-  Address EmitLoadOfReference(Address Ref, const ReferenceType *RefTy,
-                              LValueBaseInfo *BaseInfo = nullptr,
-                              TBAAAccessInfo *TBAAInfo = nullptr);
-  LValue EmitLoadOfReferenceLValue(Address Ref, const ReferenceType *RefTy);
+  Address EmitLoadOfReference(Address ReferenceAddr,
+                              const ReferenceType *ReferenceTy,
+                              LValueBaseInfo ReferenceBaseInfo,
+                              TBAAAccessInfo ReferenceTBAAInfo,
+                              bool IsVolatile = false,
+                              LValueBaseInfo *ReferenceeBaseInfo = nullptr,
+                              TBAAAccessInfo *ReferenceeTBAAInfo = nullptr);
+  LValue EmitLoadOfReferenceLValue(Address ReferenceAddr,
+                                   const ReferenceType *ReferenceTy,
+                                   LValueBaseInfo ReferenceBaseInfo,
+                                   TBAAAccessInfo ReferenceTBAAInfo);
+  LValue EmitLoadOfReferenceLValue(Address ReferenceAddr,
+                                   const ReferenceType *ReferenceTy,
+                                   AlignmentSource Source =
+                                       AlignmentSource::Type) {
+    return EmitLoadOfReferenceLValue(
+        ReferenceAddr, ReferenceTy, LValueBaseInfo(Source),
+        CGM.getTBAAAccessInfo(QualType(ReferenceTy, 0)));
+  }
 
   Address EmitLoadOfPointer(Address Ptr, const PointerType *PtrTy,
                             LValueBaseInfo *BaseInfo = nullptr,
Index: lib/CodeGen/CGStmtOpenMP.cpp
===================================================================
--- lib/CodeGen/CGStmtOpenMP.cpp
+++ lib/CodeGen/CGStmtOpenMP.cpp
@@ -412,7 +412,8 @@
       if (!VarTy->isReferenceType()) {
         if (ArgLVal.getType()->isLValueReferenceType()) {
           ArgAddr = CGF.EmitLoadOfReference(
-              ArgAddr, ArgLVal.getType()->castAs<ReferenceType>());
+              ArgAddr, ArgLVal.getType()->castAs<ReferenceType>(),
+              ArgLVal.getBaseInfo(), ArgLVal.getTBAAInfo());
         } else if (!VarTy->isVariablyModifiedType() || !VarTy->isPointerType()) {
           assert(ArgLVal.getType()->isPointerType());
           ArgAddr = CGF.EmitLoadOfPointer(
Index: lib/CodeGen/CGOpenMPRuntime.cpp
===================================================================
--- lib/CodeGen/CGOpenMPRuntime.cpp
+++ lib/CodeGen/CGOpenMPRuntime.cpp
@@ -1039,7 +1039,9 @@
       BaseLV = CGF.EmitLoadOfPointerLValue(BaseLV.getAddress(), PtrTy);
     else {
       BaseLV = CGF.EmitLoadOfReferenceLValue(BaseLV.getAddress(),
-                                             BaseTy->castAs<ReferenceType>());
+                                             BaseTy->castAs<ReferenceType>(),
+                                             BaseLV.getBaseInfo(),
+                                             BaseLV.getTBAAInfo());
     }
     BaseTy = BaseTy->getPointeeType();
   }
Index: lib/CodeGen/CGExpr.cpp
===================================================================
--- lib/CodeGen/CGExpr.cpp
+++ lib/CodeGen/CGExpr.cpp
@@ -2161,22 +2161,40 @@
   return CGF.MakeAddrLValue(Addr, T, AlignmentSource::Decl);
 }
 
-Address CodeGenFunction::EmitLoadOfReference(Address Addr,
-                                             const ReferenceType *RefTy,
-                                             LValueBaseInfo *BaseInfo,
-                                             TBAAAccessInfo *TBAAInfo) {
-  llvm::Value *Ptr = Builder.CreateLoad(Addr);
-  return Address(Ptr, getNaturalTypeAlignment(RefTy->getPointeeType(),
-                                              BaseInfo, TBAAInfo,
-                                              /* forPointeeType= */ true));
+Address
+CodeGenFunction::EmitLoadOfReference(Address ReferenceAddr,
+                                     const ReferenceType *ReferenceTy,
+                                     LValueBaseInfo ReferenceBaseInfo,
+                                     TBAAAccessInfo ReferenceTBAAInfo,
+                                     bool IsVolatile,
+                                     LValueBaseInfo *ReferenceeBaseInfo,
+                                     TBAAAccessInfo *ReferenceeTBAAInfo) {
+  llvm::LoadInst *Load = Builder.CreateLoad(ReferenceAddr, IsVolatile);
+  if (ReferenceBaseInfo.getMayAlias())
+    ReferenceTBAAInfo = CGM.getTBAAMayAliasAccessInfo();
+  CGM.DecorateInstructionWithTBAA(Load, ReferenceTBAAInfo);
+
+  return Address(Load, getNaturalTypeAlignment(ReferenceTy->getPointeeType(),
+                                               ReferenceeBaseInfo,
+                                               ReferenceeTBAAInfo,
+                                               /* forPointeeType= */ true));
 }
 
-LValue CodeGenFunction::EmitLoadOfReferenceLValue(Address RefAddr,
-                                                  const ReferenceType *RefTy) {
-  LValueBaseInfo BaseInfo;
-  TBAAAccessInfo TBAAInfo;
-  Address Addr = EmitLoadOfReference(RefAddr, RefTy, &BaseInfo, &TBAAInfo);
-  return MakeAddrLValue(Addr, RefTy->getPointeeType(), BaseInfo, TBAAInfo);
+LValue
+CodeGenFunction::EmitLoadOfReferenceLValue(Address ReferenceAddr,
+                                           const ReferenceType *ReferenceTy,
+                                           LValueBaseInfo ReferenceBaseInfo,
+                                           TBAAAccessInfo ReferenceTBAAInfo) {
+  LValueBaseInfo ReferenceeBaseInfo;
+  TBAAAccessInfo ReferenceeTBAAInfo;
+  Address ReferenceeAddr = EmitLoadOfReference(ReferenceAddr, ReferenceTy,
+                                               ReferenceBaseInfo,
+                                               ReferenceTBAAInfo,
+                                               /* IsVolatile= */ false,
+                                               &ReferenceeBaseInfo,
+                                               &ReferenceeTBAAInfo);
+  return MakeAddrLValue(ReferenceeAddr, ReferenceTy->getPointeeType(),
+                        ReferenceeBaseInfo, ReferenceeTBAAInfo);
 }
 
 Address CodeGenFunction::EmitLoadOfPointer(Address Ptr,
@@ -2218,7 +2236,7 @@
     return EmitThreadPrivateVarDeclLValue(CGF, VD, T, Addr, RealVarTy,
                                           E->getExprLoc());
   if (auto RefTy = VD->getType()->getAs<ReferenceType>()) {
-    LV = CGF.EmitLoadOfReferenceLValue(Addr, RefTy);
+    LV = CGF.EmitLoadOfReferenceLValue(Addr, RefTy, AlignmentSource::Decl);
   } else {
     LV = CGF.MakeAddrLValue(Addr, T, AlignmentSource::Decl);
   }
@@ -2340,7 +2358,8 @@
         auto I = LocalDeclMap.find(VD);
         if (I != LocalDeclMap.end()) {
           if (auto RefTy = VD->getType()->getAs<ReferenceType>())
-            return EmitLoadOfReferenceLValue(I->second, RefTy);
+            return EmitLoadOfReferenceLValue(I->second, RefTy,
+                                             AlignmentSource::Decl);
           return MakeAddrLValue(I->second, T);
         }
         LValue CapLVal =
@@ -2413,7 +2432,7 @@
     // Drill into reference types.
     LValue LV;
     if (auto RefTy = VD->getType()->getAs<ReferenceType>()) {
-      LV = EmitLoadOfReferenceLValue(addr, RefTy);
+      LV = EmitLoadOfReferenceLValue(addr, RefTy, AlignmentSource::Decl);
     } else {
       LV = MakeAddrLValue(addr, T, AlignmentSource::Decl);
     }
@@ -3749,7 +3768,7 @@
   }
 
   Address addr = base.getAddress();
-  unsigned cvr = base.getVRQualifiers();
+  unsigned RecordCVR = base.getVRQualifiers();
   if (rec->isUnion()) {
     // For unions, there is no pointer adjustment.
     assert(!FieldType->isReferenceType() && "union has reference member");
@@ -3764,22 +3783,15 @@
     addr = emitAddrOfFieldStorage(*this, addr, field);
 
     // If this is a reference field, load the reference right now.
-    if (const ReferenceType *refType = FieldType->getAs<ReferenceType>()) {
-      llvm::LoadInst *load = Builder.CreateLoad(addr, "ref");
-      if (cvr & Qualifiers::Volatile) load->setVolatile(true);
-
-      CGM.DecorateInstructionWithTBAA(load, FieldTBAAInfo);
-
-      FieldType = refType->getPointeeType();
-      CharUnits Align = getNaturalTypeAlignment(FieldType, &FieldBaseInfo,
-                                                &FieldTBAAInfo,
-                                                /* forPointeeType= */ true);
-      addr = Address(load, Align);
-
-      // Qualifiers on the struct don't apply to the referencee, and
-      // we'll pick up CVR from the actual type later, so reset these
-      // additional qualifiers now.
-      cvr = 0;
+    if (const ReferenceType *RefType = FieldType->getAs<ReferenceType>()) {
+      LValueBaseInfo RefBaseInfo = FieldBaseInfo;
+      TBAAAccessInfo RefTBAAInfo = FieldTBAAInfo;
+      addr = EmitLoadOfReference(addr, RefType, RefBaseInfo, RefTBAAInfo,
+                                 RecordCVR & Qualifiers::Volatile,
+                                 &FieldBaseInfo, &FieldTBAAInfo);
+      // Qualifiers on the struct don't apply to the referencee.
+      RecordCVR = 0;
+      FieldType = RefType->getPointeeType();
     }
   }
 
@@ -3794,7 +3806,7 @@
     addr = EmitFieldAnnotations(field, addr);
 
   LValue LV = MakeAddrLValue(addr, FieldType, FieldBaseInfo, FieldTBAAInfo);
-  LV.getQuals().addCVRQualifiers(cvr);
+  LV.getQuals().addCVRQualifiers(RecordCVR);
 
   // __weak attribute on a field is ignored.
   if (LV.getQuals().getObjCGCAttr() == Qualifiers::Weak)
Index: lib/CodeGen/CGBlocks.cpp
===================================================================
--- lib/CodeGen/CGBlocks.cpp
+++ lib/CodeGen/CGBlocks.cpp
@@ -1189,8 +1189,10 @@
                                  variable->getName());
   }
 
-  if (auto refType = capture.fieldType()->getAs<ReferenceType>())
-    addr = EmitLoadOfReference(addr, refType);
+  QualType FieldType = capture.fieldType();
+  if (auto RefType = FieldType->getAs<ReferenceType>())
+    addr = EmitLoadOfReference(addr, RefType, LValueBaseInfo(),
+                               CGM.getTBAAAccessInfo(FieldType));
 
   return addr;
 }
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to