[llvm] [JumpThreading] Add phi translation legality check in `duplicateCondBranchOnPHIIntoPred` (PR #198443)

via llvm-commits llvm-commits at lists.llvm.org
Mon May 18 20:38:04 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Hongyu Chen (XChy)

<details>
<summary>Changes</summary>

Fixes #<!-- -->197725.
The following patch will resolve https://github.com/llvm/llvm-project/issues/70651 without `LoopHeaders`.

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


2 Files Affected:

- (modified) llvm/lib/Transforms/Scalar/JumpThreading.cpp (+31) 
- (modified) llvm/test/Transforms/JumpThreading/phi-copy-to-pred.ll (+25) 


``````````diff
diff --git a/llvm/lib/Transforms/Scalar/JumpThreading.cpp b/llvm/lib/Transforms/Scalar/JumpThreading.cpp
index 415136b612ac2..0f28bce3aef2f 100644
--- a/llvm/lib/Transforms/Scalar/JumpThreading.cpp
+++ b/llvm/lib/Transforms/Scalar/JumpThreading.cpp
@@ -549,6 +549,30 @@ static Constant *getKnownConstant(Value *Val, ConstantPreference Preference) {
   return dyn_cast<ConstantInt>(Val);
 }
 
+/// Return true if phi-translating PN's incoming value from PredBB is safe.
+/// Translation is unsafe when the incoming value is not loop invariant,
+/// i.e., when its defining block is in a cycle.
+/// TODO: A more precise check would determine whether the incoming value is
+/// loop invariant specifically with respect to PredBB.
+static bool canPhiTranslate(PHINode *PN, BasicBlock *PredBB) {
+  Value *InVal = PN->getIncomingValueForBlock(PredBB);
+
+  // Non-instructions (constants, arguments) are always loop invariant.
+  auto *Inst = dyn_cast<Instruction>(InVal);
+  if (!Inst)
+    return true;
+
+  BasicBlock *DefBB = Inst->getParent();
+  // Instructions in the entry block cannot be part of a loop.
+  if (DefBB->isEntryBlock())
+    return true;
+  // InVal is loop invariant if its defining block is not in a cycle.
+  // Avoid the DomTree, which may have pending lazy updates at this point.
+  SmallVector<BasicBlock *> Succs(successors(DefBB));
+  return !isPotentiallyReachableFromMany(Succs, DefBB, /*ExclusionSet=*/nullptr,
+                                         /*DT=*/nullptr, /*LI=*/nullptr);
+}
+
 /// computeValueKnownInPredecessors - Given a basic block BB and a value V, see
 /// if we can infer that the value is a known ConstantInt/BlockAddress or undef
 /// in any of our predecessors.  If so, return the known list of value and pred
@@ -2647,6 +2671,13 @@ bool JumpThreadingPass::duplicateCondBranchOnPHIIntoPred(
     return false;
   }
 
+  // Bail out before any CFG modification if phi translation is illegal for any
+  // predecessor.
+  for (BasicBlock *Pred : PredBBs)
+    for (PHINode &PN : BB->phis())
+      if (!canPhiTranslate(&PN, Pred))
+        return false;
+
   // And finally, do it!  Start by factoring the predecessors if needed.
   std::vector<DominatorTree::UpdateType> Updates;
   BasicBlock *PredBB;
diff --git a/llvm/test/Transforms/JumpThreading/phi-copy-to-pred.ll b/llvm/test/Transforms/JumpThreading/phi-copy-to-pred.ll
index 17f3b1a3c4c2d..3638f7ec5f345 100644
--- a/llvm/test/Transforms/JumpThreading/phi-copy-to-pred.ll
+++ b/llvm/test/Transforms/JumpThreading/phi-copy-to-pred.ll
@@ -67,3 +67,28 @@ EXIT1:
 EXIT2:
   ret i32 1
 }
+
+
+define i32 @PR197725() {
+; CHECK-LABEL: @PR197725(
+; CHECK-NEXT:  exit:
+; CHECK-NEXT:    [[NOT:%.*]] = xor i32 1, 1
+; CHECK-NEXT:    ret i32 1
+;
+entry:
+  br i1 false, label %loop2, label %loop1
+
+loop1:                                         ; preds = %loop2, %entry
+  %cond = phi i1 [ %tobool, %loop2 ], [ false, %entry ]
+  %val = phi i32 [ %not_phi, %loop2 ], [ 0, %entry ]
+  %not = xor i32 %val, 1
+  br i1 %cond, label %exit, label %loop2
+
+exit:                                         ; preds = %loop2
+  ret i32 %val
+
+loop2:                                         ; preds = %loop1, %entry
+  %not_phi = phi i32 [ %not, %loop1 ], [ 0, %entry ]
+  %tobool = icmp ne i8 1, 0
+  br label %loop1
+}

``````````

</details>


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


More information about the llvm-commits mailing list