Skip to content

Commit decb874

Browse files
authored
[TBAA] Only emit pointer tbaa metedata for record types. (#116991)
Be conservative if the type isn't a record type. Handling other types may require stripping const-qualifiers inside the type, e.g. MemberPointerType. Also look through array types same as through pointer types, to not pessimize arrays of pointers. Without this, we assign different tags to the accesses for p an q in the second test in cwg158. PR: #116991
1 parent 93b8364 commit decb874

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

clang/lib/CodeGen/CodeGenTBAA.cpp

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,27 @@ llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type *Ty) {
205205
llvm::MDNode *AnyPtr = createScalarTypeNode("any pointer", getChar(), Size);
206206
if (!CodeGenOpts.PointerTBAA)
207207
return AnyPtr;
208-
// Compute the depth of the pointer and generate a tag of the form "p<depth>
209-
// <base type tag>".
208+
// C++ [basic.lval]p11 permits objects to accessed through an l-value of
209+
// similar type. Two types are similar under C++ [conv.qual]p2 if the
210+
// decomposition of the types into pointers, member pointers, and arrays has
211+
// the same structure when ignoring cv-qualifiers at each level of the
212+
// decomposition. Meanwhile, C makes T(*)[] and T(*)[N] compatible, which
213+
// would really complicate any attempt to distinguish pointers to arrays by
214+
// their bounds. It's simpler, and much easier to explain to users, to
215+
// simply treat all pointers to arrays as pointers to their element type for
216+
// aliasing purposes. So when creating a TBAA tag for a pointer type, we
217+
// recursively ignore both qualifiers and array types when decomposing the
218+
// pointee type. The only meaningful remaining structure is the number of
219+
// pointer types we encountered along the way, so we just produce the tag
220+
// "p<depth> <base type tag>". If we do find a member pointer type, for now
221+
// we just conservatively bail out with AnyPtr (below) rather than trying to
222+
// create a tag that honors the similar-type rules while still
223+
// distinguishing different kinds of member pointer.
210224
unsigned PtrDepth = 0;
211225
do {
212226
PtrDepth++;
213-
Ty = Ty->getPointeeType().getTypePtr();
227+
Ty = Ty->getPointeeType()->getBaseElementTypeUnsafe();
214228
} while (Ty->isPointerType());
215-
Ty = Context.getBaseElementType(QualType(Ty, 0)).getTypePtr();
216229
assert(!isa<VariableArrayType>(Ty));
217230
// When the underlying type is a builtin type, we compute the pointee type
218231
// string recursively, which is implicitly more forgiving than the standards
@@ -230,6 +243,12 @@ llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type *Ty) {
230243
->getString();
231244
TyName = Name;
232245
} else {
246+
// Be conservative if the type isn't a RecordType. We are specifically
247+
// required to do this for member pointers until we implement the
248+
// similar-types rule.
249+
if (!Ty->isRecordType())
250+
return AnyPtr;
251+
233252
// For non-builtin types use the mangled name of the canonical type.
234253
llvm::raw_svector_ostream TyOut(TyName);
235254
MangleCtx->mangleCanonicalTypeName(QualType(Ty, 0), TyOut);

clang/test/CXX/drs/cwg158.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
// RUN: %clang_cc1 -triple x86_64-linux -std=c++98 %s -O3 -disable-llvm-passes -pedantic-errors -emit-llvm -o - | FileCheck %s
2-
// RUN: %clang_cc1 -triple x86_64-linux -std=c++11 %s -O3 -disable-llvm-passes -pedantic-errors -emit-llvm -o - | FileCheck %s
3-
// RUN: %clang_cc1 -triple x86_64-linux -std=c++14 %s -O3 -disable-llvm-passes -pedantic-errors -emit-llvm -o - | FileCheck %s
4-
// RUN: %clang_cc1 -triple x86_64-linux -std=c++1z %s -O3 -disable-llvm-passes -pedantic-errors -emit-llvm -o - | FileCheck %s
1+
// RUN: %clang_cc1 -triple x86_64-linux -std=c++98 %s -O3 -disable-llvm-passes -pedantic-errors -emit-llvm -o - | FileCheck --check-prefixes=CHECK %s
2+
// RUN: %clang_cc1 -triple x86_64-linux -std=c++11 %s -O3 -disable-llvm-passes -pedantic-errors -emit-llvm -o - | FileCheck --check-prefixes=CHECK %s
3+
// RUN: %clang_cc1 -triple x86_64-linux -std=c++14 %s -O3 -disable-llvm-passes -pedantic-errors -emit-llvm -o - | FileCheck --check-prefixes=CHECK %s
4+
// RUN: %clang_cc1 -triple x86_64-linux -std=c++1z %s -O3 -disable-llvm-passes -pedantic-errors -emit-llvm -o - | FileCheck --check-prefixes=CHECK %s
5+
// RUN: %clang_cc1 -triple x86_64-linux -std=c++1z %s -O3 -pointer-tbaa -disable-llvm-passes -pedantic-errors -emit-llvm -o - | FileCheck --check-prefixes=CHECK,POINTER-TBAA %s
56

67
// cwg158: yes
78

89
// CHECK-LABEL: define {{.*}} @_Z1f
910
const int *f(const int * const *p, int **q) {
11+
// CHECK: load ptr, ptr %p.addr
1012
// CHECK: load ptr, {{.*}}, !tbaa ![[INTPTR_TBAA:[^,]*]]
1113
const int *x = *p;
1214
// CHECK: store ptr null, {{.*}}, !tbaa ![[INTPTR_TBAA]]
@@ -18,10 +20,24 @@ struct A {};
1820

1921
// CHECK-LABEL: define {{.*}} @_Z1g
2022
const int *(A::*const *g(const int *(A::* const **p)[3], int *(A::***q)[3]))[3] {
23+
// CHECK: load ptr, ptr %p.addr
2124
// CHECK: load ptr, {{.*}}, !tbaa ![[MEMPTR_TBAA:[^,]*]]
2225
const int *(A::*const *x)[3] = *p;
2326
// CHECK: store ptr null, {{.*}}, !tbaa ![[MEMPTR_TBAA]]
2427
*q = 0;
2528
return x;
2629
}
2730

31+
// CHECK-LABEL: define {{.*}} @_Z1h
32+
const int * h(const int * (*p)[10], int *(*q)[9]) {
33+
// CHECK: load ptr, ptr %p.addr, align 8, !tbaa [[PTRARRAY_TBAA:!.+]]
34+
const int * x = *p[0];
35+
36+
// CHECK: load ptr, ptr %q.addr, align 8, !tbaa [[PTRARRAY_TBAA]]
37+
*q[0] = 0;
38+
return x;
39+
}
40+
41+
// POINTER-TBAA: [[PTRARRAY_TBAA]] = !{[[PTRARRAY_TY:!.+]], [[PTRARRAY_TY]], i64 0}
42+
// POINTER-TBAA: [[PTRARRAY_TY]] = !{!"p2 int", [[ANYPTR:!.+]], i64 0}
43+
// POINTER-TBAA: [[ANYPTR]] = !{!"any pointer"

0 commit comments

Comments
 (0)