[llvm] [SimplifyCFG] Fix hoisting problem in SimplifyCFG (PR #78615)

Quentin Dian via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 8 17:15:17 PST 2024


================
@@ -1739,18 +1836,44 @@ bool SimplifyCFGOpt::hoistCommonCodeFromSuccessors(BasicBlock *BB,
         // leave any that were not hoisted behind (by calling moveBefore
         // rather than moveBeforePreserving).
         I1->moveBefore(TI);
-        for (auto &SuccIter : OtherSuccIterRange) {
-          Instruction *I2 = &*SuccIter++;
+        for (auto *I2 : OtherInsts) {
           assert(I2 != I1);
-          if (!I2->use_empty())
+          // Update hashcode of all instructions using I2
+          if (!I2->use_empty()) {
+            SmallVector<llvm::Instruction *, 8> PrevUsers;
+            // Once the uses of I1 are replaced, the hash value computed for
+            // those users are not valid anymore so we gather users and then
+            // recompute the hash codes for them. We need to do this only for
+            // the instructions located in the same block as I2 because we
+            // initially only hashed those instructions.
+            for (auto *user : I2->users()) {
+              if (auto *I = dyn_cast<Instruction>(user)) {
+                if (I->getParent() != I2->getParent())
+                  continue;
+                PrevUsers.push_back(I);
+              }
+            }
             I2->replaceAllUsesWith(I1);
+            for (auto &PrevUser : PrevUsers) {
+              auto NewHash = getHash(PrevUser);
+              if (OtherSuccessorsHash.find(NewHash) !=
+                  OtherSuccessorsHash.end()) {
+                OtherSuccessorsHash[NewHash].push_back(PrevUser);
+              } else {
+                SmallVector<Instruction *, 2> InstVec{PrevUser};
+                OtherSuccessorsHash[NewHash] = InstVec;
----------------
DianQK wrote:

I just think that we can hoist a batch of instruction when use hash map. Then we may don't need to update the hash value.

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


More information about the llvm-commits mailing list