Skip to content

[NFCI][PromoteMem2Reg] Don't handle the first successor out of order #142464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

vitalybuka
Copy link
Collaborator

@vitalybuka vitalybuka commented Jun 2, 2025

Just for consistency, to avoid confusing conditions.

reverse helps to avoid tests updates as nothing is
changing for for successors count <=2.

For #142461

Created using spr 1.3.6
@llvmbot llvmbot added llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms labels Jun 2, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 2, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Vitaly Buka (vitalybuka)

Changes

Additionally handler successors in regular order, so that two successors
cases are handled exactly as before, so we need to less tests.

Additionally reverse order here results in more
natural 'phi' arguments list order.


Full diff: https://github.com/llvm/llvm-project/pull/142464.diff

2 Files Affected:

  • (modified) llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp (+12-14)
  • (modified) llvm/test/Transforms/InstCombine/2009-02-20-InstCombine-SROA.ll (+2-2)
diff --git a/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp b/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
index d84b07bd1457c..62995e57b917c 100644
--- a/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
+++ b/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
@@ -1215,24 +1215,22 @@ void PromoteMem2Reg::RenamePass(BasicBlock *BB, BasicBlock *Pred,
   }
 
   // 'Recurse' to our successors.
-  succ_iterator I = succ_begin(BB), E = succ_end(BB);
-  if (I == E)
-    return;
 
   // Keep track of the successors so we don't visit the same successor twice
   SmallPtrSet<BasicBlock *, 8> VisitedSuccs;
 
-  // Handle the first successor after the rest, to mimic legacy behaviour.
-  // FIXME: Handle them in regular order.
-  VisitedSuccs.insert(*I);
-  ++I;
-
-  for (; I != E; ++I)
-    if (VisitedSuccs.insert(*I).second)
-      Worklist.emplace_back(*I, BB, IncomingVals, IncomingLocs);
-
-  Worklist.emplace_back(*succ_begin(BB), BB, std::move(IncomingVals),
-                        std::move(IncomingLocs));
+  for (BasicBlock *S : reverse(successors(BB)))
+    if (VisitedSuccs.insert(S).second) {
+      if (VisitedSuccs.size() == 1) {
+        // Let the first successor to own allocated arrays.
+        Worklist.emplace_back(S, BB, std::move(IncomingVals),
+                              std::move(IncomingLocs));
+      } else {
+        // Other successors have to make a copy.
+        Worklist.emplace_back(S, BB, Worklist.back().Values,
+                              Worklist.back().Locations);
+      }
+    }
 }
 
 void llvm::PromoteMemToReg(ArrayRef<AllocaInst *> Allocas, DominatorTree &DT,
diff --git a/llvm/test/Transforms/InstCombine/2009-02-20-InstCombine-SROA.ll b/llvm/test/Transforms/InstCombine/2009-02-20-InstCombine-SROA.ll
index b532c81556738..ef414885bf809 100644
--- a/llvm/test/Transforms/InstCombine/2009-02-20-InstCombine-SROA.ll
+++ b/llvm/test/Transforms/InstCombine/2009-02-20-InstCombine-SROA.ll
@@ -215,7 +215,7 @@ define ptr @_Z3fooRSt6vectorIiSaIiEE(ptr %X) {
 ; IC_SROA-NEXT:    [[TMP27:%.*]] = getelementptr i8, ptr [[__FIRST_ADDR_I_I_SROA_0_0]], i32 4
 ; IC_SROA-NEXT:    br label [[BB18_I_I]]
 ; IC_SROA:       bb18.i.i:
-; IC_SROA-NEXT:    [[__FIRST_ADDR_I_I_SROA_0_1:%.*]] = phi ptr [ [[TMP27]], [[BB17_I_I]] ], [ [[__FIRST_ADDR_I_I_SROA_0_0]], [[BB13_I_I]] ]
+; IC_SROA-NEXT:    [[__FIRST_ADDR_I_I_SROA_0_1:%.*]] = phi ptr [ [[__FIRST_ADDR_I_I_SROA_0_0]], [[BB13_I_I]] ], [ [[TMP27]], [[BB17_I_I]] ]
 ; IC_SROA-NEXT:    [[TMP28:%.*]] = load i32, ptr [[__FIRST_ADDR_I_I_SROA_0_1]], align 4
 ; IC_SROA-NEXT:    [[TMP29:%.*]] = icmp eq i32 [[TMP28]], 42
 ; IC_SROA-NEXT:    br i1 [[TMP29]], label [[BB20_I_I:%.*]], label [[BB21_I_I:%.*]]
@@ -225,7 +225,7 @@ define ptr @_Z3fooRSt6vectorIiSaIiEE(ptr %X) {
 ; IC_SROA-NEXT:    [[TMP30:%.*]] = getelementptr i8, ptr [[__FIRST_ADDR_I_I_SROA_0_1]], i32 4
 ; IC_SROA-NEXT:    br label [[BB22_I_I]]
 ; IC_SROA:       bb22.i.i:
-; IC_SROA-NEXT:    [[__FIRST_ADDR_I_I_SROA_0_2:%.*]] = phi ptr [ [[TMP30]], [[BB21_I_I]] ], [ [[__FIRST_ADDR_I_I_SROA_0_0]], [[BB13_I_I]] ]
+; IC_SROA-NEXT:    [[__FIRST_ADDR_I_I_SROA_0_2:%.*]] = phi ptr [ [[__FIRST_ADDR_I_I_SROA_0_0]], [[BB13_I_I]] ], [ [[TMP30]], [[BB21_I_I]] ]
 ; IC_SROA-NEXT:    [[TMP31:%.*]] = load i32, ptr [[__FIRST_ADDR_I_I_SROA_0_2]], align 4
 ; IC_SROA-NEXT:    [[TMP32:%.*]] = icmp eq i32 [[TMP31]], 42
 ; IC_SROA-NEXT:    br i1 [[TMP32]], label [[BB24_I_I:%.*]], label [[BB25_I_I:%.*]]

Created using spr 1.3.6
@vitalybuka vitalybuka requested a review from nikic June 2, 2025 19:46
@vitalybuka vitalybuka assigned fmayer and thurstond and unassigned fmayer and thurstond Jun 2, 2025
@vitalybuka vitalybuka requested review from fmayer and thurstond June 2, 2025 19:49
vitalybuka added a commit to vitalybuka/llvm-project that referenced this pull request Jun 2, 2025
Additionally handler successors in regular order, so that two successors
cases are handled exactly as before, so we need to less tests.

Additionally reverse order here results in more
natural 'phi' arguments list order.

Pull Request: llvm#142464
Created using spr 1.3.6

[skip ci]
Created using spr 1.3.6
Created using spr 1.3.6
vitalybuka added a commit to vitalybuka/llvm-project that referenced this pull request Jun 2, 2025
Additionally handler successors in regular order, so that two successors
cases are handled exactly as before, so we need to less tests.

Additionally reverse order here results in more
natural 'phi' arguments list order.

Pull Request: llvm#142464
@vitalybuka vitalybuka requested a review from nikic June 2, 2025 20:35
vitalybuka added a commit to vitalybuka/llvm-project that referenced this pull request Jun 2, 2025
Additionally handler successors in regular order, so that two successors
cases are handled exactly as before, so we need to less tests.

Additionally reverse order here results in more
natural 'phi' arguments list order.

Pull Request: llvm#142464
vitalybuka added a commit to vitalybuka/llvm-project that referenced this pull request Jun 2, 2025
Additionally handler successors in regular order, so that two successors
cases are handled exactly as before, so we need to less tests.

Additionally reverse order here results in more
natural 'phi' arguments list order.

Pull Request: llvm#142464
Created using spr 1.3.6

[skip ci]
Created using spr 1.3.6
Created using spr 1.3.6

[skip ci]
Created using spr 1.3.6
Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Created using spr 1.3.6

[skip ci]
Created using spr 1.3.6
@vitalybuka vitalybuka changed the base branch from users/vitalybuka/spr/main.nfcipromotemem2reg-dont-handle-the-first-successor-out-of-order to main June 3, 2025 16:14
Created using spr 1.3.6
vitalybuka added a commit to vitalybuka/llvm-project that referenced this pull request Jun 3, 2025
Additionally handler successors in regular order, so that two successors
cases are handled exactly as before, so we need to less tests.

Additionally reverse order here results in more
natural 'phi' arguments list order.

Pull Request: llvm#142464
@vitalybuka vitalybuka merged commit 3cb967a into main Jun 3, 2025
6 of 9 checks passed
@vitalybuka vitalybuka deleted the users/vitalybuka/spr/nfcipromotemem2reg-dont-handle-the-first-successor-out-of-order branch June 3, 2025 17:26
rorth pushed a commit to rorth/llvm-project that referenced this pull request Jun 11, 2025
…lvm#142464)

Just for consistency, to avoid confusing conditions.

`reverse` helps to avoid tests updates as nothing is
changing for for successors count <=2.

For llvm#142461
DhruvSrivastavaX pushed a commit to DhruvSrivastavaX/lldb-for-aix that referenced this pull request Jun 12, 2025
…lvm#142464)

Just for consistency, to avoid confusing conditions.

`reverse` helps to avoid tests updates as nothing is
changing for for successors count <=2.

For llvm#142461
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants