Index: include/llvm/Analysis/MemoryDependenceAnalysis.h =================================================================== --- include/llvm/Analysis/MemoryDependenceAnalysis.h +++ include/llvm/Analysis/MemoryDependenceAnalysis.h @@ -366,12 +366,12 @@ /// getNonLocalPointerDependency - Perform a full dependency query for an - /// access to the specified (non-volatile) memory location, returning the - /// set of instructions that either define or clobber the value. + /// access to the QueryInst's specified (non-volatile) memory location, + /// returning the set of instructions that either define or clobber + /// the value. /// /// This method assumes the pointer has a "NonLocal" dependency within BB. - void getNonLocalPointerDependency(const AliasAnalysis::Location &Loc, - bool isLoad, BasicBlock *BB, + void getNonLocalPointerDependency(Instruction *QueryInst, SmallVectorImpl &Result); /// removeInstruction - Remove an instruction from the dependence analysis, Index: lib/Analysis/MemDepPrinter.cpp =================================================================== --- lib/Analysis/MemDepPrinter.cpp +++ lib/Analysis/MemDepPrinter.cpp @@ -92,7 +92,6 @@ bool MemDepPrinter::runOnFunction(Function &F) { this->F = &F; - AliasAnalysis &AA = getAnalysis(); MemoryDependenceAnalysis &MDA = getAnalysis(); // All this code uses non-const interfaces because MemDep is not @@ -126,8 +125,7 @@ static_cast(nullptr))); continue; } - AliasAnalysis::Location Loc = AA.getLocation(LI); - MDA.getNonLocalPointerDependency(Loc, true, LI->getParent(), NLDI); + MDA.getNonLocalPointerDependency(LI, NLDI); } else if (StoreInst *SI = dyn_cast(Inst)) { if (!SI->isUnordered()) { // FIXME: Handle atomic/volatile stores. @@ -135,11 +133,9 @@ static_cast(nullptr))); continue; } - AliasAnalysis::Location Loc = AA.getLocation(SI); - MDA.getNonLocalPointerDependency(Loc, false, SI->getParent(), NLDI); + MDA.getNonLocalPointerDependency(SI, NLDI); } else if (VAArgInst *VI = dyn_cast(Inst)) { - AliasAnalysis::Location Loc = AA.getLocation(VI); - MDA.getNonLocalPointerDependency(Loc, false, VI->getParent(), NLDI); + MDA.getNonLocalPointerDependency(VI, NLDI); } else { llvm_unreachable("Unknown memory instruction!"); } Index: lib/Analysis/MemoryDependenceAnalysis.cpp =================================================================== --- lib/Analysis/MemoryDependenceAnalysis.cpp +++ lib/Analysis/MemoryDependenceAnalysis.cpp @@ -859,9 +859,59 @@ /// own block. /// void MemoryDependenceAnalysis:: -getNonLocalPointerDependency(const AliasAnalysis::Location &Loc, bool isLoad, - BasicBlock *FromBB, +getNonLocalPointerDependency(Instruction *QueryInst, SmallVectorImpl &Result) { + + auto getLocation = [](AliasAnalysis *AA, Instruction *Inst) { + if (auto *I = dyn_cast(Inst)) + return AA->getLocation(I); + else if (auto *I = dyn_cast(Inst)) + return AA->getLocation(I); + else if (auto *I = dyn_cast(Inst)) + return AA->getLocation(I); + else if (auto *I = dyn_cast(Inst)) + return AA->getLocation(I); + else if (auto *I = dyn_cast(Inst)) + return AA->getLocation(I); + else + llvm_unreachable("unsupported memory instruction"); + }; + + const AliasAnalysis::Location Loc = getLocation(AA, QueryInst); + bool isLoad = isa(QueryInst); + BasicBlock *FromBB = QueryInst->getParent(); + assert(FromBB); + + // This routine does not expect to deal with volatile instructions. Doing so + // would require piping through the QueryInst all the way through. + // TODO: volatiles can't be elided, but they can be reordered with other + // non-volatile accesses. + if (LoadInst *LI = dyn_cast(QueryInst)) { + assert(!LI->isVolatile()); + } else if (StoreInst *SI = dyn_cast(QueryInst)) { + assert(!SI->isVolatile()); + } + + + // We currently give up on any instruction which is ordered, but we do handle + // atomic instructions which are unordered. + // TODO: Handle ordered instructions + auto is_ordered = [](Instruction *Inst) { + if (LoadInst *LI = dyn_cast(Inst)) { + return !LI->isUnordered(); + } else if (StoreInst *SI = dyn_cast(Inst)) { + return !SI->isUnordered(); + } + return false; + }; + + if (is_ordered(QueryInst)) { + Result.push_back(NonLocalDepResult(FromBB, + MemDepResult::getUnknown(), + const_cast(Loc.Ptr))); + return; + } + assert(Loc.Ptr->getType()->isPointerTy() && "Can't get pointer deps of a non-pointer!"); Result.clear(); Index: lib/Transforms/Scalar/GVN.cpp =================================================================== --- lib/Transforms/Scalar/GVN.cpp +++ lib/Transforms/Scalar/GVN.cpp @@ -1707,8 +1707,7 @@ bool GVN::processNonLocalLoad(LoadInst *LI) { // Step 1: Find the non-local dependencies of the load. LoadDepVect Deps; - AliasAnalysis::Location Loc = VN.getAliasAnalysis()->getLocation(LI); - MD->getNonLocalPointerDependency(Loc, true, LI->getParent(), Deps); + MD->getNonLocalPointerDependency(LI, Deps); // If we had to process more than one hundred blocks to find the // dependencies, this load isn't worth worrying about. Optimizing