Skip to content

Commit 4c5fbfa

Browse files
committed
[LSV] Introduce copyMetadataForStore
1 parent c9c407b commit 4c5fbfa

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed

llvm/include/llvm/Transforms/Utils/Local.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,10 @@ void combineAAMetadata(Instruction *K, const Instruction *J);
430430
/// replacement for the source instruction).
431431
void copyMetadataForLoad(LoadInst &Dest, const LoadInst &Source);
432432

433+
/// Copy the metadata from the source instruction to the destination (the
434+
/// replace)
435+
void copyMetadataForStore(StoreInst &Dest, const StoreInst &Source);
436+
433437
/// Patch the replacement so that it is not more restrictive than the value
434438
/// being replaced. It assumes that the replacement does not get moved from
435439
/// its original position.

llvm/lib/Transforms/Utils/Local.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3510,6 +3510,46 @@ void llvm::copyMetadataForLoad(LoadInst &Dest, const LoadInst &Source) {
35103510
}
35113511
}
35123512

3513+
void llvm::copyMetadataForStore(StoreInst &Dest, const StoreInst &Source) {
3514+
SmallVector<std::pair<unsigned, MDNode *>, 8> MD;
3515+
Source.getAllMetadata(MD);
3516+
MDBuilder MDB(Dest.getContext());
3517+
Type *NewType = Dest.getType();
3518+
for (const auto &MDPair : MD) {
3519+
unsigned ID = MDPair.first;
3520+
MDNode *N = MDPair.second;
3521+
switch (ID) {
3522+
case LLVMContext::MD_dbg:
3523+
case LLVMContext::MD_tbaa:
3524+
case LLVMContext::MD_prof:
3525+
case LLVMContext::MD_tbaa_struct:
3526+
case LLVMContext::MD_alias_scope:
3527+
case LLVMContext::MD_noalias:
3528+
case LLVMContext::MD_nontemporal:
3529+
case LLVMContext::MD_access_group:
3530+
case LLVMContext::MD_noundef:
3531+
case LLVMContext::MD_noalias_addrspace:
3532+
case LLVMContext::MD_mem_parallel_loop_access:
3533+
Dest.setMetadata(ID, N);
3534+
break;
3535+
3536+
case LLVMContext::MD_nonnull:
3537+
break;
3538+
3539+
case LLVMContext::MD_align:
3540+
case LLVMContext::MD_dereferenceable:
3541+
case LLVMContext::MD_dereferenceable_or_null:
3542+
// These only directly apply if the new type is also a pointer.
3543+
if (NewType->isPointerTy())
3544+
Dest.setMetadata(ID, N);
3545+
break;
3546+
3547+
case LLVMContext::MD_range:
3548+
break;
3549+
}
3550+
}
3551+
}
3552+
35133553
void llvm::patchReplacementInstruction(Instruction *I, Value *Repl) {
35143554
auto *ReplInst = dyn_cast<Instruction>(Repl);
35153555
if (!ReplInst)

llvm/lib/Transforms/Vectorize/LoadStoreVectorizer.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,14 +1335,6 @@ void Vectorizer::insertCastsToMergeClasses(EquivalenceClassMap &EQClasses) {
13351335
if (EQClasses.size() < 2)
13361336
return;
13371337

1338-
auto CopyMetaDataFromTo = [&](Instruction *Src, Instruction *Dst) {
1339-
SmallVector<std::pair<unsigned, MDNode *>, 4> MD;
1340-
Src->getAllMetadata(MD);
1341-
for (const auto [ID, Node] : MD) {
1342-
Dst->setMetadata(ID, Node);
1343-
}
1344-
};
1345-
13461338
// For each class, determine the most defined type. This information will
13471339
// help us determine the type instructions should be casted into.
13481340
MapVector<EqClassKey, unsigned> ClassToNewTyID;
@@ -1407,7 +1399,7 @@ void Vectorizer::insertCastsToMergeClasses(EquivalenceClassMap &EQClasses) {
14071399
auto *Cast = Builder.CreateBitOrPointerCast(
14081400
NewLoad, OrigTy, NewLoad->getName() + ".cast");
14091401
LI->replaceAllUsesWith(Cast);
1410-
CopyMetaDataFromTo(LI, NewLoad);
1402+
copyMetadataForLoad(*NewLoad, *LI);
14111403
LI->eraseFromParent();
14121404
EQClasses[EC1.first].emplace_back(NewLoad);
14131405
} else {
@@ -1418,7 +1410,7 @@ void Vectorizer::insertCastsToMergeClasses(EquivalenceClassMap &EQClasses) {
14181410
SI->getValueOperand()->getName() + ".cast");
14191411
auto *NewStore = Builder.CreateStore(
14201412
Cast, getLoadStorePointerOperand(SI), SI->isVolatile());
1421-
CopyMetaDataFromTo(SI, NewStore);
1413+
copyMetadataForStore(*NewStore, *SI);
14221414
SI->eraseFromParent();
14231415
EQClasses[EC1.first].emplace_back(NewStore);
14241416
}

0 commit comments

Comments
 (0)