Skip to content

Commit 70a884f

Browse files
committed
[LSV] Introduce copyMetadataForStore
1 parent 3df5fa5 commit 70a884f

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
@@ -1333,14 +1333,6 @@ void Vectorizer::insertCastsToMergeClasses(EquivalenceClassMap &EQClasses) {
13331333
if (EQClasses.size() < 2)
13341334
return;
13351335

1336-
auto CopyMetaDataFromTo = [&](Instruction *Src, Instruction *Dst) {
1337-
SmallVector<std::pair<unsigned, MDNode *>, 4> MD;
1338-
Src->getAllMetadata(MD);
1339-
for (const auto [ID, Node] : MD) {
1340-
Dst->setMetadata(ID, Node);
1341-
}
1342-
};
1343-
13441336
// For each class, determine the most defined type. This information will
13451337
// help us determine the type instructions should be casted into.
13461338
MapVector<EqClassKey, unsigned> ClassToNewTyID;
@@ -1405,7 +1397,7 @@ void Vectorizer::insertCastsToMergeClasses(EquivalenceClassMap &EQClasses) {
14051397
auto *Cast = Builder.CreateBitOrPointerCast(
14061398
NewLoad, OrigTy, NewLoad->getName() + ".cast");
14071399
LI->replaceAllUsesWith(Cast);
1408-
CopyMetaDataFromTo(LI, NewLoad);
1400+
copyMetadataForLoad(*NewLoad, *LI);
14091401
LI->eraseFromParent();
14101402
EQClasses[EC1.first].emplace_back(NewLoad);
14111403
} else {
@@ -1416,7 +1408,7 @@ void Vectorizer::insertCastsToMergeClasses(EquivalenceClassMap &EQClasses) {
14161408
SI->getValueOperand()->getName() + ".cast");
14171409
auto *NewStore = Builder.CreateStore(
14181410
Cast, getLoadStorePointerOperand(SI), SI->isVolatile());
1419-
CopyMetaDataFromTo(SI, NewStore);
1411+
copyMetadataForStore(*NewStore, *SI);
14201412
SI->eraseFromParent();
14211413
EQClasses[EC1.first].emplace_back(NewStore);
14221414
}

0 commit comments

Comments
 (0)