r316211 - [CodeGen] Fix generation of TBAA info for array-to-pointer conversions

Ivan A. Kosarev via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 20 05:35:17 PDT 2017


Author: kosarev
Date: Fri Oct 20 05:35:17 2017
New Revision: 316211

URL: http://llvm.org/viewvc/llvm-project?rev=316211&view=rev
Log:
[CodeGen] Fix generation of TBAA info for array-to-pointer conversions

Resolves:
Fatal error: Offset not zero at the point of scalar access.
http://llvm.org/PR34992

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

Added:
    cfe/trunk/test/CodeGen/tbaa-array.cpp
Modified:
    cfe/trunk/lib/CodeGen/CGExpr.cpp

Modified: cfe/trunk/lib/CodeGen/CGExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExpr.cpp?rev=316211&r1=316210&r2=316211&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExpr.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExpr.cpp Fri Oct 20 05:35:17 2017
@@ -3072,8 +3072,6 @@ Address CodeGenFunction::EmitArrayToPoin
   // Expressions of array type can't be bitfields or vector elements.
   LValue LV = EmitLValue(E);
   Address Addr = LV.getAddress();
-  if (BaseInfo) *BaseInfo = LV.getBaseInfo();
-  if (TBAAInfo) *TBAAInfo = LV.getTBAAInfo();
 
   // If the array type was an incomplete type, we need to make sure
   // the decay ends up being the right type.
@@ -3088,7 +3086,15 @@ Address CodeGenFunction::EmitArrayToPoin
     Addr = Builder.CreateStructGEP(Addr, 0, CharUnits::Zero(), "arraydecay");
   }
 
+  // The result of this decay conversion points to an array element within the
+  // base lvalue. However, since TBAA currently does not support representing
+  // accesses to elements of member arrays, we conservatively represent accesses
+  // to the pointee object as if it had no any base lvalue specified.
+  // TODO: Support TBAA for member arrays.
   QualType EltType = E->getType()->castAsArrayTypeUnsafe()->getElementType();
+  if (BaseInfo) *BaseInfo = LV.getBaseInfo();
+  if (TBAAInfo) *TBAAInfo = CGM.getTBAAAccessInfo(EltType);
+
   return Builder.CreateElementBitCast(Addr, ConvertTypeForMem(EltType));
 }
 

Added: cfe/trunk/test/CodeGen/tbaa-array.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/tbaa-array.cpp?rev=316211&view=auto
==============================================================================
--- cfe/trunk/test/CodeGen/tbaa-array.cpp (added)
+++ cfe/trunk/test/CodeGen/tbaa-array.cpp Fri Oct 20 05:35:17 2017
@@ -0,0 +1,18 @@
+// RUN: %clang_cc1 -triple x86_64-linux -O1 -disable-llvm-passes %s \
+// RUN:     -emit-llvm -o - | FileCheck %s
+//
+// Check that we generate correct TBAA information for accesses to array
+// elements.
+
+struct A { int i; };
+struct B { A a[1]; };
+
+int foo(B *b) {
+// CHECK-LABEL: _Z3fooP1B
+// CHECK: load i32, {{.*}}, !tbaa [[TAG_A_i:!.*]]
+  return b->a->i;
+}
+
+// CHECK-DAG: [[TAG_A_i]] = !{[[TYPE_A:!.*]], [[TYPE_int:!.*]], i64 0}
+// CHECK-DAG: [[TYPE_A]] = !{!"_ZTS1A", !{{.*}}, i64 0}
+// CHECK-DAG: [[TYPE_int]] = !{!"int", !{{.*}}, i64 0}




More information about the cfe-commits mailing list