diff options
author | Nikita Popov <npopov@redhat.com> | 2025-01-09 09:34:46 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-01-09 09:34:46 +0100 |
commit | 71f7b972c3a28269ae045c4d87b1ccb5a2860852 (patch) | |
tree | 97fc1a084c012fa7a3e04082ed72d8150db11ce0 /llvm/lib/Transforms/Utils/Local.cpp | |
parent | 1b2943534fa20a61c07592a9bd90203e682ae0f4 (diff) | |
download | llvm-71f7b972c3a28269ae045c4d87b1ccb5a2860852.zip llvm-71f7b972c3a28269ae045c4d87b1ccb5a2860852.tar.gz llvm-71f7b972c3a28269ae045c4d87b1ccb5a2860852.tar.bz2 |
[Local] Make combineAAMetadata() more principled (#122091)
This moves combineAAMetadata() into Local and implements it via a new
AAOnly flag, which will intersect only AA metadata and keep other known
metadata.
The existing KnownIDs list is dropped, because it is redundant with the
switch in combineMetadata(), which already drops unknown metadata.
I tried a few variants of this, and ultimately went with the AAOnly flag
because this way we make an explicit choice for each metadata kind
supported by combineMetadata(), and ignoring the flag gives you
conservatively correct behavior.
I checked that the memcpy tests still pass if we adjust the logic for
MD_memprof/MD_callsite to drop the metadata instead of arbitrarily
picking one.
Fixes https://github.com/llvm/llvm-project/issues/121495.
Diffstat (limited to 'llvm/lib/Transforms/Utils/Local.cpp')
-rw-r--r-- | llvm/lib/Transforms/Utils/Local.cpp | 70 |
1 files changed, 27 insertions, 43 deletions
diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp index 1e4061c..2d6f6a3 100644 --- a/llvm/lib/Transforms/Utils/Local.cpp +++ b/llvm/lib/Transforms/Utils/Local.cpp @@ -3308,31 +3308,27 @@ bool llvm::removeUnreachableBlocks(Function &F, DomTreeUpdater *DTU, return Changed; } -// FIXME: https://github.com/llvm/llvm-project/issues/121495 -// Once external callers of this function are removed, either inline into -// combineMetadataForCSE, or internalize and remove KnownIDs parameter. -void llvm::combineMetadata(Instruction *K, const Instruction *J, - ArrayRef<unsigned> KnownIDs, bool DoesKMove) { +/// If AAOnly is set, only intersect alias analysis metadata and preserve other +/// known metadata. Unknown metadata is always dropped. +static void combineMetadata(Instruction *K, const Instruction *J, + bool DoesKMove, bool AAOnly = false) { SmallVector<std::pair<unsigned, MDNode *>, 4> Metadata; - K->dropUnknownNonDebugMetadata(KnownIDs); K->getAllMetadataOtherThanDebugLoc(Metadata); for (const auto &MD : Metadata) { unsigned Kind = MD.first; MDNode *JMD = J->getMetadata(Kind); MDNode *KMD = MD.second; + // TODO: Assert that this switch is exhaustive for fixed MD kinds. switch (Kind) { default: - // FIXME: https://github.com/llvm/llvm-project/issues/121495 - // Change to removing only explicitly listed other metadata, and assert - // on unknown metadata, to avoid inadvertently dropping newly added - // metadata types. K->setMetadata(Kind, nullptr); // Remove unknown metadata break; case LLVMContext::MD_dbg: llvm_unreachable("getAllMetadataOtherThanDebugLoc returned a MD_dbg"); case LLVMContext::MD_DIAssignID: - K->mergeDIAssignID(J); + if (!AAOnly) + K->mergeDIAssignID(J); break; case LLVMContext::MD_tbaa: if (DoesKMove) @@ -3353,11 +3349,12 @@ void llvm::combineMetadata(Instruction *K, const Instruction *J, intersectAccessGroups(K, J)); break; case LLVMContext::MD_range: - if (DoesKMove || !K->hasMetadata(LLVMContext::MD_noundef)) + if (!AAOnly && (DoesKMove || !K->hasMetadata(LLVMContext::MD_noundef))) K->setMetadata(Kind, MDNode::getMostGenericRange(JMD, KMD)); break; case LLVMContext::MD_fpmath: - K->setMetadata(Kind, MDNode::getMostGenericFPMath(JMD, KMD)); + if (!AAOnly) + K->setMetadata(Kind, MDNode::getMostGenericFPMath(JMD, KMD)); break; case LLVMContext::MD_invariant_load: // If K moves, only set the !invariant.load if it is present in both @@ -3366,7 +3363,7 @@ void llvm::combineMetadata(Instruction *K, const Instruction *J, K->setMetadata(Kind, JMD); break; case LLVMContext::MD_nonnull: - if (DoesKMove || !K->hasMetadata(LLVMContext::MD_noundef)) + if (!AAOnly && (DoesKMove || !K->hasMetadata(LLVMContext::MD_noundef))) K->setMetadata(Kind, JMD); break; case LLVMContext::MD_invariant_group: @@ -3376,36 +3373,39 @@ void llvm::combineMetadata(Instruction *K, const Instruction *J, // Combine MMRAs break; case LLVMContext::MD_align: - if (DoesKMove || !K->hasMetadata(LLVMContext::MD_noundef)) + if (!AAOnly && (DoesKMove || !K->hasMetadata(LLVMContext::MD_noundef))) K->setMetadata( Kind, MDNode::getMostGenericAlignmentOrDereferenceable(JMD, KMD)); break; case LLVMContext::MD_dereferenceable: case LLVMContext::MD_dereferenceable_or_null: - if (DoesKMove) + if (!AAOnly && DoesKMove) K->setMetadata(Kind, MDNode::getMostGenericAlignmentOrDereferenceable(JMD, KMD)); break; case LLVMContext::MD_memprof: - K->setMetadata(Kind, MDNode::getMergedMemProfMetadata(KMD, JMD)); + if (!AAOnly) + K->setMetadata(Kind, MDNode::getMergedMemProfMetadata(KMD, JMD)); break; case LLVMContext::MD_callsite: - K->setMetadata(Kind, MDNode::getMergedCallsiteMetadata(KMD, JMD)); + if (!AAOnly) + K->setMetadata(Kind, MDNode::getMergedCallsiteMetadata(KMD, JMD)); break; case LLVMContext::MD_preserve_access_index: // Preserve !preserve.access.index in K. break; case LLVMContext::MD_noundef: // If K does move, keep noundef if it is present in both instructions. - if (DoesKMove) + if (!AAOnly && DoesKMove) K->setMetadata(Kind, JMD); break; case LLVMContext::MD_nontemporal: // Preserve !nontemporal if it is present on both instructions. - K->setMetadata(Kind, JMD); + if (!AAOnly) + K->setMetadata(Kind, JMD); break; case LLVMContext::MD_prof: - if (DoesKMove) + if (!AAOnly && DoesKMove) K->setMetadata(Kind, MDNode::getMergedProfMetadata(KMD, JMD, K, J)); break; case LLVMContext::MD_noalias_addrspace: @@ -3437,28 +3437,12 @@ void llvm::combineMetadata(Instruction *K, const Instruction *J, } void llvm::combineMetadataForCSE(Instruction *K, const Instruction *J, - bool KDominatesJ) { - unsigned KnownIDs[] = {LLVMContext::MD_tbaa, - LLVMContext::MD_alias_scope, - LLVMContext::MD_noalias, - LLVMContext::MD_range, - LLVMContext::MD_fpmath, - LLVMContext::MD_invariant_load, - LLVMContext::MD_nonnull, - LLVMContext::MD_invariant_group, - LLVMContext::MD_align, - LLVMContext::MD_dereferenceable, - LLVMContext::MD_dereferenceable_or_null, - LLVMContext::MD_access_group, - LLVMContext::MD_preserve_access_index, - LLVMContext::MD_prof, - LLVMContext::MD_nontemporal, - LLVMContext::MD_noundef, - LLVMContext::MD_mmra, - LLVMContext::MD_noalias_addrspace, - LLVMContext::MD_memprof, - LLVMContext::MD_callsite}; - combineMetadata(K, J, KnownIDs, KDominatesJ); + bool DoesKMove) { + combineMetadata(K, J, DoesKMove); +} + +void llvm::combineAAMetadata(Instruction *K, const Instruction *J) { + combineMetadata(K, J, /*DoesKMove=*/true, /*AAOnly=*/true); } void llvm::copyMetadataForLoad(LoadInst &Dest, const LoadInst &Source) { |