[llvm] [nfc][InstCombine]Find PHI incoming block by operand number (PR #93249)

Mingming Liu via llvm-commits llvm-commits at lists.llvm.org
Fri May 24 10:26:54 PDT 2024


================
@@ -5000,26 +5000,27 @@ bool InstCombinerImpl::run() {
       BasicBlock *UserParent = nullptr;
       unsigned NumUsers = 0;
 
-      for (auto *U : I->users()) {
-        if (U->isDroppable())
+      for (Use &U : I->uses()) {
+        User *User = U.getUser();
+        if (User->isDroppable())
           continue;
         if (NumUsers > MaxSinkNumUsers)
           return std::nullopt;
 
-        Instruction *UserInst = cast<Instruction>(U);
+        Instruction *UserInst = cast<Instruction>(User);
         // Special handling for Phi nodes - get the block the use occurs in.
         if (PHINode *PN = dyn_cast<PHINode>(UserInst)) {
-          for (unsigned i = 0; i < PN->getNumIncomingValues(); i++) {
-            if (PN->getIncomingValue(i) == I) {
-              // Bail out if we have uses in different blocks. We don't do any
-              // sophisticated analysis (i.e finding NearestCommonDominator of
-              // these use blocks).
-              if (UserParent && UserParent != PN->getIncomingBlock(i))
-                return std::nullopt;
-              UserParent = PN->getIncomingBlock(i);
-            }
-          }
-          assert(UserParent && "expected to find user block!");
+          unsigned Num =
+              PHINode::getIncomingValueNumForOperand(U.getOperandNo());
+          assert(PN->getIncomingValue(Num) == I && "Expect from def-use chain");
+
+          // Bail out if we have uses in different blocks. We don't do any
+          // sophisticated analysis (i.e finding NearestCommonDominator of
+          // these use blocks).
+          BasicBlock *IncomingBlock = PN->getIncomingBlock(Num);
----------------
minglotus-6 wrote:

This makes sense. PTAL, thanks!

https://github.com/llvm/llvm-project/pull/93249


More information about the llvm-commits mailing list