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

Mingming Liu via llvm-commits llvm-commits at lists.llvm.org
Thu May 23 15:13:20 PDT 2024


https://github.com/minglotus-6 updated https://github.com/llvm/llvm-project/pull/93249

>From 493cdcaa98ac7ccbaca6c1ec892de3f82dbdb7d3 Mon Sep 17 00:00:00 2001
From: mingmingl <mingmingl at google.com>
Date: Thu, 23 May 2024 15:07:33 -0700
Subject: [PATCH 1/2] [nfc][InstCombine]Find PHI incoming block by operand
 number

---
 .../InstCombine/InstructionCombining.cpp      | 28 +++++++++----------
 1 file changed, 14 insertions(+), 14 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 6c25ff215c375..974edc8d5a8fc 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -5000,26 +5000,26 @@ 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).
+          if (UserParent && UserParent != PN->getIncomingBlock(Num))
+            return std::nullopt;
+          UserParent = PN->getIncomingBlock(Num);
         } else {
           if (UserParent && UserParent != UserInst->getParent())
             return std::nullopt;

>From 28f97a98967c65418ae9e9a87b5a12759732b578 Mon Sep 17 00:00:00 2001
From: mingmingl <mingmingl at google.com>
Date: Thu, 23 May 2024 15:11:43 -0700
Subject: [PATCH 2/2] call getIncomingBlock once

---
 llvm/lib/Transforms/InstCombine/InstructionCombining.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 974edc8d5a8fc..b0361db1708f9 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -5017,7 +5017,8 @@ bool InstCombinerImpl::run() {
           // 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(Num))
+          BasicBlock* IncomingBlock = PN->getIncomingBlock(Num);
+          if (UserParent && UserParent != IncomingBlock)
             return std::nullopt;
           UserParent = PN->getIncomingBlock(Num);
         } else {



More information about the llvm-commits mailing list