[llvm] ca49d6b - [NFC] PHITransAddr refactoring - use range-based loops and standard algorithms
Sergey Kachkov via llvm-commits
llvm-commits at lists.llvm.org
Fri Feb 3 04:10:56 PST 2023
Author: Sergey Kachkov
Date: 2023-02-03T15:10:30+03:00
New Revision: ca49d6bc88ce0e5f6d1240be3fee8438e513d33d
URL: https://github.com/llvm/llvm-project/commit/ca49d6bc88ce0e5f6d1240be3fee8438e513d33d
DIFF: https://github.com/llvm/llvm-project/commit/ca49d6bc88ce0e5f6d1240be3fee8438e513d33d.diff
LOG: [NFC] PHITransAddr refactoring - use range-based loops and standard algorithms
Differential Revision: https://reviews.llvm.org/D143179
Added:
Modified:
llvm/include/llvm/Analysis/PHITransAddr.h
llvm/lib/Analysis/PHITransAddr.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Analysis/PHITransAddr.h b/llvm/include/llvm/Analysis/PHITransAddr.h
index 2cca2e1b93d4..de9c3c4fd292 100644
--- a/llvm/include/llvm/Analysis/PHITransAddr.h
+++ b/llvm/include/llvm/Analysis/PHITransAddr.h
@@ -52,8 +52,7 @@ class PHITransAddr {
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<Instruction>(Addr))
- InstInputs.push_back(I);
+ addAsInput(Addr);
}
Value *getAddr() const { return Addr; }
@@ -63,10 +62,9 @@ class PHITransAddr {
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 6b5d10955a56..3da92770dc6c 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<Instruction *>::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<Instruction *>::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<PHINode>(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<Instruction>(I->getOperand(i)))
- RemoveInstInputs(Op, InstInputs);
- }
+ for (Value *Op : I->operands())
+ if (Instruction *OpInst = dyn_cast<Instruction>(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<Instruction>(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<GetElementPtrInst>(Inst)) {
SmallVector<Value*, 8> 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<GetElementPtrInst>(Inst)) {
SmallVector<Value*, 8> 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);
}
More information about the llvm-commits
mailing list