[llvm] [TBAA] Only keep the immutable flag if both merged tags have it (PR #225907)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 24 00:15:48 PDT 2026


================
@@ -471,6 +471,15 @@ static bool matchAccessTags(const MDNode *A, const MDNode *B,
 MDNode *MDNode::getMostGenericTBAA(MDNode *A, MDNode *B) {
   const MDNode *GenericTag;
   matchAccessTags(A, B, &GenericTag);
+  // The generic tag can be one of the given tags. It must hold for both
+  // accesses, so keep its immutable flag only if both tags have it.
+  if (GenericTag && TBAAStructTagNode(GenericTag).isTypeImmutable() &&
+      !(TBAAStructTagNode(A).isTypeImmutable() &&
+        TBAAStructTagNode(B).isTypeImmutable())) {
+    unsigned FlagOpNo = TBAAStructTagNode(GenericTag).isNewFormat() ? 4 : 3;
+    SmallVector<Metadata *, 4> Ops(GenericTag->operands().take_front(FlagOpNo));
+    GenericTag = MDNode::get(GenericTag->getContext(), Ops);
+  }
----------------
nikic wrote:

Rather than fixing this up after the fact, can we directly produce the correct GenericTag in matchAccessTags()?

If I'm tracing the code right, out of all the code paths producing GenreicTag, the only problematic one is the MayAlias path here? https://github.com/mmiftahx/llvm-project/blob/d504439fc7557a86e62a7a171ce7516682e544d7/llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp#L628

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


More information about the llvm-commits mailing list