Skip to content
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

release/19.x: Bail out jump threading on indirect branches only (#117778) #117869

Open
wants to merge 2 commits into
base: release/19.x
Choose a base branch
from

Conversation

llvmbot
Copy link
Member

@llvmbot llvmbot commented Nov 27, 2024

Backport 3c9022c 39601a6

Requested by: @nikic

The bug was introduced by
llvm#68473

Fixes: llvm#102351
(cherry picked from commit 3c9022c)
Remove check for PHI in pred as pointed out in llvm#103688
Reduced the testcase to remove redundant phi in pred

Fixes: llvm#102351
(cherry picked from commit 39601a6)
@llvmbot llvmbot added this to the LLVM 19.X Release milestone Nov 27, 2024
@llvmbot
Copy link
Member Author

llvmbot commented Nov 27, 2024

@hiraditya @nikic What do you think about merging this PR to the release branch?

@llvmbot
Copy link
Member Author

llvmbot commented Nov 27, 2024

@llvm/pr-subscribers-llvm-transforms

Author: None (llvmbot)

Changes

Backport 3c9022c 39601a6

Requested by: @nikic


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

2 Files Affected:

  • (modified) llvm/lib/Transforms/Utils/Local.cpp (+8-2)
  • (added) llvm/test/Transforms/SimplifyCFG/switch-branch-fold-indirectbr-102351.ll (+81)
diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp
index 7192efe3f16b9d..f68cbf62b9825f 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -1028,7 +1028,13 @@ CanRedirectPredsOfEmptyBBToSucc(BasicBlock *BB, BasicBlock *Succ,
   if (!BB->hasNPredecessorsOrMore(2))
     return false;
 
-  // Get single common predecessors of both BB and Succ
+  if (any_of(BBPreds, [](const BasicBlock *Pred) {
+        return isa<IndirectBrInst>(Pred->getTerminator());
+      }))
+    return false;
+
+  // Get the single common predecessor of both BB and Succ. Return false
+  // when there are more than one common predecessors.
   for (BasicBlock *SuccPred : SuccPreds) {
     if (BBPreds.count(SuccPred)) {
       if (CommonPred)
@@ -1133,7 +1139,7 @@ bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
 
   bool BBKillable = CanPropagatePredecessorsForPHIs(BB, Succ, BBPreds);
 
-  // Even if we can not fold bB into Succ, we may be able to redirect the
+  // Even if we can not fold BB into Succ, we may be able to redirect the
   // predecessors of BB to Succ.
   bool BBPhisMergeable =
       BBKillable ||
diff --git a/llvm/test/Transforms/SimplifyCFG/switch-branch-fold-indirectbr-102351.ll b/llvm/test/Transforms/SimplifyCFG/switch-branch-fold-indirectbr-102351.ll
new file mode 100644
index 00000000000000..d3713be8358db4
--- /dev/null
+++ b/llvm/test/Transforms/SimplifyCFG/switch-branch-fold-indirectbr-102351.ll
@@ -0,0 +1,81 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --prefix-filecheck-ir-name pref --version 5
+; RUN: opt < %s -passes=simplifycfg -S | FileCheck %s
+
+define i32 @foo.1(i32 %arg, ptr %arg1) {
+; CHECK-LABEL: define i32 @foo.1(
+; CHECK-SAME: i32 [[ARG:%.*]], ptr [[ARG1:%.*]]) {
+; CHECK-NEXT:  [[BB:.*]]:
+; CHECK-NEXT:    [[ALLOCA:%.*]] = alloca [2 x ptr], align 16
+; CHECK-NEXT:    store ptr blockaddress(@foo.1, %[[BB8:.*]]), ptr [[ALLOCA]], align 16
+; CHECK-NEXT:    [[GETELEMENTPTR:%.*]] = getelementptr inbounds [2 x ptr], ptr [[ALLOCA]], i64 0, i64 1
+; CHECK-NEXT:    store ptr blockaddress(@foo.1, %[[BB16:.*]]), ptr [[GETELEMENTPTR]], align 8
+; CHECK-NEXT:    br label %[[PREFBB2:.*]]
+; CHECK:       [[PREFBB2]]:
+; CHECK-NEXT:    [[PHI:%.*]] = phi i32 [ 0, %[[BB]] ], [ [[PHI14:%.*]], %[[BB13:.*]] ]
+; CHECK-NEXT:    [[PHI3:%.*]] = phi i32 [ 0, %[[BB]] ], [ [[PHI15:%.*]], %[[BB13]] ]
+; CHECK-NEXT:    switch i32 [[PHI]], label %[[BB13]] [
+; CHECK-NEXT:      i32 0, label %[[PREFBB18:.*]]
+; CHECK-NEXT:      i32 1, label %[[BB8]]
+; CHECK-NEXT:      i32 2, label %[[PREFBB11:.*]]
+; CHECK-NEXT:    ]
+; CHECK:       [[BB8]]:
+; CHECK-NEXT:    [[PHI10:%.*]] = phi i32 [ [[ARG]], %[[PREFBB18]] ], [ [[PHI3]], %[[PREFBB2]] ]
+; CHECK-NEXT:    br label %[[BB13]]
+; CHECK:       [[PREFBB11]]:
+; CHECK-NEXT:    [[CALL:%.*]] = call i32 @wombat(i32 noundef [[PHI3]])
+; CHECK-NEXT:    [[ADD:%.*]] = add nsw i32 [[PHI3]], 1
+; CHECK-NEXT:    br label %[[PREFBB18]]
+; CHECK:       [[BB13]]:
+; CHECK-NEXT:    [[PHI14]] = phi i32 [ [[PHI]], %[[PREFBB2]] ], [ 2, %[[BB8]] ]
+; CHECK-NEXT:    [[PHI15]] = phi i32 [ [[PHI3]], %[[PREFBB2]] ], [ [[PHI10]], %[[BB8]] ]
+; CHECK-NEXT:    br label %[[PREFBB2]]
+; CHECK:       [[BB16]]:
+; CHECK-NEXT:    [[CALL17:%.*]] = call i32 @wombat(i32 noundef [[ARG]])
+; CHECK-NEXT:    ret i32 0
+; CHECK:       [[PREFBB18]]:
+; CHECK-NEXT:    [[LOAD:%.*]] = load ptr, ptr [[ARG1]], align 8
+; CHECK-NEXT:    indirectbr ptr [[LOAD]], [label %[[BB8]], label %bb16]
+;
+bb:
+  %alloca = alloca [2 x ptr], align 16
+  store ptr blockaddress(@foo.1, %bb8), ptr %alloca, align 16
+  %getelementptr = getelementptr inbounds [2 x ptr], ptr %alloca, i64 0, i64 1
+  store ptr blockaddress(@foo.1, %bb16), ptr %getelementptr, align 8
+  br label %bb2
+
+bb2:                                              ; preds = %bb13, %bb
+  %phi = phi i32 [ 0, %bb ], [ %phi14, %bb13 ]
+  %phi3 = phi i32 [ 0, %bb ], [ %phi15, %bb13 ]
+  switch i32 %phi, label %bb13 [
+  i32 0, label %bb5
+  i32 1, label %bb8
+  i32 2, label %bb11
+  ]
+
+bb5:                                              ; preds = %bb2
+  br label %bb18
+
+bb8:                                              ; preds = %bb18, %bb2
+  %phi10 = phi i32 [ %arg, %bb18 ], [ %phi3, %bb2 ]
+  br label %bb13
+
+bb11:                                             ; preds = %bb2
+  %call = call i32 @wombat(i32 noundef %phi3)
+  %add = add nsw i32 %phi3, 1
+  br label %bb18
+
+bb13:                                             ; preds = %bb8, %bb2
+  %phi14 = phi i32 [ %phi, %bb2 ], [ 2, %bb8 ]
+  %phi15 = phi i32 [ %phi3, %bb2 ], [ %phi10, %bb8 ]
+  br label %bb2
+
+bb16:                                             ; preds = %bb18
+  %call17 = call i32 @wombat(i32 noundef %arg)
+  ret i32 0
+
+bb18:                                             ; preds = %bb11, %bb5
+  %load = load ptr, ptr %arg1, align 8
+  indirectbr ptr %load, [label %bb8, label %bb16]
+}
+
+declare i32 @wombat(i32)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Needs Merge
Development

Successfully merging this pull request may close these issues.

3 participants