aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/CodeGen/CodeGenTBAA.cpp
diff options
context:
space:
mode:
authorFlorian Hahn <flo@fhahn.com>2024-11-21 20:01:06 +0000
committerGitHub <noreply@github.com>2024-11-21 20:01:06 +0000
commitdecb87452d8e3b93b21ab9e4c3dd03d85cbebfa5 (patch)
treec04e2e8cc4817cdf5680ec7b17e269181f665752 /clang/lib/CodeGen/CodeGenTBAA.cpp
parent93b83642ee34d0092b94776728dad0117c2b72a1 (diff)
downloadllvm-decb87452d8e3b93b21ab9e4c3dd03d85cbebfa5.zip
llvm-decb87452d8e3b93b21ab9e4c3dd03d85cbebfa5.tar.gz
llvm-decb87452d8e3b93b21ab9e4c3dd03d85cbebfa5.tar.bz2
[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: https://github.com/llvm/llvm-project/pull/116991
Diffstat (limited to 'clang/lib/CodeGen/CodeGenTBAA.cpp')
-rw-r--r--clang/lib/CodeGen/CodeGenTBAA.cpp27
1 files changed, 23 insertions, 4 deletions
diff --git a/clang/lib/CodeGen/CodeGenTBAA.cpp b/clang/lib/CodeGen/CodeGenTBAA.cpp
index c31579e..4bcb541 100644
--- a/clang/lib/CodeGen/CodeGenTBAA.cpp
+++ b/clang/lib/CodeGen/CodeGenTBAA.cpp
@@ -205,14 +205,27 @@ llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type *Ty) {
llvm::MDNode *AnyPtr = createScalarTypeNode("any pointer", getChar(), Size);
if (!CodeGenOpts.PointerTBAA)
return AnyPtr;
- // Compute the depth of the pointer and generate a tag of the form "p<depth>
- // <base type tag>".
+ // C++ [basic.lval]p11 permits objects to accessed through an l-value of
+ // similar type. Two types are similar under C++ [conv.qual]p2 if the
+ // decomposition of the types into pointers, member pointers, and arrays has
+ // the same structure when ignoring cv-qualifiers at each level of the
+ // decomposition. Meanwhile, C makes T(*)[] and T(*)[N] compatible, which
+ // would really complicate any attempt to distinguish pointers to arrays by
+ // their bounds. It's simpler, and much easier to explain to users, to
+ // simply treat all pointers to arrays as pointers to their element type for
+ // aliasing purposes. So when creating a TBAA tag for a pointer type, we
+ // recursively ignore both qualifiers and array types when decomposing the
+ // pointee type. The only meaningful remaining structure is the number of
+ // pointer types we encountered along the way, so we just produce the tag
+ // "p<depth> <base type tag>". If we do find a member pointer type, for now
+ // we just conservatively bail out with AnyPtr (below) rather than trying to
+ // create a tag that honors the similar-type rules while still
+ // distinguishing different kinds of member pointer.
unsigned PtrDepth = 0;
do {
PtrDepth++;
- Ty = Ty->getPointeeType().getTypePtr();
+ Ty = Ty->getPointeeType()->getBaseElementTypeUnsafe();
} while (Ty->isPointerType());
- Ty = Context.getBaseElementType(QualType(Ty, 0)).getTypePtr();
assert(!isa<VariableArrayType>(Ty));
// When the underlying type is a builtin type, we compute the pointee type
// string recursively, which is implicitly more forgiving than the standards
@@ -230,6 +243,12 @@ llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type *Ty) {
->getString();
TyName = Name;
} else {
+ // Be conservative if the type isn't a RecordType. We are specifically
+ // required to do this for member pointers until we implement the
+ // similar-types rule.
+ if (!Ty->isRecordType())
+ return AnyPtr;
+
// For non-builtin types use the mangled name of the canonical type.
llvm::raw_svector_ostream TyOut(TyName);
MangleCtx->mangleCanonicalTypeName(QualType(Ty, 0), TyOut);