[llvm] [IR][JumpThreading] Fix infinite recursion on compare self-reference [updated] (PR #129501)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 21 13:55:38 PDT 2025


================
@@ -1519,12 +1519,20 @@ Constant *JumpThreadingPass::evaluateOnPredecessorEdge(BasicBlock *BB,
   }
 
   // If we have a CmpInst, try to fold it for each incoming edge into PredBB.
+  // Note that during the execution of the pass, phi nodes may become constant
+  // and may be removed, which can lead to self-referencing instructions in
+  // code that becomes unreachable. Consequently, we need to handle those
+  // instructions in unreachable code and check before going into recursion.
   if (CmpInst *CondCmp = dyn_cast<CmpInst>(V)) {
     if (CondCmp->getParent() == BB) {
-      Constant *Op0 =
-          evaluateOnPredecessorEdge(BB, PredPredBB, CondCmp->getOperand(0), DL);
-      Constant *Op1 =
-          evaluateOnPredecessorEdge(BB, PredPredBB, CondCmp->getOperand(1), DL);
+      Constant *Op0 = CondCmp->getOperand(0) == CondCmp
----------------
nikic wrote:

This handles one specific kind of recursion, but there may be recursion across multiple instructions. It would be better to add a Visited set instead.

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


More information about the llvm-commits mailing list