Index: include/llvm/Analysis/Loads.h =================================================================== --- include/llvm/Analysis/Loads.h +++ include/llvm/Analysis/Loads.h @@ -82,7 +82,8 @@ BasicBlock::iterator &ScanFrom, unsigned MaxInstsToScan = DefMaxInstsToScan, AliasAnalysis *AA = nullptr, - AAMDNodes *AATags = nullptr); + AAMDNodes *AATags = nullptr, + bool *IsLoadCSE = nullptr); } Index: lib/Analysis/Loads.cpp =================================================================== --- lib/Analysis/Loads.cpp +++ lib/Analysis/Loads.cpp @@ -322,7 +322,8 @@ Value *llvm::FindAvailableLoadedValue(LoadInst *Load, BasicBlock *ScanBB, BasicBlock::iterator &ScanFrom, unsigned MaxInstsToScan, - AliasAnalysis *AA, AAMDNodes *AATags) { + AliasAnalysis *AA, AAMDNodes *AATags, + bool *IsLoadCSE) { if (MaxInstsToScan == 0) MaxInstsToScan = ~0U; @@ -374,6 +375,8 @@ if (AATags) LI->getAAMetadata(*AATags); + if (IsLoadCSE) + *IsLoadCSE = true; return LI; } Index: lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp +++ lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp @@ -819,10 +819,12 @@ // separated by a few arithmetic operations. BasicBlock::iterator BBI(LI); AAMDNodes AATags; + bool IsLoadCSE = false; if (Value *AvailableVal = FindAvailableLoadedValue(&LI, LI.getParent(), BBI, - DefMaxInstsToScan, AA, &AATags)) { - if (LoadInst *NLI = dyn_cast(AvailableVal)) { + DefMaxInstsToScan, AA, &AATags, &IsLoadCSE)) { + if (IsLoadCSE) { + LoadInst *NLI = static_cast(AvailableVal); unsigned KnownIDs[] = { LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope, LLVMContext::MD_noalias, LLVMContext::MD_range, Index: test/Transforms/InstCombine/tbaa-store-to-load.ll =================================================================== --- /dev/null +++ test/Transforms/InstCombine/tbaa-store-to-load.ll @@ -0,0 +1,17 @@ +; RUN: opt -S -instcombine < %s 2>&1 | FileCheck %s + +define i64 @f(i64* %p1, i64* %p2) { +top: + ; check that the tbaa is preserved + ; CHECK-LABEL: @f( + ; CHECK: %v1 = load i64, i64* %p1, align 8, !tbaa !0 + ; CHECK: store i64 %v1, i64* %p2, align 8 + ; CHECK: ret i64 %v1 + %v1 = load i64, i64* %p1, align 8, !tbaa !0 + store i64 %v1, i64* %p2, align 8 + %v2 = load i64, i64* %p2, align 8 + ret i64 %v2 +} + +!0 = !{!1, !1, i64 0} +!1 = !{!"load_tbaa"}