From ca49d6bc88ce0e5f6d1240be3fee8438e513d33d Mon Sep 17 00:00:00 2001 From: Sergey Kachkov Date: Thu, 2 Feb 2023 17:49:54 +0300 Subject: [PATCH] [NFC] PHITransAddr refactoring - use range-based loops and standard algorithms Differential Revision: https://reviews.llvm.org/D143179 --- llvm/include/llvm/Analysis/PHITransAddr.h | 10 ++++----- llvm/lib/Analysis/PHITransAddr.cpp | 36 ++++++++++++------------------- 2 files changed, 18 insertions(+), 28 deletions(-) diff --git a/llvm/include/llvm/Analysis/PHITransAddr.h b/llvm/include/llvm/Analysis/PHITransAddr.h index 2cca2e1..de9c3c4 100644 --- a/llvm/include/llvm/Analysis/PHITransAddr.h +++ b/llvm/include/llvm/Analysis/PHITransAddr.h @@ -52,8 +52,7 @@ public: 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 @@ public: 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 diff --git a/llvm/lib/Analysis/PHITransAddr.cpp b/llvm/lib/Analysis/PHITransAddr.cpp index 6b5d109..3da9277 100644 --- a/llvm/lib/Analysis/PHITransAddr.cpp +++ b/llvm/lib/Analysis/PHITransAddr.cpp @@ -56,8 +56,7 @@ static bool verifySubExpr(Value *Expr, // If it's an instruction, it is either in Tmp or its operands recursively // are. - SmallVectorImpl::iterator Entry = find(InstInputs, I); - if (Entry != InstInputs.end()) { + if (auto Entry = find(InstInputs, I); Entry != InstInputs.end()) { InstInputs.erase(Entry); return true; } @@ -72,11 +71,8 @@ static bool verifySubExpr(Value *Expr, } // 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 @@ -117,8 +113,7 @@ static void RemoveInstInputs(Value *V, if (!I) return; // If the instruction is in the InstInputs list, remove it. - SmallVectorImpl::iterator Entry = find(InstInputs, I); - if (Entry != InstInputs.end()) { + if (auto Entry = find(InstInputs, I); Entry != InstInputs.end()) { InstInputs.erase(Entry); return; } @@ -126,10 +121,9 @@ static void RemoveInstInputs(Value *V, assert(!isa(I) && "Error, removing something that isn't an input"); // Otherwise, it must have instruction inputs itself. Zap them recursively. - for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { - if (Instruction *Op = dyn_cast(I->getOperand(i))) - RemoveInstInputs(Op, InstInputs); - } + for (Value *Op : I->operands()) + if (Instruction *OpInst = dyn_cast(Op)) + RemoveInstInputs(OpInst, InstInputs); } Value *PHITransAddr::translateSubExpr(Value *V, BasicBlock *CurBB, @@ -167,9 +161,8 @@ Value *PHITransAddr::translateSubExpr(Value *V, BasicBlock *CurBB, // All instruction operands are now inputs (and of course, they may also be // defined in this block, so they may need to be phi translated themselves. - for (unsigned i = 0, e = Inst->getNumOperands(); i != e; ++i) - if (Instruction *Op = dyn_cast(Inst->getOperand(i))) - InstInputs.push_back(Op); + for (Value *Op : Inst->operands()) + addAsInput(Op); } // Ok, it must be an intermediate result (either because it started that way @@ -205,11 +198,11 @@ Value *PHITransAddr::translateSubExpr(Value *V, BasicBlock *CurBB, 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); } @@ -384,9 +377,8 @@ Value *PHITransAddr::insertTranslatedSubExpr( if (GetElementPtrInst *GEP = dyn_cast(Inst)) { SmallVector GEPOps; BasicBlock *CurBB = GEP->getParent(); - for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i) { - Value *OpVal = insertTranslatedSubExpr(GEP->getOperand(i), CurBB, PredBB, - DT, NewInsts); + for (Value *Op : GEP->operands()) { + Value *OpVal = insertTranslatedSubExpr(Op, CurBB, PredBB, DT, NewInsts); if (!OpVal) return nullptr; GEPOps.push_back(OpVal); } -- 2.7.4