diff --git a/llvm/include/llvm/Analysis/MemorySSA.h b/llvm/include/llvm/Analysis/MemorySSA.h --- a/llvm/include/llvm/Analysis/MemorySSA.h +++ b/llvm/include/llvm/Analysis/MemorySSA.h @@ -1272,11 +1272,11 @@ const_cast(Location.Ptr), OriginalAccess->getBlock()->getModule()->getDataLayout(), nullptr); - if (!Translator.translateValue(OriginalAccess->getBlock(), - DefIterator.getPhiArgBlock(), DT, true)) - if (Translator.getAddr() != CurrentPair.second.Ptr) - CurrentPair.second = - CurrentPair.second.getWithNewPtr(Translator.getAddr()); + if (Value *Addr = + Translator.translateValue(OriginalAccess->getBlock(), + DefIterator.getPhiArgBlock(), DT, true)) + if (Addr != CurrentPair.second.Ptr) + CurrentPair.second = CurrentPair.second.getWithNewPtr(Addr); // Mark size as unknown, if the location is not guaranteed to be // loop-invariant for any possible loop in the function. Setting the size diff --git a/llvm/include/llvm/Analysis/PHITransAddr.h b/llvm/include/llvm/Analysis/PHITransAddr.h --- a/llvm/include/llvm/Analysis/PHITransAddr.h +++ b/llvm/include/llvm/Analysis/PHITransAddr.h @@ -52,8 +52,7 @@ PHITransAddr(Value *Addr, const DataLayout &DL, AssumptionCache *AC) : Addr(Addr), DL(DL), AC(AC) { // If the address is an instruction, the whole thing is considered an input. - if (Instruction *I = dyn_cast(Addr)) - InstInputs.push_back(I); + addAsInput(Addr); } Value *getAddr() const { return Addr; } @@ -63,10 +62,9 @@ bool needsPHITranslationFromBlock(BasicBlock *BB) const { // We do need translation if one of our input instructions is defined in // this block. - for (unsigned i = 0, e = InstInputs.size(); i != e; ++i) - if (InstInputs[i]->getParent() == BB) - return true; - return false; + return any_of(InstInputs, [BB](const auto &InstInput) { + return InstInput->getParent() == BB; + }); } /// isPotentiallyPHITranslatable - If this needs PHI translation, return true @@ -76,10 +74,9 @@ /// translateValue - PHI translate the current address up the CFG from /// CurBB to Pred, updating our state to reflect any needed changes. If - /// 'MustDominate' is true, the translated value must dominate - /// PredBB. This returns true on failure and sets Addr to null. - bool translateValue(BasicBlock *CurBB, BasicBlock *PredBB, - const DominatorTree *DT, bool MustDominate); + /// 'MustDominate' is true, the translated value must dominate PredBB. + Value *translateValue(BasicBlock *CurBB, BasicBlock *PredBB, + const DominatorTree *DT, bool MustDominate); /// translateWithInsertion - PHI translate this value into the specified /// predecessor block, inserting a computation of the value if it is diff --git a/llvm/lib/Analysis/MemoryDependenceAnalysis.cpp b/llvm/lib/Analysis/MemoryDependenceAnalysis.cpp --- a/llvm/lib/Analysis/MemoryDependenceAnalysis.cpp +++ b/llvm/lib/Analysis/MemoryDependenceAnalysis.cpp @@ -1298,8 +1298,8 @@ // Get the PHI translated pointer in this predecessor. This can fail if // not translatable, in which case the getAddr() returns null. PHITransAddr &PredPointer = PredList.back().second; - PredPointer.translateValue(BB, Pred, &DT, /*MustDominate=*/false); - Value *PredPtrVal = PredPointer.getAddr(); + Value *PredPtrVal = + PredPointer.translateValue(BB, Pred, &DT, /*MustDominate=*/false); // Check to see if we have already visited this pred block with another // pointer. If so, we can't do this lookup. This failure can occur diff --git a/llvm/lib/Analysis/PHITransAddr.cpp b/llvm/lib/Analysis/PHITransAddr.cpp --- a/llvm/lib/Analysis/PHITransAddr.cpp +++ b/llvm/lib/Analysis/PHITransAddr.cpp @@ -26,12 +26,7 @@ cl::desc("Enable phi-translation of add instructions")); static bool canPHITrans(Instruction *Inst) { - if (isa(Inst) || - isa(Inst)) - return true; - - if (isa(Inst) && - isSafeToSpeculativelyExecute(Inst)) + if (isa(Inst) || isa(Inst) || isa(Inst)) return true; if (Inst->getOpcode() == Instruction::Add && @@ -77,11 +72,8 @@ } // Validate the operands of the instruction. - for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) - if (!verifySubExpr(I->getOperand(i), InstInputs)) - return false; - - return true; + return all_of(I->operands(), + [&](Value *Op) { return verifySubExpr(Op, InstInputs); }); } /// verify - Check internal consistency of this data structure. If the @@ -182,7 +174,6 @@ // operands need to be phi translated, and if so, reconstruct it. if (CastInst *Cast = dyn_cast(Inst)) { - if (!isSafeToSpeculativelyExecute(Cast)) return nullptr; Value *PHIIn = translateSubExpr(Cast->getOperand(0), CurBB, PredBB, DT); if (!PHIIn) return nullptr; if (PHIIn == Cast->getOperand(0)) @@ -211,11 +202,11 @@ if (GetElementPtrInst *GEP = dyn_cast(Inst)) { SmallVector GEPOps; bool AnyChanged = false; - for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i) { - Value *GEPOp = translateSubExpr(GEP->getOperand(i), CurBB, PredBB, DT); + for (Value *Op : GEP->operands()) { + Value *GEPOp = translateSubExpr(Op, CurBB, PredBB, DT); if (!GEPOp) return nullptr; - AnyChanged |= GEPOp != GEP->getOperand(i); + AnyChanged |= GEPOp != Op; GEPOps.push_back(GEPOp); } @@ -226,8 +217,8 @@ if (Value *V = simplifyGEPInst(GEP->getSourceElementType(), GEPOps[0], ArrayRef(GEPOps).slice(1), GEP->isInBounds(), {DL, TLI, DT, AC})) { - for (unsigned i = 0, e = GEPOps.size(); i != e; ++i) - RemoveInstInputs(GEPOps[i], InstInputs); + for (Value *GEPOp : GEPOps) + RemoveInstInputs(GEPOp, InstInputs); return addAsInput(V); } @@ -305,10 +296,10 @@ /// PHITranslateValue - PHI translate the current address up the CFG from /// CurBB to Pred, updating our state to reflect any needed changes. If -/// 'MustDominate' is true, the translated value must dominate -/// PredBB. This returns true on failure and sets Addr to null. -bool PHITransAddr::translateValue(BasicBlock *CurBB, BasicBlock *PredBB, - const DominatorTree *DT, bool MustDominate) { +/// 'MustDominate' is true, the translated value must dominate PredBB. +Value *PHITransAddr::translateValue(BasicBlock *CurBB, BasicBlock *PredBB, + const DominatorTree *DT, + bool MustDominate) { assert(DT || !MustDominate); assert(verify() && "Invalid PHITransAddr!"); if (DT && DT->isReachableFromEntry(PredBB)) @@ -323,7 +314,7 @@ if (!DT->dominates(Inst->getParent(), PredBB)) Addr = nullptr; - return Addr == nullptr; + return Addr; } /// PHITranslateWithInsertion - PHI translate this value into the specified @@ -362,8 +353,9 @@ // See if we have a version of this value already available and dominating // PredBB. If so, there is no need to insert a new instance of it. PHITransAddr Tmp(InVal, DL, AC); - if (!Tmp.translateValue(CurBB, PredBB, &DT, /*MustDominate=*/true)) - return Tmp.getAddr(); + if (Value *Addr = + Tmp.translateValue(CurBB, PredBB, &DT, /*MustDominate=*/true)) + return Addr; // We don't need to PHI translate values which aren't instructions. auto *Inst = dyn_cast(InVal); @@ -372,7 +364,6 @@ // Handle cast of PHI translatable value. if (CastInst *Cast = dyn_cast(Inst)) { - if (!isSafeToSpeculativelyExecute(Cast)) return nullptr; Value *OpVal = insertTranslatedSubExpr(Cast->getOperand(0), CurBB, PredBB, DT, NewInsts); if (!OpVal) return nullptr; diff --git a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp --- a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp +++ b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp @@ -465,7 +465,7 @@ if (PredAddr.needsPHITranslationFromBlock(B)) { if (!PredAddr.isPotentiallyPHITranslatable()) return false; - if (PredAddr.translateValue(B, Pred, DT, false)) + if (!PredAddr.translateValue(B, Pred, DT, false)) return false; } Value *TranslatedPtr = PredAddr.getAddr();