Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion llvm/include/llvm/Transforms/Utils/Local.h
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ LLVM_ABI void combineAAMetadata(Instruction *K, const Instruction *J);

/// Copy the metadata from the source instruction to the destination (the
/// replacement for the source instruction).
LLVM_ABI void copyMetadataForAccess(Instruction &Dest, Instruction &Source);
LLVM_ABI void copyMetadataForLoad(LoadInst &Dest, const LoadInst &Source);

/// Patch the replacement so that it is not more restrictive than the value
/// being replaced. It assumes that the replacement does not get moved from
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/AMDGPU/AMDGPULowerBufferFatPointers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,7 @@ bool LegalizeBufferContentTypesVisitor::visitLoadImpl(
LoadInst *NewLI = IRB.CreateAlignedLoad(
LoadableType, NewPtr, commonAlignment(OrigLI.getAlign(), ByteOffset),
Name + ".off." + Twine(ByteOffset));
copyMetadataForAccess(*NewLI, OrigLI);
copyMetadataForLoad(*NewLI, OrigLI);
NewLI->setAAMetadata(
AANodes.adjustForAccess(ByteOffset, LoadableType, DL));
NewLI->setAtomic(OrigLI.getOrdering(), OrigLI.getSyncScopeID());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ void PointerReplacer::replace(Instruction *I) {
LT->getAlign(), LT->getOrdering(),
LT->getSyncScopeID());
NewI->takeName(LT);
copyMetadataForAccess(*NewI, *LT);
copyMetadataForLoad(*NewI, *LT);

IC.InsertNewInstWith(NewI, LT->getIterator());
IC.replaceInstUsesWith(*LT, NewI);
Expand Down Expand Up @@ -606,7 +606,7 @@ LoadInst *InstCombinerImpl::combineLoadToNewType(LoadInst &LI, Type *NewTy,
Builder.CreateAlignedLoad(NewTy, LI.getPointerOperand(), LI.getAlign(),
LI.isVolatile(), LI.getName() + Suffix);
NewLoad->setAtomic(LI.getOrdering(), LI.getSyncScopeID());
copyMetadataForAccess(*NewLoad, LI);
copyMetadataForLoad(*NewLoad, LI);
return NewLoad;
}

Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/Scalar/SROA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3272,7 +3272,7 @@ class AllocaSliceRewriter : public InstVisitor<AllocaSliceRewriter, bool> {
// Copy any metadata that is valid for the new load. This may require
// conversion to a different kind of metadata, e.g. !nonnull might change
// to !range or vice versa.
copyMetadataForAccess(*NewLI, LI);
copyMetadataForLoad(*NewLI, LI);

// Do this after copyMetadataForLoad() to preserve the TBAA shift.
if (AATags)
Expand Down
60 changes: 22 additions & 38 deletions llvm/lib/Transforms/Utils/Local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3100,70 +3100,54 @@ void llvm::combineAAMetadata(Instruction *K, const Instruction *J) {
combineMetadata(K, J, /*DoesKMove=*/true, /*AAOnly=*/true);
}

void llvm::copyMetadataForAccess(Instruction &DestI, Instruction &SourceI) {
void llvm::copyMetadataForLoad(LoadInst &Dest, const LoadInst &Source) {
SmallVector<std::pair<unsigned, MDNode *>, 8> MD;
SourceI.getAllMetadata(MD);
MDBuilder MDB(DestI.getContext());
Type *NewType = DestI.getType();

// Only needed for range metadata on loads.
const DataLayout *DL = nullptr;
const LoadInst *LSource = dyn_cast<LoadInst>(&SourceI);
if (LSource)
DL = &LSource->getDataLayout();

Source.getAllMetadata(MD);
MDBuilder MDB(Dest.getContext());
Type *NewType = Dest.getType();
const DataLayout &DL = Source.getDataLayout();
for (const auto &MDPair : MD) {
unsigned ID = MDPair.first;
MDNode *N = MDPair.second;

// Note, essentially every kind of metadata should be preserved here! This
// routine is supposed to clone a load instruction changing *only its type*.
// The only metadata it makes sense to drop is metadata which is invalidated
// when the pointer type changes. This should essentially never be the case
// in LLVM, but we explicitly switch over only known metadata to be
// conservatively correct. If you are adding metadata to LLVM which pertains
// to loads, you almost certainly want to add it here.
switch (ID) {
// Applies to both loads and stores as-is.
case LLVMContext::MD_dbg:
case LLVMContext::MD_tbaa:
case LLVMContext::MD_prof:
case LLVMContext::MD_fpmath:
case LLVMContext::MD_tbaa_struct:
case LLVMContext::MD_invariant_load:
case LLVMContext::MD_alias_scope:
case LLVMContext::MD_noalias:
case LLVMContext::MD_nontemporal:
case LLVMContext::MD_mem_parallel_loop_access:
case LLVMContext::MD_access_group:
case LLVMContext::MD_noundef:
case LLVMContext::MD_noalias_addrspace:
case LLVMContext::MD_mem_parallel_loop_access:
DestI.setMetadata(ID, N);
break;

// Load-only metadata.
case LLVMContext::MD_fpmath:
case LLVMContext::MD_invariant_load:
if (isa<LoadInst>(DestI))
DestI.setMetadata(ID, N);
// All of these directly apply.
Dest.setMetadata(ID, N);
break;

case LLVMContext::MD_nonnull:
if (auto *LDest = dyn_cast<LoadInst>(&DestI)) {
if (LSource)
copyNonnullMetadata(*LSource, N, *LDest);
}
copyNonnullMetadata(Source, N, Dest);
break;

case LLVMContext::MD_align:
case LLVMContext::MD_dereferenceable:
case LLVMContext::MD_dereferenceable_or_null:
// Applies to both loads and stores only if the new type is also a
// pointer.
// These only directly apply if the new type is also a pointer.
if (NewType->isPointerTy())
DestI.setMetadata(ID, N);
Dest.setMetadata(ID, N);
break;

case LLVMContext::MD_range:
if (auto *LDest = dyn_cast<LoadInst>(&DestI)) {
if (LSource && DL)
copyRangeMetadata(*DL, *LSource, N, *LDest);
}
break;

case LLVMContext::MD_tbaa:
if (isa<LoadInst>(DestI))
DestI.setMetadata(ID, N);
copyRangeMetadata(DL, Source, N, Dest);
break;
}
}
Expand Down
Loading