[llvm] [Instcombine] Fix infinite loop in visitSelectInst. (PR #173704)

Justin Lebar via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 26 21:27:32 PST 2025


https://github.com/jlebar created https://github.com/llvm/llvm-project/pull/173704

Doing a nop replaceOperand leads us into an infinite loop here.

This was found by a fuzzer I'm working on.  The high-level design is to
randomly generate LLVM IR, run a pass on it, and then run the original
and new IR through the interpreter.  They should produce the same
results.  Right now I'm only fuzzing instcombine.


>From 29bf361dbb9aaa7268efc39cce66b4923a04e1e9 Mon Sep 17 00:00:00 2001
From: Justin Lebar <justin.lebar at gmail.com>
Date: Mon, 22 Dec 2025 23:10:51 -0500
Subject: [PATCH] [Instcombine] Fix infinite loop in visitSelectInst.

Doing a nop replaceOperand leads us into an infinite loop here.

This was found by a fuzzer I'm working on.  The high-level design is to
randomly generate LLVM IR, run a pass on it, and then run the original
and new IR through the interpreter.  They should produce the same
results.  Right now I'm only fuzzing instcombine.
---
 .../InstCombine/InstCombineSelect.cpp         | 20 +++++++++--------
 .../instcombine-select-no-infinite-loop.ll    | 22 +++++++++++++++++++
 2 files changed, 33 insertions(+), 9 deletions(-)
 create mode 100644 llvm/test/Transforms/InstCombine/instcombine-select-no-infinite-loop.ll

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index f52bac5e600cb..5a1b0bc4d5f5f 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -4790,18 +4790,20 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
   // select (trunc nsw X to i1), X, Y --> select (trunc nsw X to i1), -1, Y
   // select (trunc nsw X to i1), Y, X --> select (trunc nsw X to i1), Y, 0
   Value *Trunc;
+  auto Zero = [&]() { return ConstantInt::get(SelType, 0); };
+  auto One = [&]() { return ConstantInt::get(SelType, 1); };
+  auto MinusOne = [&]() { return ConstantInt::getAllOnesValue(SelType); };
   if (match(CondVal, m_NUWTrunc(m_Value(Trunc)))) {
-    if (TrueVal == Trunc)
-      return replaceOperand(SI, 1, ConstantInt::get(TrueVal->getType(), 1));
-    if (FalseVal == Trunc)
-      return replaceOperand(SI, 2, ConstantInt::get(FalseVal->getType(), 0));
+    if (TrueVal == Trunc && TrueVal != One())
+      return replaceOperand(SI, 1, One());
+    if (FalseVal == Trunc && FalseVal != Zero())
+      return replaceOperand(SI, 2, Zero());
   }
   if (match(CondVal, m_NSWTrunc(m_Value(Trunc)))) {
-    if (TrueVal == Trunc)
-      return replaceOperand(SI, 1,
-                            Constant::getAllOnesValue(TrueVal->getType()));
-    if (FalseVal == Trunc)
-      return replaceOperand(SI, 2, ConstantInt::get(FalseVal->getType(), 0));
+    if (TrueVal == Trunc && TrueVal != MinusOne())
+      return replaceOperand(SI, 1, MinusOne());
+    if (FalseVal == Trunc && FalseVal != Zero())
+      return replaceOperand(SI, 2, Zero());
   }
 
   Value *MaskedLoadPtr;
diff --git a/llvm/test/Transforms/InstCombine/instcombine-select-no-infinite-loop.ll b/llvm/test/Transforms/InstCombine/instcombine-select-no-infinite-loop.ll
new file mode 100644
index 0000000000000..ec9a6bf46b3aa
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/instcombine-select-no-infinite-loop.ll
@@ -0,0 +1,22 @@
+; Just check that this completes without hitting an infinite loop.
+; RUN: opt < %s --passes=instcombine -S > /dev/null
+
+define i1 @f(i2 %0, i2 %1, i1 %2, i2 %3) {
+entry:
+  %4 = icmp sgt i2 0, %0
+  %5 = zext i1 %2 to i2
+  %6 = select i1 %4, i2 0, i2 %5
+  %7 = trunc i2 %6 to i1
+  %8 = select i1 %7, i2 %1, i2 0
+  %9 = icmp sle i1 %4, %7
+  %10 = select i1 %9, i2 0, i2 %1
+  %11 = and i2 %5, %10
+  %12 = select i1 %2, i2 0, i2 %3
+  %13 = sdiv i2 1, %12
+  %14 = icmp uge i2 %8, %11
+  %15 = sext i1 %14 to i2
+  %16 = icmp sgt i2 %13, %15
+  %17 = icmp sgt i2 %6, 0
+  %18 = lshr i1 %16, %17
+  ret i1 %18
+}



More information about the llvm-commits mailing list