[PATCH] D143179: [NFC] PHITransAddr refactoring - use range-based loops and standard algorithms

Sergei Kachkov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 2 06:50:49 PST 2023


kachkov98 created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: All.
kachkov98 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D143179

Files:
  llvm/include/llvm/Analysis/PHITransAddr.h
  llvm/lib/Analysis/PHITransAddr.cpp


Index: llvm/lib/Analysis/PHITransAddr.cpp
===================================================================
--- llvm/lib/Analysis/PHITransAddr.cpp
+++ llvm/lib/Analysis/PHITransAddr.cpp
@@ -77,11 +77,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
@@ -131,10 +128,9 @@
   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,
@@ -172,9 +168,8 @@
 
     // 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
@@ -211,11 +206,11 @@
   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);
     }
 
@@ -391,9 +386,8 @@
   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);
     }
Index: llvm/include/llvm/Analysis/PHITransAddr.h
===================================================================
--- llvm/include/llvm/Analysis/PHITransAddr.h
+++ 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<Instruction>(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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D143179.494291.patch
Type: text/x-patch
Size: 3935 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230202/b8f1dbf6/attachment.bin>


More information about the llvm-commits mailing list