[clang] [TBAA] Extend pointer TBAA to pointers of non-builtin types. (PR #110569)

John McCall via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 15 13:05:17 PDT 2024


================
@@ -221,21 +221,27 @@ llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type *Ty) {
       PtrDepth++;
       Ty = Ty->getPointeeType().getTypePtr();
     } while (Ty->isPointerType());
-    // TODO: Implement C++'s type "similarity" and consider dis-"similar"
-    // pointers distinct for non-builtin types.
+
+    SmallString<256> TyName;
     if (isa<BuiltinType>(Ty)) {
       llvm::MDNode *ScalarMD = getTypeInfoHelper(Ty);
       StringRef Name =
           cast<llvm::MDString>(
               ScalarMD->getOperand(CodeGenOpts.NewStructPathTBAA ? 2 : 0))
               ->getString();
+      TyName = Name;
+    } else if (!isa<VariableArrayType>(Ty)) {
----------------
rjmccall wrote:

I feel like we should just ignore all array structure here, whether it's variable-length or fixed-length.  For one, partially honoring array structures probably violates the VLA compatibility rules — IIUC, the C standard requires l-values of type `T (*)[n]` to be able to alias objects of type `T (*)[10]` if `n == 10` dynamically, but with your patch, we'd now create a node for that pointer-to-constant-array type with no relation to the node we'd use for the pointer-to-VLA type.  But also, I feel like trying to honor the array structure is just pointlessly strict; we can treat a `T (*)[10]` object like a `T *` object, it's not really going to hurt anything.

You should look through the array structure before you do the builtin/non-builtin breakdown.  There's a `getBaseElementType` operation you can use.

https://github.com/llvm/llvm-project/pull/110569


More information about the cfe-commits mailing list